NetworkListener

class kanzi::connect::NetworkListener : private ConnectionAdapter::Observer

A class that runs network thread and acts as a bridge between the server and clients.

This class initializes connection adapter according to given parameters and starts a network thread. It is responsible of transmitting and receiving data through message translators and maintains connections of clients.

Public Types

typedef vector<MessagePackage> MessageContainer

Container type for network messages.

Public Functions

NetworkListener(ConnectDomainSharedPtr domain, MessagePackage::SerializationFormat serializationFormat)

Constructor.

~NetworkListener()

Destructor.

void addObserver(Observer *observer)

Add observer to listen for network events.

Parameters
  • observer: Observer to add.

ConnectionAdapterSharedPtr getAdapter()

Return the adapter that this network listener uses.

Return

The adapter shared pointer. Can be null.

ConnectionAdapter *getConnectionAdapter(const int clientId) const

Find the connection adapter that handles the connection for the given client.

Return

Pointer to the connection adapter, null pointer if not found.

Parameters
  • clientId: Identifies the client.

bool getParameter(size_t id, size_t &param)

Allows to query a listener-specific parameter.

Supported parameters: ParamTypeSerializationFormat

Return

If parameter was successfully queried, true, otherwise false.

Parameters
  • id: Id of the parameter.

  • param: Value of the parameter (out).

string getRemoteIPAddress(size_t handle)

Gets the IP Address of the remote peer.

Return

IP address of the remote peer.

Parameters
  • handle: Identfies the remote peer.

int purgeInactiveConnections()

Clean up connections that are no longer active.

Return

The number of connections that were removed.

MessageArray receive(size_t clientId, bool patchClientId = true)

Retrieves incoming data packet from a given client.

Return

Messages that were received from the client.

Parameters
  • clientId: The id of the client that sends the data.

  • patchClientId: Patches the returned messages with the requested client identifier.

void removeObserver(Observer *observer)

Remove observer.

Parameters
  • observer: Observer to remove.

void reset()

Resets the internal network listener state.

void start(ConnectionAdapterSharedPtr connectionAdapter, const ConnectionAdapter::Parameters &parameters)

Starts listening connections, using given network adapter and parameters.

Parameters
  • connectionAdapter: Adapter to use for communication.

  • parameters: Parameters for the network adapter.

void stop()

Stop listening connections and tears down the networking thread.

bool transmit(size_t clientId, const MessagePackage &message)

Sends a data packet to given client.

Return

If message is sent, true, otherwise false.

Parameters
  • clientId: The id of the client that should receive the message.

  • message: Message to be sent.

void useDiagnosticsManager(DiagnosticsManagerSharedPtr diagnosticsManager)

Attach diagnostics manager to this network listener.

Public Static Attributes

const size_t ParamTypeSerializationFormat = SERIALIZATION_FORMAT_PARAM_ID

Use a parameter ID to query serialization format.

class Observer

Allows to receive notifications from network listener.

Note

These callback methods are executed in the listening network thread.

Subclassed by ConnectionManager, P2PServer

Public Functions

~Observer() = default
void onConnected(int clientId, NetworkListener *listener)

Callback to notify that connection has been established.

Parameters
  • clientId: The id of the connected client.

  • listener: Listener that triggered the notification.

void onDisconnected(int clientId, NetworkListener *listener)

Callback to notify that connection has been terminated.

Parameters
  • clientId: The id of the disconnected client.

  • listener: Listener that triggered the notification.

void onPacketReceived(int clientId, NetworkListener *listener)

Callback to notify that there is incoming data.

Parameters
  • clientId: The id of the client that sent data.

  • listener: Listener that triggered the notification.