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 {};
81 std::span<const std::pair<std::string_view, std::string_view>>
extraHeaders;
101 std::span<const uv::Buffer>
data;
116 std::span<const std::string_view> protocols = {},
117 const ClientOptions& options = {});
131 std::initializer_list<std::string_view> protocols,
133 return CreateClient(stream, uri, host, {protocols.begin(), protocols.end()},
205 std::span<const uv::Buffer> data,
206 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
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));
227 std::span<const uv::Buffer> data,
228 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
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));
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));
264 std::initializer_list<uv::Buffer> data,
265 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
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));
290 std::initializer_list<uv::Buffer> data,
291 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
303 std::span<const uv::Buffer> data,
bool fin,
304 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
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));
341 std::span<const uv::Buffer> data,
342 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
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));
378 std::span<const uv::Buffer> data,
379 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback) {
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));
402 std::span<const Frame> frames,
403 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback);
419 std::span<const Frame> frames,
420 std::function<
void(std::span<uv::Buffer>,
uv::Error)> callback);
444 template <
typename T =
void>
446 return std::static_pointer_cast<T>(m_data);
453 void SetData(std::shared_ptr<void> data) { m_data = std::move(data); }
507 std::shared_ptr<void> m_data;
514 std::string m_protocol;
517 size_t m_maxMessageSize = 128 * 1024;
518 bool m_combineFragments =
true;
521 bool m_writeInProgress =
false;
523 std::weak_ptr<WriteReq> m_curWriteReq;
524 std::weak_ptr<WriteReq> m_lastWriteReq;
530 uint64_t m_lastReceivedTime = 0;
532 size_t m_headerSize = 0;
535 size_t m_frameStart = 0;
536 uint64_t m_frameSize = UINT64_MAX;
537 uint8_t m_fragmentOpcode = 0;
540 class ClientHandshakeData;
541 std::unique_ptr<ClientHandshakeData> m_clientHandshake;
544 std::span<const std::string_view> protocols,
550 void HandleIncoming(
uv::Buffer& buf,
size_t size);
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) {
559 std::span<const Frame> frames,
560 const 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: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