-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
reconnect.cc
23 lines (22 loc) · 874 Bytes
/
reconnect.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <handy/handy.h>
using namespace handy;
int main(int argc, const char *argv[]) {
setloglevel("TRACE");
EventBase base;
Signal::signal(SIGINT, [&] { base.exit(); });
TcpServerPtr svr = TcpServer::startServer(&base, "", 2099);
exitif(svr == NULL, "start tcp server failed");
svr->onConnState([&](const TcpConnPtr &con) { // 200ms后关闭连接
if (con->getState() == TcpConn::Connected)
base.runAfter(200, [con]() {
info("close con after 200ms");
con->close();
});
});
TcpConnPtr con1 = TcpConn::createConnection(&base, "localhost", 2099);
con1->setReconnectInterval(300);
// TcpConnPtr con2 = TcpConn::createConnection(&base, "localhost", 1, 100);
// con2->setReconnectInterval(200);
base.runAfter(600, [&]() { base.exit(); });
base.loop();
}