-
Notifications
You must be signed in to change notification settings - Fork 5
/
qserverchildthread.cpp
132 lines (106 loc) · 3.57 KB
/
qserverchildthread.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
#include "qserverchildthread.h"
#include <QIODevice>
QServerChildThread::QServerChildThread(qintptr socketDescriptor, QObject *parent)
: QThread(parent), socketDescriptor(socketDescriptor) {
qDebug() << "Client connection thread initialised... " << endl;
}
void QServerChildThread::run() {
qDebug() << "TCP Client Thread Running..." << endl;
this->clientConnection = new QTcpSocket();
if (!this->clientConnection->setSocketDescriptor(this->socketDescriptor, QTcpSocket::ConnectedState, QIODevice::ReadWrite)) {
emit error(this->clientConnection->error());
return;
}
this->clientConnection->open(QIODevice::ReadWrite);
QObject::connect(this->clientConnection, SIGNAL(readyRead()),
this, SLOT(onResponse()), Qt::ConnectionType::DirectConnection);
// Keep the thread running (fucking important!)
this->exec();
}
void QServerChildThread::onResponse() {
qDebug() << "Request received " << endl;
QRegExp matchRequests("(\\w+)\\s?(\\d*)");
QString command, param;
qDebug() << "Bytes available: " << clientConnection->bytesAvailable() << endl;
while(clientConnection->canReadLine())
{
QByteArray ba = clientConnection->readLine();
QString line = QString(ba).simplified();
qDebug() << "Request: " << line << endl;
command.clear();
param.clear();
if (matchRequests.indexIn(line) != -1) {
command = matchRequests.cap(1);
param = matchRequests.cap(2);
}
if (command == "PLAY") {
if (param.toInt() != 0) {
// Start a new song
emit playSong(param.toInt());
} else {
// Continue playing a paused/stopped song
emit playSong(0);
}
}
if (command == "PAUSE") {
emit pauseSong();
}
if (command == "STOP") {
emit stopSong();
}
if (command == "VOL") {
if (param.toInt() >= 0 && param.toInt() <= 100) {
emit setVolume(param.toInt());
}
}
if (command == "SAYBYE") {
clientConnection->disconnectFromHost();
}
qDebug() << ">> " << line << endl;
}
}
QString QServerChildThread::readLine(QTcpSocket *socket )
{
QString line = "";
int bytesAvail = waitForInput( socket );
if (bytesAvail > 0) {
int cnt = 0;
bool endOfLine = false;
bool endOfStream = false;
while (cnt < bytesAvail && (!endOfLine) && (!endOfStream)) {
char ch;
int bytesRead = socket->read(&ch, sizeof(ch));
if (bytesRead == sizeof(ch)) {
cnt++;
line.append( ch );
if (ch == '\r') {
endOfLine = true;
}
}
else {
endOfStream = true;
}
}
}
return line;
}
void QServerChildThread::writeLine(QTcpSocket *socket, const QString &line) {
if (line.length() > 0) {
socket->write(line.toLocal8Bit().data());
if (! socket->waitForBytesWritten()) {
printf("writeLine: the write to the socket failed\n");
}
}
}
int QServerChildThread::waitForInput( QTcpSocket *socket ) {
int bytesAvail = -1;
while (socket->state() == QAbstractSocket::ConnectedState && bytesAvail < 0) {
if (socket->waitForReadyRead( 100 )) {
bytesAvail = socket->bytesAvailable();
}
else {
QThread::msleep(50);
}
}
return bytesAvail;
}