-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServeurLabyrinthe.cpp
199 lines (171 loc) · 4.31 KB
/
ServeurLabyrinthe.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
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
#include "ServeurLabyrinthe.h"
ServeurLabyrinthe::ServeurLabyrinthe()
: m_socketServeur{ INVALID_SOCKET },
m_socketClient{ INVALID_SOCKET },
m_Carte{ 0.1, 40, 20 }
{
const long VERSION_DEMANDEE = MAKEWORD(2, 2);
WSAData configWinsock;
if (WSAStartup(VERSION_DEMANDEE, &configWinsock) != 0)
exit(erreur("Demarrage"));
}
ServeurLabyrinthe::~ServeurLabyrinthe()
{
fermer_socket(m_socketServeur);
fermer_socket(m_socketClient);
WSACleanup();
}
void ServeurLabyrinthe::Initialize(int port)
{
m_Carte.generer();
while (est_invalide(m_socketServeur))
{
m_socketServeur = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
}
reserver_port(m_socketServeur, port);
}
void ServeurLabyrinthe::Start()
{
std::string message;
while (true)
{
while(est_invalide(m_socketClient))
{
accepter_client();
}
message = tolowercase(recevoir());
switch (message[0])
{
case DROITE:
m_Carte.tryMoveRight();
break;
case GAUCHE:
m_Carte.tryMoveLeft();
break;
case HAUT:
m_Carte.tryMoveUp();
break;
case BAS:
m_Carte.tryMoveDown();
break;
default:
break;
}
if (m_Carte.is_PartieFinie())
{
envoyer("*");
}
else
{
// Envoyer nouveau vecteur.
std::vector<std::vector<char>> vector = m_Carte.getVec();
for (int y = 0; y < m_Carte.height(); ++y)
{
std::string s(vector[y].begin(), vector[y].end());
envoyer(s);
}
envoyer("@");
}
}
}
void ServeurLabyrinthe::fermer_socket(socket_t sck)
{
if (!est_invalide(sck))
{
shutdown(sck, SD_BOTH);
closesocket(m_socketServeur);
}
}
void ServeurLabyrinthe::reserver_port(socket_t sck, unsigned short port)
{
sockaddr_in nom = { 0 };
nom.sin_family = AF_INET;
nom.sin_port = htons(port);
auto resultat = bind(
sck, reinterpret_cast<const sockaddr*>(&nom), sizeof(sockaddr_in)
);
if (est_erreur(resultat))
exit(erreur("Port ", port, ", obtention"));
// Créer la file d’attente pour demandes de connexion
int taille_file = 16; // arbitraire
if (est_erreur(listen(sck, taille_file)))
exit(erreur("Creer file d'attente de taille ", taille_file));
}
void ServeurLabyrinthe::accepter_client()
{
sockaddr_in homologue = { 0 };
int taille = sizeof(sockaddr_in);
auto sck = accept(m_socketServeur, reinterpret_cast<sockaddr *>(&homologue), &taille);
if (!est_invalide(sck))
m_socketClient = sck;
}
std::string ServeurLabyrinthe::recevoir()
{
const int CAPACITE = 64; // arbitraire
char tampon[CAPACITE];
std::string message_recu;
bool reception_completee = false;
do
{
auto n = recv(m_socketClient, tampon, CAPACITE, 0);
if (est_erreur(n))
exit(erreur("Réception"));
message_recu.append(tampon + 0, tampon + n);
if (tampon[n - 1] == DELIMITEUR_FIN)
reception_completee = true;
} while (!reception_completee);
if (!message_recu.empty()) message_recu.pop_back(); // éliminer le délimiteur
return message_recu;
}
void ServeurLabyrinthe::envoyer(std::string msg)
{
// Note : Pas besoin de htons vu que les strings contiennent des chars de 8 bits,
// qui sont donc toujours dans le bon ordre.
msg += DELIMITEUR_FIN;
while (!msg.empty())
{
auto plus_recent = send(
m_socketClient, msg.c_str(), static_cast<int>(msg.size()), 0
);
if (est_erreur(plus_recent))
exit(erreur("Envoi"));
msg = msg.substr(plus_recent);
}
}
std::string ServeurLabyrinthe::tolowercase(std::string msg)
{
std::locale loc;
std::string temp;
for (std::string::size_type i = 0; i<msg.length(); ++i)
temp.push_back(std::tolower(msg[i], loc));
return temp;
}
#pragma region Traitement erreur
template <class T>
void ServeurLabyrinthe::print(std::ostream &os, T && arg)
{
os << arg;
}
template <class T, class ... Args>
void ServeurLabyrinthe::print(std::ostream &os, T && arg, Args && ... args)
{
print(os, std::forward<T>(arg));
print(os, std::forward<Args>(args)...);
}
template <class ... Args>
int ServeurLabyrinthe::erreur(Args && ... args)
{
const int code = WSAGetLastError();
print(std::cerr, std::forward<Args>(args)...);
std::cerr << "; erreur " << code << std::endl;
return code;
}
bool ServeurLabyrinthe::est_invalide(socket_t sck)
{
return sck == INVALID_SOCKET;
}
bool ServeurLabyrinthe::est_erreur(socket_operation_t op)
{
return op == SOCKET_ERROR;
}
#pragma endregion