1
0

TcpClientEventLoop_test.cpp 3.7 KB

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