-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtcpserver.cpp
179 lines (159 loc) · 4.94 KB
/
tcpserver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <QPushButton>
#include "tcpserver.h"
using namespace TCPSERVER_PRIVATE;
TcpServer::TcpServer(OnlineManagerBar *onlinemanagerbar, QObject *parent):
QTcpServer(parent), onlineManagerBar(onlinemanagerbar)
{
}
void TcpServer::on_socket_activate(bool activate)
{
if(activate){
quint16 port = 20171;
if(listen(QHostAddress::Any, port)
|| listen(QHostAddress::Any, port+1)
|| listen(QHostAddress::Any, port+2)
|| listen(QHostAddress::Any, port+3)
|| listen(QHostAddress::Any, port+4)
|| listen(QHostAddress::Any))
{
qDebug() << "Server started!" << this->serverPort();
emit tell_socket_port(QString("PORT[%1]").arg(this->serverPort()));
}
else
{
qDebug() << "Server could not start";
emit tell_socket_port(QString("PORT[fail]"));
}
}
else{
this->close();
}
}
// This function is called by QTcpServer when a new connection is available.
void TcpServer::incomingConnection(qintptr socketDescriptor)
{
// We have a new connection
qDebug() << socketDescriptor << " Connecting...";
// Every new connection will be run in a newly created thread
MyThread *thread = new MyThread(socketDescriptor, this);
// connect signal/slot
// once a thread is not needed, it will be beleted later
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
bool TcpServer::tcpcmd_start_record(char *outmsg)
{
bool currentstuts = onlineManagerBar->isStarted();
if(currentstuts){
strcpy(outmsg, "Error! Already started before.");
return false;
}
bool btn_enabled = onlineManagerBar->isConnected();
if(!btn_enabled){
strcpy(outmsg, "Error! Device not connected.");
return false;
}
onlineManagerBar->on_BTN_start_clicked();
strcpy(outmsg, "OK! Task should be starting.");
return true;
}
bool TcpServer::tcpcmd_stop_record(char *outmsg)
{
bool currentstuts = onlineManagerBar->isStarted();
if(!currentstuts){
strcpy(outmsg, "Error! Already stopped before.");
return false;
}
bool btn_enabled = onlineManagerBar->isConnected();
if(!btn_enabled){
strcpy(outmsg, "Error! Device not connected.");
return false;
}
emit onlineManagerBar->BTN_start->clicked();
strcpy(outmsg, "OK! Task should be stopping.");
return true;
}
int TcpServer::tcpcmd_query_record(char *outmsg)
{
bool btn_enabled = onlineManagerBar->isConnected();
if(!btn_enabled){
strcpy(outmsg, "Device not connected.");
return 0;
}
bool currentstuts = onlineManagerBar->isStarted();
if(currentstuts){
strcpy(outmsg, "Running.");
return 2;
}
else{
strcpy(outmsg, "Stopped.");
return 1;
}
}
MyThread::MyThread(qintptr ID, TcpServer *tcpServer, QObject *parent) :
QThread(parent)
{
this->socketDescriptor = ID;
this->tcpServer = tcpServer;
}
void MyThread::run()
{
// thread starts here
qDebug() << " Thread started";
socket = new QTcpSocket();
// set the ID
if(!socket->setSocketDescriptor(this->socketDescriptor))
{
// something's wrong, we just emit a signal
emit error(socket->error());
return;
}
// connect socket and signal
// note - Qt::DirectConnection is used because it's multithreaded
// This makes the slot to be invoked immediately, when the signal is emitted.
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
// We'll have multiple clients, we want to know which is which
qDebug() << socketDescriptor << " Client connected";
// make this thread a loop,
// thread will stay alive so that signal/slot to function properly
// not dropped out in the middle when thread dies
exec();
}
void MyThread::disconnected()
{
qDebug() << socketDescriptor << " Disconnected";
socket->deleteLater();
exit(0);
}
void MyThread::readyRead()
{
const int MaxLength = 1024;
char buffer[MaxLength+1];
qint64 byteCount = socket->read(buffer, MaxLength);
buffer[byteCount] = 0;
qDebug() << socket->bytesAvailable() << buffer;
QString strbuffer = QString(buffer);
strbuffer = strbuffer.trimmed();
const char *buffertrim = strbuffer.toStdString().c_str();
char response[MaxLength+1];
if(strcmp(buffertrim, "start_record")==0)
{
tcpServer->tcpcmd_start_record(response);
}
else if(strcmp(buffertrim, "stop_record")==0)
{
tcpServer->tcpcmd_stop_record(response);
}
else if(strcmp(buffertrim, "query_record")==0)
{
tcpServer->tcpcmd_query_record(response);
}
else
{
strcpy(response, "Error! Not a valid command.");
}
socket->write(response);
socket->flush();
socket->waitForBytesWritten(100);
}