HTTP module

ws::WebSocketAdapter

[WebSocket]({#ref classicy_1_1http_1_1ws_1_1WebSocket #}) protocol adapter for both client and server endpoints.

WebSocketAdapter

#include <icy/http/websocket.h>

Inherits: SocketEmitter Subclassed by: ConnectionAdapter, WebSocket

WebSocket protocol adapter for both client and server endpoints.

Public Attributes

ReturnNameDescription
net::Socket::PtrsocketPointer to the underlying socket. Sent data will be proxied to this socket.

socket

net::Socket::Ptr socket

Pointer to the underlying socket. Sent data will be proxied to this socket.

Public Methods

ReturnNameDescription
WebSocketAdapterCreates a WebSocketAdapter using the given socket, mode and HTTP message objects.
ssize_tsend virtualFrames and sends data to the peer's address.
ssize_tsend virtualFrames and sends data to a specific peer address (for UDP-backed sockets).
ssize_tsendOwned virtualSends an owned payload buffer to the connected peer.
ssize_tsendOwned virtual
boolshutdown virtualSends a WebSocket CLOSE frame with the given status code and message, then closes the underlying socket.
voidsendClientRequest virtualClient side.
voidhandleClientResponse virtualParses 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.
voidhandleServerRequest virtualServer side.
voidonHandshakeComplete virtualCalled 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

  • socket The underlying TCP or SSL socket.

  • mode ServerSide or ClientSide.

  • request HTTP request used for the handshake.

  • response HTTP 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

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

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

  • statusCode WebSocket close status code (e.g. 1000 for normal close).

  • statusMessage Human-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

  • buffer Buffer containing the server's HTTP response.

  • peerAddr Address 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

  • buffer Buffer containing the client's HTTP upgrade request.

  • peerAddr Address of the peer.


onHandshakeComplete

virtual

virtual void onHandshakeComplete()

Called when the WebSocket handshake completes. Emits the connect event to downstream handlers.

Protected Attributes

ReturnNameDescription
WebSocketFramerframer
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

ReturnNameDescription
boolsendControlFrame
voidresetFrameState

sendControlFrame

bool sendControlFrame(ws::Opcode opcode, const char * payload, size_t payloadLen, const net::Address & peerAddr)

resetFrameState

void resetFrameState()

Private Methods

ReturnNameDescription
boolonSocketConnect virtualCalled by the socket on connect; initiates the client handshake.
boolonSocketRecv virtualCalled by the socket on each received buffer; handles handshake or frame parsing depending on state.
boolonSocketClose virtualCalled 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.