summaryrefslogtreecommitdiff
path: root/src/websocket.hh
diff options
context:
space:
mode:
Diffstat (limited to 'src/websocket.hh')
-rw-r--r--src/websocket.hh88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/websocket.hh b/src/websocket.hh
new file mode 100644
index 0000000..f7a99f1
--- /dev/null
+++ b/src/websocket.hh
@@ -0,0 +1,88 @@
+#ifndef WEBSOCKET_HH
+#define WEBSOCKET_HH
+
+#include <memory>
+#include <span>
+#include <string_view>
+
+namespace logger {
+class Logger;
+} // namespace logger
+
+namespace looper {
+class Looper;
+} // namespace looper
+
+namespace cfg {
+class Config;
+} // namespace cfg
+
+namespace http {
+class Response;
+class Request;
+} // namespace http
+
+namespace ws {
+
+class Message {
+ public:
+ virtual ~Message() = default;
+
+ [[nodiscard]]
+ virtual bool is_text() const = 0;
+
+ [[nodiscard]]
+ virtual bool is_binary() const = 0;
+
+ [[nodiscard]]
+ virtual std::string_view text() const = 0;
+
+ [[nodiscard]]
+ virtual std::span<uint8_t const> binary() const = 0;
+
+ static std::unique_ptr<Message> create(std::string_view text);
+ static std::unique_ptr<Message> create(std::span<uint8_t const> data);
+
+ protected:
+ Message() = default;
+ Message(Message const&) = delete;
+ Message& operator=(Message const&) = delete;
+};
+
+class Server {
+ public:
+ virtual ~Server() = default;
+
+ class Delegate {
+ public:
+ virtual ~Delegate() = default;
+
+ virtual std::unique_ptr<Message> handle(Message const& msg) = 0;
+
+ protected:
+ Delegate() = default;
+ };
+
+ virtual std::unique_ptr<http::Response> handle(
+ http::Request const& request) = 0;
+
+ virtual void send_text_to_all(std::string_view text);
+ virtual void send_binary_to_all(std::span<uint8_t const> data);
+ virtual void send_to_all(Message const& message) = 0;
+
+ protected:
+ Server() = default;
+
+ private:
+ Server(Server const&) = delete;
+ Server& operator=(Server const&) = delete;
+};
+
+std::unique_ptr<Server> create_server(logger::Logger& logger,
+ cfg::Config const& cfg,
+ looper::Looper& looper,
+ Server::Delegate& delegate);
+
+} // namespace ws
+
+#endif // WEBSOCKET_HH