10#include <initializer_list>
21#include "wpi/util/SmallVector.hpp"
32class WebSocket :
public std::enable_shared_from_this<WebSocket> {
33 struct private_init {};
79 std::span<const std::pair<std::string_view, std::string_view>>
extraHeaders;
99 std::span<const uv::Buffer>
data;
113 uv::Stream& stream, std::string_view uri, std::string_view host,
114 std::span<const std::string_view> protocols = {},
115 const ClientOptions& options = {});
128 uv::Stream& stream, std::string_view uri, std::string_view host,
129 std::initializer_list<std::string_view> protocols,
131 return CreateClient(stream, uri, host, {protocols.begin(), protocols.end()},
149 uv::Stream& stream, std::string_view key, std::string_view version,
150 std::string_view protocol = {});
195 void Close(uint16_t
code = 1005, std::string_view reason = {});
203 std::span<const uv::Buffer> data,
204 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
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));
225 std::span<const uv::Buffer> data,
226 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
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));
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));
262 std::initializer_list<uv::Buffer> data,
263 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
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));
288 std::initializer_list<uv::Buffer> data,
289 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
301 std::span<const uv::Buffer> data,
bool fin,
302 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
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));
339 std::span<const uv::Buffer> data,
340 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
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));
376 std::span<const uv::Buffer> data,
377 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
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));
400 std::span<const Frame> frames,
401 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback);
417 std::span<const Frame> frames,
418 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback);
431 void Fail(uint16_t
code = 1002, std::string_view reason =
"protocol error");
436 void Terminate(uint16_t
code = 1006, std::string_view reason =
"terminated");
442 template <
typename T =
void>
444 return std::static_pointer_cast<T>(m_data);
451 void SetData(std::shared_ptr<void> data) { m_data = std::move(data); }
505 std::shared_ptr<void> m_data;
512 std::string m_protocol;
515 size_t m_maxMessageSize = 128 * 1024;
516 bool m_combineFragments =
true;
519 bool m_writeInProgress =
false;
521 std::weak_ptr<WriteReq> m_curWriteReq;
522 std::weak_ptr<WriteReq> m_lastWriteReq;
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;
538 class ClientHandshakeData;
539 std::unique_ptr<ClientHandshakeData> m_clientHandshake;
541 void StartClient(std::string_view uri, std::string_view host,
542 std::span<const std::string_view> protocols,
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);
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) {
557 std::span<const Frame> frames,
558 const std::function<
void(std::span<uv::Buffer>, uv::Error)>& callback);
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