/
echo_s.cpp
56 lines (48 loc) · 1.21 KB
/
echo_s.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
#include <iostream>
#include <string>
#include <thread>
#include <functional>
#include "socket.hpp"
int main(int argc, char** argv)
{
using namespace std;
if(argc != 2)
{
cerr << "USAGE: " << argv[0] << " [port]" << endl;
return 1;
}
try
{
auto port = uint16_t(stoul(argv[1]));
auto server = server_socket(port);
server.set_accept_handler(
[&](server_socket& ss, client_socket cs, host_info_t info)
{
cout << info.host << " (" << info.ip << ") : " << info.port << " connected" << endl;
cs.set_data_handler([=, sref = ref(server)](client_socket& cs, socket_buffer_t data)
{
auto msg = string((const char*)data.data(), data.size());
if(msg == "die")
{
sref.get().close();
}
else if(!msg.empty())
{
cout << info.host << " > " << msg << endl;
cs.send({ rbegin(data), rend(data) });
}
});
thread([=, cs = std::move(cs)]() mutable
{
while(cs.receive());
cout << info.host << " (" << info.ip << ") : " << info.port << " disconnected" << endl;
}).detach();
});
cout << "[ctrl-c] to exit, listening on port " << port << endl;
while(server.accept());
}
catch(std::exception& ex)
{
cerr << ex.what() << endl;
}
}