SocketEmitter
SocketEmitter
#include <icy/net/socketemitter.h>Inherits:
SocketAdapterSubclassed 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
| Return | Name | Description |
|---|---|---|
LocalSignal< bool(Socket &)> | Connect | Signals that the socket is connected. |
LocalSignal< bool(Socket &, const MutableBuffer &, const Address &)> | Recv | Signals when data is received by the socket. |
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. |
LocalSignal< bool(Socket &)> | Close | Signals that the underlying socket is closed. |
Socket::Ptr | impl | Pointer to the underlying socket. Sent data will be proxied to this socket. |
Connect
LocalSignal< bool(Socket &)> ConnectSignals that the socket is connected.
Recv
LocalSignal< bool(Socket &, const MutableBuffer &, const Address &)> RecvSignals when data is received by the socket.
Error
LocalSignal< bool(Socket &, const icy::Error &)> ErrorSignals that the socket is closed in error. This signal will be sent just before the Closed signal.
Close
LocalSignal< bool(Socket &)> CloseSignals that the underlying socket is closed.
impl
Socket::Ptr implPointer to the underlying socket. Sent data will be proxied to this socket.
Public Methods
| Return | Name | Description |
|---|---|---|
SocketEmitter | Creates the SocketEmitter and optionally attaches it to a socket. If socket is provided, this emitter registers itself as a receiver. | |
SocketEmitter | Copy constructor; copies all signal connections and attaches to the same socket. | |
~SocketEmitter virtual | Destroys the SocketAdapter. | |
void | addReceiver virtual | Attaches a SocketAdapter as a receiver; wires it to all four socket signals. |
void | removeReceiver virtual | Detaches a SocketAdapter from all four socket signals. |
void | swap virtual | Replaces the underlying socket with socket. |
T * | as inline | Returns the underlying socket cast to type T, or nullptr if the cast fails. |
Socket * | operator-> const inline | Returns 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
socketOptional 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
thatThe SocketEmitter to copy from.
~SocketEmitter
virtual
virtual ~SocketEmitter() noexceptDestroys the SocketAdapter.
addReceiver
virtual
virtual void addReceiver(SocketAdapter * adapter)Attaches a SocketAdapter as a receiver; wires it to all four socket signals.
Parameters
adapterThe adapter to attach; its priority determines signal ordering.
removeReceiver
virtual
virtual void removeReceiver(SocketAdapter * adapter)Detaches a SocketAdapter from all four socket signals.
Parameters
adapterThe 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
socketThe 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
TDerived socket type to cast to.
Returns
Pointer to the socket as T, or nullptr on type mismatch.
operator->
const inline
inline Socket * operator->() constReturns 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
| Return | Name | Description |
|---|---|---|
bool | onSocketConnect virtual | Forwards the connect event to chained adapters, then fires the Connect signal. |
bool | onSocketRecv virtual | Forwards the recv event to chained adapters, then fires the Recv signal. |
bool | onSocketError virtual | Forwards the error event to chained adapters, then fires the Error signal. |
bool | onSocketClose virtual | Forwards 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.
