Skip to content

Mirror::NetworkConnection

A High level network connection. This is used for connections from client-to-server and for connection from server-to-client. More...

Inherited by NetworkConnectionToClient, NetworkConnectionToServer

Public Functions

Name
virtual abstract void Disconnect() =0
Disconnects this connection.
void Send< T >(T msg, int channelId =Channels.DefaultReliable)
This sends a network message with a message ID on the connection. This message is sent on channel zero, which by default is the reliable channel.
override string ToString()
bool InvokeHandler< T >(T msg, int channelId)
This function invokes the registered handler function for a message.

Public Properties

Name
abstract string address
The IP address / URL / FQDN associated with the connection. Can be useful for a game master to do IP Bans etc.
NetworkIdentity identity
The NetworkIdentity for this connection.

Public Attributes

Name
const int LocalConnectionId
readonly int connectionId
Unique identifier for this connection that is assigned by the transport layer.
bool isAuthenticated
Flag that indicates the client has been authenticated.
object authenticationData
General purpose object to hold authentication data, character selection, tokens, etc. associated with the connection for reference after Authentication completes.
bool isReady
Flag that tells if the connection has been marked as "ready" by a client calling ClientScene.Ready().
float lastMessageTime
The last time that a message was received on this connection.
readonly HashSet< NetworkIdentity > clientOwnedObjects
A list of the NetworkIdentity objects owned by this connection. This list is read-only.

Detailed Description

class Mirror::NetworkConnection;

A High level network connection. This is used for connections from client-to-server and for connection from server-to-client.

A NetworkConnection corresponds to a specific connection for a host in the transport layer. It has a connectionId that is assigned by the transport layer and passed to the Initialize function.

A NetworkClient has one NetworkConnection. A NetworkServerSimple manages multiple NetworkConnections. The NetworkServer has multiple "remote" connections and a "local" connection for the local client.

The NetworkConnection class provides message sending and handling facilities. For sending data over a network, there are methods to send message objects, byte arrays, and NetworkWriter objects. To handle data arriving from the network, handler functions can be registered for message Ids, byte arrays can be processed by HandleBytes(), and NetworkReader object can be processed by HandleReader().

NetworkConnection objects also act as observers for networked objects. When a connection is an observer of a networked object with a NetworkIdentity, then the object will be visible to corresponding client for the connection, and incremental state changes will be sent to the client.

There are many virtual functions on NetworkConnection that allow its behaviour to be customized. NetworkClient and NetworkServer can both be made to instantiate custom classes derived from NetworkConnection by setting their networkConnectionClass member variable.

Public Functions Documentation

function Disconnect

virtual abstract void Disconnect() =0

Disconnects this connection.

Reimplemented by: Mirror::NetworkConnectionToServer::Disconnect, Mirror::NetworkConnectionToClient::Disconnect, Mirror::ULocalConnectionToClient::Disconnect

function Send< T >

inline void Send< T >(
    T msg,
    int channelId =Channels.DefaultReliable
)

This sends a network message with a message ID on the connection. This message is sent on channel zero, which by default is the reliable channel.

Parameters:

  • msg The message to send.
  • channelId The transport layer channel to send on.

Template Parameters:

  • T The message type to unregister.

function ToString

inline override string ToString()

function InvokeHandler< T >

inline bool InvokeHandler< T >(
    T msg,
    int channelId
)

This function invokes the registered handler function for a message.

Parameters:

  • msg The message object to process.

Template Parameters:

  • T The message type to unregister.

Return: Returns true if the handler was successfully invoked

Network connections used by the NetworkClient and NetworkServer use this function for handling network messages.

Public Property Documentation

property address

abstract string address;

The IP address / URL / FQDN associated with the connection. Can be useful for a game master to do IP Bans etc.

property identity

NetworkIdentity identity;

The NetworkIdentity for this connection.

Public Attributes Documentation

variable LocalConnectionId

const int LocalConnectionId = 0;

variable connectionId

readonly int connectionId;

Unique identifier for this connection that is assigned by the transport layer.

On a server, this Id is unique for every connection on the server. On a client this Id is local to the client, it is not the same as the Id on the server for this connection.

Transport layers connections begin at one. So on a client with a single connection to a server, the connectionId of that connection will be one. In NetworkServer, the connectionId of the local connection is zero.

Clients do not know their connectionId on the server, and do not know the connectionId of other clients on the server.

variable isAuthenticated

bool isAuthenticated;

Flag that indicates the client has been authenticated.

variable authenticationData

object authenticationData;

General purpose object to hold authentication data, character selection, tokens, etc. associated with the connection for reference after Authentication completes.

variable isReady

bool isReady;

Flag that tells if the connection has been marked as "ready" by a client calling ClientScene.Ready().

This property is read-only. It is set by the system on the client when ClientScene.Ready() is called, and set by the system on the server when a ready message is received from a client.

A client that is ready is sent spawned objects by the server and updates to the state of spawned objects. A client that is not ready is not sent spawned objects.

variable lastMessageTime

float lastMessageTime;

The last time that a message was received on this connection.

This includes internal system messages (such as Commands and ClientRpc calls) and user messages.

variable clientOwnedObjects

readonly HashSet< NetworkIdentity > clientOwnedObjects = new HashSet<[NetworkIdentity](/Documentation/Cops%20And%20Robbers/Classes/classMirror_1_1NetworkIdentity/)>();

A list of the NetworkIdentity objects owned by this connection. This list is read-only.

This includes the player object for the connection - if it has localPlayerAutority set, and any objects spawned with local authority or set with AssignLocalAuthority.

This list can be used to validate messages from clients, to ensure that clients are only trying to control objects that they own.


Updated on 18 January 2021 at 10:57:16 UTC