-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreatedServerSocket.cpp
executable file
·129 lines (103 loc) · 3 KB
/
ThreatedServerSocket.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
#include "ThreatedServerSocket.h"
#include <QBuffer>
#include <QDataStream>
ThreatedServerSocket::ThreatedServerSocket(Control *ctrl, QObject *parent) :
QThread(parent)
{
this->save = NULL;
this->socket = NULL;
this->ctrl = ctrl;
connect(this, SIGNAL(wantEnd()), this->ctrl, SLOT(closeApplication()));
connect(this, SIGNAL(wantDeviceList()), this->ctrl, SLOT(deviceListWanted()));
connect(this->ctrl, SIGNAL(sendDeviceList(QStringList)), this, SLOT(send(QStringList)));
connect(this, SIGNAL(startRecording(QString,QString)), this->ctrl, SLOT(startRecording(QString,QString)));
}
void ThreatedServerSocket::newConnection()
{
if (this->socket)
{
this->socket->deleteLater();
}
this->socket = this->tcpServer->nextPendingConnection();
connect(this->socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
}
QString ThreatedServerSocket::readString( QDataStream& ds)
{
qint16 len;
ds >> len;
QByteArray ba((int)len, '\0');
ds.readRawData(ba.data(), (uint)len);
QString text = QString::fromUtf8(ba);
return text;
}
void ThreatedServerSocket::readyRead()
{
QByteArray ba = this->socket->readAll();
QDataStream ds(ba);
while (!ds.atEnd())
{
qint8 cmd;
ds >> cmd;
switch (cmd)
{
case -1:
emit wantEnd();
break;
case 1:
emit wantDeviceList();
break;
case 2:
{
QString deviceName = readString(ds);
QString fileName = readString(ds);
emit startRecording(deviceName, fileName);
}
break;
case 3:
emit wantBuffer();
}
}
}
void ThreatedServerSocket::send(QByteArray data)
{
QDataStream ds(this->socket);
ds << (qint8)3;
ds.writeBytes(data.data(), data.count());
}
void ThreatedServerSocket::send(int pos, qint16 left1, qint16 left2, qint16 right1, qint16 right2)
{
QDataStream ds(this->socket);
ds << (qint8)1;
ds << pos;
ds << left1;
ds << left2;
ds << right1;
ds << right2;
}
void ThreatedServerSocket::send(QStringList deviceList)
{
QDataStream ds(this->socket);
ds << (qint8)2;
ds << (qint16) deviceList.count();
foreach (QString text, deviceList)
{
ds << (qint16) text.count();
QByteArray ba = text.toUtf8();
ds.writeRawData(ba.data(), ba.count());
}
}
void ThreatedServerSocket::slotRecordingStarted()
{
this->save = ctrl->getSaveThread();
connect(this->save, SIGNAL(signalPeaks(int,qint16,qint16,qint16,qint16)), this, SLOT(send(int,qint16,qint16,qint16,qint16)));
connect(this, SIGNAL(wantBuffer()), this->save, SLOT(bufferWanted()));
connect(this->save, SIGNAL(signalBuffer(QByteArray)), this, SLOT(send(QByteArray)));
}
void ThreatedServerSocket::run()
{
QTcpServer svr;
this->tcpServer = &svr;
connect(&svr, SIGNAL(newConnection()), this, SLOT(newConnection()));
svr.listen(QHostAddress::LocalHost, 61435);
this->exec();
}