TcpPlugin.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef __TCPPLUGIN_HPP_
  2. #define __TCPPLUGIN_HPP_
  3. #include <iostream>
  4. #include "UtilBase.hpp"
  5. #include "TCPClient.hpp"
  6. #include "requests.hpp"
  7. #include "config.hpp"
  8. #include "md5.h"
  9. #include "uuid.hpp"
  10. #include "ExpelDevice.h"
  11. #include "ExpelPlugin.hpp"
  12. #include "uuid.hpp"
  13. #include "PluginSubscriber.hpp"
  14. using namespace std;
  15. using namespace utility;
  16. namespace gsd
  17. {
  18. class TcpPlugin: public PluginSubscriber<TcpPlugin>, public PluginBase
  19. {
  20. private:
  21. TcpPlugin(): PluginBase(){
  22. }
  23. public:
  24. using Ptr = std::shared_ptr<TcpPlugin>;
  25. /**
  26. * @description: getPtr
  27. * @return {*}
  28. */
  29. static std::shared_ptr<TcpPlugin> getPtr();
  30. /**
  31. * @description: CreateNew
  32. * @return {*}
  33. */
  34. static std::shared_ptr<TcpPlugin> CreateNew();
  35. ~TcpPlugin(){}
  36. /**
  37. * @description: 初始化
  38. * @return {*}
  39. */
  40. virtual bool Init();
  41. /**
  42. * @description:
  43. * @return {*}
  44. */
  45. virtual bool StartTask();
  46. /**
  47. * @description: 释放
  48. * @return {*}
  49. */
  50. virtual void Destroy();
  51. /**
  52. * @description:
  53. * @return {*}
  54. */
  55. virtual bool Alive();
  56. /**
  57. * @description: 发送心跳数据
  58. * @return {*}
  59. */
  60. void SendHeartbeatData();
  61. /**
  62. * @description: 登录Netty
  63. * @return {*}
  64. */
  65. void TCPLoginNetty();
  66. /**
  67. * @description: 获取登录结果
  68. * @return {*}
  69. */
  70. bool getLoginNetty();
  71. /**
  72. * @description: 监听Netty的数据
  73. * @param {Ptr} &buf
  74. * @return {*}
  75. */
  76. void ListenNettyData(const Buffer::Ptr &buf);
  77. /**
  78. * @description: 发送请求
  79. * @param {string} RequestId UUID
  80. * @param {string} CommandEnum 命令枚举
  81. * @param {string&} data 数据
  82. * @param {function<void(std::string)>} t 回调函数(请求结果, 返回的数据)
  83. * @return {*}
  84. */
  85. void sendRequest(std::string RequestId,std::string CommandEnum, std::string& data,function<void(int, std::string)> t);
  86. /**
  87. * @description: RestPlugin
  88. * @return {*}
  89. */
  90. virtual bool RestPlugin(){
  91. return true;
  92. }
  93. private:
  94. // TCP对象
  95. TCPClient::Ptr tcpClient = nullptr;
  96. Timer::Ptr HeartbeatTimer = nullptr;
  97. Timer::Ptr NettyLoginTimer = nullptr;
  98. bool TcpNettyLogin = false;
  99. // 线程池
  100. std::shared_ptr<ThreadPool> pool = nullptr;
  101. };
  102. } // namespace gsd
  103. #endif