CppServer  1.0.4.0
C++ Server Library
ws.h
Go to the documentation of this file.
1 
9 #ifndef CPPSERVER_WS_H
10 #define CPPSERVER_WS_H
11 
14 
15 #include "system/uuid.h"
16 
17 #include <array>
18 #include <mutex>
19 
20 namespace CppServer {
21 
26 namespace WS {
27 
29 class WebSocket
30 {
31 public:
33  static const uint8_t WS_FIN = 0x80;
35  static const uint8_t WS_TEXT = 0x01;
37  static const uint8_t WS_BINARY = 0x02;
39  static const uint8_t WS_CLOSE = 0x08;
41  static const uint8_t WS_PING = 0x09;
43  static const uint8_t WS_PONG = 0x0A;
44 
46  WebSocket(const WebSocket&) = delete;
47  WebSocket(WebSocket&&) = delete;
48  ~WebSocket() = default;
49 
50  WebSocket& operator=(const WebSocket&) = delete;
52 
54  std::string_view ws_nonce() const noexcept { return std::string_view((char*)_ws_nonce.data(), _ws_nonce.size()); }
55 
57 
62  bool PerformClientUpgrade(const HTTP::HTTPResponse& response, const CppCommon::UUID& id);
63 
65 
70  bool PerformServerUpgrade(const HTTP::HTTPRequest& request, HTTP::HTTPResponse& response);
71 
73 
80  void PrepareSendFrame(uint8_t opcode, bool mask, const void* buffer, size_t size, int status = 0);
81 
83 
87  void PrepareReceiveFrame(const void* buffer, size_t size);
88 
90  size_t RequiredReceiveFrameSize();
91 
93  void ClearWSBuffers();
94 
96  void InitWSNonce();
97 
98 protected:
100 
107  virtual void onWSConnecting(HTTP::HTTPRequest& request) {}
109 
112  virtual void onWSConnected(const HTTP::HTTPResponse& response) {}
113 
115 
124  virtual bool onWSConnecting(const HTTP::HTTPRequest& request, HTTP::HTTPResponse& response) { return true; }
126 
129  virtual void onWSConnected(const HTTP::HTTPRequest& request) {}
130 
132  virtual void onWSDisconnected() {}
133 
135 
139  virtual void onWSReceived(const void* buffer, size_t size) {}
140 
142 
147  virtual void onWSClose(const void* buffer, size_t size, int status = 1000) {}
149 
153  virtual void onWSPing(const void* buffer, size_t size) {}
155 
159  virtual void onWSPong(const void* buffer, size_t size) {}
160 
162 
165  virtual void onWSError(const std::string& message) {}
166 
167 protected:
169  bool _ws_handshaked{false};
170 
172  uint8_t _ws_opcode;
174  bool _ws_frame_received{false};
176  bool _ws_final_received{false};
178  size_t _ws_header_size{0};
180  size_t _ws_payload_size{0};
182  std::vector<uint8_t> _ws_receive_frame_buffer;
184  std::vector<uint8_t> _ws_receive_final_buffer;
186  uint8_t _ws_receive_mask[4];
187 
189  std::mutex _ws_send_lock;
191  std::vector<uint8_t> _ws_send_buffer;
193  uint8_t _ws_send_mask[4];
194 
196  std::array<uint8_t, 16> _ws_nonce;
197 
199 
202  virtual void SendResponse(const HTTP::HTTPResponse& response) {}
203 };
204 
205 } // namespace WS
206 } // namespace CppServer
207 
208 #endif // CPPSERVER_WS_H
WebSocket utility class.
Definition: ws.h:30
size_t _ws_payload_size
Received frame payload size.
Definition: ws.h:180
static const uint8_t WS_CLOSE
Close frame.
Definition: ws.h:39
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
std::vector< uint8_t > _ws_receive_frame_buffer
Receive frame buffer.
Definition: ws.h:182
WebSocket & operator=(const WebSocket &)=delete
virtual void onWSClose(const void *buffer, size_t size, int status=1000)
Handle WebSocket client close notification.
Definition: ws.h:147
bool _ws_frame_received
Received frame flag.
Definition: ws.h:174
static const uint8_t WS_BINARY
Binary frame.
Definition: ws.h:37
std::string_view ws_nonce() const noexcept
Get the WebSocket random nonce.
Definition: ws.h:54
WebSocket(const WebSocket &)=delete
static const uint8_t WS_PING
Ping frame.
Definition: ws.h:41
void InitWSNonce()
Initialize WebSocket random nonce.
Definition: ws.cpp:21
void PrepareReceiveFrame(const void *buffer, size_t size)
Prepare WebSocket receive frame.
Definition: ws.cpp:273
virtual void onWSConnecting(HTTP::HTTPRequest &request)
Handle WebSocket client connecting notification.
Definition: ws.h:107
virtual void onWSReceived(const void *buffer, size_t size)
Handle WebSocket received notification.
Definition: ws.h:139
static const uint8_t WS_FIN
Final frame.
Definition: ws.h:33
virtual void onWSPing(const void *buffer, size_t size)
Handle WebSocket ping notification.
Definition: ws.h:153
std::vector< uint8_t > _ws_receive_final_buffer
Receive final buffer.
Definition: ws.h:184
uint8_t _ws_receive_mask[4]
Receive mask.
Definition: ws.h:186
std::array< uint8_t, 16 > _ws_nonce
WebSocket random nonce of 16 bytes.
Definition: ws.h:196
bool PerformClientUpgrade(const HTTP::HTTPResponse &response, const CppCommon::UUID &id)
Perform WebSocket client upgrade.
Definition: ws.cpp:26
std::mutex _ws_send_lock
Send buffer lock.
Definition: ws.h:189
virtual void onWSConnected(const HTTP::HTTPRequest &request)
Handle WebSocket server session connected notification.
Definition: ws.h:129
WebSocket(WebSocket &&)=delete
virtual void onWSConnected(const HTTP::HTTPResponse &response)
Handle WebSocket client connected notification.
Definition: ws.h:112
uint8_t _ws_opcode
Received frame opcode.
Definition: ws.h:172
void ClearWSBuffers()
Clear WebSocket send/receive buffers.
Definition: ws.cpp:484
virtual void onWSPong(const void *buffer, size_t size)
Handle WebSocket pong notification.
Definition: ws.h:159
static const uint8_t WS_PONG
Pong frame.
Definition: ws.h:43
virtual void onWSError(const std::string &message)
Handle WebSocket error notification.
Definition: ws.h:165
size_t RequiredReceiveFrameSize()
Required WebSocket receive frame size.
Definition: ws.cpp:458
std::vector< uint8_t > _ws_send_buffer
Send buffer.
Definition: ws.h:191
WebSocket & operator=(WebSocket &&)=delete
uint8_t _ws_send_mask[4]
Send mask.
Definition: ws.h:193
virtual void SendResponse(const HTTP::HTTPResponse &response)
Send WebSocket server upgrade response.
Definition: ws.h:202
bool _ws_final_received
Received final flag.
Definition: ws.h:176
virtual bool onWSConnecting(const HTTP::HTTPRequest &request, HTTP::HTTPResponse &response)
Handle WebSocket server session validating notification.
Definition: ws.h:124
size_t _ws_header_size
Received frame header size.
Definition: ws.h:178
virtual void onWSDisconnected()
Handle WebSocket client disconnected notification.
Definition: ws.h:132
bool PerformServerUpgrade(const HTTP::HTTPRequest &request, HTTP::HTTPResponse &response)
Perform WebSocket server upgrade.
Definition: ws.cpp:103
bool _ws_handshaked
Handshaked flag.
Definition: ws.h:169
HTTP request definition.
HTTP response definition.
C++ Server project definitions.
Definition: asio.h:56