WPILibC++ 2024.3.2
|
RFC 6455 compliant WebSocket client and server implementation. More...
#include <wpinet/WebSocket.h>
Classes | |
struct | ClientOptions |
Client connection options. More... | |
struct | Frame |
Frame. More... | |
Public Types | |
enum | State { CONNECTING = 0 , OPEN , CLOSING , FAILED , CLOSED } |
Connection states. More... | |
Public Member Functions | |
WebSocket (uv::Stream &stream, bool server, const private_init &) | |
WebSocket (const WebSocket &)=delete | |
WebSocket (WebSocket &&)=delete | |
WebSocket & | operator= (const WebSocket &)=delete |
WebSocket & | operator= (WebSocket &&)=delete |
~WebSocket () | |
State | GetState () const |
Get connection state. More... | |
bool | IsOpen () const |
Return if the connection is open. More... | |
uv::Stream & | GetStream () const |
Get the underlying stream. More... | |
std::string_view | GetProtocol () const |
Get the selected sub-protocol. More... | |
void | SetMaxMessageSize (size_t size) |
Set the maximum message size. More... | |
void | SetCombineFragments (bool combine) |
Set whether or not fragmented frames should be combined. More... | |
void | Close (uint16_t code=1005, std::string_view reason={}) |
Initiate a closing handshake. More... | |
void | SendText (std::span< const uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback) |
Send a text message. More... | |
void | SendText (std::initializer_list< uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback) |
Send a text message. More... | |
void | SendBinary (std::span< const uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback) |
Send a binary message. More... | |
void | SendBinary (std::initializer_list< uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback) |
Send a binary message. More... | |
void | SendTextFragment (std::span< const uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback) |
Send a text message fragment. More... | |
void | SendTextFragment (std::initializer_list< uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback) |
Send a text message fragment. More... | |
void | SendBinaryFragment (std::span< const uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback) |
Send a text message fragment. More... | |
void | SendBinaryFragment (std::initializer_list< uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback) |
Send a text message fragment. More... | |
void | SendFragment (std::span< const uv::Buffer > data, bool fin, std::function< void(std::span< uv::Buffer >, uv::Error)> callback) |
Send a continuation frame. More... | |
void | SendFragment (std::initializer_list< uv::Buffer > data, bool fin, std::function< void(std::span< uv::Buffer >, uv::Error)> callback) |
Send a continuation frame. More... | |
void | SendPing (std::function< void(uv::Error)> callback=nullptr) |
Send a ping frame with no data. More... | |
void | SendPing (std::span< const uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback) |
Send a ping frame. More... | |
void | SendPing (std::initializer_list< uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback) |
Send a ping frame. More... | |
void | SendPong (std::function< void(uv::Error)> callback=nullptr) |
Send a pong frame with no data. More... | |
void | SendPong (std::span< const uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback) |
Send a pong frame. More... | |
void | SendPong (std::initializer_list< uv::Buffer > data, std::function< void(std::span< uv::Buffer >, uv::Error)> callback) |
Send a pong frame. More... | |
void | SendFrames (std::span< const Frame > frames, std::function< void(std::span< uv::Buffer >, uv::Error)> callback) |
Send multiple frames. More... | |
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. More... | |
bool | IsWriteInProgress () const |
Returns whether or not a previous TrySendFrames is still in progress. More... | |
void | Fail (uint16_t code=1002, std::string_view reason="protocol error") |
Fail the connection. More... | |
void | Terminate (uint16_t code=1006, std::string_view reason="terminated") |
Forcibly close the connection. More... | |
template<typename T = void> | |
std::shared_ptr< T > | GetData () const |
Gets user-defined data. More... | |
void | SetData (std::shared_ptr< void > data) |
Sets user-defined data. More... | |
void | Shutdown () |
Shuts down and closes the underlying stream. More... | |
uint64_t | GetLastReceivedTime () const |
Gets the last time data was received on the stream. More... | |
Static Public Member Functions | |
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. More... | |
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. More... | |
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. More... | |
Public Attributes | |
sig::Signal< std::string_view > | open |
Open event. More... | |
sig::Signal< uint16_t, std::string_view > | closed |
Close event. More... | |
sig::Signal< std::string_view, bool > | text |
Text message event. More... | |
sig::Signal< std::span< const uint8_t >, bool > | binary |
Binary message event. More... | |
sig::Signal< std::span< const uint8_t > > | ping |
Ping event. More... | |
sig::Signal< std::span< const uint8_t > > | pong |
Pong event. More... | |
Static Public Attributes | |
static constexpr uint8_t | kOpCont = 0x00 |
static constexpr uint8_t | kOpText = 0x01 |
static constexpr uint8_t | kOpBinary = 0x02 |
static constexpr uint8_t | kOpClose = 0x08 |
static constexpr uint8_t | kOpPing = 0x09 |
static constexpr uint8_t | kOpPong = 0x0A |
static constexpr uint8_t | kOpMask = 0x0F |
static constexpr uint8_t | kFlagFin = 0x80 |
static constexpr uint8_t | kFlagControl = 0x08 |
RFC 6455 compliant WebSocket client and server implementation.
wpi::WebSocket::WebSocket | ( | uv::Stream & | stream, |
bool | server, | ||
const private_init & | |||
) |
|
delete |
|
delete |
wpi::WebSocket::~WebSocket | ( | ) |
void wpi::WebSocket::Close | ( | uint16_t | code = 1005 , |
std::string_view | reason = {} |
||
) |
Initiate a closing handshake.
code | A numeric status code (defaults to 1005, no status code) |
reason | A human-readable string explaining why the connection is closing (optional). |
|
inlinestatic |
Starts a client connection by performing the initial client handshake.
An open event is emitted when the handshake completes. This sets the stream user data to the websocket.
stream | Connection stream |
uri | The Request-URI to send |
host | The host or host:port to send |
protocols | The list of subprotocols |
options | Handshake options |
|
static |
Starts a client connection by performing the initial client handshake.
An open event is emitted when the handshake completes. This sets the stream user data to the websocket.
stream | Connection stream |
uri | The Request-URI to send |
host | The host or host:port to send |
protocols | The list of subprotocols |
options | Handshake options |
|
static |
Starts a server connection by performing the initial server side handshake.
This should be called after the HTTP headers have been received. An open event is emitted when the handshake completes. This sets the stream user data to the websocket.
stream | Connection stream |
key | The value of the Sec-WebSocket-Key header field in the client request |
version | The value of the Sec-WebSocket-Version header field in the client request |
protocol | The subprotocol to send to the client (in the Sec-WebSocket-Protocol header field). |
void wpi::WebSocket::Fail | ( | uint16_t | code = 1002 , |
std::string_view | reason = "protocol error" |
||
) |
Fail the connection.
|
inline |
Gets user-defined data.
|
inline |
Gets the last time data was received on the stream.
|
inline |
Get the selected sub-protocol.
Only valid in or after the open() event.
|
inline |
Get connection state.
|
inline |
Get the underlying stream.
|
inline |
Return if the connection is open.
Messages can only be sent on open connections.
|
inline |
Returns whether or not a previous TrySendFrames is still in progress.
Calling TrySendFrames if this returns true will return all frames.
|
inline |
Send a binary message.
data | Data to send |
callback | Callback which is invoked when the write completes. |
|
inline |
Send a binary message.
data | Data to send |
callback | Callback which is invoked when the write completes. |
|
inline |
Send a text message fragment.
This must be followed by one or more SendFragment() calls, where the last one has fin=True, to complete the message.
data | Data to send |
callback | Callback which is invoked when the write completes. |
|
inline |
Send a text message fragment.
This must be followed by one or more SendFragment() calls, where the last one has fin=True, to complete the message.
data | Data to send |
callback | Callback which is invoked when the write completes. |
|
inline |
Send a continuation frame.
This is used to send additional parts of a message started with SendTextFragment() or SendBinaryFragment().
data | Data to send |
fin | Set to true if this is the final fragment of the message |
callback | Callback which is invoked when the write completes. |
|
inline |
Send a continuation frame.
This is used to send additional parts of a message started with SendTextFragment() or SendBinaryFragment().
data | Data to send |
fin | Set to true if this is the final fragment of the message |
callback | Callback which is invoked when the write completes. |
void wpi::WebSocket::SendFrames | ( | std::span< const Frame > | frames, |
std::function< void(std::span< uv::Buffer >, uv::Error)> | callback | ||
) |
Send multiple frames.
frames | Frame type/data pairs |
callback | Callback which is invoked when the write completes. |
|
inline |
Send a ping frame with no data.
callback | Optional callback which is invoked when the ping frame write completes. |
|
inline |
Send a ping frame.
data | Data to send in the ping frame |
callback | Callback which is invoked when the ping frame write completes. |
|
inline |
Send a ping frame.
data | Data to send in the ping frame |
callback | Callback which is invoked when the ping frame write completes. |
|
inline |
Send a pong frame with no data.
callback | Optional callback which is invoked when the pong frame write completes. |
|
inline |
Send a pong frame.
data | Data to send in the pong frame |
callback | Callback which is invoked when the pong frame write completes. |
|
inline |
Send a pong frame.
data | Data to send in the pong frame |
callback | Callback which is invoked when the pong frame write completes. |
|
inline |
Send a text message.
data | UTF-8 encoded data to send |
callback | Callback which is invoked when the write completes. |
|
inline |
Send a text message.
data | UTF-8 encoded data to send |
callback | Callback which is invoked when the write completes. |
|
inline |
Send a text message fragment.
This must be followed by one or more SendFragment() calls, where the last one has fin=True, to complete the message.
data | UTF-8 encoded data to send |
callback | Callback which is invoked when the write completes. |
|
inline |
Send a text message fragment.
This must be followed by one or more SendFragment() calls, where the last one has fin=True, to complete the message.
data | UTF-8 encoded data to send |
callback | Callback which is invoked when the write completes. |
|
inline |
Set whether or not fragmented frames should be combined.
Default is to combine. If fragmented frames are combined, the text and binary callbacks will always have the second parameter (fin) set to true.
combine | True if fragmented frames should be combined. |
|
inline |
Sets user-defined data.
data | User-defined arbitrary data. |
|
inline |
Set the maximum message size.
Default is 128 KB. If configured to combine fragments this maximum applies to the entire message (all combined fragments).
size | Maximum message size in bytes |
void wpi::WebSocket::Shutdown | ( | ) |
Shuts down and closes the underlying stream.
void wpi::WebSocket::Terminate | ( | uint16_t | code = 1006 , |
std::string_view | reason = "terminated" |
||
) |
Forcibly close the connection.
std::span< const Frame > wpi::WebSocket::TrySendFrames | ( | std::span< const Frame > | frames, |
std::function< void(std::span< uv::Buffer >, uv::Error)> | callback | ||
) |
Try to send multiple frames.
Tries to send as many frames as possible immediately, and only queues the "last" frame it can (as the network queue will almost always fill partway through a frame). The frames following the last frame will NOT be queued for transmission; the caller is responsible for how to handle (e.g. re-send) those frames (e.g. when the callback is called).
frames | Frame type/data pairs |
callback | Callback which is invoked when the write completes of the last frame that is not returned. |
sig::Signal<std::span<const uint8_t>, bool> wpi::WebSocket::binary |
Binary message event.
Emitted when a binary message is received. The first parameter is the data, the second parameter is true if the data is the last fragment of the message.
sig::Signal<uint16_t, std::string_view> wpi::WebSocket::closed |
Close event.
Emitted when the connection is closed. The first parameter is a numeric value indicating the status code explaining why the connection has been closed. The second parameter is a human-readable string explaining the reason why the connection has been closed.
|
staticconstexpr |
|
staticconstexpr |
|
staticconstexpr |
|
staticconstexpr |
|
staticconstexpr |
|
staticconstexpr |
|
staticconstexpr |
|
staticconstexpr |
|
staticconstexpr |
sig::Signal<std::string_view> wpi::WebSocket::open |
Open event.
Emitted when the connection is open and ready to communicate. The parameter is the selected subprotocol.
sig::Signal<std::span<const uint8_t> > wpi::WebSocket::ping |
Ping event.
Emitted when a ping message is received. A pong message is automatically sent in response, so this is simply a notification.
sig::Signal<std::span<const uint8_t> > wpi::WebSocket::pong |
Pong event.
Emitted when a pong message is received.
sig::Signal<std::string_view, bool> wpi::WebSocket::text |
Text message event.
Emitted when a text message is received. The first parameter is the data, the second parameter is true if the data is the last fragment of the message.