WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
WebSocket.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdint.h>
8
9#include <functional>
10#include <initializer_list>
11#include <memory>
12#include <span>
13#include <string>
14#include <string_view>
15#include <utility>
16
17#include "wpi/net/uv/Buffer.hpp"
18#include "wpi/net/uv/Error.hpp"
19#include "wpi/net/uv/Timer.hpp"
20#include "wpi/util/Signal.h"
21#include "wpi/util/SmallVector.hpp"
22
23namespace wpi::net {
24
25namespace uv {
26class Stream;
27} // namespace uv
28
29/**
30 * RFC 6455 compliant WebSocket client and server implementation.
31 */
32class WebSocket : public std::enable_shared_from_this<WebSocket> {
33 struct private_init {};
34
35 public:
36 static constexpr uint8_t kOpCont = 0x00;
37 static constexpr uint8_t kOpText = 0x01;
38 static constexpr uint8_t kOpBinary = 0x02;
39 static constexpr uint8_t kOpClose = 0x08;
40 static constexpr uint8_t kOpPing = 0x09;
41 static constexpr uint8_t kOpPong = 0x0A;
42 static constexpr uint8_t kOpMask = 0x0F;
43 static constexpr uint8_t kFlagFin = 0x80;
44 static constexpr uint8_t kFlagControl = 0x08;
45
46 WebSocket(uv::Stream& stream, bool server, const private_init&);
47 WebSocket(const WebSocket&) = delete;
48 WebSocket(WebSocket&&) = delete;
49 WebSocket& operator=(const WebSocket&) = delete;
52
53 /**
54 * Connection states.
55 */
56 enum State {
57 /** The connection is not yet open. */
59 /** The connection is open and ready to communicate. */
61 /** The connection is in the process of closing. */
63 /** The connection failed. */
65 /** The connection is closed. */
67 };
68
69 /**
70 * Client connection options.
71 */
73 ClientOptions() : handshakeTimeout{(uv::Timer::Time::max)()} {}
74
75 /** Timeout for the handshake request. */
77
78 /** Additional headers to include in handshake. */
79 std::span<const std::pair<std::string_view, std::string_view>> extraHeaders;
80 };
81
82 /**
83 * Frame. Used by SendFrames().
84 */
85 struct Frame {
86 static constexpr uint8_t kText = kFlagFin | kOpText;
87 static constexpr uint8_t kBinary = kFlagFin | kOpBinary;
88 static constexpr uint8_t kTextFragment = kOpText;
89 static constexpr uint8_t kBinaryFragment = kOpBinary;
90 static constexpr uint8_t kFragment = kOpCont;
91 static constexpr uint8_t kFinalFragment = kFlagFin | kOpCont;
92 static constexpr uint8_t kPing = kFlagFin | kOpPing;
93 static constexpr uint8_t kPong = kFlagFin | kOpPong;
94
95 constexpr Frame(uint8_t opcode, std::span<const uv::Buffer> data)
96 : opcode{opcode}, data{data} {}
97
98 uint8_t opcode;
99 std::span<const uv::Buffer> data;
100 };
101
102 /**
103 * Starts a client connection by performing the initial client handshake.
104 * An open event is emitted when the handshake completes.
105 * This sets the stream user data to the websocket.
106 * @param stream Connection stream
107 * @param uri The Request-URI to send
108 * @param host The host or host:port to send
109 * @param protocols The list of subprotocols
110 * @param options Handshake options
111 */
112 static std::shared_ptr<WebSocket> CreateClient(
113 uv::Stream& stream, std::string_view uri, std::string_view host,
114 std::span<const std::string_view> protocols = {},
115 const ClientOptions& options = {});
116
117 /**
118 * Starts a client connection by performing the initial client handshake.
119 * An open event is emitted when the handshake completes.
120 * This sets the stream user data to the websocket.
121 * @param stream Connection stream
122 * @param uri The Request-URI to send
123 * @param host The host or host:port to send
124 * @param protocols The list of subprotocols
125 * @param options Handshake options
126 */
127 static std::shared_ptr<WebSocket> CreateClient(
128 uv::Stream& stream, std::string_view uri, std::string_view host,
129 std::initializer_list<std::string_view> protocols,
130 const ClientOptions& options = {}) {
131 return CreateClient(stream, uri, host, {protocols.begin(), protocols.end()},
132 options);
133 }
134
135 /**
136 * Starts a server connection by performing the initial server side handshake.
137 * This should be called after the HTTP headers have been received.
138 * An open event is emitted when the handshake completes.
139 * This sets the stream user data to the websocket.
140 * @param stream Connection stream
141 * @param key The value of the Sec-WebSocket-Key header field in the client
142 * request
143 * @param version The value of the Sec-WebSocket-Version header field in the
144 * client request
145 * @param protocol The subprotocol to send to the client (in the
146 * Sec-WebSocket-Protocol header field).
147 */
148 static std::shared_ptr<WebSocket> CreateServer(
149 uv::Stream& stream, std::string_view key, std::string_view version,
150 std::string_view protocol = {});
151
152 /**
153 * Get connection state.
154 */
155 State GetState() const { return m_state; }
156
157 /**
158 * Return if the connection is open. Messages can only be sent on open
159 * connections.
160 */
161 bool IsOpen() const { return m_state == OPEN; }
162
163 /**
164 * Get the underlying stream.
165 */
166 uv::Stream& GetStream() const { return m_stream; }
167
168 /**
169 * Get the selected sub-protocol. Only valid in or after the open() event.
170 */
171 std::string_view GetProtocol() const { return m_protocol; }
172
173 /**
174 * Set the maximum message size. Default is 128 KB. If configured to combine
175 * fragments this maximum applies to the entire message (all combined
176 * fragments).
177 * @param size Maximum message size in bytes
178 */
179 void SetMaxMessageSize(size_t size) { m_maxMessageSize = size; }
180
181 /**
182 * Set whether or not fragmented frames should be combined. Default is to
183 * combine. If fragmented frames are combined, the text and binary callbacks
184 * will always have the second parameter (fin) set to true.
185 * @param combine True if fragmented frames should be combined.
186 */
187 void SetCombineFragments(bool combine) { m_combineFragments = combine; }
188
189 /**
190 * Initiate a closing handshake.
191 * @param code A numeric status code (defaults to 1005, no status code)
192 * @param reason A human-readable string explaining why the connection is
193 * closing (optional).
194 */
195 void Close(uint16_t code = 1005, std::string_view reason = {});
196
197 /**
198 * Send a text message.
199 * @param data UTF-8 encoded data to send
200 * @param callback Callback which is invoked when the write completes.
201 */
203 std::span<const uv::Buffer> data,
204 std::function<void(std::span<uv::Buffer>, uv::Error)> callback) {
205 Send(kFlagFin | kOpText, data, std::move(callback));
206 }
207
208 /**
209 * Send a text message.
210 * @param data UTF-8 encoded data to send
211 * @param callback Callback which is invoked when the write completes.
212 */
214 std::initializer_list<uv::Buffer> data,
215 std::function<void(std::span<uv::Buffer>, uv::Error)> callback) {
216 SendText({data.begin(), data.end()}, std::move(callback));
217 }
218
219 /**
220 * Send a binary message.
221 * @param data Data to send
222 * @param callback Callback which is invoked when the write completes.
223 */
225 std::span<const uv::Buffer> data,
226 std::function<void(std::span<uv::Buffer>, uv::Error)> callback) {
227 Send(kFlagFin | kOpBinary, data, std::move(callback));
228 }
229
230 /**
231 * Send a binary message.
232 * @param data Data to send
233 * @param callback Callback which is invoked when the write completes.
234 */
236 std::initializer_list<uv::Buffer> data,
237 std::function<void(std::span<uv::Buffer>, uv::Error)> callback) {
238 SendBinary({data.begin(), data.end()}, std::move(callback));
239 }
240
241 /**
242 * Send a text message fragment. This must be followed by one or more
243 * SendFragment() calls, where the last one has fin=True, to complete the
244 * message.
245 * @param data UTF-8 encoded data to send
246 * @param callback Callback which is invoked when the write completes.
247 */
249 std::span<const uv::Buffer> data,
250 std::function<void(std::span<uv::Buffer>, uv::Error)> callback) {
251 Send(kOpText, data, std::move(callback));
252 }
253
254 /**
255 * Send a text message fragment. This must be followed by one or more
256 * SendFragment() calls, where the last one has fin=True, to complete the
257 * message.
258 * @param data UTF-8 encoded data to send
259 * @param callback Callback which is invoked when the write completes.
260 */
262 std::initializer_list<uv::Buffer> data,
263 std::function<void(std::span<uv::Buffer>, uv::Error)> callback) {
264 SendTextFragment({data.begin(), data.end()}, std::move(callback));
265 }
266
267 /**
268 * Send a text message fragment. This must be followed by one or more
269 * SendFragment() calls, where the last one has fin=True, to complete the
270 * message.
271 * @param data Data to send
272 * @param callback Callback which is invoked when the write completes.
273 */
275 std::span<const uv::Buffer> data,
276 std::function<void(std::span<uv::Buffer>, uv::Error)> callback) {
277 Send(kOpBinary, data, std::move(callback));
278 }
279
280 /**
281 * Send a text message fragment. This must be followed by one or more
282 * SendFragment() calls, where the last one has fin=True, to complete the
283 * message.
284 * @param data Data to send
285 * @param callback Callback which is invoked when the write completes.
286 */
288 std::initializer_list<uv::Buffer> data,
289 std::function<void(std::span<uv::Buffer>, uv::Error)> callback) {
290 SendBinaryFragment({data.begin(), data.end()}, std::move(callback));
291 }
292
293 /**
294 * Send a continuation frame. This is used to send additional parts of a
295 * message started with SendTextFragment() or SendBinaryFragment().
296 * @param data Data to send
297 * @param fin Set to true if this is the final fragment of the message
298 * @param callback Callback which is invoked when the write completes.
299 */
301 std::span<const uv::Buffer> data, bool fin,
302 std::function<void(std::span<uv::Buffer>, uv::Error)> callback) {
303 Send(kOpCont | (fin ? kFlagFin : 0), data, std::move(callback));
304 }
305
306 /**
307 * Send a continuation frame. This is used to send additional parts of a
308 * message started with SendTextFragment() or SendBinaryFragment().
309 * @param data Data to send
310 * @param fin Set to true if this is the final fragment of the message
311 * @param callback Callback which is invoked when the write completes.
312 */
314 std::initializer_list<uv::Buffer> data, bool fin,
315 std::function<void(std::span<uv::Buffer>, uv::Error)> callback) {
316 SendFragment({data.begin(), data.end()}, fin, std::move(callback));
317 }
318
319 /**
320 * Send a ping frame with no data.
321 * @param callback Optional callback which is invoked when the ping frame
322 * write completes.
323 */
324 void SendPing(std::function<void(uv::Error)> callback = nullptr) {
325 SendPing({}, [f = std::move(callback)](auto bufs, uv::Error err) {
326 if (f) {
327 f(err);
328 }
329 });
330 }
331
332 /**
333 * Send a ping frame.
334 * @param data Data to send in the ping frame
335 * @param callback Callback which is invoked when the ping frame
336 * write completes.
337 */
339 std::span<const uv::Buffer> data,
340 std::function<void(std::span<uv::Buffer>, uv::Error)> callback) {
341 SendControl(kFlagFin | kOpPing, data, std::move(callback));
342 }
343
344 /**
345 * Send a ping frame.
346 * @param data Data to send in the ping frame
347 * @param callback Callback which is invoked when the ping frame
348 * write completes.
349 */
351 std::initializer_list<uv::Buffer> data,
352 std::function<void(std::span<uv::Buffer>, uv::Error)> callback) {
353 SendPing({data.begin(), data.end()}, std::move(callback));
354 }
355
356 /**
357 * Send a pong frame with no data.
358 * @param callback Optional callback which is invoked when the pong frame
359 * write completes.
360 */
361 void SendPong(std::function<void(uv::Error)> callback = nullptr) {
362 SendPong({}, [f = std::move(callback)](auto bufs, uv::Error err) {
363 if (f) {
364 f(err);
365 }
366 });
367 }
368
369 /**
370 * Send a pong frame.
371 * @param data Data to send in the pong frame
372 * @param callback Callback which is invoked when the pong frame
373 * write completes.
374 */
376 std::span<const uv::Buffer> data,
377 std::function<void(std::span<uv::Buffer>, uv::Error)> callback) {
378 SendControl(kFlagFin | kOpPong, data, std::move(callback));
379 }
380
381 /**
382 * Send a pong frame.
383 * @param data Data to send in the pong frame
384 * @param callback Callback which is invoked when the pong frame
385 * write completes.
386 */
388 std::initializer_list<uv::Buffer> data,
389 std::function<void(std::span<uv::Buffer>, uv::Error)> callback) {
390 SendPong({data.begin(), data.end()}, std::move(callback));
391 }
392
393 /**
394 * Send multiple frames.
395 *
396 * @param frames Frame type/data pairs
397 * @param callback Callback which is invoked when the write completes.
398 */
400 std::span<const Frame> frames,
401 std::function<void(std::span<uv::Buffer>, uv::Error)> callback);
402
403 /**
404 * Try to send multiple frames. Tries to send as many frames as possible
405 * immediately, and only queues the "last" frame it can (as the network queue
406 * will almost always fill partway through a frame). The frames following
407 * the last frame will NOT be queued for transmission; the caller is
408 * responsible for how to handle (e.g. re-send) those frames (e.g. when the
409 * callback is called).
410 *
411 * @param frames Frame type/data pairs
412 * @param callback Callback which is invoked when the write completes of the
413 * last frame that is not returned.
414 * @return Remaining frames that will not be sent
415 */
416 std::span<const Frame> TrySendFrames(
417 std::span<const Frame> frames,
418 std::function<void(std::span<uv::Buffer>, uv::Error)> callback);
419
420 /**
421 * Returns whether or not a previous TrySendFrames is still in progress.
422 * Calling TrySendFrames if this returns true will return all frames.
423 *
424 * @return True if a TryWrite is in progress
425 */
426 bool IsWriteInProgress() const { return m_writeInProgress; }
427
428 /**
429 * Fail the connection.
430 */
431 void Fail(uint16_t code = 1002, std::string_view reason = "protocol error");
432
433 /**
434 * Forcibly close the connection.
435 */
436 void Terminate(uint16_t code = 1006, std::string_view reason = "terminated");
437
438 /**
439 * Gets user-defined data.
440 * @return User-defined data if any, nullptr otherwise.
441 */
442 template <typename T = void>
443 std::shared_ptr<T> GetData() const {
444 return std::static_pointer_cast<T>(m_data);
445 }
446
447 /**
448 * Sets user-defined data.
449 * @param data User-defined arbitrary data.
450 */
451 void SetData(std::shared_ptr<void> data) { m_data = std::move(data); }
452
453 /**
454 * Shuts down and closes the underlying stream.
455 */
456 void Shutdown();
457
458 /**
459 * Gets the last time data was received on the stream.
460 * @return Timestamp
461 */
462 uint64_t GetLastReceivedTime() const { return m_lastReceivedTime; }
463
464 /**
465 * Open event. Emitted when the connection is open and ready to communicate.
466 * The parameter is the selected subprotocol.
467 */
469
470 /**
471 * Close event. Emitted when the connection is closed. The first parameter
472 * is a numeric value indicating the status code explaining why the connection
473 * has been closed. The second parameter is a human-readable string
474 * explaining the reason why the connection has been closed.
475 */
477
478 /**
479 * Text message event. Emitted when a text message is received.
480 * The first parameter is the data, the second parameter is true if the
481 * data is the last fragment of the message.
482 */
484
485 /**
486 * Binary message event. Emitted when a binary message is received.
487 * The first parameter is the data, the second parameter is true if the
488 * data is the last fragment of the message.
489 */
491
492 /**
493 * Ping event. Emitted when a ping message is received. A pong message is
494 * automatically sent in response, so this is simply a notification.
495 */
497
498 /**
499 * Pong event. Emitted when a pong message is received.
500 */
502
503 private:
504 // user data
505 std::shared_ptr<void> m_data;
506
507 // constructor parameters
508 uv::Stream& m_stream;
509 bool m_server;
510
511 // subprotocol, set via constructor (server) or handshake (client)
512 std::string m_protocol;
513
514 // user-settable configuration
515 size_t m_maxMessageSize = 128 * 1024;
516 bool m_combineFragments = true;
517
518 // outgoing write request
519 bool m_writeInProgress = false;
520 class WriteReq;
521 std::weak_ptr<WriteReq> m_curWriteReq;
522 std::weak_ptr<WriteReq> m_lastWriteReq;
523
524 // operating state
525 State m_state = CONNECTING;
526
527 // incoming message buffers/state
528 uint64_t m_lastReceivedTime = 0;
529 wpi::util::SmallVector<uint8_t, 14> m_header;
530 size_t m_headerSize = 0;
531 wpi::util::SmallVector<uint8_t, 1024> m_payload;
532 wpi::util::SmallVector<uint8_t, 64> m_controlPayload;
533 size_t m_frameStart = 0;
534 uint64_t m_frameSize = UINT64_MAX;
535 uint8_t m_fragmentOpcode = 0;
536
537 // temporary data used only during client handshake
538 class ClientHandshakeData;
539 std::unique_ptr<ClientHandshakeData> m_clientHandshake;
540
541 void StartClient(std::string_view uri, std::string_view host,
542 std::span<const std::string_view> protocols,
543 const ClientOptions& options);
544 void StartServer(std::string_view key, std::string_view version,
545 std::string_view protocol);
546 void SendClose(uint16_t code, std::string_view reason);
547 void SetClosed(uint16_t code, std::string_view reason, bool failed = false);
548 void HandleIncoming(uv::Buffer& buf, size_t size);
549 void SendControl(
550 uint8_t opcode, std::span<const uv::Buffer> data,
551 std::function<void(std::span<uv::Buffer>, uv::Error)> callback);
552 void Send(uint8_t opcode, std::span<const uv::Buffer> data,
553 std::function<void(std::span<uv::Buffer>, uv::Error)> callback) {
554 SendFrames({{Frame{opcode, data}}}, std::move(callback));
555 }
556 void SendError(
557 std::span<const Frame> frames,
558 const std::function<void(std::span<uv::Buffer>, uv::Error)>& callback);
559};
560
561} // namespace wpi::net
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or glfw and nanopb were modified for use in Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source code
Definition ThirdPartyNotices.txt:122
A timer class.
Definition Timer.hpp:36
void Close(uint16_t code=1005, std::string_view reason={})
Initiate a closing handshake.
uv::Stream & GetStream() const
Get the underlying stream.
Definition WebSocket.hpp:166
void SendPing(std::initializer_list< uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback)
Send a ping frame.
Definition WebSocket.hpp:350
void SendText(std::initializer_list< uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback)
Send a text message.
Definition WebSocket.hpp:213
void SendPing(std::function< void(uv::Error)> callback=nullptr)
Send a ping frame with no data.
Definition WebSocket.hpp:324
static constexpr uint8_t kFlagFin
Definition WebSocket.hpp:43
static std::shared_ptr< WebSocket > CreateServer(uv::Stream &stream, std::string_view key, std::string_view version, std::string_view protocol={})
Starts a server connection by performing the initial server side handshake.
bool IsWriteInProgress() const
Returns whether or not a previous TrySendFrames is still in progress.
Definition WebSocket.hpp:426
std::string_view GetProtocol() const
Get the selected sub-protocol.
Definition WebSocket.hpp:171
wpi::util::sig::Signal< uint16_t, std::string_view > closed
Close event.
Definition WebSocket.hpp:476
static constexpr uint8_t kOpMask
Definition WebSocket.hpp:42
void SetCombineFragments(bool combine)
Set whether or not fragmented frames should be combined.
Definition WebSocket.hpp:187
uint64_t GetLastReceivedTime() const
Gets the last time data was received on the stream.
Definition WebSocket.hpp:462
void SendTextFragment(std::initializer_list< uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback)
Send a text message fragment.
Definition WebSocket.hpp:261
std::shared_ptr< T > GetData() const
Gets user-defined data.
Definition WebSocket.hpp:443
void SendFragment(std::span< const uv::Buffer > data, bool fin, std::function< void(std::span< uv::Buffer >, uv::Error)> callback)
Send a continuation frame.
Definition WebSocket.hpp:300
void SetMaxMessageSize(size_t size)
Set the maximum message size.
Definition WebSocket.hpp:179
static constexpr uint8_t kOpBinary
Definition WebSocket.hpp:38
State
Connection states.
Definition WebSocket.hpp:56
@ CLOSING
The connection is in the process of closing.
Definition WebSocket.hpp:62
@ CLOSED
The connection is closed.
Definition WebSocket.hpp:66
@ CONNECTING
The connection is not yet open.
Definition WebSocket.hpp:58
@ OPEN
The connection is open and ready to communicate.
Definition WebSocket.hpp:60
@ FAILED
The connection failed.
Definition WebSocket.hpp:64
WebSocket(WebSocket &&)=delete
void SendTextFragment(std::span< const uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback)
Send a text message fragment.
Definition WebSocket.hpp:248
bool IsOpen() const
Return if the connection is open.
Definition WebSocket.hpp:161
void SendPing(std::span< const uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback)
Send a ping frame.
Definition WebSocket.hpp:338
static constexpr uint8_t kOpPing
Definition WebSocket.hpp:40
void SendBinaryFragment(std::span< const uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback)
Send a text message fragment.
Definition WebSocket.hpp:274
WebSocket(uv::Stream &stream, bool server, const private_init &)
static constexpr uint8_t kFlagControl
Definition WebSocket.hpp:44
static constexpr uint8_t kOpText
Definition WebSocket.hpp:37
static constexpr uint8_t kOpCont
Definition WebSocket.hpp:36
static std::shared_ptr< WebSocket > CreateClient(uv::Stream &stream, std::string_view uri, std::string_view host, std::span< const std::string_view > protocols={}, const ClientOptions &options={})
Starts a client connection by performing the initial client handshake.
wpi::util::sig::Signal< std::span< const uint8_t > > ping
Ping event.
Definition WebSocket.hpp:496
wpi::util::sig::Signal< std::span< const uint8_t > > pong
Pong event.
Definition WebSocket.hpp:501
void SendText(std::span< const uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback)
Send a text message.
Definition WebSocket.hpp:202
WebSocket & operator=(WebSocket &&)=delete
wpi::util::sig::Signal< std::string_view, bool > text
Text message event.
Definition WebSocket.hpp:483
void SendFrames(std::span< const Frame > frames, std::function< void(std::span< uv::Buffer >, uv::Error)> callback)
Send multiple frames.
void Shutdown()
Shuts down and closes the underlying stream.
void Terminate(uint16_t code=1006, std::string_view reason="terminated")
Forcibly close the connection.
wpi::util::sig::Signal< std::string_view > open
Open event.
Definition WebSocket.hpp:468
static constexpr uint8_t kOpPong
Definition WebSocket.hpp:41
void SendBinary(std::span< const uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback)
Send a binary message.
Definition WebSocket.hpp:224
void SendPong(std::span< const uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback)
Send a pong frame.
Definition WebSocket.hpp:375
void SendPong(std::function< void(uv::Error)> callback=nullptr)
Send a pong frame with no data.
Definition WebSocket.hpp:361
State GetState() const
Get connection state.
Definition WebSocket.hpp:155
std::span< const Frame > TrySendFrames(std::span< const Frame > frames, std::function< void(std::span< uv::Buffer >, uv::Error)> callback)
Try to send multiple frames.
void SetData(std::shared_ptr< void > data)
Sets user-defined data.
Definition WebSocket.hpp:451
static constexpr uint8_t kOpClose
Definition WebSocket.hpp:39
void SendPong(std::initializer_list< uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback)
Send a pong frame.
Definition WebSocket.hpp:387
void SendBinary(std::initializer_list< uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback)
Send a binary message.
Definition WebSocket.hpp:235
WebSocket(const WebSocket &)=delete
void SendBinaryFragment(std::initializer_list< uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback)
Send a text message fragment.
Definition WebSocket.hpp:287
static std::shared_ptr< WebSocket > CreateClient(uv::Stream &stream, std::string_view uri, std::string_view host, std::initializer_list< std::string_view > protocols, const ClientOptions &options={})
Starts a client connection by performing the initial client handshake.
Definition WebSocket.hpp:127
void SendFragment(std::initializer_list< uv::Buffer > data, bool fin, std::function< void(std::span< uv::Buffer >, uv::Error)> callback)
Send a continuation frame.
Definition WebSocket.hpp:313
WebSocket & operator=(const WebSocket &)=delete
void Fail(uint16_t code=1002, std::string_view reason="protocol error")
Fail the connection.
wpi::util::sig::Signal< std::span< const uint8_t >, bool > binary
Binary message event.
Definition WebSocket.hpp:490
Data buffer.
Definition Buffer.hpp:22
Error code.
Definition Error.hpp:14
Stream handle.
Definition Stream.hpp:66
std::chrono::duration< uint64_t, std::milli > Time
Definition Timer.hpp:29
Definition Errors.hpp:112
Definition Prepare.hpp:14
Definition raw_socket_ostream.hpp:9
SignalBase< detail::NullMutex, T... > Signal
Specialization of SignalBase to be used in single threaded contexts.
Definition Signal.h:809
Client connection options.
Definition WebSocket.hpp:72
uv::Timer::Time handshakeTimeout
Timeout for the handshake request.
Definition WebSocket.hpp:76
ClientOptions()
Definition WebSocket.hpp:73
std::span< const std::pair< std::string_view, std::string_view > > extraHeaders
Additional headers to include in handshake.
Definition WebSocket.hpp:79
Frame.
Definition WebSocket.hpp:85
static constexpr uint8_t kBinaryFragment
Definition WebSocket.hpp:89
static constexpr uint8_t kText
Definition WebSocket.hpp:86
std::span< const uv::Buffer > data
Definition WebSocket.hpp:99
static constexpr uint8_t kTextFragment
Definition WebSocket.hpp:88
static constexpr uint8_t kFinalFragment
Definition WebSocket.hpp:91
uint8_t opcode
Definition WebSocket.hpp:98
static constexpr uint8_t kFragment
Definition WebSocket.hpp:90
static constexpr uint8_t kPing
Definition WebSocket.hpp:92
static constexpr uint8_t kPong
Definition WebSocket.hpp:93
constexpr Frame(uint8_t opcode, std::span< const uv::Buffer > data)
Definition WebSocket.hpp:95
static constexpr uint8_t kBinary
Definition WebSocket.hpp:87