CWebsocketServer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. namespace Chromium {
  17. typedef websocketpp::server<websocketpp::config::asio> server;
  18. extern server m_wsserver;
  19. class CWebsocketServer : public ISocketCallback {
  20. public:
  21. CWebsocketServer(const char* strPath, CEntityBase* m_pEntity);
  22. static void stopServer() {
  23. m_wsserver.stop();
  24. DbgEx("%s end", __FUNCTION__);
  25. }
  26. ~CWebsocketServer()
  27. {
  28. m_doRunThread.interrupt();
  29. m_doReLinkThread.interrupt();
  30. m_doRunThread.join();
  31. m_doReLinkThread.join();
  32. m_wsserver.stop();
  33. DbgEx("%s end", __FUNCTION__);
  34. }
  35. void run();
  36. // socket callbacks
  37. void message_from_socket(CMessage& msg, unsigned int id);
  38. bool isWebSocketSuceess() { return m_initSuccess; }
  39. const std::map<unsigned, unsigned>& getNotifyPool() {
  40. return m_notifyPool;
  41. };
  42. 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);
  43. //static server m_wsserver;
  44. private:
  45. void message_handler(websocketpp::connection_hdl hdl, server::message_ptr msg);
  46. void deal_msg(std::string& payload, websocketpp::connection_hdl hdl);
  47. void deal_webchromium_msg(std::string& payload, websocketpp::connection_hdl hdl, int messageType);
  48. void deal_sessionBreakMsg(std::string& payload, websocketpp::connection_hdl hdl);
  49. void deal_logMsg(std::string& payload, websocketpp::connection_hdl hdl, int messageType);
  50. void open_handler(websocketpp::connection_hdl hdl);
  51. void close_handler(websocketpp::connection_hdl hdl);
  52. void do_run();
  53. void init_websocket();
  54. void init_entity_sessions();
  55. void do_sendJson(std::string js, int messageType, int hdlID, unsigned int id);
  56. void do_sendJsonBroadcast(std::string js);
  57. void WriteToFramework(CMessage* msg);
  58. #if(defined _WIN32 || defined _WIN64)
  59. std::string string_to_utf8(const std::string& str);
  60. std::string utf8_to_string(const std::string& str);
  61. #endif
  62. void myTest();
  63. void updateMsgPool(std::string entityName, std::string& payload, websocketpp::connection_hdl hdl);
  64. void storeEntityWithCLass(std::string entityName, std::string entityClass);
  65. std::pair<bool, std::string> getEntityClass(std::string entityName);
  66. void do_sendJsonStartSession(std::string entityName, std::string entityClass);
  67. void do_relink();
  68. std::string restroreTransId(std::string payLoad);
  69. void updateNotifyPool(unsigned hdl, unsigned transId); //if transId == 0 , unregister the item, else insert a register
  70. private:
  71. CWSCodec* m_serializer;
  72. CSocketClient* m_socket;
  73. EntitySessionManager* m_esm;
  74. boost::asio::io_service m_ios;
  75. boost::thread m_doRunThread, m_doReLinkThread;
  76. std::map<unsigned int, websocketpp::connection_hdl> m_connection_hdls;
  77. std::map<std::string, std::vector<std::pair<websocketpp::connection_hdl, std::string>> > m_msg_pool;
  78. std::map<std::string, std::string> m_entityAndClass;//不知道为什么不能用map<string,map<>>,猜测应该是websocketpp::connection_hdl生成问题
  79. CEntityBase* m_pEntity;
  80. boost::mutex m_dealMsgLock;
  81. bool m_initSuccess;
  82. std::map<unsigned, unsigned> m_notifyPool; //hdl和notify transId对应
  83. };
  84. }
  85. #endif