HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NET_WebSocket Class Reference

Class used for web socket operations. More...

#include <NET_WebSocket.h>

+ Inheritance diagram for NET_WebSocket:

Classes

class  Buffer
 
class  Event
 

Public Types

enum  FrameType : OpCode {
  Continue = OpCode_Continue, Utf8 = OpCode_Utf8, Binary = OpCode_Binary, Terminate = OpCode_Terminate,
  Ping = OpCode_Ping, Pong = OpCode_Pong
}
 The currently supported different frame types. More...
 
enum  ErrorCode { READ_FRAME }
 All possible error codes that could occur to be sent to the onError callback. More...
 
enum  State { CONNECTING, OPEN, CLOSING, CLOSED }
 
using Mask = uint32
 

Public Member Functions

 NET_WebSocket ()
 
virtual ~NET_WebSocket ()=default
 
int getFD () const
 The file descriptor of this websocket. More...
 
const UT_Urlurl () const
 The url this websocket is using. More...
 
bool send (const UT_StringHolder &data)
 Send a message to the other end of the websocket. This function will send the data immediately and does NOT queue the data. This is a blocking function. More...
 
bool reconnect (const UT_Url &url, const std::chrono::milliseconds &timeout)
 
virtual bool disconnect ()
 
void sendPing (const UT_StringRef &ping=UT_StringHolder::theEmptyString)
 Send a ping to the server. Regular use does not need to worry about this. Other abstractions will do this for you. More...
 
void sendPong (const UT_StringRef &pong=UT_StringHolder::theEmptyString)
 
bool isConnected () const
 Tell if this websocket is currently in use. More...
 
bool usableStream () const
 
uint32 maxFrameSize () const
 The current max frame size used. More...
 
void setMaxFrameSize (uint32 size)
 Set the max number of bytes sent in each frame. More...
 
exint bufferedAmount () const
 The current amount of bytes waiting to be sent over the network. More...
 
bool hasData ()
 
virtual void onEvent (const Event &ev)
 

Static Public Member Functions

static bool readAll (NET_WebStream *stream, UT_WorkBuffer &msg, FrameType &type)
 Read an entire message in. Really only useful for testing purposes. More...
 
static bool write (NET_WebStream *stream, FrameType type, const UT_StringRef &message, Mask mask=0, exint max_frame_size=-1)
 Write a websocket message to the stream. More...
 
static bool makeFrame (UT_WorkBuffer &frame, FrameType, const UT_StringView mview, Mask mask=0, bool is_finished=true)
 Package a frame which is suitable for sending through a web stream. More...
 
static bool makeMessage (UT_WorkBuffer &full_message, FrameType type, const UT_StringRef &message, Mask mask, exint max_fram_size)
 
template<typename SocketT = NET_WebSocket, typename... Args>
static UT_SharedPtr< SocketT > connect (const UT_Url &url, const std::chrono::milliseconds &timeout, Args &&...args)
 
static constexpr UT_StringLit frameTypeToString (FrameType type)
 Convert the frame type to string representation of this frame type. More...
 

Static Public Attributes

static uint64 theMax16FrameSize
 

Protected Member Functions

bool internalSend (FrameType type, const UT_StringRef &message, exint max_frame_size)
 Create the entire message and place it on the queue to send. More...
 
void flush_ ()
 Flush the currently queue messages to send over the network. More...
 
Event read_ (const std::chrono::milliseconds &timeout=std::chrono::milliseconds(500))
 Read a single frame from the server. If you need to read the entire message then loop until this function returns false or is_finished is true. This function will call the callbacks when a full message is received. More...
 

Static Protected Member Functions

static bool setFrameType (FrameType &type, OpCode op_code)
 

Protected Attributes

UT_Url myUrl
 
NET_WebStreamPtr myStream
 
uint32 myMaxFrameSize
 
bool myIsWaitingForPong
 
NET_Time myLastHeartbeat
 
Buffer myReadBuffer
 
SYS_AtomicInt64 myBufferedAmount
 
UT_Lock myMessageLock
 
NET_CircularBuffer< char > myMessageQueue
 
State myState
 
UT_FastRandom myRand
 

Friends

class NET_WebSocketMonitor
 

Detailed Description

Class used for web socket operations.

Definition at line 47 of file NET_WebSocket.h.

Member Typedef Documentation

Definition at line 62 of file NET_WebSocket.h.

Member Enumeration Documentation

All possible error codes that could occur to be sent to the onError callback.

Enumerator
READ_FRAME 

Definition at line 142 of file NET_WebSocket.h.

enum NET_WebSocket::FrameType : OpCode

The currently supported different frame types.

Enumerator
Continue 
Utf8 
Binary 
Terminate 
Ping 
Pong 

Definition at line 64 of file NET_WebSocket.h.

Enumerator
CONNECTING 
OPEN 
CLOSING 
CLOSED 

Definition at line 171 of file NET_WebSocket.h.

Constructor & Destructor Documentation

NET_WebSocket::NET_WebSocket ( )
virtual NET_WebSocket::~NET_WebSocket ( )
virtualdefault

Member Function Documentation

exint NET_WebSocket::bufferedAmount ( ) const
inline

The current amount of bytes waiting to be sent over the network.

Returns
The number of bytes waiting to be sent.

Definition at line 312 of file NET_WebSocket.h.

template<typename SocketT = NET_WebSocket, typename... Args>
static UT_SharedPtr<SocketT> NET_WebSocket::connect ( const UT_Url url,
const std::chrono::milliseconds &  timeout,
Args &&...  args 
)
inlinestatic

Definition at line 205 of file NET_WebSocket.h.

virtual bool NET_WebSocket::disconnect ( )
virtual
void NET_WebSocket::flush_ ( )
protected

Flush the currently queue messages to send over the network.

