-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsocketthread.cpp
More file actions
67 lines (60 loc) · 1.81 KB
/
Copy pathsocketthread.cpp
File metadata and controls
67 lines (60 loc) · 1.81 KB
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
#include "socketthread.h"
#include <sstream>
#include <cstring>
using namespace std;
SocketThread::SocketThread(QString ip, QObject *parent)
{
socket = new QTcpSocket();
socket->connectToHost(ip, 1234);
connect(socket, SIGNAL(readyRead()), this, SLOT(newMessage()));
}
void SocketThread::sendMess(QString s)
{
s.append(QChar(23));
socket->write(s.toLocal8Bit());
}
void SocketThread::newMessage() {
if(QTcpSocket *s = dynamic_cast<QTcpSocket *> (sender())) {
message.append(s->readAll());
if(!message.contains(QChar(23)))
return;
QStringList l = message.split(QChar(23));
message = l.takeLast();
for(int i = 0; i < l.size();i++) {
string tmpS = l[i].toStdString();
stringstream stream(tmpS);
int a, num, num2;
stream >> a;
if(a == 1 || a == 2) {
if(a == 2) {
double b, c;
stream >> num >> num2 >> b >> c;
emit(movePlayer(num, num2, b, c));
}
else if(a == 1) {
double b, c;
stream >> num >> b >> c;
emit(drawLine(num, b, c));
}
}
else if(a == 3 || a == 4) {
emit playerN(a - 2);
}
else if(a == 5) {
double b, c;
stream >> b >> c;
emit ballMv(b, c);
}
else if(a == 10) {
string t1, t2;
stream >> t1 >> t2;
QString qt = QString::fromStdString(t1), qt2 = QString::fromStdString(t2);
emit startGame(qt, qt2);
}
else if(a == 11)
emit nameAccepted();
else if(a == 12)
emit changeTurn();
}
}
}