CppServer 1.0.6.0
C++ Server Library
Loading...
Searching...
No Matches
wss_client.cpp
Go to the documentation of this file.
1
8
10
11namespace CppServer {
12namespace WS {
13
15{
16 _sync_connect = true;
17 return HTTPSClient::Connect();
18}
19
20bool WSSClient::Connect(const std::shared_ptr<Asio::TCPResolver>& resolver)
21{
22 _sync_connect = true;
23 return HTTPSClient::Connect(resolver);
24}
25
27{
28 _sync_connect = false;
29 return HTTPSClient::ConnectAsync();
30}
31
32bool WSSClient::ConnectAsync(const std::shared_ptr<Asio::TCPResolver>& resolver)
33{
34 _sync_connect = false;
35 return HTTPSClient::ConnectAsync(resolver);
36}
37
39{
40 // Clear WebSocket send/receive buffers
42
43 // Fill the WebSocket upgrade HTTP request
45
46 // Set body of the WebSocket upgrade HTTP request
47 _request.SetBody();
48
49 // Send the WebSocket upgrade HTTP request
50 if (_sync_connect)
51 Send(_request.cache());
52 else
53 SendAsync(_request.cache());
54}
55
57{
58 // Disconnecting WebSocket
60 {
62 }
63}
64
66{
67 // Disconnect WebSocket
69 {
70 _ws_handshaked = false;
72 }
73
74 // Reset WebSocket upgrade HTTP request and response
75 _request.Clear();
76 _response.Clear();
77
78 // Clear WebSocket send/receive buffers
80
81 // Initialize new WebSocket random nonce
83}
84
85void WSSClient::onReceived(const void* buffer, size_t size)
86{
87 // Check for WebSocket handshaked status
89 {
90 // Prepare receive frame
91 PrepareReceiveFrame(buffer, size);
92 return;
93 }
94
95 HTTPSClient::onReceived(buffer, size);
96}
97
99{
100 // Check for WebSocket handshaked status
101 if (_ws_handshaked)
102 return;
103
104 // Try to perform WebSocket upgrade
105 if (!PerformClientUpgrade(response, id()))
106 {
107 HTTPSClient::onReceivedResponseHeader(response);
108 return;
109 }
110}
111
113{
114 // Check for WebSocket handshaked status
115 if (_ws_handshaked)
116 {
117 // Prepare receive frame from the remaining response body
118 auto body = response.body();
119 PrepareReceiveFrame(body.data(), body.size());
120 return;
121 }
122
123 HTTPSClient::onReceivedResponse(response);
124}
125
126void WSSClient::onReceivedResponseError(const HTTP::HTTPResponse& response, const std::string& error)
127{
128 // Check for WebSocket handshaked status
129 if (_ws_handshaked)
130 {
131 onError(asio::error::fault, "WebSocket error", error);
132 return;
133 }
134
135 HTTPSClient::onReceivedResponseError(response, error);
136}
137
139{
140 std::string result;
141
142 if (!_ws_handshaked)
143 return result;
144
145 std::vector<uint8_t> cache;
146
147 // Receive WebSocket frame data
148 while (!_ws_final_received)
149 {
150 while (!_ws_frame_received)
151 {
152 size_t required = RequiredReceiveFrameSize();
153 cache.resize(required);
154 size_t received = HTTPSClient::Receive(cache.data(), required);
155 if (received != required)
156 return result;
157 PrepareReceiveFrame(cache.data(), received);
158 }
160 PrepareReceiveFrame(nullptr, 0);
161 }
162
163 // Copy WebSocket frame data
165 PrepareReceiveFrame(nullptr, 0);
166 return result;
167}
168
169std::string WSSClient::ReceiveText(const CppCommon::Timespan& timeout)
170{
171 std::string result;
172
173 if (!_ws_handshaked)
174 return result;
175
176 std::vector<uint8_t> cache;
177
178 // Receive WebSocket frame data
179 while (!_ws_final_received)
180 {
181 while (!_ws_frame_received)
182 {
183 size_t required = RequiredReceiveFrameSize();
184 cache.resize(required);
185 size_t received = HTTPSClient::Receive(cache.data(), required, timeout);
186 if (received != required)
187 return result;
188 PrepareReceiveFrame(cache.data(), received);
189 }
191 PrepareReceiveFrame(nullptr, 0);
192 }
193
194 // Copy WebSocket frame data
196 PrepareReceiveFrame(nullptr, 0);
197 return result;
198}
199
200std::vector<uint8_t> WSSClient::ReceiveBinary()
201{
202 std::vector<uint8_t> result;
203
204 if (!_ws_handshaked)
205 return result;
206
207 std::vector<uint8_t> cache;
208
209 // Receive WebSocket frame data
210 while (!_ws_final_received)
211 {
212 while (!_ws_frame_received)
213 {
214 size_t required = RequiredReceiveFrameSize();
215 cache.resize(required);
216 size_t received = HTTPSClient::Receive(cache.data(), required);
217 if (received != required)
218 return result;
219 PrepareReceiveFrame(cache.data(), received);
220 }
222 PrepareReceiveFrame(nullptr, 0);
223 }
224
225 // Copy WebSocket frame data
227 PrepareReceiveFrame(nullptr, 0);
228 return result;
229}
230
231std::vector<uint8_t> WSSClient::ReceiveBinary(const CppCommon::Timespan& timeout)
232{
233 std::vector<uint8_t> result;
234
235 if (!_ws_handshaked)
236 return result;
237
238 std::vector<uint8_t> cache;
239
240 // Receive WebSocket frame data
241 while (!_ws_final_received)
242 {
243 while (!_ws_frame_received)
244 {
245 size_t required = RequiredReceiveFrameSize();
246 cache.resize(required);
247 size_t received = HTTPSClient::Receive(cache.data(), required, timeout);
248 if (received != required)
249 return result;
250 PrepareReceiveFrame(cache.data(), received);
251 }
253 PrepareReceiveFrame(nullptr, 0);
254 }
255
256 // Copy WebSocket frame data
258 PrepareReceiveFrame(nullptr, 0);
259 return result;
260}
261
262} // namespace WS
263} // namespace CppServer
virtual size_t Send(const void *buffer, size_t size)
Send data to the server (synchronous).
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
Definition ssl_client.h:338
virtual bool SendAsync(const void *buffer, size_t size)
Send data to the server (asynchronous).
std::string_view body() const noexcept
Get the HTTP response body.
HTTPResponse _response
HTTP response.
HTTPRequest _request
HTTP request.
std::vector< uint8_t > ReceiveBinary()
void onReceivedResponse(const HTTP::HTTPResponse &response) override
Handle HTTP response received notification.
bool ConnectAsync() override
Connect the client (asynchronous).
void onReceivedResponseError(const HTTP::HTTPResponse &response, const std::string &error) override
Handle HTTP response error notification.
void onReceived(const void *buffer, size_t size) override
Handle buffer received notification.
bool Connect() override
Connect the client (synchronous).
void onHandshaked() override
Handle session handshaked notification.
void onDisconnecting() override
Handle client disconnecting notification.
void onDisconnected() override
Handle client disconnected notification.
void onReceivedResponseHeader(const HTTP::HTTPResponse &response) override
Handle HTTP response header received notification.
size_t _ws_payload_size
Received frame payload size.
Definition ws.h:182
bool _ws_frame_received
Received frame flag.
Definition ws.h:176
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
std::vector< uint8_t > _ws_receive_final_buffer
Receive final buffer.
Definition ws.h:186
bool PerformClientUpgrade(const HTTP::HTTPResponse &response, const CppCommon::UUID &id)
Perform WebSocket client upgrade.
Definition ws.cpp:26
void ClearWSBuffers()
Clear WebSocket send/receive buffers.
Definition ws.cpp:486
size_t RequiredReceiveFrameSize()
Required WebSocket receive frame size.
Definition ws.cpp:460
bool _ws_final_received
Received final flag.
Definition ws.h:178
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 _ws_handshaked
Handshaked flag.
Definition ws.h:171
WebSocket definitions.
C++ Server project definitions.
Definition asio.h:56
WebSocket secure client definition.