static constexpr UT_StringLit NET_WebSocket::frameTypeToString ( FrameType  type)
inlinestatic

Convert the frame type to string representation of this frame type.

Parameters
typeThe frame type to convert.
Returns
The string representation of the frame type.

Definition at line 222 of file NET_WebSocket.h.

int NET_WebSocket::getFD ( ) const
inline

The file descriptor of this websocket.

Returns
The file descriptor

Definition at line 253 of file NET_WebSocket.h.

bool NET_WebSocket::hasData ( )
bool NET_WebSocket::internalSend ( FrameType  type,
const UT_StringRef message,
exint  max_frame_size 
)
protected

Create the entire message and place it on the queue to send.

Parameters
typeThe type of message.
messageThe message to include.
maskThe mask for the message.
max_frame_sizeThe max frame size for the message.
Returns
True if creating the message and buffering did not fail.
bool NET_WebSocket::isConnected ( ) const
inline

Tell if this websocket is currently in use.

Returns
True if this websocket is still in use.

Definition at line 297 of file NET_WebSocket.h.

static bool NET_WebSocket::makeFrame ( UT_WorkBuffer frame,
FrameType  ,
const UT_StringView  mview,
Mask  mask = 0,
bool  is_finished = true 
)
static

Package a frame which is suitable for sending through a web stream.

static bool NET_WebSocket::makeMessage ( UT_WorkBuffer full_message,
FrameType  type,
const UT_StringRef message,
Mask  mask,
exint  max_fram_size 
)
static

Package up the message into the full message suitable for sending on the socket.

uint32 NET_WebSocket::maxFrameSize ( ) const
inline

The current max frame size used.

Returns
The max frame size

Definition at line 303 of file NET_WebSocket.h.

virtual void NET_WebSocket::onEvent ( const Event ev)
inlinevirtual

Reimplemented in NET_WebSocketAPI.

Definition at line 320 of file NET_WebSocket.h.

Event NET_WebSocket::read_ ( const std::chrono::milliseconds &  timeout = std::chrono::milliseconds(500))
protected

Read a single frame from the server. If you need to read the entire message then loop until this function returns false or is_finished is true. This function will call the callbacks when a full message is received.

Returns
True if the read frame was a success.
static bool NET_WebSocket::readAll ( NET_WebStream stream,
UT_WorkBuffer msg,
FrameType type 
)
static

Read an entire message in. Really only useful for testing purposes.

bool NET_WebSocket::reconnect ( const UT_Url url,
const std::chrono::milliseconds &  timeout 
)
bool NET_WebSocket::send ( const UT_StringHolder data)
inline

Send a message to the other end of the websocket. This function will send the data immediately and does NOT queue the data. This is a blocking function.

Parameters
data

Definition at line 270 of file NET_WebSocket.h.

void NET_WebSocket::sendPing ( const UT_StringRef ping = UT_StringHolder::theEmptyString)
inline

Send a ping to the server. Regular use does not need to worry about this. Other abstractions will do this for you.

Definition at line 281 of file NET_WebSocket.h.

void NET_WebSocket::sendPong ( const UT_StringRef pong = UT_StringHolder::theEmptyString)
inline

Definition at line 287 of file NET_WebSocket.h.

static bool NET_WebSocket::setFrameType ( FrameType type,
OpCode  op_code 
)
inlinestaticprotected

Definition at line 325 of file NET_WebSocket.h.

void NET_WebSocket::setMaxFrameSize ( uint32  size)
inline

Set the max number of bytes sent in each frame.

Parameters
sizeThe new max frame size

Definition at line 307 of file NET_WebSocket.h.

const UT_Url& NET_WebSocket::url ( ) const
inline

The url this websocket is using.

Returns
The url object representing the used url.

Definition at line 263 of file NET_WebSocket.h.

bool NET_WebSocket::usableStream ( ) const
inline

Definition at line 298 of file NET_WebSocket.h.

static bool NET_WebSocket::write ( NET_WebStream stream,
FrameType  type,
const UT_StringRef message,
Mask  mask = 0,
exint  max_frame_size = -1 
)
static

Write a websocket message to the stream.

Friends And Related Function Documentation

friend class NET_WebSocketMonitor
friend

Definition at line 323 of file NET_WebSocket.h.

Member Data Documentation

SYS_AtomicInt64 NET_WebSocket::myBufferedAmount
protected

Definition at line 390 of file NET_WebSocket.h.

bool NET_WebSocket::myIsWaitingForPong
protected

Definition at line 384 of file NET_WebSocket.h.

NET_Time NET_WebSocket::myLastHeartbeat
protected

Definition at line 386 of file NET_WebSocket.h.

uint32 NET_WebSocket::myMaxFrameSize
protected

Definition at line 382 of file NET_WebSocket.h.

UT_Lock NET_WebSocket::myMessageLock
mutableprotected

Definition at line 392 of file NET_WebSocket.h.

NET_CircularBuffer<char> NET_WebSocket::myMessageQueue
protected

Definition at line 393 of file NET_WebSocket.h.

UT_FastRandom NET_WebSocket::myRand
protected

Definition at line 397 of file NET_WebSocket.h.

Buffer NET_WebSocket::myReadBuffer
protected

Definition at line 388 of file NET_WebSocket.h.

State NET_WebSocket::myState
protected

Definition at line 395 of file NET_WebSocket.h.

NET_WebStreamPtr NET_WebSocket::myStream
protected

Definition at line 381 of file NET_WebSocket.h.

UT_Url NET_WebSocket::myUrl
protected

Definition at line 379 of file NET_WebSocket.h.

uint64 NET_WebSocket::theMax16FrameSize
static

Definition at line 248 of file NET_WebSocket.h.


The documentation for this class was generated from the following file: