#ifndef CWebsocketServer_h #define CWebsocketServer_h #include "openssl/ec.h" #include "websocketpp/config/asio.hpp" #include "websocketpp/server.hpp" #include "CMessage.h" #include "CWSCodec.h" #include "CSocketClient.h" #include "ISocketCallback.h" #include #include #include "baseEx.h" #include "EntitySessionManager.h" #include #include typedef websocketpp::server server; typedef websocketpp::server server_wss; // pull out the type of messages sent by our config typedef websocketpp::config::asio::message_type::ptr message_ptr; typedef websocketpp::lib::shared_ptr context_ptr; namespace Chromium { extern server m_wsserver, m_ws_sm2_server; extern server_wss m_server_wss; class CWebsocketServer : public ISocketCallback { public: CWebsocketServer(const char* strPath, CEntityBase* m_pEntity); static void stopServer() { m_wsserver.stop(); m_ws_sm2_server.stop(); //m_server_wss.stop(); DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("%s end", __FUNCTION__); } ~CWebsocketServer() { m_doRunThread.interrupt(); m_doReLinkThread.interrupt(); m_doRunThread.join(); m_doReLinkThread.join(); m_wsserver.stop(); m_ws_sm2_server.stop(); //m_server_wss.stop(); DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("%s end", __FUNCTION__); } void run(); // socket callbacks void message_from_socket(CMessage& msg, unsigned int id); bool isWebSocketSuceess() { return m_initSuccess; } const std::map& getNotifyPool() { return ConfigManager::getInstance().m_notifyPool; }; 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); //static server m_wsserver; void do_send_msg(int hdl, std::string msg); void do_send_msg(websocketpp::connection_hdl hdl, std::string msg); private: void message_handler(websocketpp::connection_hdl hdl, server::message_ptr msg); void message_handler_ws_sm2(websocketpp::connection_hdl hdl, server::message_ptr msg); void deal_msg(std::string& payload, websocketpp::connection_hdl hdl, int srcTransId); void deal_webchromium_msg(std::string& payload, websocketpp::connection_hdl hdl, int messageType); void deal_sessionBreakMsg(std::string& payload, websocketpp::connection_hdl hdl); void deal_logMsg(std::string& payload, websocketpp::connection_hdl hdl, int messageType); void open_handler(websocketpp::connection_hdl hdl); void open_handler_wss(websocketpp::connection_hdl hdl); void open_handler_ws_sm2(websocketpp::connection_hdl hdl); void close_handler(websocketpp::connection_hdl hdl); void close_handler_wss(websocketpp::connection_hdl hdl); void close_handler_ws_sm2(websocketpp::connection_hdl hdl); void do_run(); void init_websocket(); void init_entity_sessions(); void do_sendJson(std::string js, int messageType, int hdlID, unsigned int id, socket_msgInfo &msgInfo); void do_sendJsonBroadcast(std::string js); template bool sendBroadCast(T &connections, Y &senders, std::string msg); void WriteToFramework(CMessage* msg); #if(defined _WIN32 || defined _WIN64) std::string string_to_utf8(const std::string& str); std::string utf8_to_string(const std::string& str); #endif void myTest(); void updateMsgPool(std::string entityName, std::string& payload, websocketpp::connection_hdl hdl); void storeEntityWithCLass(std::string entityName, std::string entityClass); std::pair getEntityClass(std::string entityName); void do_sendJsonStartSession(std::string entityName, std::string entityClass); void do_relink(); std::pair restroreTransId(std::string payLoad); void updateNotifyPool(unsigned hdl, unsigned transId); //if transId == 0 , unregister the item, else insert a register private: CWSCodec* m_serializer; CSocketClient* m_socket; EntitySessionManager* m_esm; boost::asio::io_service m_ios; boost::thread m_doRunThread, m_doReLinkThread; CEntityBase* m_pEntity; boost::mutex m_dealMsgLock; bool m_initSuccess; }; } #endif