Transport

ConnectionManager

class ConnectionManager : public Object

Abstract base class for transport-layer connection management.

ConnectionManager provides a protocol-agnostic interface for managing network connections. It abstracts the differences between connection-oriented (TCP) and connectionless (UDP) transports, allowing applications to switch between protocols without changing their code structure.

The interface supports both client and server modes:

  • Client mode: Call Connect() to establish connection(s) to a server

  • Server mode: Call Bind() to accept incoming connections

Both modes support bidirectional communication via Send() and ReceiveCallback.

Implementations may provide protocol-specific extensions. For example, TcpConnectionManager adds connection pooling control methods that are not part of this base interface.

Example client usage:

Ptr<ConnectionManager> conn = CreateObject<TcpConnectionManager>();
conn->SetNode(node);
conn->SetReceiveCallback(MakeCallback(&MyApp::OnReceive, this));
conn->Connect(serverAddress);
conn->Send(packet);

Example server usage:

Ptr<ConnectionManager> conn = CreateObject<TcpConnectionManager>();
conn->SetNode(node);
conn->SetReceiveCallback(MakeCallback(&MyApp::OnReceive, this));
conn->SetConnectionCallback(MakeCallback(&MyApp::OnNewClient, this));
conn->Bind(9000);
// Later, in OnReceive:
conn->Send(responsePacket, clientAddress);

Subclassed by ns3::TcpConnectionManager, ns3::UdpConnectionManager

Public Types

typedef Callback<void, Ptr<Packet>, const Address&> ReceiveCallback

Callback signature for receiving data.

Param packet:

The received packet.

Param from:

The address of the sender.

Public Functions

ConnectionManager()

Default constructor.

~ConnectionManager() override

Destructor.

virtual void SetNode(Ptr<Node> node) = 0

Set the node this connection manager operates on.

Must be called before Bind() or Connect().

Parameters:

node – The node to use for socket creation.

virtual Ptr<Node> GetNode() const = 0

Get the node this connection manager operates on.

Returns:

The node, or nullptr if not set.

virtual void Bind(uint16_t port) = 0

Bind to a port and start accepting connections (server mode).

For TCP, this creates a listening socket and accepts incoming connections. For UDP, this binds to the port and starts receiving datagrams.

Parameters:

port – The port number to bind to.

virtual void Bind(const Address &local) = 0

Bind to a specific local address and start accepting connections.

Parameters:

local – The local address (IP and port) to bind to.

virtual void Connect(const Address &remote) = 0

Connect to a remote server (client mode).

For TCP, this establishes a connection to the server. If connection pooling is enabled (TcpConnectionManager), multiple connections may be created.

For UDP, this sets the default destination address for Send().

Parameters:

remote – The remote server address to connect to.

virtual bool Send(Ptr<Packet> packet) = 0

Send a packet to the default/connected peer.

In client mode, sends to the server specified in Connect(). In server mode with a single client, sends to that client.

Parameters:

packet – The packet to send.

Returns:

true if the packet was sent successfully, false on failure.

virtual bool Send(Ptr<Packet> packet, const Address &to) = 0

Send a packet to a specific peer.

In server mode, this routes the packet to the connection for that peer. In client mode with connection pooling, this sends to the server (the ‘to’ address should match the connected server).

Parameters:
  • packet – The packet to send.

  • to – The destination address.

Returns:

true if the packet was sent successfully, false on failure.

virtual void SetReceiveCallback(ReceiveCallback callback) = 0

Set the callback for receiving data.

The callback is invoked whenever data is received, with the packet and the sender’s address.

Parameters:

callback – The receive callback.

virtual void Close() = 0

Close all connections and release resources.

virtual void Close(const Address &peer) = 0

Close the connection(s) to a specific peer.

For TCP in server mode, closes the socket for that specific client. For TCP in client mode with connection pooling, closes ALL pooled connections to the server (since all pool connections share the same peer). For UDP, this is a no-op (connectionless).

Parameters:

peer – The peer address to disconnect.

virtual std::string GetName() const = 0

Get the name of this connection manager implementation.

Returns:

A string identifying the implementation (e.g., “TCP”, “UDP”).

virtual bool IsReliable() const = 0

Check if this transport provides reliable delivery.

Returns:

True for TCP, false for UDP.

virtual bool IsConnected() const = 0

Check if the connection manager has an active connection.

For TCP clients: returns true if at least one connection is established. For TCP servers: returns true if listening socket is active. For UDP: returns true if socket exists with a default destination set.

