CppServer 1.0.6.0
C++ Server Library
Loading...
Searching...
No Matches
ws.h
Go to the documentation of this file.
1
8
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
20namespace CppServer {
21
26namespace WS {
27
30{
31public:
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
91
93 void ClearWSBuffers();
94
96 void InitWSNonce();
97
98protected:
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 onWSDisconnecting() {}
134 virtual void onWSDisconnected() {}
135
137
141 virtual void onWSReceived(const void* buffer, size_t size) {}
142
144
149 virtual void onWSClose(const void* buffer, size_t size, int status = 1000) {}
151
155 virtual void onWSPing(const void* buffer, size_t size) {}
157
161 virtual void onWSPong(const void* buffer, size_t size) {}
162
164
167 virtual void onWSError(const std::string& message) {}
168
169protected:
171 bool _ws_handshaked{false};
172
174 uint8_t _ws_opcode;
184 std::vector<uint8_t> _ws_receive_frame_buffer;
186 std::vector<uint8_t> _ws_receive_final_buffer;
189
191 std::mutex _ws_send_lock;
193 std::vector<uint8_t> _ws_send_buffer;
195 uint8_t _ws_send_mask[4];
196
198 std::array<uint8_t, 16> _ws_nonce;
199
201
204 virtual void SendResponse(const HTTP::HTTPResponse& response) {}
205};
206
207} // namespace WS
208} // namespace CppServer
209
210#endif // CPPSERVER_WS_H
size_t _ws_payload_size
Received frame payload size.
Definition ws.h:182
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:214
static const uint8_t WS_TEXT
Text frame.
Definition ws.h:35
WebSocket & operator=(WebSocket &&)=delete
std::vector< uint8_t > _ws_receive_frame_buffer
Receive frame buffer.
Definition ws.h:184
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:149
bool _ws_frame_received
Received frame flag.
Definition ws.h:176
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:275
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:141
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:155
std::vector< uint8_t > _ws_receive_final_buffer
Receive final buffer.
Definition ws.h:186
uint8_t _ws_receive_mask[4]
Receive mask.
Definition ws.h:188
std::array< uint8_t, 16 > _ws_nonce
WebSocket random nonce of 16 bytes.
Definition ws.h:198
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:191
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:174
void ClearWSBuffers()
Clear WebSocket send/receive buffers.
Definition ws.cpp:486
virtual void onWSPong(const void *buffer, size_t size)
Handle WebSocket pong notification.
Definition ws.h:161
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:167
size_t RequiredReceiveFrameSize()
Required WebSocket receive frame size.
Definition ws.cpp:460
std::vector< uint8_t > _ws_send_buffer
Send buffer.
Definition ws.h:193
uint8_t _ws_send_mask[4]
Send mask.
Definition ws.h:195
virtual void SendResponse(const HTTP::HTTPResponse &response)
Send WebSocket server upgrade response.
Definition ws.h:204
bool _ws_final_received
Received final flag.
Definition ws.h:178
virtual bool onWSConnecting(const HTTP::HTTPRequest &request, HTTP::HTTPResponse &response)
Handle WebSocket server session validating notification.
Definition ws.h:124
virtual void onWSDisconnecting()
Handle WebSocket disconnecting notification.
Definition ws.h:132
size_t _ws_header_size
Received frame header size.
Definition ws.h:180
virtual void onWSDisconnected()
Handle WebSocket disconnected notification.
Definition ws.h:134
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:171
HTTP request definition.
HTTP response definition.
WebSocket definitions.
C++ Server project definitions.
Definition asio.h:56