TCPClient.hpp 3.2 KB

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