linux版本中间件

WebSocketServer.h 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #pragma once
  2. #include <websocketpp/config/asio.hpp>
  3. #include <websocketpp/server.hpp>
  4. #include <websocketpp/common/thread.hpp>
  5. #include <websocketpp/common/memory.hpp>
  6. #include <boost/bind.hpp>
  7. #include <functional>
  8. #include <iostream>
  9. #include <string>
  10. #include <codecvt>
  11. #include <thread>
  12. #include <unordered_map>
  13. #include <mutex>
  14. #include <regex>
  15. #include <json/json.h>
  16. //#pragma comment(lib,"libssl.lib")
  17. //#pragma comment(lib,"libcrypto.lib")
  18. //#pragma comment(lib,"Crypt32.lib")
  19. typedef websocketpp::server<websocketpp::config::asio> server_plain;
  20. typedef websocketpp::server<websocketpp::config::asio_tls> server_tls;
  21. typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
  22. enum WEBSOCKET_TYPE
  23. {
  24. NORMAL = 0,
  25. TLS = 1,
  26. };
  27. class CWebSocketServer
  28. {
  29. public:
  30. CWebSocketServer();
  31. ~CWebSocketServer();
  32. bool init(uint16_t port ,uint16_t tlsPort ,std::string sslFile,std::string sslPwd);
  33. bool sendMsg(websocketpp::connection_hdl hdl, std::string msg);
  34. bool sendMsg(long conID, std::string msg);
  35. void run();
  36. void asyncRun();
  37. void close(websocketpp::connection_hdl hdl); // 关闭与指定客户端的连接
  38. void stop();
  39. typedef std::function<void(const long&, const std::string &, const std::string &)> Callback_RecvMsg;
  40. typedef std::function<void(const long&)> Callback_Close;
  41. void setCallbackRecvMsg(Callback_RecvMsg pRecvFun) { this->pRecvFun = pRecvFun; };
  42. void setCallbackClose(Callback_Close pCloseFun) { this->pCloseFun = pCloseFun; };
  43. private:
  44. template <typename EndpointType>
  45. void on_open(EndpointType* s, websocketpp::connection_hdl hdl, WEBSOCKET_TYPE type) {
  46. const long conID = reinterpret_cast<long>(hdl.lock().get());
  47. std::unique_lock<std::mutex>lock(m_mut);
  48. m_servers.insert(std::make_pair(hdl.lock().get(), type));
  49. m_conIds.insert(std::make_pair(conID,hdl));
  50. }
  51. template <typename EndpointType>
  52. void on_close(EndpointType* s, websocketpp::connection_hdl hdl, WEBSOCKET_TYPE type) {
  53. const long conID = reinterpret_cast<long>(hdl.lock().get());
  54. if (this->pCloseFun != nullptr)
  55. this->pCloseFun(conID); // 连接断开回调函数
  56. std::unique_lock<std::mutex>lock(m_mut);
  57. m_servers.erase(hdl.lock().get());
  58. m_conIds.erase(conID);
  59. }
  60. template <typename EndpointType>
  61. void on_message(EndpointType* s, websocketpp::connection_hdl hdl, typename EndpointType::message_ptr msg) {
  62. const long conID = reinterpret_cast<long>(hdl.lock().get());
  63. const std::string msgInfo = std::move(msg->get_payload());
  64. const std::string str = std::regex_replace(s->get_con_from_hdl(hdl)->get_remote_endpoint(), std::regex("::ffff:"),"");
  65. if (this->pRecvFun != nullptr && !msgInfo.empty())
  66. this->pRecvFun(conID, msgInfo, str); // 收到消息回调函数
  67. }
  68. std::string get_password() {
  69. return m_sslPwd;
  70. }
  71. context_ptr on_tls_init(websocketpp::connection_hdl hdl) {
  72. std::cout << "on_tls_init called with hdl: " << hdl.lock().get() << std::endl;
  73. context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23_server);
  74. try {
  75. ctx->set_options(boost::asio::ssl::context::default_workarounds |
  76. boost::asio::ssl::context::no_sslv2 |
  77. boost::asio::ssl::context::no_sslv3 |
  78. boost::asio::ssl::context::single_dh_use);
  79. ctx->set_password_callback(bind(&CWebSocketServer::get_password,this));
  80. ctx->use_certificate_chain_file(m_sslFile);
  81. ctx->use_private_key_file(m_sslFile, boost::asio::ssl::context::pem);
  82. }
  83. catch (std::exception& e) {
  84. char str[1024];
  85. snprintf(str, 1024, "[%s :%d][ %s]", __FUNCTION__, __LINE__, e.what());
  86. std::cout << str << std::endl;
  87. }
  88. return ctx;
  89. }
  90. private:
  91. boost::asio::io_service ios;
  92. websocketpp::lib::shared_ptr<websocketpp::lib::thread> m_Thread; // 线程
  93. server_plain endpoint_plain;
  94. server_tls endpoint_tls;
  95. std::string m_sslFile;
  96. std::string m_sslPwd;
  97. std::unordered_map<void*, WEBSOCKET_TYPE> m_servers;
  98. std::unordered_map<long, websocketpp::connection_hdl> m_conIds;
  99. std::mutex m_mut; //map锁
  100. Callback_RecvMsg pRecvFun; // 接收消息回调函数
  101. Callback_Close pCloseFun; // 连接断开回调函数
  102. };