wsClientMgr.h 3.4 KB

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