-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtcpserverhandler.cpp
More file actions
155 lines (131 loc) · 4.94 KB
/
tcpserverhandler.cpp
File metadata and controls
155 lines (131 loc) · 4.94 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
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
#include "tcpserverhandler.h"
TcpServerHandler::TcpServerHandler(int portNumber): QObject(nullptr){
mTcpServer = new QTcpServer();
connect(mTcpServer, &QTcpServer::newConnection, this, &TcpServerHandler::acceptTcpConnection);
mTcpServer->listen(QHostAddress::Any, portNumber);
qDebug() << "TCP Listening on port:" << portNumber;
}
void TcpServerHandler::acceptTcpConnection(){
QTcpSocket* socket = mTcpServer->nextPendingConnection();
if (!socket){
qDebug() << "Error: got invalid pending connection!";
}
connect(socket, &QIODevice::readyRead, this, &TcpServerHandler::readTcpPacket);
connect(socket, &QAbstractSocket::disconnected, this, &TcpServerHandler::lostConnection);
}
void TcpServerHandler::readTcpPacket(){
QTcpSocket* readSocket = qobject_cast<QTcpSocket*>(sender());
/*QHostAddress senderAddress = readSocket->peerAddress();
int port = readSocket->peerPort();*/
QByteArray data = readSocket->readAll();
//qDebug() << "Addr: " << senderAddress << " Port: " << port << " Data: " << data;
int messageCode = data[0];
data.remove(0, 1);
if(messageCode == 0){
//Host game: code hostName
QString hostName = getNextStringSegement(&data);
qDebug() << "starting game with hostName: " << hostName;
//Create new game and append to list
QString key = QUuid::createUuid().toString(QUuid::Id128).left(20);
Game newGame(key, hostName, readSocket);
games.append(newGame);
QByteArray output;
//4 Means clients will store the gameId
int msg = 4;
output.append(msg);
output.append(key.length());
output.append(key);
readSocket->write(output);
} else if(messageCode == 1){
//Join game: code playerName gameId
QString playerName = getNextStringSegement(&data);
QString gameId = getNextStringSegement(&data);
qDebug() << "player: " << playerName << " attempting to join host: " << gameId;
int gameIndex = getGameIndex(gameId);
if(gameIndex >= 0){
games[gameIndex].addPlayer(playerName,readSocket);
QByteArray output;
output.append(1);
readSocket->write(output);
} else {
qDebug() << "Could not find game with gameId: " << gameId;
QByteArray output;
int msg = 0;
output.append(msg);
readSocket->write(output);
}
} else if(messageCode == 2){
//Move: code gameId from to
QString gameId = getNextStringSegement(&data);
qDebug() << "Game with id: " << gameId << " attempting to move";
if(doesGameExist(gameId)){
int gameIndex = getGameIndex(gameId);
QString from = getNextStringSegement(&data);
QString to = getNextStringSegement(&data);
QByteArray output;
//2 is the code for the clients to read a move
output.append(2);
output.append(from.length());
output.append(from);
output.append(to.length());
output.append(to);
games[gameIndex].getRecieverSocket(readSocket)->write(output);
} else {
qDebug() << "Move failed, Game with id: " << gameId << " does not exist";
QByteArray output;
int msg = 0;
output.append(msg);
readSocket->write(output);
}
} else if(messageCode == 3){
//Request games: code
QByteArray output;
//3 is the code for clients to accept a list of games
output.append(3);
//Append the amount of games
output.append(games.size());
for(int i=0;i<games.size();i++){
QString hostName = games[i].getHostPlayer();
QString gameId = games[i].getGameId();
output.append(hostName.length());
output.append(hostName);
output.append(gameId.length());
output.append(gameId);
}
readSocket->write(output);
} else if(messageCode == 4){
//Quit?
}
//readSocket->write(data);*/
}
void TcpServerHandler::lostConnection(){
QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender());
if(socket){
for(int i=0;i<games.size();i++){
if(games[i].socketExists(socket)){
games.removeAt(i);
return;
}
}
delete socket;
}
}
bool TcpServerHandler::doesGameExist(QString gameId){
return (getGameIndex(gameId) >= 0);
}
int TcpServerHandler::getGameIndex(QString gameId){
for(int i=0;i<games.size();i++){
if(games[i].getGameId().compare(gameId)==0){
return i;
}
}
return -1;
}
QString TcpServerHandler::getNextStringSegement(QByteArray *data){
int segmentLength = *data[0];
data->remove(0, 1);
QByteArray segment = QByteArray(*data, segmentLength);
QString stringSegment(segment);
data->remove(0, segmentLength);
return stringSegment;
}