1
0

TcpClientEventLoop_test.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * TcpClientEventLoop_test.cpp
  3. *
  4. * @build make evpp
  5. * @server bin/TcpServer_test 1234
  6. * @client bin/TcpClientEventLoop_test 1234
  7. *
  8. */
  9. #include "TcpClient.h"
  10. #include "htime.h"
  11. #define TEST_RECONNECT 1
  12. #define TEST_TLS 0
  13. using namespace hv;
  14. class MyTcpClient : public TcpClient {
  15. public:
  16. MyTcpClient(EventLoopPtr loop = NULL) : TcpClient(loop) {
  17. onConnection = [this](const SocketChannelPtr& channel) {
  18. std::string peeraddr = channel->peeraddr();
  19. if (channel->isConnected()) {
  20. printf("connected to %s! connfd=%d\n", peeraddr.c_str(), channel->fd());
  21. // send(time) every 3s
  22. setInterval(3000, [channel](TimerID timerID){
  23. if (channel->isConnected()) {
  24. if (channel->isWriteComplete()) {
  25. char str[DATETIME_FMT_BUFLEN] = {0};
  26. datetime_t dt = datetime_now();
  27. datetime_fmt(&dt, str);
  28. channel->write(str);
  29. }
  30. } else {
  31. killTimer(timerID);
  32. }
  33. });
  34. } else {
  35. printf("disconnected to %s! connfd=%d\n", peeraddr.c_str(), channel->fd());
  36. }
  37. if (isReconnect()) {
  38. printf("reconnect cnt=%d, delay=%d\n", reconn_setting->cur_retry_cnt, reconn_setting->cur_delay);
  39. }
  40. };
  41. onMessage = [](const SocketChannelPtr& channel, Buffer* buf) {
  42. printf("< %.*s\n", (int)buf->size(), (char*)buf->data());
  43. };
  44. }
  45. int connect(int port) {
  46. int connfd = createsocket(port);
  47. if (connfd < 0) {
  48. return connfd;
  49. }
  50. #if TEST_RECONNECT
  51. // reconnect: 1,2,4,8,10,10,10...
  52. reconn_setting_t reconn;
  53. reconn_setting_init(&reconn);
  54. reconn.min_delay = 1000;
  55. reconn.max_delay = 10000;
  56. reconn.delay_policy = 2;
  57. setReconnect(&reconn);
  58. #endif
  59. #if TEST_TLS
  60. withTLS();
  61. #endif
  62. printf("client connect to port %d, connfd=%d ...\n", port, connfd);
  63. start();
  64. return connfd;
  65. }
  66. };
  67. typedef std::shared_ptr<MyTcpClient> MyTcpClientPtr;
  68. int TestMultiClientsRunInOneEventLoop(int port, int nclients) {
  69. auto loop_thread = std::make_shared<EventLoopThread>();
  70. loop_thread->start();
  71. std::map<int, MyTcpClientPtr> clients;
  72. for (int i = 0; i < nclients; ++i) {
  73. MyTcpClient* client = new MyTcpClient(loop_thread->loop());
  74. client->connect(port);
  75. clients[i] = MyTcpClientPtr(client);
  76. }
  77. // press Enter to stop
  78. while (getchar() != '\n');
  79. loop_thread->stop();
  80. loop_thread->join();
  81. return 0;
  82. }
  83. int main(int argc, char* argv[]) {
  84. if (argc < 2) {
  85. printf("Usage: %s port\n", argv[0]);
  86. return -10;
  87. }
  88. int port = atoi(argv[1]);
  89. int nclients = 100;
  90. if (argc > 2) {
  91. nclients = atoi(argv[2]);
  92. }
  93. return TestMultiClientsRunInOneEventLoop(port, nclients);
  94. }