58#define WS_BUFFER_SEGMENTS 4
60#define WS_BUFFER_MIN 10
61#define WS_ACCUM_DLY ((TICKS_PER_SECOND / 4) + 1)
62#define WS_FLUSH_TIMEOUT 0
64#define WS_FIN_BIT 0x80
68#define WS_OP_CLOSE 0x8
95 struct WS_Client_Short
101 } __attribute__((packed));
111 } __attribute__((packed));
114 struct WS_Client_Long
122 } __attribute__((packed));
125 struct WS_Server_Short
130 } __attribute__((packed));
139 } __attribute__((packed));
142 struct WS_Server_Long
149 } __attribute__((packed));
171 static const char *WS_StateStr(WS_State state);
181 WS_STAT_NORM_CLOSE = 1000,
182 WS_STAT_GOING_AWAY = 1001,
183 WS_STAT_PROT_ERROR = 1002,
184 WS_STAT_UNACCEPT_TYPE = 1003,
186 WS_STAT_NONE_ABNORMAL = 1006,
187 WS_STAT_NOT_CONSISTENT = 1007,
188 WS_STAT_POLICY_VIOLATION = 1008,
189 WS_STAT_MSG_TOO_BIG = 1009,
190 WS_STAT_EXPECTED_EXTENSION = 1010,
191 WS_STAT_UNEXPECTED_COND = 1011,
192 WS_STAT_TLS_FAILURE = 1015,
782 bool m_serverNotClient;
784 volatile bool m_CtlBlock;
785 uint32_t m_currentIndex;
788 uint8_t m_frameHeadBuf[14];
789 uint8_t m_frameHeadNext;
791 uint32_t m_currentMask;
792 uint32_t m_remainingLen;
794 uint8_t m_txFrameHeadBuf[14];
795 uint8_t m_txFrameHeadNext;
796 uint8_t m_txFrameHeadLen;
797 uint32_t m_txCurrMask;
799 int m_txFlushInProgress;
800 bool m_txCtlSendInProgress;
803 struct PendingCtlWait {
805 PendingCtlWait *pNext;
807 PendingCtlWait *m_ctlWait;
811 OS_SEM *m_pingWaitSem;
813 uint32_t m_txBufferMin;
815 void ServerRxFromTCP();
816 bool ReadRemainingHeader();
817 void ProcessCtlFrame();
818 bool ValidatePong(uint32_t seed, uint32_t len);
819 void Close(
bool closedAfterSend, uint16_t code = 0,
const char *reason = NULL,
int len = 0);
823 void Init(
int fd_tcp,
int fd_ws,
bool serverNotClient);
824 void Flush_Header(uint8_t *buf, uint32_t mask, uint32_t len);
826 void AddCtlWait(PendingCtlWait *pWait);
827 void PostToCtlWait();
829 static uint32_t s_flushTick;
830 static fd_set s_pendingCtlSocks;
831 static OS_CRIT s_ws_crit_obj;
832 static bool s_bFinishedInit;
834 static void StaticInit();
835 static int GetNewSocket(
int fd_tcp,
bool serverNotClient);
837 static WebSocket *GetRecordFromTCP(
int fd);
840 static int CoreConnect(
IPADDR ip,
const char *host,
const char *resource,
const char *protocol,
int portnum,
bool useSSL =
false);
2498 int Ping(uint32_t len, uint32_t *sentTick);
4458 static int ws_readwto(
int fd,
char *buf,
int nbytes,
int timeout);
4679 static int ws_read(
int fd,
char *buf,
int nbytes);
4949 static int ws_write(
int fd,
const char *buf,
int nbytes);
6667 static int Connect(
const char * host,
const char * resource,
const char * protocol,
int portnum = 80,
bool useSSL =
false);
6692 static inline int Connect(
const char * host,
const char * resource,
int portnum = 80,
bool useSSL =
false)
6693 {
return Connect(host, resource, NULL, portnum, useSSL); }
6910 static int Connect(
IPADDR host,
const char * resource,
const char * protocol,
int portnum = 80,
bool useSSL =
false);
6936 static inline int Connect(
IPADDR host,
const char * resource,
int portnum = 80,
bool useSSL =
false)
6937 {
return Connect(host, resource, NULL, portnum, useSSL); }
7822int WSPing(
int fd, uint32_t len, uint32_t *sentTick);
Used to hold and manipulate IPv4 and IPv6 addresses in dual stack mode.
Definition ipv6_addr.h:41
WebSocket connection management class.
Definition websockets.h:93
static int ws_readwto(int fd, char *buf, int nbytes, int timeout)
Read data from a WebSocket connection with timeout.
static int Connect(IPADDR host, const char *resource, int portnum=80, bool useSSL=false)
Connect to a WebSocket server using IP address without protocol negotiation.
Definition websockets.h:6936
static int Connect(IPADDR host, const char *resource, const char *protocol, int portnum=80, bool useSSL=false)
Connect to a WebSocket server using IP address with optional protocol negotiation.
int GetWriteSpace()
Get available transmit buffer space in bytes.
Definition websockets.h:3897
static void ws_write_notify(int fd)
TCP write notification callback invoked when buffer space becomes available.
static int GetFreeWebSocketCount()
Get the number of available WebSocket connection slots.
static WebSocket WSSockStructs[WS_MAX_SOCKS]
Static array of WebSocket connection structures.
Definition websockets.h:4191
static void ws_flush(int fd)
Flush the transmit buffer of a WebSocket connection.
int Ping(uint32_t len, uint32_t *sentTick)
Send a WebSocket ping frame and record the timestamp.
static void GetFlushTime()
Updates the global flush timing for WebSocket message delivery.
OS_CRIT socket_crit
Critical section for thread-safe socket operations.
Definition websockets.h:775
static int ws_read(int fd, char *buf, int nbytes)
Read data from a WebSocket connection.
static int promote_tcp_ws(int tcp_fd)
Promote a TCP socket to a WebSocket connection after successful handshake.
static int s_openSocketCount
Number of currently open WebSocket connections.
Definition websockets.h:4020
static int Connect(const char *host, const char *resource, int portnum=80, bool useSSL=false)
Connect to a WebSocket server using hostname without protocol negotiation.
Definition websockets.h:6692
static void RunSkippedCallBack()
Executes the callback function when a scheduled run is skipped.
WS_State GetState()
Get the current connection state of the WebSocket.
Definition websockets.h:2266
void Flush()
Flush buffered transmit data to the network.
static WebSocket * GetWebSocketRecord(int fd)
Find a WebSocket record by its file descriptor.
static int Connect(const char *host, const char *resource, const char *protocol, int portnum=80, bool useSSL=false)
Connect to a WebSocket server using hostname with optional protocol negotiation.
static void DumpSockets()
Dump detailed state information for all active WebSocket connections.
void DumpSock()
Dump detailed state information for this WebSocket connection.
int GetPingReplyTick(uint32_t *replyTick)
Get the tick count when the last pong was received.
static int ws_setoption(int fd, int option)
Set a socket option on a WebSocket connection.
static int ws_externalclose(int fd)
Close a WebSocket connection from an external caller.
static int ws_clroption(int fd, int option)
Clear a socket option on a WebSocket connection.
int WriteData(const char *buf, int nbytes)
Write data to the WebSocket connection.
void Pong(uint32_t len)
Send a pong frame in response to a ping.
int m_fd_ws
WebSocket file descriptor (application-level identifier)
Definition websockets.h:523
int WaitForPingReply(uint32_t timeout)
Block waiting for a pong reply to a previously sent ping.
int getoption()
Get the current socket options bitmap.
static int ws_getoption(int fd)
Get the current socket options for a WebSocket connection.
int ReadData(char *buf, int nbytes)
Read data from the WebSocket connection.
int setoption(int option)
Set a socket option on this WebSocket connection.
int m_fd_tcp
TCP socket file descriptor (underlying transport)
Definition websockets.h:371
static void ws_read_notify(int fd)
TCP read notification callback invoked when data is available on the WebSocket.
static int ws_write(int fd, const char *buf, int nbytes)
Write data to a WebSocket connection.
int clroption(int option)
Clear a socket option on this WebSocket connection.
FIFO buffer storage using linked pool buffers.
Definition buffers.h:443
uint16_t SpaceAvail()
Get number of bytes available for writing.
int WSUpgrade(HTTP_Request *req, int sock)
Upgrade an HTTP connection to a WebSocket connection.
int WSPing(int fd, uint32_t len, uint32_t *sentTick)
Send a WebSocket ping frame to test connection liveness and measure round-trip time.
int WSWaitForPingReply(int fd, uint32_t timeout)
Wait for a pong reply to a previously sent ping with a specified timeout.
int WSGetPingReplyTick(int fd, uint32_t *replyTick)
Retrieve the timestamp of the most recent pong reply received on a WebSocket connection.
#define WS_MAX_SOCKS
Maximum number of simultaneous WebSocket connections supported.
Definition websockets.h:59
HTTP Request Structure.
Definition http.h:87