CWebsocketServer.h 3.2 KB

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