aboutsummaryrefslogtreecommitdiff
path: root/zenhttp/include
diff options
context:
space:
mode:
authorPer Larsson <[email protected]>2022-02-18 14:48:41 +0100
committerPer Larsson <[email protected]>2022-02-18 14:48:41 +0100
commit08cc02bf1b5cad75ed7b97c0dc7cfc082da537c4 (patch)
tree8fd8e7189c9807280003eabb3040cb35db2e5cd4 /zenhttp/include
parentWeb socket client is shared between I/O thead and client. (diff)
downloadzen-08cc02bf1b5cad75ed7b97c0dc7cfc082da537c4.tar.xz
zen-08cc02bf1b5cad75ed7b97c0dc7cfc082da537c4.zip
Basic websocket service and test.
Diffstat (limited to 'zenhttp/include')
-rw-r--r--zenhttp/include/zenhttp/websocket.h55
1 files changed, 52 insertions, 3 deletions
diff --git a/zenhttp/include/zenhttp/websocket.h b/zenhttp/include/zenhttp/websocket.h
index 908b24636..1ab9b4804 100644
--- a/zenhttp/include/zenhttp/websocket.h
+++ b/zenhttp/include/zenhttp/websocket.h
@@ -2,9 +2,12 @@
#include <zencore/memory.h>
+#include <compare>
#include <functional>
#include <memory>
+#pragma once
+
namespace asio {
class io_context;
}
@@ -12,6 +15,47 @@ class io_context;
namespace zen {
class CbPackage;
+class CbObject;
+class BinaryWriter;
+
+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{};
+};
+
+class WebSocketServer;
+
+class WebSocketService
+{
+public:
+ virtual ~WebSocketService() = default;
+
+ virtual bool HandleMessage(WebSocketId Id, const CbPackage& Msg) = 0;
+ void Configure(WebSocketServer& Server);
+
+protected:
+ WebSocketService() = default;
+
+ void PublishMessage(WebSocketId Id, CbPackage&& Msg);
+ void PublishMessage(WebSocketId Id, CbObject&& Msg);
+
+private:
+ WebSocketServer* m_Server = nullptr;
+};
struct WebSocketServerOptions
{
@@ -24,8 +68,10 @@ class WebSocketServer
public:
virtual ~WebSocketServer() = default;
- virtual bool Run(const WebSocketServerOptions& Options) = 0;
- virtual void Shutdown() = 0;
+ virtual void RegisterService(WebSocketService& Service) = 0;
+ virtual bool Run(const WebSocketServerOptions& Options) = 0;
+ virtual void Shutdown() = 0;
+ virtual void PublishMessage(WebSocketId Id, CbPackage&& Msg) = 0;
static std::unique_ptr<WebSocketServer> Create();
};
@@ -41,7 +87,6 @@ enum class WebSocketState : uint32_t
enum class WebSocketEvent : uint32_t
{
- kNone,
kConnected,
kDisconnected,
kError
@@ -68,6 +113,8 @@ public:
virtual void Disconnect() = 0;
virtual bool IsConnected() const = 0;
virtual WebSocketState State() const = 0;
+ virtual void SendMsg(CbObject&& Msg) = 0;
+ virtual void SendMsg(CbPackage&& Msg) = 0;
virtual void On(WebSocketEvent Evt, EventCallback&& Cb) = 0;
virtual void OnMessage(MessageCallback&& Cb) = 0;
@@ -86,6 +133,8 @@ struct WebSocketMessageHeader
bool IsValid() const;
static bool Read(MemoryView Memory, WebSocketMessageHeader& OutHeader);
+
+ static void Write(BinaryWriter& Writer, const CbPackage& Msg);
};
} // namespace zen