gsd_TcpPlugin.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. *
  3. * gsd_TcpPlugin.h
  4. *
  5. */
  6. #pragma once
  7. #include <drogon/plugins/Plugin.h>
  8. #include "TCPClient.hpp"
  9. #include "Util/TimeTicker.h"
  10. #include "Poller/Timer.h"
  11. #include "dataTypeList.h"
  12. #include "md5.h"
  13. #include "uuid.hpp"
  14. #include "Util/SqlPool.h"
  15. #include "expel.h"
  16. #include <drogon/drogon.h>
  17. using namespace toolkit;
  18. namespace gsd
  19. {
  20. class TcpPlugin : public drogon::Plugin<TcpPlugin>
  21. {
  22. public:
  23. TcpPlugin() {}
  24. /// This method must be called by drogon to initialize and start the plugin.
  25. /// It must be implemented by the user.
  26. virtual void initAndStart(const Json::Value &config) override;
  27. /// This method must be called by drogon to shutdown the plugin.
  28. /// It must be implemented by the user.
  29. virtual void shutdown() override;
  30. /**
  31. * @description: 启动任务
  32. * @return {*}
  33. */
  34. void StartTask();
  35. /**
  36. * @description: 发送心跳数据
  37. * @return {*}
  38. */
  39. void SendHeartbeatData();
  40. /**
  41. * @description: 登录Netty
  42. * @return {*}
  43. */
  44. void TCPLoginNetty();
  45. /**
  46. * @description: 获取登录结果
  47. * @return {*}
  48. */
  49. bool getLoginNetty();
  50. /**
  51. * @description: 监听Netty的数据
  52. * @param {Ptr} &buf
  53. * @return {*}
  54. */
  55. void ListenNettyData(const Buffer::Ptr &buf);
  56. /**
  57. * @description: 获取与Netty的通讯是否存活
  58. * @return {*}
  59. */
  60. bool getAlive();
  61. /**
  62. * @description: 发送请求
  63. * @param {string} RequestId UUID
  64. * @param {string} CommandEnum 命令枚举
  65. * @param {string&} data 数据
  66. * @param {function<void(std::string)>} t 回调函数(请求结果, 返回的数据)
  67. * @return {*}
  68. */
  69. void sendRequest(std::string RequestId,std::string CommandEnum, std::string& data,function<void(int, std::string)> t);
  70. private:
  71. // TCP对象
  72. TCPClient::Ptr tcpClient = nullptr;
  73. // 心跳定时器
  74. Timer::Ptr HeartbeatTimer = nullptr;
  75. bool TcpNettyLogin = false;
  76. // 线程池
  77. std::shared_ptr<ThreadPool> pool = nullptr;
  78. };
  79. }