5#ifndef WPINET_WEBSOCKET_H_
6#define WPINET_WEBSOCKET_H_
11#include <initializer_list>
34class WebSocket :
public std::enable_shared_from_this<WebSocket> {
35 struct private_init {};
37 static constexpr uint8_t kOpCont = 0x00;
38 static constexpr uint8_t kOpText = 0x01;
39 static constexpr uint8_t kOpBinary = 0x02;
40 static constexpr uint8_t kOpClose = 0x08;
41 static constexpr uint8_t kOpPing = 0x09;
42 static constexpr uint8_t kOpPong = 0x0A;
43 static constexpr uint8_t kOpMask = 0x0F;
44 static constexpr uint8_t kFlagFin = 0x80;
80 std::span<const std::pair<std::string_view, std::string_view>>
extraHeaders;
100 std::span<const uv::Buffer>
data;
115 std::span<const std::string_view> protocols = {},
116 const ClientOptions& options = {});
130 std::initializer_list<std::string_view> protocols,
132 return CreateClient(stream, uri, host, {protocols.begin(), protocols.end()},
204 std::span<const uv::Buffer>
data,
205 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
206 Send(kFlagFin | kOpText,
data, std::move(callback));
215 std::initializer_list<uv::Buffer>
data,
216 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
226 std::span<const uv::Buffer>
data,
227 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
228 Send(kFlagFin | kOpBinary,
data, std::move(callback));
237 std::initializer_list<uv::Buffer>
data,
238 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
250 std::span<const uv::Buffer>
data,
251 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
252 Send(kOpText,
data, std::move(callback));
263 std::initializer_list<uv::Buffer>
data,
264 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
276 std::span<const uv::Buffer>
data,
277 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
278 Send(kOpBinary,
data, std::move(callback));
289 std::initializer_list<uv::Buffer>
data,
290 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
302 std::span<const uv::Buffer>
data,
bool fin,
303 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
304 Send(kOpCont | (fin ? kFlagFin : 0),
data, std::move(callback));
315 std::initializer_list<uv::Buffer>
data,
bool fin,
316 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
340 std::span<const uv::Buffer>
data,
341 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
342 Send(kFlagFin | kOpPing,
data, std::move(callback));
352 std::initializer_list<uv::Buffer>
data,
353 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
377 std::span<const uv::Buffer>
data,
378 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
379 Send(kFlagFin | kOpPong,
data, std::move(callback));
389 std::initializer_list<uv::Buffer>
data,
390 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
401 std::span<const Frame> frames,
402 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback);
418 template <
typename T =
void>
420 return std::static_pointer_cast<T>(m_data);
474 std::shared_ptr<void> m_data;
481 std::string m_protocol;
484 size_t m_maxMessageSize = 128 * 1024;
485 bool m_combineFragments =
true;
492 size_t m_headerSize = 0;
494 size_t m_frameStart = 0;
499 class ClientHandshakeData;
500 std::unique_ptr<ClientHandshakeData> m_clientHandshake;
503 std::span<const std::string_view> protocols,
510 void Send(
uint8_t opcode, std::span<const uv::Buffer>
data,
511 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
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:128
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:314
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:249
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:214
sig::Signal< uint16_t, std::string_view > closed
Close event.
Definition: WebSocket.h:446
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:419
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:325
sig::Signal< std::span< const uint8_t >, bool > binary
Binary message event.
Definition: WebSocket.h:460
WebSocket & operator=(const WebSocket &)=delete
sig::Signal< std::string_view > open
Open event.
Definition: WebSocket.h:438
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:301
sig::Signal< std::string_view, bool > text
Text message event.
Definition: WebSocket.h:453
State GetState() const
Get connection state.
Definition: WebSocket.h:156
sig::Signal< std::span< const uint8_t > > ping
Ping event.
Definition: WebSocket.h:465
sig::Signal< std::span< const uint8_t > > pong
Pong event.
Definition: WebSocket.h:470
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:339
bool IsOpen() const
Return if the connection is open.
Definition: WebSocket.h:162
State
Connection states.
Definition: WebSocket.h:57
@ CLOSING
The connection is in the process of closing.
Definition: WebSocket.h:63
@ CLOSED
The connection is closed.
Definition: WebSocket.h:67
@ OPEN
The connection is open and ready to communicate.
Definition: WebSocket.h:61
@ FAILED
The connection failed.
Definition: WebSocket.h:65
@ CONNECTING
The connection is not yet open.
Definition: WebSocket.h:59
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:376
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:275
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:362
WebSocket(const WebSocket &)=delete
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:188
void SetData(std::shared_ptr< void > data)
Sets user-defined data.
Definition: WebSocket.h:427
std::string_view GetProtocol() const
Get the selected sub-protocol.
Definition: WebSocket.h:172
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:236
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:167
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:225
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:288
void SetMaxMessageSize(size_t size)
Set the maximum message size.
Definition: WebSocket.h:180
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:203
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:388
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:351
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:262
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:520
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.
constexpr common_t< T1, T2 > max(const T1 x, const T2 y) noexcept
Compile-time pairwise maximum function.
Definition: max.hpp:35
EIGEN_CONSTEXPR Index size(const T &x)
Definition: Meta.h:479
::uint64_t uint64_t
Definition: Meta.h:58
::uint16_t uint16_t
Definition: Meta.h:54
::uint8_t uint8_t
Definition: Meta.h:52
Definition: AprilTagFieldLayout.h:18
Definition: format.h:1552
Client connection options.
Definition: WebSocket.h:73
uv::Timer::Time handshakeTimeout
Timeout for the handshake request.
Definition: WebSocket.h:77
ClientOptions()
Definition: WebSocket.h:74
std::span< const std::pair< std::string_view, std::string_view > > extraHeaders
Additional headers to include in handshake.
Definition: WebSocket.h:80
Frame.
Definition: WebSocket.h:86
static constexpr uint8_t kPing
Definition: WebSocket.h:93
static constexpr uint8_t kPong
Definition: WebSocket.h:94
static constexpr uint8_t kFragment
Definition: WebSocket.h:91
static constexpr uint8_t kBinaryFragment
Definition: WebSocket.h:90
std::span< const uv::Buffer > data
Definition: WebSocket.h:100
static constexpr uint8_t kText
Definition: WebSocket.h:87
Frame(uint8_t opcode, std::span< const uv::Buffer > data)
Definition: WebSocket.h:96
static constexpr uint8_t kFinalFragment
Definition: WebSocket.h:92
uint8_t opcode
Definition: WebSocket.h:99
static constexpr uint8_t kTextFragment
Definition: WebSocket.h:89
static constexpr uint8_t kBinary
Definition: WebSocket.h:88