forked from yedf2/handy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodec-cli.cc
28 lines (25 loc) · 941 Bytes
/
codec-cli.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
#include <handy/handy.h>
using namespace std;
using namespace handy;
void reconnect2(EventBase* base, string host, short port) {
TcpConnPtr con = TcpConn::createConnection(base, host, port);
con->onMsg(new LengthCodec, [](const TcpConnPtr& con, Slice msg) {
info("recv msg: %.*s", (int)msg.size(), msg.data());
});
con->onState([=](const TcpConnPtr& con) {
info("onState called state: %d", con->getState());
if (con->getState() == TcpConn::Connected) {
con->sendMsg("hello");
} else if (con->getState() == TcpConn::Closed || con->getState() == TcpConn::Failed) {
base->runAfter(3000, [=] { reconnect2(base, host, port); });
}
});
}
int main(int argc, const char* argv[]) {
setloglevel("TRACE");
EventBase base;
Signal::signal(SIGINT, [&]{ base.exit(); });
reconnect2(&base, "localhost", 99);
base.loop();
info("program exited");
}