CppServer  1.0.4.0
C++ Server Library
ws_server.h
Go to the documentation of this file.
1 
9 #ifndef CPPSERVER_HTTP_WS_SERVER_H
10 #define CPPSERVER_HTTP_WS_SERVER_H
11 
12 #include "ws_session.h"
13 
15 
16 namespace CppServer {
17 namespace WS {
18 
20 
28 class WSServer : public HTTP::HTTPServer, protected WebSocket
29 {
30 public:
31  using HTTPServer::HTTPServer;
32 
33  WSServer(const WSServer&) = delete;
34  WSServer(WSServer&&) = delete;
35  virtual ~WSServer() = default;
36 
37  WSServer& operator=(const WSServer&) = delete;
38  WSServer& operator=(WSServer&&) = delete;
39 
40  // WebSocket connection methods
41  virtual bool CloseAll() { return CloseAll(0, nullptr, 0); }
42  virtual bool CloseAll(int status) { return CloseAll(status, nullptr, 0); }
43  virtual bool CloseAll(int status, const void* buffer, size_t size);
44  virtual bool CloseAll(int status, std::string_view text);
45 
47  bool Multicast(const void* buffer, size_t size) override;
48 
49  // WebSocket multicast text methods
50  size_t MulticastText(const void* buffer, size_t size) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_TEXT, false, buffer, size); return Multicast(_ws_send_buffer.data(), _ws_send_buffer.size()); }
51  size_t MulticastText(std::string_view text) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_TEXT, false, text.data(), text.size()); return Multicast(_ws_send_buffer.data(), _ws_send_buffer.size()); }
52 
53  // WebSocket multicast binary methods
54  size_t MulticastBinary(const void* buffer, size_t size) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_BINARY, false, buffer, size); return Multicast(_ws_send_buffer.data(), _ws_send_buffer.size()); }
55  size_t MulticastBinary(std::string_view text) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_BINARY, false, text.data(), text.size()); return Multicast(_ws_send_buffer.data(), _ws_send_buffer.size()); }
56 
57  // WebSocket multicast ping methods
58  size_t MulticastPing(const void* buffer, size_t size) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_PING, false, buffer, size); return Multicast(_ws_send_buffer.data(), _ws_send_buffer.size()); }
59  size_t MulticastPing(std::string_view text) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_PING, false, text.data(), text.size()); return Multicast(_ws_send_buffer.data(), _ws_send_buffer.size()); }
60 
61 protected:
62  std::shared_ptr<Asio::TCPSession> CreateSession(const std::shared_ptr<Asio::TCPServer>& server) override { return std::make_shared<WSSession>(std::dynamic_pointer_cast<WSServer>(server)); }
63 };
64 
67 } // namespace WS
68 } // namespace CppServer
69 
70 #endif // CPPSERVER_HTTP_WS_SERVER_H
WebSocket server.
Definition: ws_server.h:29
virtual bool CloseAll(int status)
Definition: ws_server.h:42
size_t MulticastBinary(std::string_view text)
Definition: ws_server.h:55
size_t MulticastText(const void *buffer, size_t size)
Definition: ws_server.h:50
virtual ~WSServer()=default
size_t MulticastBinary(const void *buffer, size_t size)
Definition: ws_server.h:54
size_t MulticastPing(const void *buffer, size_t size)
Definition: ws_server.h:58
WSServer & operator=(const WSServer &)=delete
std::shared_ptr< Asio::TCPSession > CreateSession(const std::shared_ptr< Asio::TCPServer > &server) override
Definition: ws_server.h:62
size_t MulticastPing(std::string_view text)
Definition: ws_server.h:59
size_t MulticastText(std::string_view text)
Definition: ws_server.h:51
WSServer(WSServer &&)=delete
virtual bool CloseAll()
Definition: ws_server.h:41
WSServer & operator=(WSServer &&)=delete
WSServer(const WSServer &)=delete
bool Multicast(const void *buffer, size_t size) override
Multicast data to all connected WebSocket sessions.
Definition: ws_server.cpp:36
WebSocket utility class.
Definition: ws.h:30
void PrepareSendFrame(uint8_t opcode, bool mask, const void *buffer, size_t size, int status=0)
Prepare WebSocket send frame.
Definition: ws.cpp:212
static const uint8_t WS_TEXT
Text frame.
Definition: ws.h:35
static const uint8_t WS_BINARY
Binary frame.
Definition: ws.h:37
static const uint8_t WS_PING
Ping frame.
Definition: ws.h:41
static const uint8_t WS_FIN
Final frame.
Definition: ws.h:33
std::mutex _ws_send_lock
Send buffer lock.
Definition: ws.h:189
std::vector< uint8_t > _ws_send_buffer
Send buffer.
Definition: ws.h:191
HTTP server definition.
C++ Server project definitions.
Definition: asio.h:56
WebSocket session definition.