Returns:

True if connected/ready to send, false otherwise.

Public Static Functions

static TypeId GetTypeId()

Get the type ID.

Returns:

The object TypeId.

TcpConnectionManager

class TcpConnectionManager : public ns3::ConnectionManager

TCP implementation of ConnectionManager with optional connection pooling.

TcpConnectionManager provides reliable, ordered delivery using TCP sockets. It supports both client and server modes with full bidirectional communication.

Public Types

typedef uint64_t ConnectionId

Connection identifier for explicit connection control.

typedef Callback<void, const Address&> ConnectionCallback

Callback signature for TCP connection events.

Used for new connection notifications (server/client) and connection close notifications.

Param peer:

The address of the remote peer.

Public Functions

TcpConnectionManager()

Default constructor.

~TcpConnectionManager() override

Destructor.

virtual void SetNode(Ptr<Node> node) override

Set the node this connection manager operates on.

Must be called before Bind() or Connect().

Parameters:

node – The node to use for socket creation.

virtual Ptr<Node> GetNode() const override

Get the node this connection manager operates on.

Returns:

The node, or nullptr if not set.

virtual void Bind(uint16_t port) override

Bind to a port and start accepting connections (server mode).

For TCP, this creates a listening socket and accepts incoming connections. For UDP, this binds to the port and starts receiving datagrams.

Parameters:

port – The port number to bind to.

virtual void Bind(const Address &local) override

Bind to a specific local address and start accepting connections.

Parameters:

local – The local address (IP and port) to bind to.

virtual void Connect(const Address &remote) override

Connect to a remote server (client mode).

For TCP, this establishes a connection to the server. If connection pooling is enabled (TcpConnectionManager), multiple connections may be created.

For UDP, this sets the default destination address for Send().

Parameters:

remote – The remote server address to connect to.

virtual bool Send(Ptr<Packet> packet) override

Send a packet to the default/connected peer.

In client mode, sends to the server specified in Connect(). In server mode with a single client, sends to that client.

Parameters:

packet – The packet to send.

Returns:

true if the packet was sent successfully, false on failure.

virtual bool Send(Ptr<Packet> packet, const Address &to) override

Send a packet to a specific peer.

In server mode, this routes the packet to the connection for that peer. In client mode with connection pooling, this sends to the server (the ‘to’ address should match the connected server).

Parameters:
  • packet – The packet to send.

  • to – The destination address.

Returns:

true if the packet was sent successfully, false on failure.

virtual void SetReceiveCallback(ReceiveCallback callback) override

Set the callback for receiving data.

The callback is invoked whenever data is received, with the packet and the sender’s address.

Parameters:

callback – The receive callback.

virtual void Close() override

Close all connections and release resources.

virtual void Close(const Address &peer) override

Close the connection(s) to a specific peer.

For TCP in server mode, closes the socket for that specific client. For TCP in client mode with connection pooling, closes ALL pooled connections to the server (since all pool connections share the same peer). For UDP, this is a no-op (connectionless).

Parameters:

peer – The peer address to disconnect.

virtual std::string GetName() const override

Get the name of this connection manager implementation.

Returns:

A string identifying the implementation (e.g., “TCP”, “UDP”).

virtual bool IsReliable() const override

Check if this transport provides reliable delivery.

Returns:

True for TCP, false for UDP.

virtual bool IsConnected() const override

Check if the connection manager has an active connection.

For TCP clients: returns true if at least one connection is established. For TCP servers: returns true if listening socket is active. For UDP: returns true if socket exists with a default destination set.

Returns:

True if connected/ready to send, false otherwise.

void SetConnectionCallback(ConnectionCallback callback)

Set the callback for new connection events (TCP-specific).

For servers, invoked when a new client connects. For clients, invoked when connection is established.

Parameters:

callback – The connection callback.

void SetCloseCallback(ConnectionCallback callback)

Set the callback for connection close events (TCP-specific).

Invoked when a connection is closed, either by the remote peer or due to an error.

Parameters:

callback – The close callback.

void SetConnectionFailedCallback(ConnectionCallback callback)

Set the callback for connection failure events (TCP-specific).

Invoked when a connection attempt fails (client mode only).

Parameters:

callback – The failure callback.

ConnectionId AcquireConnection()

Acquire an idle connection from the pool (client mode).

Returns a connection identifier that can be used with Send(connId, packet). The connection is marked as busy until ReleaseConnection() is called.

