-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswServer.cpp
48 lines (41 loc) · 1.39 KB
/
swServer.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
#include "swServer.h"
#include "swFactory.h"
#include "swClientInitMsg.h"
swServer::swServer(swGame* parent, int port) : QObject(parent), game(parent) {
qserver = new QTcpServer(this);
connect(qserver, SIGNAL(newConnection()), this, SLOT(addConnection()));
qserver->listen(QHostAddress::Any, port);
}
void swServer::recvMsg() {
foreach(QTcpSocket* sock, socks) {
while(sock->bytesAvailable()) {
swMessage* msg = swFactory::readMessage(sock);
msg->servHandle(this, sock);
delete msg; // why does this cause problems!?
}
}
}
void swServer::addConnection() {
QTcpSocket* sock = qserver->nextPendingConnection();
connect(sock, SIGNAL(readyRead()), this, SLOT(recvMsg()));
connect(sock, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(sockError(QAbstractSocket::SocketError)));
socks.append(sock);
// send the client init message
swClientInitMsg msg;
msg.players = clientList;
msg.settings = game->settings;
swFactory::writeObject(&msg, sock);
}
swPlayer* swServer::findPlayer(QTcpSocket* sock) {
for(int i = 0; i < clientList.count(); i++) {
swPlayer* player = &clientList[i];
if(player->sock == sock)
return player;
}
return NULL;
}
QString swServer::address() {
return qserver->serverAddress().toString();
}
void swServer::sockError(QAbstractSocket::SocketError) {
}