Net module

SocketAdapter

Abstract adapter interface for socket send/receive chains.

SocketAdapter

#include <icy/net/socketadapter.h>

Subclassed by: Connection, ConnectionAdapter, ConnectionStream, Server, Socket, SocketEmitter

Abstract adapter interface for socket send/receive chains.

SocketAdapter is the abstract interface for all socket classes. A SocketAdapter can also be attached to a Socket in order to override default Socket callbacks and behaviour, while still maintaining the default Socket interface (see Socket::setAdapter).

This class can also be extended to implement custom processing for received socket data before it is dispatched to the application (see PacketSocketEmitter and Transaction classes).

Public Attributes

ReturnNameDescription
intpriorityThe priority of this adapter for STL sort operations.

priority

int priority = 0

The priority of this adapter for STL sort operations.

Public Methods

ReturnNameDescription
SocketAdapterCreates the SocketAdapter.
~SocketAdapter virtualDestroys the SocketAdapter.
ssize_tsend 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.
ssize_tsend virtual
ssize_tsendOwned virtualSends an owned payload buffer to the connected peer.
ssize_tsendOwned virtual
ssize_tsendPacket 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.
ssize_tsendPacket virtual
voidsendPacket 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.
voidsetSender virtualSets the pointer to the outgoing data adapter. Send methods proxy data to this adapter by default.
SocketAdapter *senderReturns the output SocketAdapter pointer.
voidaddReceiver virtualSets the pointer to the incoming data adapter. Events proxy data to this adapter by default.
voidremoveReceiver virtualRemove the given receiver.
boolhasReceiver virtualReturns true if the given receiver is connected.
std::vector< SocketAdapter * >receiversReturns all currently registered input SocketAdapter pointers. Dead (removed) entries are excluded from the returned list.
boolonSocketConnect 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.
boolonSocketRecv virtualCalled when data is received from the socket. Forwards the event to all registered receivers in priority order.
boolonSocketError virtualCalled when the socket encounters an error. Forwards the event to all registered receivers in priority order.
boolonSocketClose virtualCalled when the socket is closed. Forwards the event to all registered receivers in priority order.

SocketAdapter

SocketAdapter(SocketAdapter * sender)

Creates the SocketAdapter.


~SocketAdapter

virtual

virtual ~SocketAdapter() noexcept

Destroys the SocketAdapter.


send

virtual

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

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


send

virtual

virtual ssize_t send(const char * data, size_t len, const Address & peerAddress, int flags)

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 Address & peerAddress, int flags)

sendPacket

virtual

virtual ssize_t sendPacket(const IPacket & packet, int flags)

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


sendPacket

virtual

virtual ssize_t sendPacket(const IPacket & packet, const Address & peerAddress, int flags)

sendPacket

virtual

virtual void sendPacket(IPacket & packet)

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


setSender

virtual

virtual void setSender(SocketAdapter * adapter)

Sets the pointer to the outgoing data adapter. Send methods proxy data to this adapter by default.


sender

SocketAdapter * sender()

Returns the output SocketAdapter pointer.


addReceiver

virtual

virtual void addReceiver(SocketAdapter * adapter)

Sets the pointer to the incoming data adapter. Events proxy data to this adapter by default.


removeReceiver

virtual

virtual void removeReceiver(SocketAdapter * adapter)

Remove the given receiver.

By default this function does nothing unless the given receiver matches the current receiver.


hasReceiver

virtual

virtual bool hasReceiver(SocketAdapter * adapter)

Returns true if the given receiver is connected.


receivers

std::vector< SocketAdapter * > receivers()

Returns all currently registered input SocketAdapter pointers. Dead (removed) entries are excluded from the returned list.


onSocketConnect

virtual

virtual bool onSocketConnect(Socket & socket)

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

Parameters

  • socket The connected socket.

Returns

true to stop propagation to subsequent receivers.


onSocketRecv

virtual

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

Called when data is received from the socket. Forwards the event to all registered receivers in priority order.

Parameters

  • socket The socket that received data.

  • buffer The received data buffer.

  • peerAddress Address of the sender.

Returns

true to stop propagation to subsequent receivers.


onSocketError

virtual

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

Called when the socket encounters an error. Forwards the event to all registered receivers in priority order.

Parameters

  • socket The socket that encountered the error.

  • error Error details.

Returns

true to stop propagation to subsequent receivers.


onSocketClose

virtual

virtual bool onSocketClose(Socket & socket)

Called when the socket is closed. Forwards the event to all registered receivers in priority order.

Parameters

  • socket The socket that was closed.

Returns

true to stop propagation to subsequent receivers.

Protected Attributes

ReturnNameDescription
SocketAdapter *_sender
std::vector< Ref >_receivers
bool_dirty

_sender

SocketAdapter * _sender

_receivers

std::vector< Ref > _receivers

_dirty

bool _dirty = false

Protected Methods

ReturnNameDescription
voidcleanupReceivers virtual

cleanupReceivers

virtual

virtual void cleanupReceivers()