wsClientMgr.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @brief
  3. websocket客户端管理器类
  4. * @version
  5. V 1.0.0
  6. * @author
  7. 王益俊
  8. * @date
  9. 创建时间: 2018-08-17\n
  10. * @note
  11. 2018-08-17 创建类。\n
  12. * @warning
  13. * @bug
  14. */
  15. #pragma once
  16. #include <vector>
  17. #include <string>
  18. #include <map>
  19. #include <boost/serialization/singleton.hpp>
  20. #include "wsClient.h"
  21. #include "ws_common.h"
  22. #include "jsonBuilder.h"
  23. namespace YA
  24. {
  25. class wsClientMgr
  26. {
  27. private:
  28. std::vector<std::shared_ptr<wsClient>> __wsClientList;//ws客户端列表
  29. std::map<std::string,int> __uriIndexList;//uri和客户端下标对应表
  30. std::string __LastError;//最新错误
  31. std::recursive_mutex __lock;//锁
  32. jsonBuilder __jsBuilder;//json生成器
  33. public:
  34. wsClientMgr();
  35. ~wsClientMgr();
  36. /**
  37. * @brief 重载[]运算符。
  38. * @param [in] int index 下标\n
  39. * @return 下标对应的客户端连接\n
  40. */
  41. std::shared_ptr<wsClient> operator[]( int index )
  42. {
  43. std::lock_guard<std::recursive_mutex> lock( __lock );
  44. std::shared_ptr<wsClient> pClient;
  45. if ( index < 0 )
  46. {
  47. return pClient;
  48. }
  49. if ( index < (int)__wsClientList.size() )
  50. {
  51. pClient = __wsClientList[index];
  52. }
  53. return pClient;
  54. }
  55. /**
  56. * @brief 构造函数。
  57. * @param [in] const std::vector<std::string> uri_list 服务器uri列表\n
  58. * @param [in] const std::map<std::string, MSG_HANDLE_FUNC_TYPE>& MsgFuncList 处理函数列表\n
  59. * @return 连接服务器是否成功\n @retval 0: 成功 -1: 失败\n
  60. */
  61. void Build( const std::vector<std::string> uri_list, const std::map<std::string, MSG_HANDLE_FUNC_TYPE>& MsgFuncList );
  62. /**
  63. * @brief 连接函数。
  64. * @param [in] int time_out 超时(单位:秒),默认是3秒\n
  65. * @return 连接服务器是否成功.0: 成功 -1: 失败\n
  66. */
  67. int connect( int time_out = 3 );
  68. /**
  69. * @brief 登录函数。
  70. */
  71. void login();
  72. /**
  73. * @brief 发送数据函数。
  74. * @param [in] const std::string & Cmd 要发送的命令\n
  75. * @param [in] const std::string& Data 要发送的数据\n
  76. */
  77. void send( const std::string & Cmd, const std::string & Data );
  78. /**
  79. * @brief 根据URI(连接串)获得对应的客户端函数。
  80. * @param [in] const std::string& URI \n
  81. * @return 获得的客户端连接\n
  82. */
  83. std::shared_ptr<wsClient> GetClientByURI( const std::string& URI );
  84. /**
  85. * @brief 关闭函数。
  86. */
  87. void close();
  88. /**
  89. * @brief 获得是否已连接服务器端函数。
  90. * @return 是否已连接服务器\n
  91. */
  92. bool IsConnected();
  93. /**
  94. * @brief 获得是否已登录成功函数。
  95. * @return 是否已登录成功\n
  96. */
  97. bool IsLogined();
  98. /**
  99. * @brief 获得Socket是否已打开函数。
  100. * @return Socket是否已打开\n
  101. */
  102. bool IsSktOpened();
  103. /**
  104. * @brief 发送车辆进入特殊区域函数。
  105. * @param [in] const _BASE_CARD_& stCard 卡结构体\n
  106. */
  107. void SendSpecialAreaProcess( const _BASE_CARD_& stCard );
  108. /**
  109. * @brief 检测是否有连接断开,并重连
  110. * @param [in] cur_time : 当前时刻 (秒)
  111. */
  112. void CheckClientConnect(unsigned int cur_time);
  113. private:
  114. /**
  115. * @brief 指定的wsClient重连webServer。
  116. * @param [in] ws : 连接
  117. */
  118. void Reconnect(std::shared_ptr<wsClient> & ws);
  119. /**
  120. * @brief 断开指定的wsCliennt
  121. * @param [in] ws : 连接
  122. */
  123. void Disconnet(std::shared_ptr<wsClient> & ws);
  124. };
  125. }
  126. //单件相关定义
  127. typedef boost::serialization::singleton<YA::wsClientMgr> singleton_wsClientMgr;
  128. #define swsClientMgr singleton_wsClientMgr::get_mutable_instance()
  129. #define swsClientMgr_const singleton_wsClientMgr::get_const_instance()