ws::WebSocketAdapter
WebSocketAdapter
#include <icy/http/websocket.h>Inherits:
SocketEmitterSubclassed by:ConnectionAdapter,WebSocket
WebSocket protocol adapter for both client and server endpoints.
Public Attributes
| Return | Name | Description |
|---|---|---|
net::Socket::Ptr | socket | Pointer to the underlying socket. Sent data will be proxied to this socket. |
socket
net::Socket::Ptr socketPointer to the underlying socket. Sent data will be proxied to this socket.
Public Methods
| Return | Name | Description |
|---|---|---|
WebSocketAdapter | Creates a WebSocketAdapter using the given socket, mode and HTTP message objects. | |
ssize_t | send virtual | Frames and sends data to the peer's address. |
ssize_t | send virtual | Frames and sends data to a specific peer address (for UDP-backed sockets). |
ssize_t | sendOwned virtual | Sends an owned payload buffer to the connected peer. |
ssize_t | sendOwned virtual | |
bool | shutdown virtual | Sends a WebSocket CLOSE frame with the given status code and message, then closes the underlying socket. |
void | sendClientRequest virtual | Client side. |
void | handleClientResponse virtual | Parses the server's HTTP upgrade response and completes the handshake. Any data remaining in the buffer after the HTTP response is re-fed as WebSocket frames. |
void | handleServerRequest virtual | Server side. |
void | onHandshakeComplete virtual | Called when the WebSocket handshake completes. Emits the connect event to downstream handlers. |
WebSocketAdapter
WebSocketAdapter(const net::Socket::Ptr & socket, ws::Mode mode, http::Request & request, http::Response & response)Creates a WebSocketAdapter using the given socket, mode and HTTP message objects.
Parameters
socketThe underlying TCP or SSL socket.modeServerSide or ClientSide.requestHTTP request used for the handshake.responseHTTP response used for the handshake.
send
virtual
virtual ssize_t send(const char * data, size_t len, int flags)Frames and sends data to the peer's address.
Parameters
dataPointer to the payload.lenPayload length in bytes.flagsws::SendFlags::Text or ws::SendFlags::Binary.
Returns
Number of bytes sent, or -1 on error.
send
virtual
virtual ssize_t send(const char * data, size_t len, const net::Address & peerAddr, int flags)Frames and sends data to a specific peer address (for UDP-backed sockets).
Parameters
dataPointer to the payload.lenPayload length in bytes.peerAddrDestination address.flagsws::SendFlags::Text or ws::SendFlags::Binary.
Returns
Number of bytes sent, or -1 on error.
sendOwned
virtual
virtual ssize_t sendOwned(Buffer && buffer, int flags)Sends an owned payload buffer to the connected peer.
The buffer is moved through the adapter chain and retained by the transport layer until async write completion.
sendOwned
virtual
virtual ssize_t sendOwned(Buffer && buffer, const net::Address & peerAddr, int flags)shutdown
virtual
virtual bool shutdown(uint16_t statusCode, const std::string & statusMessage)Sends a WebSocket CLOSE frame with the given status code and message, then closes the underlying socket.
Parameters
statusCodeWebSocket close status code (e.g. 1000 for normal close).statusMessageHuman-readable reason for closing.
Returns
true if the close frame was sent successfully.
sendClientRequest
virtual
virtual void sendClientRequest()Client side.
Sends the WebSocket HTTP upgrade request to initiate the handshake. Called automatically on socket connect.
handleClientResponse
virtual
virtual void handleClientResponse(const MutableBuffer & buffer, const net::Address & peerAddr)Parses the server's HTTP upgrade response and completes the handshake. Any data remaining in the buffer after the HTTP response is re-fed as WebSocket frames.
Parameters
bufferBuffer containing the server's HTTP response.peerAddrAddress of the peer.
handleServerRequest
virtual
virtual void handleServerRequest(const MutableBuffer & buffer, const net::Address & peerAddr)Server side.
Parses the client's HTTP upgrade request and sends the 101 response.
Parameters
bufferBuffer containing the client's HTTP upgrade request.peerAddrAddress of the peer.
onHandshakeComplete
virtual
virtual void onHandshakeComplete()Called when the WebSocket handshake completes. Emits the connect event to downstream handlers.
Protected Attributes
| Return | Name | Description |
|---|---|---|
WebSocketFramer | framer | |
http::Request & | _request | |
http::Response & | _response | |
ws::CloseState | _closeState |
framer
WebSocketFramer framer_request
http::Request & _request_response
http::Response & _response_closeState
ws::CloseState _closeState {}Protected Methods
| Return | Name | Description |
|---|---|---|
bool | sendControlFrame | |
void | resetFrameState |
sendControlFrame
bool sendControlFrame(ws::Opcode opcode, const char * payload, size_t payloadLen, const net::Address & peerAddr)resetFrameState
void resetFrameState()Private Methods
| Return | Name | Description |
|---|---|---|
bool | onSocketConnect virtual | Called by the socket on connect; initiates the client handshake. |
bool | onSocketRecv virtual | Called by the socket on each received buffer; handles handshake or frame parsing depending on state. |
bool | onSocketClose virtual | Called by the socket on close; resets framer state. |
onSocketConnect
virtual
virtual bool onSocketConnect(net::Socket & socket)Called by the socket on connect; initiates the client handshake.
onSocketRecv
virtual
virtual bool onSocketRecv(net::Socket & socket, const MutableBuffer & buffer, const net::Address & peerAddress)Called by the socket on each received buffer; handles handshake or frame parsing depending on state.
onSocketClose
virtual
virtual bool onSocketClose(net::Socket & socket)Called by the socket on close; resets framer state.
