test_tcpclient_reconnect.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <iostream>
  2. #include <string>
  3. #include "tcpclient.h"
  4. #include "mswin_special/sys/DumpFile.h"
  5. using namespace std;
  6. using namespace uv;
  7. std::string serverip;
  8. int call_time = 0;
  9. bool is_exist = false;
  10. void CloseCB(int clientid, void* userdata)
  11. {
  12. fprintf(stdout, "cliend close\n");
  13. TCPClient* client = (TCPClient*)userdata;
  14. client->Close();
  15. }
  16. void ReadCB(const NetPacket& packet, const unsigned char* buf, void* userdata)
  17. {
  18. char senddata[256] = {0};
  19. TCPClient* client = (TCPClient*)userdata;
  20. sprintf(senddata, "****recv server data(%p,%d)", client, packet.datalen);
  21. fprintf(stdout, "%s\n", senddata);
  22. NetPacket tmppack = packet;
  23. tmppack.datalen = (std::min)(strlen(senddata), sizeof(senddata) - 1);
  24. std::string retstr = PacketData(tmppack, (const unsigned char*)senddata);
  25. if (client->Send(&retstr[0], retstr.length()) <= 0) {
  26. fprintf(stdout, "(%p)send error.%s\n", client, client->GetLastErrMsg());
  27. }
  28. fprintf(stdout, "call time %d\n", ++call_time);
  29. //if (call_time > 50) {
  30. // is_exist = true;
  31. //}
  32. }
  33. void ReConnectCB(NET_EVENT_TYPE eventtype, void* userdata)
  34. {
  35. TCPClient* client = (TCPClient*)userdata;
  36. if (NET_EVENT_TYPE_RECONNECT == eventtype) {
  37. fprintf(stdout, "succeed reconnect.\n");
  38. char senddata[256];
  39. memset(senddata, 0, sizeof(senddata));
  40. sprintf(senddata, "client(%p) call %d", client, ++call_time);
  41. NetPacket packet;
  42. packet.header = 0x01;
  43. packet.tail = 0x02;
  44. packet.datalen = (std::min)(strlen(senddata), sizeof(senddata) - 1);
  45. std::string str = PacketData(packet, (const unsigned char*)senddata);
  46. if (client->Send(&str[0], str.length()) <= 0) {
  47. fprintf(stdout, "(%p)send error.%s\n", client, client->GetLastErrMsg());
  48. } else {
  49. fprintf(stdout, "send succeed:%s\n", senddata);
  50. }
  51. } else {
  52. fprintf(stdout, "server disconnect.\n");
  53. }
  54. }
  55. int main(int argc, char** argv)
  56. {
  57. if (argc != 3) {
  58. fprintf(stdout, "usage: %s server_ip_address clientcount\neg.%s 192.168.1.1 50\n", argv[0], argv[0]);
  59. return 0;
  60. }
  61. DeclareDumpFile();
  62. serverip = argv[1];
  63. const int clientsize = std::stoi(argv[2]);
  64. TCPClient pClients(0x01, 0x02);
  65. TCPClient::StartLog("log/");
  66. int i = 0;
  67. char senddata[256];
  68. pClients.SetRecvCB(ReadCB, &pClients);
  69. pClients.SetClosedCB(CloseCB, &pClients);
  70. pClients.SetReconnectCB(ReConnectCB, &pClients);
  71. if (!pClients.Connect(serverip.c_str(), 12345)) {
  72. fprintf(stdout, "connect error:%s\n", pClients.GetLastErrMsg());
  73. } else {
  74. fprintf(stdout, "client(%p) connect succeed.\n", &pClients);
  75. }
  76. memset(senddata, 0, sizeof(senddata));
  77. sprintf(senddata, "client(%p) call %d", &pClients, ++call_time);
  78. NetPacket packet;
  79. packet.header = 0x01;
  80. packet.tail = 0x02;
  81. packet.datalen = (std::min)(strlen(senddata), sizeof(senddata) - 1);
  82. std::string str = PacketData(packet, (const unsigned char*)senddata);
  83. if (pClients.Send(&str[0], str.length()) <= 0) {
  84. fprintf(stdout, "(%p)send error.%s\n", &pClients, pClients.GetLastErrMsg());
  85. } else {
  86. fprintf(stdout, "send succeed:%s\n", senddata);
  87. }
  88. while (!is_exist) {
  89. Sleep(10);
  90. }
  91. return 0;
  92. }