forked from yedf2/handy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yedf
committed
Mar 21, 2016
1 parent
79ddeb2
commit e4594b1
Showing
13 changed files
with
145 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#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, "", 99); | ||
exitif(svr == NULL, "start tcp server failed"); | ||
svr->onConnState([](const TcpConnPtr& con) { | ||
if (con->getState() == TcpConn::Connected) { | ||
con->addIdleCB(2, [](const TcpConnPtr& con){ | ||
info("idle for 2 seconds, close connection"); | ||
con->close(); | ||
}); | ||
} | ||
}); | ||
auto con = TcpConn::createConnection(&base, "localhost", 99); | ||
base.runAfter(3000, [&](){base.exit();}); | ||
base.loop(); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#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, "", 99); | ||
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", 99); | ||
con1->setReconnectInterval(300); | ||
// TcpConnPtr con2 = TcpConn::createConnection(&base, "localhost", 1, 100); | ||
// con2->setReconnectInterval(200); | ||
base.runAfter(600, [&](){base.exit();}); | ||
base.loop(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <handy/handy.h> | ||
using namespace handy; | ||
|
||
int main(int argc, const char* argv[]) { | ||
EventBase base; | ||
Signal::signal(SIGINT, [&]{ base.exit(); }); | ||
TcpServerPtr svr = TcpServer::startServer(&base, "", 99); | ||
exitif(svr == NULL, "start tcp server failed"); | ||
TcpConnPtr con = TcpConn::createConnection(&base, "localhost", 99); | ||
std::thread th([con,&base](){ | ||
sleep(1); | ||
info("thread want to close an connection"); | ||
base.safeCall([con](){ con->close(); }); //其他线程需要操作连接,应当通过safeCall把操作交给io线程来做 | ||
}); | ||
base.runAfter(1500, [&base](){base.exit();}); | ||
base.loop(); | ||
th.join(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include <handy/handy.h> | ||
using namespace handy; | ||
|
||
int main(int argc, const char* argv[]) { | ||
EventBase base; | ||
Signal::signal(SIGINT, [&]{ base.exit(); }); | ||
info("program begin"); | ||
base.runAfter(200, [](){ | ||
info("a task in runAfter 200ms"); | ||
}); | ||
base.runAfter(100, [](){ | ||
info("a task in runAfter 100ms interval 1000ms"); | ||
}, 1000); | ||
TimerId id = base.runAt(time(NULL)*1000+300, [](){ | ||
info("a task in runAt now+300 interval 500ms"); | ||
}, 500); | ||
base.runAfter(2000, [&](){ | ||
info("cancel task of interval 500ms"); | ||
base.cancel(id); | ||
}); | ||
base.runAfter(3000, [&](){ | ||
base.exit(); | ||
}); | ||
base.loop(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters