diff options
| author | Stefan Boberg <[email protected]> | 2023-10-04 13:13:23 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-10-04 13:13:23 +0200 |
| commit | 3e8db7cd243e8be3b2d5fea2490b9ad70f765590 (patch) | |
| tree | c1533ae25b0b717dd2393960c5449aadd56807c4 /src/zenhttp/include | |
| parent | factored out http parser from asio into separate files (#444) (diff) | |
| download | zen-3e8db7cd243e8be3b2d5fea2490b9ad70f765590.tar.xz zen-3e8db7cd243e8be3b2d5fea2490b9ad70f765590.zip | |
removed websocket protocol support(#445)
removed websocket support since it is not used right now and is unlikely to be used in the future
Diffstat (limited to 'src/zenhttp/include')
| -rw-r--r-- | src/zenhttp/include/zenhttp/httptest.h | 6 | ||||
| -rw-r--r-- | src/zenhttp/include/zenhttp/websocket.h | 256 |
2 files changed, 1 insertions, 261 deletions
diff --git a/src/zenhttp/include/zenhttp/httptest.h b/src/zenhttp/include/zenhttp/httptest.h index 74db69785..a4008fb5e 100644 --- a/src/zenhttp/include/zenhttp/httptest.h +++ b/src/zenhttp/include/zenhttp/httptest.h @@ -5,7 +5,6 @@ #include <zencore/logging.h> #include <zencore/stats.h> #include <zenhttp/httpserver.h> -#include <zenhttp/websocket.h> #include <atomic> @@ -16,7 +15,7 @@ namespace zen { /** * Test service to facilitate testing the HTTP framework and client interactions */ -class HttpTestingService : public HttpService, public WebSocketService +class HttpTestingService : public HttpService { public: HttpTestingService(); @@ -43,9 +42,6 @@ public: }; private: - virtual void RegisterHandlers(WebSocketServer& Server) override; - virtual bool HandleRequest(const WebSocketMessage& Request) override; - HttpRequestRouter m_Router; std::atomic<uint32_t> m_Counter{0}; metrics::OperationTiming m_TimingStats; diff --git a/src/zenhttp/include/zenhttp/websocket.h b/src/zenhttp/include/zenhttp/websocket.h deleted file mode 100644 index adca7e988..000000000 --- a/src/zenhttp/include/zenhttp/websocket.h +++ /dev/null @@ -1,256 +0,0 @@ -// Copyright Epic Games, Inc. All Rights Reserved. - -#include <zencore/compactbinarypackage.h> -#include <zencore/memory.h> - -#include <compare> -#include <functional> -#include <future> -#include <memory> -#include <optional> - -#pragma once - -namespace asio { -class io_context; -} - -namespace zen { - -class BinaryWriter; - -/** - * A unique socket ID. - */ -class WebSocketId -{ - static std::atomic_uint32_t NextId; - -public: - WebSocketId() = default; - - uint32_t Value() const { return m_Value; } - - auto operator<=>(const WebSocketId&) const = default; - - static WebSocketId New() { return WebSocketId(NextId.fetch_add(1)); } - -private: - WebSocketId(uint32_t Value) : m_Value(Value) {} - - uint32_t m_Value{}; -}; - -/** - * Type of web socket message. - */ -enum class WebSocketMessageType : uint8_t -{ - kInvalid, - kNotification, - kRequest, - kStreamRequest, - kResponse, - kStreamResponse, - kStreamCompleteResponse, - kCount -}; - -inline std::string_view -ToString(WebSocketMessageType Type) -{ - switch (Type) - { - case WebSocketMessageType::kInvalid: - return std::string_view("Invalid"); - case WebSocketMessageType::kNotification: - return std::string_view("Notification"); - case WebSocketMessageType::kRequest: - return std::string_view("Request"); - case WebSocketMessageType::kStreamRequest: - return std::string_view("StreamRequest"); - case WebSocketMessageType::kResponse: - return std::string_view("Response"); - case WebSocketMessageType::kStreamResponse: - return std::string_view("StreamResponse"); - case WebSocketMessageType::kStreamCompleteResponse: - return std::string_view("StreamCompleteResponse"); - default: - return std::string_view("Unknown"); - }; -} - -/** - * Web socket message. - */ -class WebSocketMessage -{ - struct Header - { - static constexpr uint32_t ExpectedMagic = 0x7a776d68; // zwmh - - uint64_t MessageSize{}; - uint32_t Magic{ExpectedMagic}; - uint32_t CorrelationId{}; - uint32_t StatusCode{200u}; - WebSocketMessageType MessageType{}; - uint8_t Reserved[3] = {0}; - - bool IsValid() const; - }; - - static_assert(sizeof(Header) == 24); - - static std::atomic_uint32_t NextCorrelationId; - -public: - static constexpr size_t HeaderSize = sizeof(Header); - - WebSocketMessage() = default; - - WebSocketId SocketId() const { return m_SocketId; } - void SetSocketId(WebSocketId Id) { m_SocketId = Id; } - uint64_t MessageSize() const { return m_Header.MessageSize; } - void SetMessageType(WebSocketMessageType MessageType); - void SetCorrelationId(uint32_t Id) { m_Header.CorrelationId = Id; } - uint32_t CorrelationId() const { return m_Header.CorrelationId; } - uint32_t StatusCode() const { return m_Header.StatusCode; } - void SetStatusCode(uint32_t StatusCode) { m_Header.StatusCode = StatusCode; } - WebSocketMessageType MessageType() const { return m_Header.MessageType; } - - const CbPackage& Body() const { return m_Body.value(); } - void SetBody(CbPackage&& Body); - void SetBody(CbObject&& Body); - bool HasBody() const { return m_Body.has_value(); } - - void Save(BinaryWriter& Writer); - bool TryLoadHeader(MemoryView Memory); - - bool IsValid() const { return m_Header.MessageType != WebSocketMessageType::kInvalid; } - -private: - Header m_Header{}; - WebSocketId m_SocketId{}; - std::optional<CbPackage> m_Body; -}; - -class WebSocketServer; - -/** - * Base class for handling web socket requests and notifications from connected client(s). - */ -class WebSocketService -{ -public: - virtual ~WebSocketService() = default; - - void Configure(WebSocketServer& Server); - - virtual bool HandleRequest(const WebSocketMessage&) { ZEN_ASSERT(false); } - virtual void HandleNotification(const WebSocketMessage&) { ZEN_ASSERT(false); } - -protected: - WebSocketService() = default; - - virtual void RegisterHandlers(WebSocketServer& Server) = 0; - void SendStreamResponse(WebSocketId SocketId, uint32_t CorrelationId, CbPackage&& StreamResponse, bool IsStreamComplete); - void SendStreamResponse(WebSocketId SocketId, uint32_t CorrelationId, CbObject&& StreamResponse, bool IsStreamComplete); - - WebSocketServer& SocketServer() - { - ZEN_ASSERT(m_SocketServer); - return *m_SocketServer; - } - -private: - WebSocketServer* m_SocketServer{}; -}; - -/** - * Server options. - */ -struct WebSocketServerOptions -{ - uint16_t Port = 2337; - uint32_t ThreadCount = 1; -}; - -/** - * The web socket server manages client connections and routing of requests and notifications. - */ -class WebSocketServer -{ -public: - virtual ~WebSocketServer() = default; - - virtual bool Run() = 0; - virtual void Shutdown() = 0; - - virtual void RegisterService(WebSocketService& Service) = 0; - virtual void RegisterNotificationHandler(std::string_view Key, WebSocketService& Service) = 0; - virtual void RegisterRequestHandler(std::string_view Key, WebSocketService& Service) = 0; - - virtual void SendNotification(WebSocketMessage&& Notification) = 0; - virtual void SendResponse(WebSocketMessage&& Response) = 0; - - static std::unique_ptr<WebSocketServer> Create(const WebSocketServerOptions& Options); -}; - -/** - * The state of the web socket. - */ -enum class WebSocketState : uint32_t -{ - kNone, - kHandshaking, - kConnected, - kDisconnected, - kError -}; - -/** - * Type of web socket client event. - */ -enum class WebSocketEvent : uint32_t -{ - kConnected, - kDisconnected, - kError -}; - -/** - * Web socket client connection info. - */ -struct WebSocketConnectInfo -{ - std::string Host; - int16_t Port{8848}; - std::string Endpoint; - std::vector<std::string> Protocols; - uint16_t Version{13}; -}; - -/** - * A connection to a web socket server for sending requests and listening for notifications. - */ -class WebSocketClient -{ -public: - using EventCallback = std::function<void()>; - using NotificationCallback = std::function<void(WebSocketMessage&&)>; - - virtual ~WebSocketClient() = default; - - virtual std::future<bool> Connect(const WebSocketConnectInfo& Info) = 0; - virtual void Disconnect() = 0; - virtual bool IsConnected() const = 0; - virtual WebSocketState State() const = 0; - - virtual std::future<WebSocketMessage> SendRequest(WebSocketMessage&& Request) = 0; - virtual void OnNotification(NotificationCallback&& Cb) = 0; - virtual void OnEvent(WebSocketEvent Evt, EventCallback&& Cb) = 0; - - static std::shared_ptr<WebSocketClient> Create(asio::io_context& IoCtx); -}; - -} // namespace zen |