-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
238 lines (201 loc) · 5.38 KB
/
server.cpp
File metadata and controls
238 lines (201 loc) · 5.38 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <QtNetwork>
#include <QHostAddress>
#include <stdlib.h>
#include "serveurwidget.h"
#include "server.h"
#include <iostream>
/**
* @brief Server::Server
* @param parent
*/
Server::Server(QObject* parent)
: Connexion(parent)
{
server = new QTcpServer(this);
clients = new QList<QTcpSocket*>;
clients_pseudos = new QList<QString>;
// init pour les déplacements
deplacements = new QList<concurrency::concurrent_queue<string>*>();
deplacements->append(new concurrency::concurrent_queue<string>());
deplacements->append(new concurrency::concurrent_queue<string>());
deplacements->append(new concurrency::concurrent_queue<string>());
deplacements->append(new concurrency::concurrent_queue<string>());
mapper = new QSignalMapper(this);
connect(server, &QTcpServer::newConnection,
this, &Server::acceptConnection);
connect(mapper, SIGNAL(mapped(int)), this, SLOT(startRead(int)));
server->listen();
mutexes = new QList<std::mutex*>();
mutexes->append(new std::mutex());
mutexes->append(new std::mutex());
mutexes->append(new std::mutex());
mutexes->append(new std::mutex());
mutexes->at(0)->lock();
mutexes->at(1)->lock();
mutexes->at(2)->lock();
mutexes->at(3)->lock();
}
/**
* @brief Server::~Server
*/
Server::~Server()
{
server->close();
}
/**
* @brief Server::acceptConnection
* accepte une connection si il reste de la place
*/
void Server::acceptConnection()
{
QTcpSocket *sock = server->nextPendingConnection();
if (clients->size() < 3) {
clients->append(sock);
mapper->setMapping(clients->last(), clients->length()-1);
connect(clients->last(), SIGNAL(readyRead()), mapper, SLOT(map()));
connect(clients->last(), SIGNAL(disconnected()),
this, SLOT(closeConnection()));
sock->write("0;ok",5);
}
else {
sock->write("0;nop",6);
sock->close();
}
}
/**
* @brief Server::closeConnection
* ferme la connection avec un client
*/
void Server::closeConnection()
{
QTcpSocket *sock = (QTcpSocket*) sender();
clients_pseudos->removeAt(clients->indexOf(sock));
clients->removeAt(clients->indexOf(sock));
sw->updateClients();
}
/**
* @brief Server::startRead
* @param index
* traite les instructions
*/
void Server::startRead(int index)
{
char buffer[1020] = {0};
clients->at(index)->read(buffer, clients->at(index)->bytesAvailable());
QString instruction = QString::fromUtf8(buffer);
QStringList list = instruction.split(";");
switch(list.at(0).toInt())
{
case 0:
clients_pseudos->append(buffer);
sw->setJoueur(list.at(1), index);
break;
// instruction déplacement : G/H/B
case 1:
this->ajouterDeplacement(index,list.at(1).toStdString());
break;
// instruction action gemme
case 2:
break;
// instruction action levier : couleur
case 3:
break;
}
}
/**
* @brief Server::getAdresseIP
* @return l'ip du serveur
*/
QString Server::getAdresseIP()
{
QString ipAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
// use the first non-localhost IPv4 address
for (int i = 0; i < ipAddressesList.size(); ++i) {
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
ipAddressesList.at(i).toIPv4Address()) {
ipAddress = ipAddressesList.at(i).toString();
break;
}
}
// if we did not find one, use IPv4 localhost
if (ipAddress.isEmpty())
ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
return ipAddress;
}
quint16 Server::getPort()
{
return server->serverPort();
}
void Server::setServeurWidget(ServeurWidget *sw)
{
this->sw = sw;
}
/**
* @brief Server::getPseudosClients
* @return liste des noms des clients
*/
QList<QString>* Server::getPseudosClients() {
return clients_pseudos;
}
/**
* @brief Server::envoyerInstructionDemarrerPartie
* informe les clients que la partie commence
*/
void Server::envoyerInstructionDemarrerPartie()
{
for (int i = 0; i < clients->size(); i++) {
char buffer[1020] = {0};
strcpy(buffer,std::to_string(1).c_str());
strcat(buffer,";");
strcat(buffer,std::to_string(i).c_str());
clients->at(i)->write(buffer,sizeof(buffer));
}
}
/**
* @brief Server::atest
* @param numero
* @param buffer
*/
void Server::atest(int numero, const char*buffer)
{
clients->at(numero)->write(buffer, sizeof(buffer));
}
/**
* @brief Server::sendNouvellePosition
* @param numero numero du client
* @param buffer
*/
void Server::sendNouvellePosition(int numero, const char* buffer)
{
clients->at(numero)->write(buffer,sizeof(buffer));
}
/**
* @brief Server::ajouterDeplacement
* @param numero numero du joueur
* @param direction nouvelle direction du joueur
*/
void Server::ajouterDeplacement(int numero, string direction)
{
this->deplacements->at(numero)->push(direction);
this->mutexes->at(numero)->unlock();
}
/**
* @brief Server::initThreads
* initialise les Threads de calcul de position
*/
void Server::initThreads()
{
for (int i = 0; i < 4; i++) {
CalculPosition *cp = new CalculPosition(this->game, this, this->deplacements->at(i), i);
cp->start();
}
}
/**
* @brief Server::initGame
* @param game
*/
void Server::initGame(Game* game)
{
this->game = game;
}