wsClientMgr.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. 重载[]运算符。
  39. * @param [in] int index 下标\n
  40. * @return 下标对应的客户端\n
  41. * @note
  42. * @warning
  43. * @bug
  44. */
  45. std::shared_ptr<wsClient> operator[]( int index )
  46. {
  47. std::lock_guard<std::recursive_mutex> lock( __lock );
  48. std::shared_ptr<wsClient> pClient;
  49. if ( index < 0 )
  50. {
  51. return pClient;
  52. }
  53. if ( index < (int)__wsClientList.size() )
  54. {
  55. pClient = __wsClientList[index];
  56. }
  57. return pClient;
  58. }
  59. /**
  60. * @brief
  61. ;构造函数。
  62. * @param [in] const std::vector<std::string> uri_list 服务器uri列表\n
  63. * @param [in] const std::map<std::string, MSG_HANDLE_FUNC_TYPE>& MsgFuncList 处理函数列表\n
  64. * @return 连接服务器是否成功\n
  65. * @retval 0 成功\n
  66. * @retval -1 失败\n
  67. * @note
  68. * @warning
  69. * @bug
  70. */
  71. void Build( const std::vector<std::string> uri_list, const std::map<std::string, MSG_HANDLE_FUNC_TYPE>& MsgFuncList );
  72. /**
  73. * @brief
  74. ;连接函数。
  75. * @param [in] int time_out 超时(单位:秒),默认是3秒\n
  76. * @return 连接服务器是否成功\n
  77. * @retval 0 成功\n
  78. * @retval -1 失败\n
  79. * @note
  80. * @warning
  81. * @bug
  82. */
  83. int connect( int time_out = 3 );
  84. /**
  85. * @brief
  86. ;登录函数。
  87. * @param 无\n
  88. * @return 无\n
  89. * @note
  90. * @warning
  91. * @bug
  92. */
  93. void login();
  94. /**
  95. * @brief
  96. 发送数据函数。
  97. * @param [in] const std::string & Cmd 要发送的命令\n
  98. * @param [in] const std::string& Data 要发送的数据\n
  99. * @return 无\n
  100. * @note
  101. * @warning
  102. * @bug
  103. */
  104. void send( const std::string & Cmd, const std::string & Data );
  105. /**
  106. * @brief
  107. 根据URI(连接串)获得对应的客户端函数。
  108. * @param [in] const std::string& URI \n
  109. * @return 获得的客户端\n
  110. * @note
  111. * @warning
  112. * @bug
  113. */
  114. std::shared_ptr<wsClient> GetClientByURI( const std::string& URI );
  115. /**
  116. * @brief
  117. 关闭函数。
  118. * @param 无\n
  119. * @return 无\n
  120. * @note
  121. * @warning
  122. * @bug
  123. */
  124. void close();
  125. /**
  126. * @brief
  127. 获得是否已连接服务器端函数。
  128. * @param 无\n
  129. * @return 是否已连接服务器\n
  130. * @note
  131. * @warning
  132. * @bug
  133. */
  134. bool IsConnected();
  135. /**
  136. * @brief
  137. 获得是否已登录成功函数。
  138. * @param 无\n
  139. * @return 是否已登录成功\n
  140. * @note
  141. * @warning
  142. * @bug
  143. */
  144. bool IsLogined();
  145. /**
  146. * @brief
  147. 获得Socket是否已打开函数。
  148. * @param 无\n
  149. * @return Socket是否已打开\n
  150. * @note
  151. * @warning
  152. * @bug
  153. */
  154. bool IsSktOpened();
  155. /**
  156. * @brief
  157. 发送车辆进入特殊区域函数。
  158. * @param [in] const _BASE_CARD_& stCard 卡结构体\n
  159. * @return 无\n
  160. * @note
  161. * @warning
  162. * @bug
  163. */
  164. void SendSpecialAreaProcess( const _BASE_CARD_& stCard );
  165. };
  166. }
  167. //单件相关定义
  168. typedef boost::serialization::singleton<YA::wsClientMgr> singleton_wsClientMgr;
  169. #define swsClientMgr singleton_wsClientMgr::get_mutable_instance()
  170. #define swsClientMgr_const singleton_wsClientMgr::get_const_instance()