CppServer 1.0.6.0
C++ Server Library
Loading...
Searching...
No Matches
ws_client.cpp
Go to the documentation of this file.
1
8
10
11namespace CppServer {
12namespace WS {
13
15{
16 _sync_connect = true;
17 return HTTPClient::Connect();
18}
19
20bool WSClient::Connect(const std::shared_ptr<Asio::TCPResolver>& resolver)
21{
22 _sync_connect = true;
23 return HTTPClient::Connect(resolver);
24}
25
27{
28 _sync_connect = false;
29 return HTTPClient::ConnectAsync();
30}
31
32bool WSClient::ConnectAsync(const std::shared_ptr<Asio::TCPResolver>& resolver)
33{
34 _sync_connect = false;
35 return HTTPClient::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 WSClient::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 HTTPClient::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 HTTPClient::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 HTTPClient::onReceivedResponse(response);
124}
125
126void WSClient::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 HTTPClient::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 = HTTPClient::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 WSClient::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 = HTTPClient::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> WSClient::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 = HTTPClient::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> WSClient::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 = HTTPClient::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 bool SendAsync(const void *buffer, size_t size)
Send data to the server (asynchronous).
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
Definition tcp_client.h:324
HTTPResponse _response
HTTP response.
HTTPRequest _request
HTTP request.
std::string_view body() const noexcept
Get the HTTP response body.
bool ConnectAsync() override
Connect the client (asynchronous).
Definition ws_client.cpp:26
std::string ReceiveText()
void onReceivedResponse(const HTTP::HTTPResponse &response) override
Handle HTTP response received notification.
void onReceivedResponseHeader(const HTTP::HTTPResponse &response) override
Handle HTTP response header received notification.
Definition ws_client.cpp:98
void onDisconnected() override
Handle client disconnected notification.
Definition ws_client.cpp:65
bool Connect() override
Connect the client (synchronous).
Definition ws_client.cpp:14
void onReceivedResponseError(const HTTP::HTTPResponse &response, const std::string &error) override
Handle HTTP response error notification.
void onDisconnecting() override
Handle client disconnecting notification.
Definition ws_client.cpp:56
std::vector< uint8_t > ReceiveBinary()
void onConnected() override
Handle client connected notification.
Definition ws_client.cpp:38
void onReceived(const void *buffer, size_t size) override
Handle buffer received notification.
Definition ws_client.cpp:85
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 client definition.