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