12 #ifndef __NET_WEBSOCKET_H__
13 #define __NET_WEBSOCKET_H__
34 #include <hboost/uuid/uuid.hpp>
35 #include <hboost/uuid/uuid_generators.hpp>
36 #include <hboost/uuid/uuid_io.hpp>
37 #include <hboost/any.hpp>
51 static constexpr OpCode OpCode_Continue = 0x0;
52 static constexpr OpCode OpCode_Utf8 = 0x01;
53 static constexpr OpCode OpCode_Binary = 0x02;
54 static constexpr OpCode OpCode_Terminate = 0x08;
55 static constexpr OpCode OpCode_Ping = 0x09;
56 static constexpr OpCode OpCode_Pong = 0x0a;
66 Continue = OpCode_Continue,
68 Binary = OpCode_Binary,
69 Terminate = OpCode_Terminate,
91 const std::chrono::milliseconds&
timeout,
93 bool& is_client_closed);
94 ParseResult readFrame(
96 const std::chrono::milliseconds&
timeout,
98 bool &is_client_closed);
110 enum class ParseState
122 ParseResult readAndParse_(
124 const std::chrono::milliseconds&
timeout,
125 bool& is_client_closed);
126 void resetForNextFrame_();
135 unsigned myIsMasked : 1, myIsFinished : 1;
136 ParseState myCurrentState;
188 exint max_frame_size = -1);
194 bool is_finished =
true);
197 static bool makeMessage(
202 exint max_fram_size);
204 template <
typename SocketT =
NET_WebSocket,
typename... Args>
207 const std::chrono::milliseconds&
timeout,
211 = UTmakeShared<SocketT>(std::forward<Args>(
args)...);
212 ws->reconnect(url, timeout);
257 return myStream->getFD();
276 bool reconnect(
const UT_Url& url,
const std::chrono::milliseconds&
timeout);
277 virtual bool disconnect();
285 myIsWaitingForPong =
true;
315 return myMessageQueue.size();
329 case OpCode_Continue:
330 type = FrameType::Continue;
333 type = FrameType::Utf8;
336 type = FrameType::Binary;
338 case OpCode_Terminate:
339 type = FrameType::Terminate;
342 type = FrameType::Ping;
345 type = FrameType::Pong;
364 exint max_frame_size);
376 const std::chrono::milliseconds&
timeout
377 = std::chrono::milliseconds(500));
404 using id_t = hboost::uuids::uuid;
413 myPromises.emplace(
id, std::move(p));
418 return hboost::uuids::random_generator()();
428 #endif // __NET_WEBSOCKET_H__
FrameType frameType() const
NET_Future< NET_WebAPIResponse > addPromise(id_t id)
SYS_AtomicInt64 myBufferedAmount
std::promise< T > NET_Promise
FrameType
The currently supported different frame types.
void sendPong(const UT_StringRef &pong=UT_StringHolder::theEmptyString)
bool send(const UT_StringHolder &data)
Send a message to the other end of the websocket. This function will send the data immediately and do...
bool isConnected() const
Tell if this websocket is currently in use.
const UT_Url & url() const
The url this websocket is using.
UT_StringHolder myMessage
static uint64 theMax16FrameSize
unsigned long long uint64
UT_StringHolder data() const
exint bufferedAmount() const
The current amount of bytes waiting to be sent over the network.
ErrorCode
All possible error codes that could occur to be sent to the onError callback.
void read(T &in, bool &v)
static bool setFrameType(FrameType &type, OpCode op_code)
NET_CircularBuffer< char > myMessageQueue
std::enable_shared_from_this< T > UTenable_shared_from_this
A utility class to do read-only operations on a subset of an existing string.
virtual void onEvent(const Event &ev)
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
GLint GLenum GLsizei GLint GLsizei const void * data
static const UT_StringHolder theEmptyString
Class used for web socket operations.
GLsizei GLenum GLuint GLuint GLsizei GLchar * message
static constexpr UT_StringLit frameTypeToString(FrameType type)
Convert the frame type to string representation of this frame type.
void setMaxFrameSize(uint32 size)
Set the max number of bytes sent in each frame.
static hboost::uuids::uuid generateId()
UT_UniquePtr< NET_WebStream > NET_WebStreamPtr
GLuint GLuint GLsizei GLenum type
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 ...
std::future< T > NET_Future
uint32 maxFrameSize() const
The current max frame size used.
NET_WebStreamPtr myStream
static UT_SharedPtr< SocketT > connect(const UT_Url &url, const std::chrono::milliseconds &timeout, Args &&...args)
FrameType
Different types of local frames that can be computed for components.
GLbitfield GLuint64 timeout
int getFD() const
The file descriptor of this websocket.
void write(T &out, bool v)
UT_SharedPtr< NET_WebSocket > NET_WebSocketSPtr
bool usableStream() const