diff options
Diffstat (limited to 'src/zenhttp/servers/wsasio.h')
| -rw-r--r-- | src/zenhttp/servers/wsasio.h | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/src/zenhttp/servers/wsasio.h b/src/zenhttp/servers/wsasio.h index e8bb3b1d2..64602ee46 100644 --- a/src/zenhttp/servers/wsasio.h +++ b/src/zenhttp/servers/wsasio.h @@ -8,6 +8,12 @@ ZEN_THIRD_PARTY_INCLUDES_START #include <asio.hpp> +#if defined(ASIO_HAS_LOCAL_SOCKETS) +# include <asio/local/stream_protocol.hpp> +#endif +#if ZEN_USE_OPENSSL +# include <asio/ssl.hpp> +#endif ZEN_THIRD_PARTY_INCLUDES_END #include <deque> @@ -21,22 +27,23 @@ class HttpServer; namespace zen::asio_http { /** - * WebSocket connection over an ASIO TCP socket + * WebSocket connection over an ASIO stream socket * - * Owns the TCP socket (moved from HttpServerConnection after the 101 handshake) + * Templated on SocketType to support both TCP and Unix domain sockets. + * Owns the socket (moved from HttpServerConnection after the 101 handshake) * and runs an async read/write loop to exchange WebSocket frames. * * Lifetime is managed solely through intrusive reference counting (RefCounted). - * The async read/write callbacks capture Ref<WsAsioConnection> to keep the - * connection alive for the duration of the async operation. The service layer - * also holds a Ref<WebSocketConnection>. + * The async read/write callbacks capture Ref<> to keep the connection alive + * for the duration of the async operation. The service layer also holds a + * Ref<WebSocketConnection>. */ - -class WsAsioConnection : public WebSocketConnection +template<typename SocketType> +class WsAsioConnectionT : public WebSocketConnection { public: - WsAsioConnection(std::unique_ptr<asio::ip::tcp::socket> Socket, IWebSocketHandler& Handler, HttpServer* Server); - ~WsAsioConnection() override; + WsAsioConnectionT(std::unique_ptr<SocketType> Socket, IWebSocketHandler& Handler, HttpServer* Server); + ~WsAsioConnectionT() override; /** * Start the async read loop. Must be called once after construction @@ -61,10 +68,10 @@ private: void DoClose(uint16_t Code, std::string_view Reason); - std::unique_ptr<asio::ip::tcp::socket> m_Socket; - IWebSocketHandler& m_Handler; - zen::HttpServer* m_HttpServer; - asio::streambuf m_ReadBuffer; + std::unique_ptr<SocketType> m_Socket; + IWebSocketHandler& m_Handler; + zen::HttpServer* m_HttpServer; + asio::streambuf m_ReadBuffer; RwLock m_WriteLock; std::deque<std::vector<uint8_t>> m_WriteQueue; @@ -74,4 +81,14 @@ private: std::atomic<bool> m_CloseSent{false}; }; +using WsAsioConnection = WsAsioConnectionT<asio::ip::tcp::socket>; + +#if defined(ASIO_HAS_LOCAL_SOCKETS) +using WsAsioUnixConnection = WsAsioConnectionT<asio::local::stream_protocol::socket>; +#endif + +#if ZEN_USE_OPENSSL +using WsAsioSslConnection = WsAsioConnectionT<asio::ssl::stream<asio::ip::tcp::socket>>; +#endif + } // namespace zen::asio_http |