-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobots-server.cpp
533 lines (458 loc) · 15.4 KB
/
robots-server.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#include <chrono>
#include <iostream>
#include <functional>
#include <random>
#include <thread>
#include <boost/asio.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <boost/program_options.hpp>
#include "common.hpp"
#include "debug.hpp"
#include "messages.hpp"
#include "streamable-buffer.hpp"
#include "serialization.hpp"
#include "safe-queue.hpp"
namespace po = boost::program_options;
namespace ip = boost::asio::ip;
using ip::tcp;
using ip::udp;
using seed_t = uint32_t;
std::ostream& operator<<(std::ostream& os, unsigned char c) {
return os << +c;
}
struct ServerParams {
bomb_timer_t bomb_timer;
players_count_t players_count;
turn_duration_t turn_duration;
explosion_radius_t explosion_radius;
initial_blocks_t initial_blocks;
game_length_t game_length;
server_name_t server_name;
pos_t size_x;
pos_t size_y;
};
class Server {
// max 25 tcp connections
static constexpr size_t max_clients = 25;
// max 100 unreceived messages
static constexpr size_t max_queue_size = 100;
const ServerParams params;
const port_t port;
std::minstd_rand random;
struct ClientInfo {
std::shared_ptr<tcp::socket> sock;
ClientMessage last_msg;
std::optional<player_id_t> player_id;
};
std::map<tcp::endpoint, ClientInfo> clients;
std::mutex mutex_clients;
struct PlayerInfo {
std::string name;
Position pos;
ClientMessage msg;
Player player;
friend std::ostream& operator<<(std::ostream& os, PlayerInfo x) {
return os << x.name << x.pos;
}
};
std::condition_variable cond_players;
std::mutex mutex_players;
std::map<player_id_t, PlayerInfo> players;
enum class State { Lobby, Maintenance, Playing };
std::atomic<State> state;
boost::asio::io_service io_service;
std::vector<ServerMessageTurn> turns;
std::mutex mutex_turns;
std::vector<Event> turn_events;
const ServerMessageHello hello = ServerMessageHello {
.server_name = params.server_name,
.players_count = params.players_count,
.size_x = params.size_x,
.size_y = params.size_y,
.game_length = params.game_length,
.explosion_radius = params.explosion_radius,
.bomb_timer = params.bomb_timer
};
void client_connected(std::shared_ptr<tcp::socket> sock) {
ip::tcp::endpoint client_endpoint = sock->remote_endpoint();
println("Connected:", client_endpoint);
std::scoped_lock lock {mutex_clients};
assert(clients.size() < max_clients);
clients[client_endpoint].sock = sock;
}
void client_disconnected(ip::tcp::endpoint client_endpoint) {
println("Disconnected:", client_endpoint);
{
std::scoped_lock lock {mutex_clients};
auto it = clients.find(client_endpoint);
assert(it != clients.end());
try {
it->second.sock->shutdown(boost::asio::ip::tcp::socket::shutdown_both);
} catch (const boost::system::system_error& e) {}
clients.erase(it);
}
// if the client has already been accepted, forget them
// std::scoped_lock lock {mutex_players};
// auto it = players.find(client_endpoint);
// if (it != players.end()) {
// players.erase(it);
// }
// cond_players.notify_one();
}
void await_players() {
std::unique_lock lock {mutex_players};
cond_players.wait(lock,
[this] { return players.size() == params.players_count; }
);
}
void init_game() {
println("Generating new board...");
std::scoped_lock lock {mutex_players};
for (auto& [player_id, player] : players) {
player.pos = {
.x = static_cast<pos_t>(random() % params.size_x),
.y = static_cast<pos_t>(random() % params.size_y)
};
turn_events.push_back(EventPlayerMoved {.player_id = player_id, .position = player.pos});
}
for (size_t i=0; i < params.initial_blocks; ++i) {
Position pos = {
.x = static_cast<pos_t>(random() % params.size_x),
.y = static_cast<pos_t>(random() % params.size_y)
};
turn_events.push_back(EventBlockPlaced {.position = pos});
}
cond_players.notify_one();
}
void broadcast_message(ServerMessage&& msg) {
std::scoped_lock lock {mutex_clients};
streamable_buffer sbuffer;
for (auto& [key, client] : clients) {
sbuffer << msg;
try {
send(sbuffer, *client.sock);
} catch (const boost::system::system_error& e) {
println("Error writing to client!");
}
assert(sbuffer.empty());
}
}
void broadcast_turn(turn_t turn) {
println("Broadcasting current state for turn:", turn);
std::scoped_lock lock {mutex_turns};
auto msg = ServerMessageTurn {
.turn = turn,
.events = turn_events
};
turns.push_back(msg);
broadcast_message(msg);
turn_events.clear();
}
std::optional<Event> get_event([[maybe_unused]]player_id_t player_id, [[maybe_unused]]ClientMessageJoin msg) {
return {};
}
std::optional<Event> get_event([[maybe_unused]]player_id_t player_id, [[maybe_unused]]ClientMessagePlaceBlock msg) {
return {};
}
std::optional<Event> get_event([[maybe_unused]]player_id_t player_id, [[maybe_unused]]ClientMessagePlaceBomb msg) {
return {};
}
std::optional<Event> get_event([[maybe_unused]]player_id_t player_id, [[maybe_unused]]ClientMessageMove msg) {
PlayerInfo& player = players[player_id];
Position new_pos = player.pos;
if (player.pos.x > 0 && msg.direction == 3) { new_pos.x--; }
else if (player.pos.x < params.size_x - 1 && msg.direction == 1) { new_pos.x++; }
else if (player.pos.y < params.size_y - 1 && msg.direction == 0) { new_pos.y++; }
else if (player.pos.y > 0 && msg.direction == 2) { new_pos.y--; }
return EventPlayerMoved { .player_id = player_id, .position = new_pos };
}
void apply_player_moves() {
std::scoped_lock lock {mutex_turns, mutex_players};
for (auto elt : players) {
player_id_t player_id = elt.first;
PlayerInfo& player = elt.second;
ClientMessage msg = player.msg;
std::optional<Event> event = std::visit(
[this, player_id] (auto x) { return get_event(player_id, x); },
msg
);
if (event) { turn_events.push_back(*event); }
}
}
void finish_game() {
println("Cleaning up...");
{
std::scoped_lock lock {mutex_clients};
for (auto& [_, client] : clients) {
client.player_id = std::nullopt;
}
}
{
std::scoped_lock lock {mutex_players};
players = {};
cond_players.notify_one();
}
broadcast_message(ServerMessageGameEnded {});
println("Broadcasting GameEnded finished!");
}
void send_past_turns(tcp::endpoint client_endpoint) {
auto it = clients.find(client_endpoint);
assert(it != clients.end());
ClientInfo& client = it->second;
streamable_buffer sbuffer;
for (ServerMessageTurn turn : turns) {
sbuffer << turn;
try {
send(sbuffer, *client.sock);
} catch (const boost::system::system_error& e) {
println("Error writing to client!");
}
assert(sbuffer.empty());
}
}
void send_players(tcp::endpoint client_endpoint) {
std::scoped_lock lock {mutex_players};
streamable_buffer sbuffer;
for (auto [player_id, player] : players) {
sbuffer << ServerMessageAcceptedPlayer {
.player_id = player_id,
.player = player.player
};
try {
auto it = clients.find(client_endpoint);
assert(it != clients.end());
ClientInfo& client = it->second;
send(sbuffer, *client.sock);
} catch (const boost::system::system_error& e) {
println("Error writing to client!");
}
assert(sbuffer.empty());
}
}
void handle_client_msg(tcp::endpoint client_endpoint, const ClientMessageJoin& msg) {
println("Client wants to join");
if (state != State::Lobby) {
send_players(client_endpoint);
send_past_turns(client_endpoint);
return;
}
{
std::scoped_lock lock {mutex_clients};
auto it = clients.find(client_endpoint);
if (it == clients.end()) { return; }
if (it->second.player_id) { return; }
}
player_id_t player_id;
Player player;
{
std::scoped_lock lock {mutex_players};
player_id = static_cast<player_id_t>(players.size());
std::stringstream ss;
ss << client_endpoint;
std::string addr = ss.str();
player = Player { .name = msg.name, .address = addr };
auto [_, inserted] = players.insert({
player_id,
PlayerInfo {
.name = msg.name,
.pos = {},
.msg = {},
.player = player
}
});
if (!inserted) { return; }
}
{
std::scoped_lock lock {mutex_clients};
clients[client_endpoint].player_id = player_id;
}
cond_players.notify_one();
println("Client joins:", client_endpoint);
broadcast_message(
ServerMessageAcceptedPlayer {
.player_id = player_id,
.player = player
}
);
println("Current players:", players);
}
std::optional<player_id_t> get_player_id(tcp::endpoint client_endpoint) {
std::scoped_lock lock {mutex_clients};
return clients[client_endpoint].player_id;
}
void set_input(tcp::endpoint client_endpoint, const ClientMessage& msg) {
if (state != State::Playing) { return; }
auto player_id = get_player_id(client_endpoint);
if (!player_id) { return; };
std::scoped_lock lock {mutex_players};
players[*player_id].msg = msg;
}
void handle_client_msg(tcp::endpoint client_endpoint, const ClientMessagePlaceBomb& msg) {
println("Client wants to place a bomb");
set_input(client_endpoint, msg);
}
void handle_client_msg(tcp::endpoint client_endpoint, const ClientMessagePlaceBlock& msg) {
println("Client wants to place a block!!");
set_input(client_endpoint, msg);
}
void handle_client_msg(tcp::endpoint client_endpoint, const ClientMessageMove& msg) {
println("Client wants to move to:", msg.direction);
set_input(client_endpoint, msg);
}
public:
Server(ServerParams params, port_t port, seed_t seed)
: params(params),
port(port),
random(seed)
{}
void accept_clients() {
tcp::acceptor a (io_service, tcp::endpoint(tcp::v6(), port));
while (true) {
// println("Waiting for client...");
std::shared_ptr<tcp::socket> sock {new tcp::socket(io_service)};
a.accept(*sock);
sock->set_option(ip::tcp::no_delay(true));
std::thread handler {
[this, sock] { this->handle_session(sock); }
};
handler.detach();
}
}
void start() {
std::thread acceptor {[this] { accept_clients(); }};
std::chrono::duration<turn_duration_t, std::milli> turn_duration {params.turn_duration};
while (true) {
println("Lobby.");
state = State::Lobby;
await_players();
init_game();
broadcast_message(ServerMessageGameStarted {});
for (game_length_t turn = 0; turn < params.game_length; ++turn) {
broadcast_turn(turn);
state = State::Playing;
std::this_thread::sleep_for(turn_duration);
apply_player_moves();
println("End of turn:", turn);
}
finish_game();
println("End of game!");
}
acceptor.join();
}
void handle_session(std::shared_ptr<tcp::socket> sock) {
{
// we do so without getting mutex - because the mutex we use for the
// whole map and here we modify something that only this thread accesses
// also, do it before client_connected, because afterwise writes may
// occur to this socket in another thread
streamable_buffer sbuffer;
sbuffer << hello;
try {
send(sbuffer, *sock);
} catch (const boost::system::system_error& e) {
std::cerr << "Error: unable to send hello" << std::endl;
return;
}
}
ip::tcp::endpoint client_endpoint;
try {
client_endpoint = sock->remote_endpoint();
client_connected(sock);
} catch (const boost::system::system_error& e) {
std::cerr << "Error: unable to connect client" << std::endl;
return;
}
streamable_buffer sbuffer;
sbuffer.set_provider([sock](size_t n){ return read(*sock, n); });
while (true) {
ClientMessage msg;
try {
sbuffer >> msg;
} catch (const boost::system::system_error& e) {
std::cerr << "Error: unable to read from client" << std::endl;
client_disconnected(client_endpoint);
return;
} catch (const invalid_message& e) {
std::cerr << "Error: invalid message from client" << std::endl;
client_disconnected(client_endpoint);
return;
}
if (std::holds_alternative<ClientMessageMove>(msg)) {
if (std::get<ClientMessageMove>(msg).direction > 3) {
std::cerr << "Client: Invalid direction value" << std::endl;
client_disconnected(client_endpoint);
return;
}
}
std::visit(
[this, client_endpoint] (auto&& x) {
handle_client_msg(client_endpoint, x);
},
msg
);
}
}
};
int main(int argc, char* argv[]) {
po::options_description desc("Options");
static_assert(sizeof(uint32_t) >= sizeof(players_count_t));
desc.add_options()
("help,h", "display help message")
("bomb-timer,b", po::value<bomb_timer_t>()->required(), "bomb timer")
// reading uint8_t with std::cin is tricky, read something wider
("players-count,c", po::value<uint32_t>()->required(), "players count")
("turn-duration,d", po::value<turn_duration_t>()->required(), "turn duration")
("explosion-radius,e", po::value<explosion_radius_t>()->required(), "explosion radius")
("initial-blocks,k", po::value<initial_blocks_t>()->required(), "initial blocks")
("game-length,l", po::value<game_length_t>()->required(), "game-length")
("server-name,n", po::value<std::string>()->required(), "server name")
("port,p", po::value<port_t>()->required(), "port")
(
"seed,s",
po::value<seed_t>()->default_value(
static_cast<seed_t>(std::chrono::system_clock::now().time_since_epoch().count())
),
"seed"
)
("size-x,x", po::value<pos_t>()->required(), "size x")
("size-y,y", po::value<pos_t>()->required(), "size y")
;
po::variables_map vm;
try {
po::store(po::parse_command_line(argc, argv, desc), vm);
} catch (const po::error& e) {
std::cerr << e.what() << std::endl;
return 1;
}
if (vm.count("help")) {
std::cout << desc << std::endl;
return 0;
}
try {
po::notify(vm);
} catch (const po::error& e) {
std::cerr << e.what() << std::endl;
return 1;
}
uint32_t raw_n_players = vm["players-count"].as<uint32_t>();
if (raw_n_players > std::numeric_limits<players_count_t>::max()) {
std::cerr << "Too many players required! Should be 0-255" << std::endl;
return 1;
}
ServerParams params {
.bomb_timer = vm["bomb-timer"].as<bomb_timer_t>(),
.players_count = static_cast<players_count_t>(raw_n_players),
.turn_duration = vm["turn-duration"].as<turn_duration_t>(),
.explosion_radius = vm["explosion-radius"].as<explosion_radius_t>(),
.initial_blocks = vm["initial-blocks"].as<initial_blocks_t>(),
.game_length = vm["game-length"].as<game_length_t>(),
.server_name = vm["server-name"].as<server_name_t>(),
.size_x = vm["size-x"].as<pos_t>(),
.size_y = vm["size-y"].as<pos_t>()
};
port_t port = vm["port"].as<port_t>();
seed_t seed = vm["seed"].as<seed_t>();
Server server (params, port, seed);
server.start();
return 0;
}