Http

ws::WebSocketAdapter

WebSocket protocol adapter for both client and server endpoints.

WebSocketAdapter

#include <websocket.h>

class WebSocketAdapter

Defined in src/http/include/icy/http/websocket.h:267

Inherits
SocketEmitter

Friends

NameDescription
WebSocketFramer

WebSocketFramer

friend class WebSocketFramer

Defined in src/http/include/icy/http/websocket.h:350

Public Attributes

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

socket

net::Socket::Ptr socket

Defined in src/http/include/icy/http/websocket.h:304

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 virtual overrideFrames and sends data to the peer's address.
ssize_tsend virtual overrideFrames and sends data to a specific peer address (for UDP-backed sockets).
ssize_tsendOwned virtual override
ssize_tsendOwned virtual override
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)

Defined in src/http/include/icy/http/websocket.h:275

Creates a WebSocketAdapter using the given socket, mode and HTTP message objects.

Parameters

ParameterTypeDescription
socketconst net::Socket::Ptr &The underlying TCP or SSL socket.
modews::ModeServerSide or ClientSide.
requesthttp::Request &HTTP request used for the handshake.
responsehttp::Response &HTTP response used for the handshake.

send

virtual override

virtual ssize_t send(const char * data, size_t len, int flags = 0) override

Defined in src/http/include/icy/http/websocket.h:283

Frames and sends data to the peer's address.

Returns

Number of bytes sent, or -1 on error.

Parameters

ParameterTypeDescription
dataconst char *Pointer to the payload.
lensize_tPayload length in bytes.
flagsintws::SendFlags::Text or ws::SendFlags::Binary.

send

virtual override

virtual ssize_t send(const char * data, size_t len, const net::Address & peerAddr, int flags = 0) override

Defined in src/http/include/icy/http/websocket.h:291

Frames and sends data to a specific peer address (for UDP-backed sockets).

Returns

Number of bytes sent, or -1 on error.

Parameters

ParameterTypeDescription
dataconst char *Pointer to the payload.
lensize_tPayload length in bytes.
peerAddrconst net::Address &Destination address.
flagsintws::SendFlags::Text or ws::SendFlags::Binary.

sendOwned

virtual override

virtual ssize_t sendOwned(Buffer && buffer, int flags = 0) override

Defined in src/http/include/icy/http/websocket.h:292


sendOwned

virtual override

virtual ssize_t sendOwned(Buffer && buffer, const net::Address & peerAddr, int flags = 0) override

Defined in src/http/include/icy/http/websocket.h:293


shutdown

virtual

virtual bool shutdown(uint16_t statusCode, const std::string & statusMessage)

Defined in src/http/include/icy/http/websocket.h:300

Sends a WebSocket CLOSE frame with the given status code and message, then closes the underlying socket.

Returns

true if the close frame was sent successfully.

Parameters

ParameterTypeDescription
statusCodeuint16_tWebSocket close status code (e.g. 1000 for normal close).
statusMessageconst std::string &Human-readable reason for closing.

sendClientRequest

virtual

virtual void sendClientRequest()

Defined in src/http/include/icy/http/websocket.h:311

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)

Defined in src/http/include/icy/http/websocket.h:317

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

ParameterTypeDescription
bufferconst MutableBuffer &Buffer containing the server's HTTP response.
peerAddrconst net::Address &Address of the peer.

handleServerRequest

virtual

virtual void handleServerRequest(const MutableBuffer & buffer, const net::Address & peerAddr)

Defined in src/http/include/icy/http/websocket.h:325

Server side.

Parses the client's HTTP upgrade request and sends the 101 response.

Parameters

ParameterTypeDescription
bufferconst MutableBuffer &Buffer containing the client's HTTP upgrade request.
peerAddrconst net::Address &Address of the peer.

onHandshakeComplete

virtual

virtual void onHandshakeComplete()

Defined in src/http/include/icy/http/websocket.h:339

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

Reimplemented by

Protected Attributes

ReturnNameDescription
WebSocketFramerframer
http::Request &_request
http::Response &_response
ws::CloseState_closeState

framer

WebSocketFramer framer

Type: WebSocketFramer

Defined in src/http/include/icy/http/websocket.h:352


_request

http::Request & _request

Defined in src/http/include/icy/http/websocket.h:353


_response

http::Response & _response

Defined in src/http/include/icy/http/websocket.h:354


_closeState

ws::CloseState _closeState {ws::CloseState::Open}

Type: ws::CloseState

Defined in src/http/include/icy/http/websocket.h:355

Protected Methods

ReturnNameDescription
~WebSocketAdapter virtual
boolsendControlFrame
voidresetFrameState

~WebSocketAdapter

virtual

virtual ~WebSocketAdapter()

Defined in src/http/include/icy/http/websocket.h:342


sendControlFrame

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

Defined in src/http/include/icy/http/websocket.h:344


resetFrameState

void resetFrameState()

Defined in src/http/include/icy/http/websocket.h:348

Private Methods

ReturnNameDescription
boolonSocketConnect virtual overrideCalled by the socket on connect; initiates the client handshake.
boolonSocketRecv virtual overrideCalled by the socket on each received buffer; handles handshake or frame parsing depending on state.
boolonSocketClose virtual overrideCalled by the socket on close; resets framer state.

onSocketConnect

virtual override

virtual bool onSocketConnect(net::Socket & socket) override

Defined in src/http/include/icy/http/websocket.h:328

Called by the socket on connect; initiates the client handshake.


onSocketRecv

virtual override

virtual bool onSocketRecv(net::Socket & socket, const MutableBuffer & buffer, const net::Address & peerAddress) override

Defined in src/http/include/icy/http/websocket.h:332

Called by the socket on each received buffer; handles handshake or frame parsing depending on state.


onSocketClose

virtual override

virtual bool onSocketClose(net::Socket & socket) override

Defined in src/http/include/icy/http/websocket.h:335

Called by the socket on close; resets framer state.