-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
44 lines (39 loc) · 1.25 KB
/
server.cpp
File metadata and controls
44 lines (39 loc) · 1.25 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
#include "Connection.hpp"
#include "Game.hpp"
#include <iostream>
#include <set>
#include <chrono>
int main(int argc, char **argv) {
if (argc != 2) {
std::cerr << "Usage:\n\t./server <port>" << std::endl;
return 1;
}
Server server(argv[1]);
Game state;
while (1) {
server.poll([&](Connection *c, Connection::Event evt){
if (evt == Connection::OnOpen) {
} else if (evt == Connection::OnClose) {
} else { assert(evt == Connection::OnRecv);
if (c->recv_buffer[0] == 'h') {
c->recv_buffer.erase(c->recv_buffer.begin(), c->recv_buffer.begin() + 1);
std::cout << c << ": Got hello." << std::endl;
} else if (c->recv_buffer[0] == 's') {
if (c->recv_buffer.size() < 1 + sizeof(float)) {
return; //wait for more data
} else {
memcpy(&state.paddle.x, c->recv_buffer.data() + 1, sizeof(float));
c->recv_buffer.erase(c->recv_buffer.begin(), c->recv_buffer.begin() + 1 + sizeof(float));
}
}
}
}, 0.01);
//every second or so, dump the current paddle position:
static auto then = std::chrono::steady_clock::now();
auto now = std::chrono::steady_clock::now();
if (now > then + std::chrono::seconds(1)) {
then = now;
std::cout << "Current paddle position: " << state.paddle.x << std::endl;
}
}
}