-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerManager.cpp
253 lines (200 loc) · 6.02 KB
/
PlayerManager.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "PlayerManager.h"
#include "config.h"
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <algorithm>
#include <iostream>
#include <cstring>
PlayerManager::PlayerManager(int sockfd, GameState& gameState, ServerThread& st, ClientThread& ct)
: sockfd(sockfd), joined(false), id(-1), gameState(gameState), st(st), ct(ct), nb_msg(0), running(false) {
}
PlayerManager::~PlayerManager() {
stop();
if (msg_thread.joinable())
msg_thread.join();
close(sockfd);
}
void PlayerManager::start(int playerId) {
this->joined = playerId == -1;
this->id = playerId;
bool created = playerId == -1 ? true : gameState.addPlayer(playerId);
if (!running && created) {
running = true;
if (playerId >= 0)
std::cout << "Connection with client " << playerId << std::endl;
msg_thread = std::thread(&PlayerManager::processMessage, this);
} else {
throw std::string("Cannot create player");
}
}
void PlayerManager::stop() {
if (running) {
running = false;
cv.notify_one();
std::cout << "Closing connection with client " << id << std::endl;
}
}
void PlayerManager::addMessage(const std::string& msg) {
if (!running)
return;
std::unique_lock<std::mutex> lck(msg_mx);
size_t nb_new = std::count(msg.begin(), msg.end(), '\0');
buffer += msg;
if (nb_new > 0)
nb_msg += nb_new;
cv.notify_one();
}
void PlayerManager::processMessage() {
size_t pos, old_pos;
std::string tmp, cmd;
while (running) {
std::unique_lock<std::mutex> lck(msg_mx);
while (nb_msg <= 0 && running) cv.wait(lck);
tmp.swap(buffer);
lck.unlock();
if (!running)
break;
old_pos = 0;
while ((pos = tmp.find('\0', old_pos)) != std::string::npos) {
cmd.append(tmp, old_pos, pos - old_pos);
if (cmd == "exit") {
stop();
gameState.removePlayer(id);
return;
}
if (id >= 0) {
if (cmd == "join") {
if (!joined) {
join();
joined = true;
}
} else if (cmd == "S" || cmd == "E" || cmd == "N" || cmd == "W" || cmd == "NoMove") {
move(cmd);
}
} else if (cmd.compare(0, 7, "connect") == 0) {
if (tmp.length() < old_pos + 12 || tmp[old_pos + 11] != '\0')
break;
int* p_id = (int*)(tmp.data() + old_pos + 7);
this->id = ntohl(*p_id);
pos = old_pos + 11;
nb_msg -= std::count(tmp.begin() + old_pos, tmp.begin() + pos, '\0');
std::cout << "Connection with client " << this->id << std::endl;
}
if (cmd == "server") {
if (nb_msg < 2)
break;
std::string host, port;
old_pos = pos + 1;
pos = tmp.find('\0', old_pos);
port = tmp.substr(old_pos, pos - old_pos);
host = getHost();
st.newServer(this, host, port);
nb_msg--;
} else if (cmd.compare(0, sizeof(MOVE_PLAYER) - 1, MOVE_PLAYER) == 0) {
if (tmp.length() < old_pos + sizeof(MOVE_PLAYER) + 5
|| tmp[old_pos + sizeof(MOVE_PLAYER) + 4] != '\0')
break;
char dir = tmp[old_pos + sizeof(MOVE_PLAYER) - 1];
int* p_id = (int*)(tmp.data() + old_pos + sizeof(MOVE_PLAYER));
ct.movePlayer(ntohl(*p_id), dir);
sendHead(PLAYER_MOVED);
pos = old_pos + sizeof(MOVE_PLAYER) + 4;
nb_msg -= std::count(tmp.begin() + old_pos, tmp.begin() + pos, '\0');
} else if (cmd == REQUEST_STATE) {
if (ct.releaseState()) {
sendHead(STATE_ACQUIRED);
} else {
sendHead(NOT_OWNER);
}
}
nb_msg--;
old_pos = pos + 1;
cmd.erase();
}
cmd = tmp.substr(old_pos);
tmp.erase();
}
}
void PlayerManager::join() {
std::string msg;
uint32_t nId = htonl(id);
uint32_t N = htonl(gameState.getSize());
uint32_t size = htonl(8);
uint32_t head = htonl(INIT_CLIENT);
msg.append((char*) &head, 4);
msg.append((char*) &size, 4);
msg.append((char*) &nId, 4);
msg.append((char*) &N, 4);
sendMsg(msg);
}
void PlayerManager::move(const std::string& cmd) {
if (cmd != "NoMove")
ct.syncMove(id, cmd[0]);
sendState(htonl(UPDATE_STATE));
}
void PlayerManager::createBackupServer() {
sendState(htonl(CREATE_SERVER), true);
}
void PlayerManager::sendState(uint32_t head, bool send_size) {
std::string msg;
msg.append((char*) &head, 4);
const std::string state = gameState.getState();
uint32_t size = htonl(state.size() + (send_size ? 4 : 0));
msg.append((char*) &size, 4);
if (send_size) {
uint32_t N = htonl(gameState.getSize());
msg.append((char*) &N, 4);
}
msg += state;
sendMsg(msg);
}
void PlayerManager::sendHead(uint32_t head) {
std::string msg;
uint32_t n_head = ntohl(head);
uint32_t size = ntohl(0);
msg.append((char*) &n_head, 4);
msg.append((char*) &size, 4);
sendMsg(msg);
}
void PlayerManager::sendNewServer(const std::string& host, const std::string& port) {
std::string msg;
uint32_t head = htonl(NEW_SERVER);
uint32_t size = htonl(host.size() + port.size() + 2);
msg.append((char*) &head, 4);
msg.append((char*) &size, 4);
msg += host;
msg += '\0';
msg += port;
msg += '\0';
sendMsg(msg);
}
void PlayerManager::sendMsg(const std::string& msg) {
ssize_t n;
size_t len = msg.size();
const char* p = msg.data();
std::lock_guard<std::mutex> sock_lck(sock_mtx);
while ((n = send(sockfd, p, len, 0)) > 0) {
p += n;
len -= n;
}
if (n < 0) {
std::cerr << "Error while sending message to client " << id << std::endl;
stop();
}
}
const std::string PlayerManager::getHost() const {
char host[INET6_ADDRSTRLEN];
struct sockaddr_storage addr;
socklen_t len = sizeof(addr);
getpeername(sockfd, (struct sockaddr*)&addr, &len);
// deal with both IPv4 and IPv6:
if (addr.ss_family == AF_INET) {
struct sockaddr_in *s = (struct sockaddr_in *) &addr;
inet_ntop(AF_INET, &s->sin_addr, host, sizeof(host));
} else { // AF_INET6
struct sockaddr_in6 *s = (struct sockaddr_in6 *) &addr;
inet_ntop(AF_INET6, &s->sin6_addr, host, sizeof(host));
}
return host;
}