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(ConnectDomain *domain, MessagePackage::SerializationFormat serializationFormat)

Constructor.

virtual ~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.

Returns

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.

Parameters

clientId – Identifies the client.

Returns

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

virtual bool getParameter(size_t id, size_t &param)

Allows to query a listener-specific parameter.

Supported parameters: ParamTypeSerializationFormat

Parameters
  • id – Id of the parameter.

  • param – Value of the parameter (out).

Returns

If parameter was successfully queried, true, otherwise false.

string getRemoteIPAddress(size_t handle)

Gets the IP Address of the remote peer.

Parameters

handle – Identfies the remote peer.

Returns

IP address of the remote peer.

int purgeInactiveConnections()

Clean up connections that are no longer active.

Returns

The number of connections that were removed.

MessageArray receive(size_t clientId, bool patchClientId = true)

Retrieves incoming data packet from a given client.

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

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

Returns

Messages that were received from the client.

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.

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

  • message – Message to be sent.

Returns

If message is sent, true, otherwise false.

void useDiagnosticsManager(DiagnosticsManagerSharedPtr diagnosticsManager)

Attach diagnostics manager to this network listener.

Public Static Attributes

static 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

inline virtual void onConnected(int, NetworkListener*)

Callback to notify that connection has been established.

Parameters
  • clientId – The id of the connected client.

  • listener – Listener that triggered the notification.

inline virtual void onDisconnected(int, NetworkListener*)

Callback to notify that connection has been terminated.

Parameters
  • clientId – The id of the disconnected client.

  • listener – Listener that triggered the notification.

inline virtual void onPacketReceived(int, NetworkListener*)

Callback to notify that there is incoming data.

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

  • listener – Listener that triggered the notification.