123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- #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 <boost/asio.hpp>
- #include <boost/thread/mutex.hpp>
- #include "baseEx.h"
- #include "EntitySessionManager.h"
- #include <memory>
- #include <vector>
- typedef websocketpp::server<websocketpp::config::asio> server;
- typedef websocketpp::server<websocketpp::config::asio_tls> 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<websocketpp::lib::asio::ssl::context> 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<unsigned, unsigned>& getNotifyPool() {
- return 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);
- 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);
- void do_sendJsonBroadcast(std::string js);
- template <typename T, typename Y>
- 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<bool, std::string> getEntityClass(std::string entityName);
- void do_sendJsonStartSession(std::string entityName, std::string entityClass);
- void do_relink();
- std::string 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;
- std::map<unsigned int, websocketpp::connection_hdl> m_connection_hdls;
- std::map<unsigned int, websocketpp::connection_hdl> m_connection_ws_sm2_hdls;
- std::map<unsigned int, websocketpp::connection_hdl> m_connection_wss_hdls;
- std::map<unsigned int, std::shared_ptr<SM2_Encrypt_Manager>> m_ws_sm2_hdls_manager;
- std::map<std::string, std::vector<std::pair<websocketpp::connection_hdl, std::string>> > m_msg_pool;
- std::map<std::string, std::string> m_entityAndClass;//不知道为什么不能用map<string,map<>>,猜测应该是websocketpp::connection_hdl生成问题
- CEntityBase* m_pEntity;
- boost::mutex m_dealMsgLock;
- bool m_initSuccess;
- std::map<unsigned, unsigned> m_notifyPool; //hdl和notify transId对应
- };
- }
- #endif
|