-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
67 lines (53 loc) · 1.21 KB
/
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
#include "clarcnet.h"
#include <execinfo.h>
#include <iostream>
#include <mutex>
#include <thread>
using namespace clarcnet;
using namespace std;
void network(server* sv) {
for (;;) {
packets ps = sv->process();
// std::cout << "got " << ps.size() << " packets" << std::endl;
for (auto const& p : ps) {
switch (p.mid) {
case ID_CONNECTION:
{
std::cout << "client " << p.fd << " connected from " << sv->address(p.fd) << std::endl;
packet r(0, ID_USER);
r.push_back('w');
r.push_back('e');
r.push_back('l');
r.push_back('c');
r.push_back('o');
r.push_back('m');
r.push_back('e');
sv->send(p.fd, std::move(r));
}
break;
case ID_DISCONNECTION:
{
std::cout << "client " << p.fd << " disconnected" << std::endl;
}
break;
case ID_USER:
{
std::cout << "client " << p.fd << " sent user data of size " << p.size() << std::endl;
for (auto i = 1; i < p.size(); ++i)
std::cout << p[i];
std::cout << std::endl;
}
break;
default:
break;
}
}
}
}
int main(int argc, char* argv[]) {
server* sv = new server(1111);
cout << "created server" << endl;
thread t(network, sv);
t.join();
delete sv;
}