Net module

SocketEmitter

[SocketAdapter]({#ref classicy_1_1net_1_1SocketAdapter #}) that exposes socket events as signals.

SocketEmitter

#include <icy/net/socketemitter.h>

Inherits: SocketAdapter Subclassed by: WebSocketAdapter, PacketSocketEmitter

SocketAdapter that exposes socket events as signals.

Aside from adding a signal interface, the class wraps the underlying socket instance and is designed to be used much like a std::unique_ptr by overriding the -> operator.

Public Attributes

ReturnNameDescription
LocalSignal< bool(Socket &)>ConnectSignals that the socket is connected.
LocalSignal< bool(Socket &, const MutableBuffer &, const Address &)>RecvSignals when data is received by the socket.
LocalSignal< bool(Socket &, const icy::Error &)>ErrorSignals that the socket is closed in error. This signal will be sent just before the Closed signal.
LocalSignal< bool(Socket &)>CloseSignals that the underlying socket is closed.
Socket::PtrimplPointer to the underlying socket. Sent data will be proxied to this socket.

Connect

LocalSignal< bool(Socket &)> Connect

Signals that the socket is connected.


Recv

LocalSignal< bool(Socket &, const MutableBuffer &, const Address &)> Recv

Signals when data is received by the socket.


Error

LocalSignal< bool(Socket &, const icy::Error &)> Error

Signals that the socket is closed in error. This signal will be sent just before the Closed signal.


Close

LocalSignal< bool(Socket &)> Close

Signals that the underlying socket is closed.


impl

Socket::Ptr impl

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

Public Methods

ReturnNameDescription
SocketEmitterCreates the SocketEmitter and optionally attaches it to a socket. If socket is provided, this emitter registers itself as a receiver.
SocketEmitterCopy constructor; copies all signal connections and attaches to the same socket.
~SocketEmitter virtualDestroys the SocketAdapter.
voidaddReceiver virtualAttaches a SocketAdapter as a receiver; wires it to all four socket signals.
voidremoveReceiver virtualDetaches a SocketAdapter from all four socket signals.
voidswap virtualReplaces the underlying socket with socket.
T *as inlineReturns the underlying socket cast to type T, or nullptr if the cast fails.
Socket *operator-> 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.

SocketEmitter

SocketEmitter(const Socket::Ptr & socket)

Creates the SocketEmitter and optionally attaches it to a socket. If socket is provided, this emitter registers itself as a receiver.

Parameters

  • socket Optional socket to attach to; pass nullptr to attach later via swap().

SocketEmitter

SocketEmitter(const SocketEmitter & that)

Copy constructor; copies all signal connections and attaches to the same socket.

Parameters


~SocketEmitter

virtual

virtual ~SocketEmitter() noexcept

Destroys the SocketAdapter.


addReceiver

virtual

virtual void addReceiver(SocketAdapter * adapter)

Attaches a SocketAdapter as a receiver; wires it to all four socket signals.

Parameters

  • adapter The adapter to attach; its priority determines signal ordering.

removeReceiver

virtual

virtual void removeReceiver(SocketAdapter * adapter)

Detaches a SocketAdapter from all four socket signals.

Parameters

  • adapter The adapter to detach.

swap

virtual

virtual void swap(const Socket::Ptr & socket)

Replaces the underlying socket with socket.

Throws std::logic_error if the emitter already has an attached socket.

Parameters

  • socket The new socket to attach.

as

inline

template<class T> inline T * as()

Returns the underlying socket cast to type T, or nullptr if the cast fails.

Parameters

  • T Derived socket type to cast to.

Returns

Pointer to the socket as T, or nullptr on type mismatch.


operator->

const inline

inline Socket * operator->() const

Returns a raw pointer to the underlying socket for direct method access. Follows shared_ptr semantics; the caller must not delete the returned pointer.

Returns

Raw pointer to the socket (never null if a socket was attached).

Protected Methods

ReturnNameDescription
boolonSocketConnect virtualForwards the connect event to chained adapters, then fires the Connect signal.
boolonSocketRecv virtualForwards the recv event to chained adapters, then fires the Recv signal.
boolonSocketError virtualForwards the error event to chained adapters, then fires the Error signal.
boolonSocketClose virtualForwards the close event to chained adapters, then fires the Close signal.

onSocketConnect

virtual

virtual bool onSocketConnect(Socket & socket)

Forwards the connect event to chained adapters, then fires the Connect signal.


onSocketRecv

virtual

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

Forwards the recv event to chained adapters, then fires the Recv signal.


onSocketError

virtual

virtual bool onSocketError(Socket & socket, const icy::Error & error)

Forwards the error event to chained adapters, then fires the Error signal.


onSocketClose

virtual

virtual bool onSocketClose(Socket & socket)

Forwards the close event to chained adapters, then fires the Close signal.