TCPClient.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #pragma once
  2. #include <signal.h>
  3. #include <iostream>
  4. #include "Util/logger.h"
  5. #include "Network/TcpClient.h"
  6. #include "Util/NoticeCenter.h"
  7. #include "Notices.h"
  8. #include <condition_variable>
  9. #include <ctime>
  10. #include <map>
  11. #include <thread>
  12. #include "Util/util.h"
  13. #include "Util/logger.h"
  14. #include "Network/Socket.h"
  15. #include "Util/TimeTicker.h"
  16. #include "Util/NoticeCenter.h"
  17. #include "Poller/Timer.h"
  18. using namespace std;
  19. using namespace toolkit;
  20. #define Request_timeout 4000
  21. #define Internal_Server_Error 500
  22. #define Unauthorized 401
  23. #define Forbiddem 403
  24. #define Request_Time_out 408
  25. class TCPClient:public TcpClient
  26. {
  27. public:
  28. struct RequestAckTask
  29. {
  30. std::shared_ptr<Ticker> ticker = nullptr;
  31. std::string CommandEnum;
  32. function<void(int, std::string)> task; // 回调任务
  33. bool Finish = false;
  34. };
  35. public:
  36. typedef std::shared_ptr<TCPClient> Ptr;
  37. TCPClient():TcpClient() {
  38. }
  39. ~TCPClient() {}
  40. /**
  41. * @description: 发送请求
  42. * @param {string} RequestId UUID
  43. * @param {string} CommandEnum 命令枚举
  44. * @param {string&} data 数据
  45. * @param {function<void(std::string)>} t 回调函数(请求结果, 返回的数据)
  46. * @return {*}
  47. */
  48. void sendRequest(std::string RequestId,std::string CommandEnum, std::string& data,function<void(int, std::string)> t);
  49. /**
  50. * @description: 接收请求响应
  51. * @param {string&} json
  52. * @return {*}
  53. */
  54. int requestAck(std::string& json);
  55. protected:
  56. /**
  57. * @description: 连接成功时触发
  58. * @param {SockException} &ex 连接info
  59. * @return {*}
  60. * @author: lishengyin
  61. */
  62. virtual void onConnect(const SockException &ex) override;
  63. /**
  64. * @description: 接收数据时触发
  65. * @param {Ptr} &pBuf 接受到的数据
  66. * @return {*}
  67. * @author: lishengyin
  68. */
  69. virtual void onRecv(const Buffer::Ptr &pBuf) override;
  70. /**
  71. * @description: 发送阻塞时触发
  72. * @param {*}
  73. * @return {*}
  74. * @author: lishengyin
  75. */
  76. virtual void onFlush() override;
  77. /**
  78. * @description: EOF时触发
  79. * @param {SockException} &ex 错误信息
  80. * @return {*}
  81. * @author: lishengyin
  82. */
  83. virtual void onErr(const SockException &ex) override;
  84. /**
  85. * @description: 心跳 2S触发一次
  86. * @param {*}
  87. * @return {*}
  88. * @author: lishengyin
  89. */
  90. virtual void onManager() override;
  91. /**
  92. * @description: 超时处理
  93. * @param {*}
  94. * @return {*}
  95. */
  96. void requestOvertimeWork(std::string requestId, std::string CommandEnum);
  97. private:
  98. int _nTick = 0;
  99. std::string _IP;
  100. int32_t _port;
  101. Buffer::Ptr sbuf = nullptr;
  102. map<std::string, RequestAckTask> requestTasks;
  103. };