中间件底层,websocket

NetClient.h 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. #include <websocketpp/config/asio_no_tls_client.hpp>
  3. #include <websocketpp/client.hpp>
  4. #include <boost/asio/steady_timer.hpp >
  5. #include <iostream>
  6. #include <atomic>
  7. #include <afx.h>
  8. #include "NetInterface.h"
  9. #include "Scheduler.h"
  10. using websocketpp::lib::placeholders::_1;
  11. using websocketpp::lib::placeholders::_2;
  12. class CPduEntity;
  13. class CNetClient:public CNetInterface
  14. {
  15. typedef websocketpp::client<websocketpp::config::asio_client> client;
  16. typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
  17. typedef CNetClient this_type;
  18. public:
  19. CNetClient();
  20. virtual ~CNetClient();
  21. // 接口函数
  22. virtual void setLinkStateChanged(LinkStateChangedFuncCB funcCB) ; // PDU连接状态变化事件处理
  23. virtual void setRecvCommand(RecvComdFuncCB funcCB) ; // PDU命令到达事件处理
  24. virtual bool CreatePduClient(CString a_strFarIp,
  25. int a_nFarPort,
  26. PDU_DEV_TYPE a_nLocalType,
  27. int a_nLocalId,
  28. bool IsAutoReconnect);
  29. virtual bool ClosePduClient(PDU_DEV_TYPE a_nFarType,
  30. int a_nFarId); // 关闭PDU通讯客户端
  31. virtual bool Send(CPduEntity* a_pCmd, PDU_DEV_TYPE a_nDestType = PDU_DEV_TYPE_UNKNOWN, int a_nDestId = 0);
  32. private:
  33. void SetLinkContent(PDU_LINK_STATE a_nLinkInfo);
  34. void SetTimerListen(); // 开启心跳定时
  35. void KillTimerListen(); // 关闭心跳定时
  36. void SetTimerConn(); // 开启自动连接
  37. void KillTimerConn(); // 关闭自动连接
  38. private:
  39. void on_open(websocketpp::connection_hdl hdl);
  40. void on_fail(websocketpp::connection_hdl hdl);
  41. void on_close(websocketpp::connection_hdl hdl);
  42. void on_message(websocketpp::connection_hdl hdl, message_ptr msg);
  43. int init();
  44. bool ConnectToServer(const CString& a_strFarIp,
  45. int a_nFarPort,
  46. PDU_DEV_TYPE a_nLocalType,
  47. int a_nLocalId); // 连接到SERVER端
  48. bool connect();
  49. void close();
  50. void terminate();
  51. public:
  52. static CNetClient* getInstance() { return &instance; }
  53. private:
  54. bool __SendBuf(unsigned char a_szBuf[], std::uint32_t a_nBufLen);
  55. void onTime(const std::int32_t& TimerId);
  56. inline std::string devTypeToStr(PDU_DEV_TYPE devType);
  57. private:
  58. boost::asio::io_service ios;
  59. client c;
  60. websocketpp::lib::shared_ptr<websocketpp::lib::thread> thread_;
  61. websocketpp::lib::shared_ptr<websocketpp::lib::thread> thread1_;
  62. websocketpp::connection_hdl hdl_;
  63. std::string uri;
  64. std::unique_ptr<CScheduler> m_pSchedulerHeart; // 心跳定时器
  65. std::unique_ptr<CScheduler> m_pSchedulerAutoConn; // 心跳定时器
  66. // 属性
  67. PDU_DEV_TYPE m_DevType; // 本端设备类型
  68. int m_DevId; // 本端设备ID
  69. bool m_IsAutoReconnect; // 是否自动重连
  70. std::atomic_bool m_IsConnSuccess; // 连接是否成功
  71. RecvComdFuncCB m_RecvComdFuncCB; // 消息回调函数
  72. LinkStateChangedFuncCB m_LinkStateChangedFuncCB; // 连接状态变化回调函数
  73. static CNetClient instance;
  74. };
  75. //需要重复打开关闭连接时,只调用一次init_asio()函数,
  76. //然后调用start_perpetual()将endpoint设置为永久的,
  77. //不会在连接断开时自动退出。需要结束循环,调用stop_perpetual()