diff options
| author | Per Larsson <[email protected]> | 2022-02-21 15:00:02 +0100 |
|---|---|---|
| committer | Per Larsson <[email protected]> | 2022-02-21 15:00:02 +0100 |
| commit | 41782efc63d7f88525596d6724a1bb86d6fdcfa4 (patch) | |
| tree | 80567b8cc256c361abb8f81b53680f2fa6ec6fcc /zenhttp/websocketasio.cpp | |
| parent | Refactored websocket message. (diff) | |
| download | zen-41782efc63d7f88525596d6724a1bb86d6fdcfa4.tar.xz zen-41782efc63d7f88525596d6724a1bb86d6fdcfa4.zip | |
Added option to enable websockets.
Diffstat (limited to 'zenhttp/websocketasio.cpp')
| -rw-r--r-- | zenhttp/websocketasio.cpp | 51 |
1 files changed, 26 insertions, 25 deletions
diff --git a/zenhttp/websocketasio.cpp b/zenhttp/websocketasio.cpp index 13d0177ee..c2ce7ca64 100644 --- a/zenhttp/websocketasio.cpp +++ b/zenhttp/websocketasio.cpp @@ -582,10 +582,10 @@ WsThreadPool::Stop() class WsServer final : public WebSocketServer { public: - WsServer() = default; + WsServer(const WebSocketServerOptions& Options) : m_Options(Options) {} virtual ~WsServer() { Shutdown(); } - virtual bool Run(const WebSocketServerOptions& Options) override; + virtual bool Run() override; virtual void Shutdown() override; virtual void RegisterService(WebSocketService& Service) override; @@ -614,6 +614,7 @@ private: using RequestHandlerMap = std::unordered_map<std::string_view, WebSocketService*>; using NotificationHandlerMap = std::unordered_map<std::string_view, std::vector<WebSocketService*>>; + WebSocketServerOptions m_Options; asio::io_service m_IoSvc; std::unique_ptr<asio::ip::tcp::acceptor> m_Acceptor; std::unique_ptr<WsThreadPool> m_ThreadPool; @@ -634,7 +635,7 @@ WsServer::RegisterService(WebSocketService& Service) } bool -WsServer::Run(const WebSocketServerOptions& Options) +WsServer::Run() { m_Acceptor = std::make_unique<asio::ip::tcp::acceptor>(m_IoSvc, asio::ip::tcp::v6()); @@ -645,7 +646,7 @@ WsServer::Run(const WebSocketServerOptions& Options) m_Acceptor->set_option(asio::socket_base::send_buffer_size(256 * 1024)); asio::error_code Ec; - m_Acceptor->bind(asio::ip::tcp::endpoint(asio::ip::address_v6::any(), Options.Port), Ec); + m_Acceptor->bind(asio::ip::tcp::endpoint(asio::ip::address_v6::any(), m_Options.Port), Ec); if (Ec) { @@ -657,12 +658,12 @@ WsServer::Run(const WebSocketServerOptions& Options) m_Acceptor->listen(); m_Running = true; - ZEN_LOG_INFO(LogWebSocket, "web socket server running on port '{}'", Options.Port); + ZEN_LOG_INFO(LogWebSocket, "web socket server running on port '{}'", m_Options.Port); AcceptConnection(); m_ThreadPool = std::make_unique<WsThreadPool>(m_IoSvc); - m_ThreadPool->Start(Options.ThreadCount); + m_ThreadPool->Start(m_Options.ThreadCount); return true; } @@ -720,29 +721,29 @@ WsServer::AcceptConnection() asio::ip::tcp::socket& SocketRef = *Socket.get(); m_Acceptor->async_accept(SocketRef, [this, ConnectedSocket = std::move(Socket)](const asio::error_code& Ec) mutable { - if (Ec) - { - ZEN_LOG_WARN(LogWebSocket, "accept connection FAILED, reason '{}'", Ec.message()); - } - else + if (m_Running) { - auto Connection = std::make_shared<WsConnection>(WebSocketId::New(), std::move(ConnectedSocket)); - - ZEN_LOG_DEBUG(LogWebSocket, "accept connection '#{} {}' OK", Connection->Id().Value(), Connection->RemoteAddr()); - + if (Ec) { - std::unique_lock _(m_ConnMutex); - m_Connections[Connection->Id()] = Connection; + ZEN_LOG_WARN(LogWebSocket, "accept connection FAILED, reason '{}'", Ec.message()); } + else + { + auto Connection = std::make_shared<WsConnection>(WebSocketId::New(), std::move(ConnectedSocket)); - Connection->SetParser(std::make_unique<HttpMessageParser>(HttpMessageParserType::kRequest)); - Connection->SetState(WebSocketState::kHandshaking); + ZEN_LOG_DEBUG(LogWebSocket, "accept connection '#{} {}' OK", Connection->Id().Value(), Connection->RemoteAddr()); - ReadMessage(Connection); - } + { + std::unique_lock _(m_ConnMutex); + m_Connections[Connection->Id()] = Connection; + } + + Connection->SetParser(std::make_unique<HttpMessageParser>(HttpMessageParserType::kRequest)); + Connection->SetState(WebSocketState::kHandshaking); + + ReadMessage(Connection); + } - if (m_Running) - { AcceptConnection(); } }); @@ -1522,9 +1523,9 @@ WebSocketService::Configure(WebSocketServer& Server) } std::unique_ptr<WebSocketServer> -WebSocketServer::Create() +WebSocketServer::Create(const WebSocketServerOptions& Options) { - return std::make_unique<websocket::WsServer>(); + return std::make_unique<websocket::WsServer>(Options); } std::shared_ptr<WebSocketClient> |