Home
HTTP module

ws::WebSocketAdapter

WebSocket protocol adapter for both client and server endpoints.

WebSocketAdapter

#include <icy/http/websocket.h>
class WebSocketAdapter

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

Inherits: SocketEmitter Subclassed by: ConnectionAdapter, WebSocket

WebSocket protocol adapter for both client and server endpoints.

List of all members

NameKindOwner
WebSocketFramerfriendDeclared here
socketvariableDeclared here
WebSocketAdapterfunctionDeclared here
sendfunctionDeclared here
sendfunctionDeclared here
sendOwnedfunctionDeclared here
sendOwnedfunctionDeclared here
shutdownfunctionDeclared here
sendClientRequestfunctionDeclared here
handleClientResponsefunctionDeclared here
handleServerRequestfunctionDeclared here
onHandshakeCompletefunctionDeclared here
framervariableDeclared here
_requestvariableDeclared here
_responsevariableDeclared here
_closeStatevariableDeclared here
~WebSocketAdapterfunctionDeclared here
sendControlFramefunctionDeclared here
resetFrameStatefunctionDeclared here
onSocketConnectfunctionDeclared here
onSocketRecvfunctionDeclared here
onSocketClosefunctionDeclared here
ConnectvariableInherited from SocketEmitter
RecvvariableInherited from SocketEmitter
ErrorvariableInherited from SocketEmitter
ClosevariableInherited from SocketEmitter
implvariableInherited from SocketEmitter
SocketEmitterfunctionInherited from SocketEmitter
SocketEmitterfunctionInherited from SocketEmitter
~SocketEmitterfunctionInherited from SocketEmitter
addReceiverfunctionInherited from SocketEmitter
removeReceiverfunctionInherited from SocketEmitter
swapfunctionInherited from SocketEmitter
asfunctionInherited from SocketEmitter
operator->functionInherited from SocketEmitter
onSocketConnectfunctionInherited from SocketEmitter
onSocketRecvfunctionInherited from SocketEmitter
onSocketErrorfunctionInherited from SocketEmitter
onSocketClosefunctionInherited from SocketEmitter
priorityvariableInherited from SocketAdapter
SocketAdapterfunctionInherited from SocketAdapter
~SocketAdapterfunctionInherited from SocketAdapter
sendfunctionInherited from SocketAdapter
sendfunctionInherited from SocketAdapter
sendOwnedfunctionInherited from SocketAdapter
sendOwnedfunctionInherited from SocketAdapter
sendPacketfunctionInherited from SocketAdapter
sendPacketfunctionInherited from SocketAdapter
sendPacketfunctionInherited from SocketAdapter
setSenderfunctionInherited from SocketAdapter
senderfunctionInherited from SocketAdapter
addReceiverfunctionInherited from SocketAdapter
removeReceiverfunctionInherited from SocketAdapter
hasReceiverfunctionInherited from SocketAdapter
receiversfunctionInherited from SocketAdapter
onSocketConnectfunctionInherited from SocketAdapter
onSocketRecvfunctionInherited from SocketAdapter
onSocketErrorfunctionInherited from SocketAdapter
onSocketClosefunctionInherited from SocketAdapter
_sendervariableInherited from SocketAdapter
_receiversvariableInherited from SocketAdapter
_dirtyvariableInherited from SocketAdapter
cleanupReceiversfunctionInherited from SocketAdapter

Inherited from SocketEmitter

KindNameDescription
variableConnectSignals that the socket is connected.
variableRecvSignals when data is received by the socket.
variableErrorSignals that the socket is closed in error. This signal will be sent just before the Closed signal.
variableCloseSignals that the underlying socket is closed.
variableimplPointer to the underlying socket. Sent data will be proxied to this socket.
functionSocketEmitterCreates the SocketEmitter and optionally attaches it to a socket. If socket is provided, this emitter registers itself as a receiver.
functionSocketEmitterCopy constructor; copies all signal connections and attaches to the same socket.
function~SocketEmitter virtual noexceptDestroys the SocketAdapter.
functionaddReceiver virtual overrideAttaches a SocketAdapter as a receiver; wires it to all four socket signals.
functionremoveReceiver virtual overrideDetaches a SocketAdapter from all four socket signals.
functionswap virtualReplaces the underlying socket with socket.
functionas inlineReturns the underlying socket cast to type T, or nullptr if the cast fails.
functionoperator-> const inlineReturns a raw pointer to the underlying socket for direct method access. Follows shared_ptr semantics; the caller must not delete the returned pointer.
functiononSocketConnect virtual overrideForwards the connect event to chained adapters, then fires the Connect signal.
functiononSocketRecv virtual overrideForwards the recv event to chained adapters, then fires the Recv signal.
functiononSocketError virtual overrideForwards the error event to chained adapters, then fires the Error signal.
functiononSocketClose virtual overrideForwards the close event to chained adapters, then fires the Close signal.

Inherited from SocketAdapter

KindNameDescription
variablepriorityThe priority of this adapter for STL sort operations.
functionSocketAdapterCreates the SocketAdapter.
function~SocketAdapter virtual noexceptDestroys the SocketAdapter.
functionsend virtualSends the given data buffer to the connected peer. Returns the number of bytes sent or -1 on error. No exception will be thrown. For TCP sockets the given peer address must match the connected peer address.
functionsend virtual
functionsendOwned virtualSends an owned payload buffer to the connected peer.
functionsendOwned virtual
functionsendPacket virtualSends the given packet to the connected peer. Returns the number of bytes sent or -1 on error. No exception will be thrown. For TCP sockets the given peer address must match the connected peer address.
functionsendPacket virtual
functionsendPacket virtualSends the given packet to the connected peer. This method provides delegate compatibility, and unlike other send methods throws an exception if the underlying socket is closed.
functionsetSender virtualSets the pointer to the outgoing data adapter. Send methods proxy data to this adapter by default.
functionsenderReturns the output SocketAdapter pointer.
functionaddReceiver virtualSets the pointer to the incoming data adapter. Events proxy data to this adapter by default.
functionremoveReceiver virtualRemove the given receiver.
functionhasReceiver virtualReturns true if the given receiver is connected.
functionreceiversReturns all currently registered input SocketAdapter pointers. Dead (removed) entries are excluded from the returned list.
functiononSocketConnect virtualCalled when the socket establishes a connection. Forwards the event to all registered receivers in priority order. Override to intercept before the application sees the event.
functiononSocketRecv virtualCalled when data is received from the socket. Forwards the event to all registered receivers in priority order.
functiononSocketError virtualCalled when the socket encounters an error. Forwards the event to all registered receivers in priority order.
functiononSocketClose virtualCalled when the socket is closed. Forwards the event to all registered receivers in priority order.
variable_sender
variable_receivers
variable_dirty
functioncleanupReceivers virtual

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 overrideSends an owned payload buffer to the connected peer.
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

  • 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 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.

Parameters

Returns

Number of bytes sent, or -1 on error.

Reimplements

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).

Parameters

Returns

Number of bytes sent, or -1 on error.

Reimplements

sendOwned

virtual override

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

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

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.

Reimplements

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

Reimplements

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.

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()

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

  • 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)

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

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

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

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 {}

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

Protected Methods

ReturnNameDescription
boolsendControlFrame
voidresetFrameState

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.

Reimplements

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.

Reimplements

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.

Reimplements