TcpPlugin.hpp 2.6 KB

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