CWebsocketServer.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #ifndef CWebsocketServer_h
  2. #define CWebsocketServer_h
  3. #include "openssl/ec.h"
  4. #include "websocketpp/config/asio.hpp"
  5. #include "websocketpp/server.hpp"
  6. #include "CMessage.h"
  7. #include "CWSCodec.h"
  8. #include "CSocketClient.h"
  9. #include "ISocketCallback.h"
  10. #include <boost/asio.hpp>
  11. #include <boost/thread/mutex.hpp>
  12. #include "baseEx.h"
  13. #include "EntitySessionManager.h"
  14. #include <memory>
  15. #include <vector>
  16. typedef websocketpp::server<websocketpp::config::asio> server;
  17. typedef websocketpp::server<websocketpp::config::asio_tls> server_wss;
  18. // pull out the type of messages sent by our config
  19. typedef websocketpp::config::asio::message_type::ptr message_ptr;
  20. typedef websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context> context_ptr;
  21. namespace Chromium {
  22. extern server m_wsserver, m_ws_sm2_server;
  23. extern server_wss m_server_wss;
  24. class CWebsocketServer : public ISocketCallback {
  25. public:
  26. CWebsocketServer(const char* strPath, CEntityBase* m_pEntity);
  27. static void stopServer() {
  28. m_wsserver.stop();
  29. m_ws_sm2_server.stop();
  30. //m_server_wss.stop();
  31. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("%s end", __FUNCTION__);
  32. }
  33. ~CWebsocketServer()
  34. {
  35. m_doRunThread.interrupt();
  36. m_doReLinkThread.interrupt();
  37. m_doRunThread.join();
  38. m_doReLinkThread.join();
  39. m_wsserver.stop();
  40. m_ws_sm2_server.stop();
  41. //m_server_wss.stop();
  42. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("%s end", __FUNCTION__);
  43. }
  44. void run();
  45. // socket callbacks
  46. void message_from_socket(CMessage& msg, unsigned int id);
  47. bool isWebSocketSuceess() { return m_initSuccess; }
  48. const std::map<unsigned, unsigned>& getNotifyPool() {
  49. return m_notifyPool;
  50. };
  51. void do_send_notifyMsg(unsigned hdlID, unsigned transId, const std::string& reason, const std::string& errmsg, const std::string& rebootTime, const DWORD& dwSysError, const DWORD& dwUserCode);
  52. //static server m_wsserver;
  53. void do_send_msg(int hdl, std::string msg);
  54. void do_send_msg(websocketpp::connection_hdl hdl, std::string msg);
  55. private:
  56. void message_handler(websocketpp::connection_hdl hdl, server::message_ptr msg);
  57. void message_handler_ws_sm2(websocketpp::connection_hdl hdl, server::message_ptr msg);
  58. void deal_msg(std::string& payload, websocketpp::connection_hdl hdl);
  59. void deal_webchromium_msg(std::string& payload, websocketpp::connection_hdl hdl, int messageType);
  60. void deal_sessionBreakMsg(std::string& payload, websocketpp::connection_hdl hdl);
  61. void deal_logMsg(std::string& payload, websocketpp::connection_hdl hdl, int messageType);
  62. void open_handler(websocketpp::connection_hdl hdl);
  63. void open_handler_wss(websocketpp::connection_hdl hdl);
  64. void open_handler_ws_sm2(websocketpp::connection_hdl hdl);
  65. void close_handler(websocketpp::connection_hdl hdl);
  66. void close_handler_wss(websocketpp::connection_hdl hdl);
  67. void close_handler_ws_sm2(websocketpp::connection_hdl hdl);
  68. void do_run();
  69. void init_websocket();
  70. void init_entity_sessions();
  71. void do_sendJson(std::string js, int messageType, int hdlID, unsigned int id);
  72. void do_sendJsonBroadcast(std::string js);
  73. template <typename T, typename Y>
  74. bool sendBroadCast(T &connections, Y &senders, std::string msg);
  75. void WriteToFramework(CMessage* msg);
  76. #if(defined _WIN32 || defined _WIN64)
  77. std::string string_to_utf8(const std::string& str);
  78. std::string utf8_to_string(const std::string& str);
  79. #endif
  80. void myTest();
  81. void updateMsgPool(std::string entityName, std::string& payload, websocketpp::connection_hdl hdl);
  82. void storeEntityWithCLass(std::string entityName, std::string entityClass);
  83. std::pair<bool, std::string> getEntityClass(std::string entityName);
  84. void do_sendJsonStartSession(std::string entityName, std::string entityClass);
  85. void do_relink();
  86. std::string restroreTransId(std::string payLoad);
  87. void updateNotifyPool(unsigned hdl, unsigned transId); //if transId == 0 , unregister the item, else insert a register
  88. private:
  89. CWSCodec* m_serializer;
  90. CSocketClient* m_socket;
  91. EntitySessionManager* m_esm;
  92. boost::asio::io_service m_ios;
  93. boost::thread m_doRunThread, m_doReLinkThread;
  94. std::map<unsigned int, websocketpp::connection_hdl> m_connection_hdls;
  95. std::map<unsigned int, websocketpp::connection_hdl> m_connection_ws_sm2_hdls;
  96. std::map<unsigned int, websocketpp::connection_hdl> m_connection_wss_hdls;
  97. std::map<unsigned int, std::shared_ptr<SM2_Encrypt_Manager>> m_ws_sm2_hdls_manager;
  98. std::map<std::string, std::vector<std::pair<websocketpp::connection_hdl, std::string>> > m_msg_pool;
  99. std::map<std::string, std::string> m_entityAndClass;//不知道为什么不能用map<string,map<>>,猜测应该是websocketpp::connection_hdl生成问题
  100. CEntityBase* m_pEntity;
  101. boost::mutex m_dealMsgLock;
  102. bool m_initSuccess;
  103. std::map<unsigned, unsigned> m_notifyPool; //hdl和notify transId对应
  104. };
  105. }
  106. #endif