forked from yedf2/handy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hsha.cc
41 lines (38 loc) · 1.19 KB
/
hsha.cc
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
#include <handy/handy.h>
using namespace std;
using namespace handy;
int main(int argc, const char *argv[]) {
setloglevel("TRACE");
EventBase base;
HSHAPtr hsha = HSHA::startServer(&base, "", 2099, 4);
exitif(!hsha, "bind failed");
Signal::signal(SIGINT, [&, hsha] {
base.exit();
hsha->exit();
signal(SIGINT, SIG_DFL);
});
hsha->onMsg(new LineCodec, [](const TcpConnPtr &con, const string &input) {
int ms = rand() % 1000;
info("processing a msg");
usleep(ms * 1000);
return util::format("%s used %d ms", input.c_str(), ms);
});
for (int i = 0; i < 5; i++) {
TcpConnPtr con = TcpConn::createConnection(&base, "localhost", 2099);
con->onMsg(new LineCodec, [](const TcpConnPtr &con, Slice msg) {
info("%.*s recved", (int) msg.size(), msg.data());
con->close();
});
con->onState([](const TcpConnPtr &con) {
if (con->getState() == TcpConn::Connected) {
con->sendMsg("hello");
}
});
}
base.runAfter(1000, [&, hsha] {
base.exit();
hsha->exit();
});
base.loop();
info("program exited");
}