Returns:

A connection ID, or INVALID_CONNECTION if no idle connections.

ConnectionId AcquireConnection(const Address &peer)

Acquire the connection to a specific peer (server mode).

Parameters:

peer – The peer address.

Returns:

A connection ID, or INVALID_CONNECTION if not connected to that peer.

bool Send(ConnectionId connId, Ptr<Packet> packet)

Send a packet on a specific connection.

Parameters:
  • connId – The connection identifier from AcquireConnection().

  • packet – The packet to send.

void ReleaseConnection(ConnectionId connId)

Release a connection back to the pool.

Parameters:

connId – The connection identifier to release.

uint32_t GetConnectionCount() const

Get the number of connections in the pool.

Returns:

The pool size (client mode) or number of accepted connections (server mode).

uint32_t GetIdleConnectionCount() const

Get the number of idle (available) connections.

Returns:

The number of connections not currently acquired.

uint32_t GetActiveConnectionCount() const

Get the number of active (acquired) connections.

Returns:

The number of connections currently in use.

Public Static Functions

static TypeId GetTypeId()

Get the type ID.

Returns:

The object TypeId.

Public Static Attributes

static const ConnectionId INVALID_CONNECTION = 0

Invalid connection identifier.

UdpConnectionManager

class UdpConnectionManager : public ns3::ConnectionManager

UDP implementation of ConnectionManager.

UdpConnectionManager provides unreliable, unordered datagram delivery using UDP sockets.

Public Functions

UdpConnectionManager()

Default constructor.

~UdpConnectionManager() override

Destructor.

virtual void SetNode(Ptr<Node> node) override

Set the node this connection manager operates on.

Must be called before Bind() or Connect().

Parameters:

node – The node to use for socket creation.

virtual Ptr<Node> GetNode() const override

Get the node this connection manager operates on.

Returns:

The node, or nullptr if not set.

virtual void Bind(uint16_t port) override

Bind to a port and start accepting connections (server mode).

For TCP, this creates a listening socket and accepts incoming connections. For UDP, this binds to the port and starts receiving datagrams.

Parameters:

port – The port number to bind to.

virtual void Bind(const Address &local) override

Bind to a specific local address and start accepting connections.

Parameters:

local – The local address (IP and port) to bind to.

virtual void Connect(const Address &remote) override

Connect to a remote server (client mode).

For TCP, this establishes a connection to the server. If connection pooling is enabled (TcpConnectionManager), multiple connections may be created.

For UDP, this sets the default destination address for Send().

Parameters:

remote – The remote server address to connect to.

virtual bool Send(Ptr<Packet> packet) override

Send a packet to the default/connected peer.

In client mode, sends to the server specified in Connect(). In server mode with a single client, sends to that client.

Parameters:

packet – The packet to send.

Returns:

true if the packet was sent successfully, false on failure.

virtual bool Send(Ptr<Packet> packet, const Address &to) override

Send a packet to a specific peer.

In server mode, this routes the packet to the connection for that peer. In client mode with connection pooling, this sends to the server (the ‘to’ address should match the connected server).

Parameters:
  • packet – The packet to send.

  • to – The destination address.

Returns:

true if the packet was sent successfully, false on failure.

virtual void SetReceiveCallback(ReceiveCallback callback) override

Set the callback for receiving data.

The callback is invoked whenever data is received, with the packet and the sender’s address.

Parameters:

callback – The receive callback.

virtual void Close() override

Close all connections and release resources.

virtual void Close(const Address &peer) override

Close the connection(s) to a specific peer.

For TCP in server mode, closes the socket for that specific client. For TCP in client mode with connection pooling, closes ALL pooled connections to the server (since all pool connections share the same peer). For UDP, this is a no-op (connectionless).

Parameters:

peer – The peer address to disconnect.

virtual std::string GetName() const override

Get the name of this connection manager implementation.

Returns:

A string identifying the implementation (e.g., “TCP”, “UDP”).

virtual bool IsReliable() const override

Check if this transport provides reliable delivery.

Returns:

True for TCP, false for UDP.

virtual bool IsConnected() const override

Check if the connection manager has an active connection.

For TCP clients: returns true if at least one connection is established. For TCP servers: returns true if listening socket is active. For UDP: returns true if socket exists with a default destination set.

Returns:

True if connected/ready to send, false otherwise.

Public Static Functions

static TypeId GetTypeId()

Get the type ID.

Returns:

The object TypeId.