Mirror Networking
Mirror.NetworkConnection Class Referenceabstract

Base NetworkConnection class for server-to-client and client-to-server connection. More...

Inheritance diagram for Mirror.NetworkConnection:
Mirror.NetworkConnectionToClient Mirror.NetworkConnectionToServer Mirror.LocalConnectionToClient Mirror.LocalConnectionToServer

Public Member Functions

void Send< T > (T message, int channelId=Channels.Reliable)
 Send a NetworkMessage to this connection over the given channel. More...
 
abstract void Disconnect ()
 Disconnects this connection. More...
 
override string ToString ()
 

Public Attributes

readonly int connectionId
 Unique identifier for this connection that is assigned by the transport layer. More...
 
bool isAuthenticated
 Flag that indicates the client has been authenticated. More...
 
object authenticationData
 General purpose object to hold authentication data, character selection, tokens, etc. More...
 
bool isReady
 A server connection is ready after joining the game world. More...
 
float lastMessageTime
 Last time a message was received for this connection. Includes system and user messages. More...
 

Static Public Attributes

const int LocalConnectionId = 0
 

Protected Member Functions

Batcher GetBatchForChannelId (int channelId)
 
abstract void SendToTransport (ArraySegment< byte > segment, int channelId=Channels.Reliable)
 

Static Protected Member Functions

static bool ValidatePacketSize (ArraySegment< byte > segment, int channelId)
 

Protected Attributes

Dictionary< int, Batcherbatches = new Dictionary<int, Batcher>()
 

Properties

HashSet< NetworkIdentityobserving [get]
 NetworkIdentities that this connection can see More...
 
abstract string address [get]
 IP address of the connection. Can be useful for game master IP bans etc. More...
 
NetworkIdentity identity [get, set]
 This connection's main object (usually the player object). More...
 
HashSet< NetworkIdentityclientOwnedObjects [get]
 All NetworkIdentities owned by this connection. Can be main player, pets, etc. More...
 
double remoteTimeStamp [get, set]
 last batch's remote timestamp. not interpolated. useful for NetworkTransform etc. More...
 

Detailed Description

Base NetworkConnection class for server-to-client and client-to-server connection.

Definition at line 9 of file NetworkConnection.cs.

Member Function Documentation

◆ Disconnect()

abstract void Mirror.NetworkConnection.Disconnect ( )
pure virtual

◆ GetBatchForChannelId()

Batcher Mirror.NetworkConnection.GetBatchForChannelId ( int  channelId)
protected

Definition at line 87 of file NetworkConnection.cs.

88 {
89 // get existing or create new writer for the channelId
90 Batcher batch;
91 if (!batches.TryGetValue(channelId, out batch))
92 {
93 // get max batch size for this channel
94 int threshold = Transport.activeTransport.GetBatchThreshold(channelId);
95
96 // create batcher
97 batch = new Batcher(threshold);
98 batches[channelId] = batch;
99 }
100 return batch;
101 }

◆ Send< T >()

void Mirror.NetworkConnection.Send< T > ( message,
int  channelId = Channels.Reliable 
)

Send a NetworkMessage to this connection over the given channel.

Type Constraints
T :struct 
T :NetworkMessage 

Definition at line 131 of file NetworkConnection.cs.

132 : struct, NetworkMessage
133 {
134 using (NetworkWriterPooled writer = NetworkWriterPool.Get())
135 {
136 // pack message and send allocation free
137 MessagePacking.Pack(message, writer);
138 NetworkDiagnostics.OnSend(message, channelId, writer.Position, 1);
139 Send(writer.ToArraySegment(), channelId);
140 }
141 }

◆ ValidatePacketSize()

static bool Mirror.NetworkConnection.ValidatePacketSize ( ArraySegment< byte >  segment,
int  channelId 
)
staticprotected

Definition at line 108 of file NetworkConnection.cs.

109 {
110 int max = Transport.activeTransport.GetMaxPacketSize(channelId);
111 if (segment.Count > max)
112 {
113 Debug.LogError($"NetworkConnection.ValidatePacketSize: cannot send packet larger than {max} bytes, was {segment.Count} bytes");
114 return false;
115 }
116
117 if (segment.Count == 0)
118 {
119 // zero length packets getting into the packet queues are bad.
120 Debug.LogError("NetworkConnection.ValidatePacketSize: cannot send zero bytes");
121 return false;
122 }
123
124 // good size
125 return true;
126 }

Member Data Documentation

◆ authenticationData

object Mirror.NetworkConnection.authenticationData

General purpose object to hold authentication data, character selection, tokens, etc.

Definition at line 27 of file NetworkConnection.cs.

◆ batches

Dictionary<int, Batcher> Mirror.NetworkConnection.batches = new Dictionary<int, Batcher>()
protected

Definition at line 62 of file NetworkConnection.cs.

◆ connectionId

readonly int Mirror.NetworkConnection.connectionId

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

Definition at line 21 of file NetworkConnection.cs.

◆ isAuthenticated

bool Mirror.NetworkConnection.isAuthenticated

Flag that indicates the client has been authenticated.

Definition at line 24 of file NetworkConnection.cs.

◆ isReady

bool Mirror.NetworkConnection.isReady

A server connection is ready after joining the game world.

Definition at line 33 of file NetworkConnection.cs.

◆ lastMessageTime

float Mirror.NetworkConnection.lastMessageTime

Last time a message was received for this connection. Includes system and user messages.

Definition at line 39 of file NetworkConnection.cs.

◆ LocalConnectionId

const int Mirror.NetworkConnection.LocalConnectionId = 0
static

Definition at line 11 of file NetworkConnection.cs.

Property Documentation

◆ address

abstract string Mirror.NetworkConnection.address
get

IP address of the connection. Can be useful for game master IP bans etc.

Definition at line 36 of file NetworkConnection.cs.

36{ get; }

◆ clientOwnedObjects

HashSet<NetworkIdentity> Mirror.NetworkConnection.clientOwnedObjects
get

All NetworkIdentities owned by this connection. Can be main player, pets, etc.

Definition at line 51 of file NetworkConnection.cs.

◆ identity

NetworkIdentity Mirror.NetworkConnection.identity
getset

This connection's main object (usually the player object).

Definition at line 42 of file NetworkConnection.cs.

42{ get; internal set; }

◆ observing

HashSet<NetworkIdentity> Mirror.NetworkConnection.observing
get

NetworkIdentities that this connection can see

Definition at line 16 of file NetworkConnection.cs.

◆ remoteTimeStamp

double Mirror.NetworkConnection.remoteTimeStamp
getset

last batch's remote timestamp. not interpolated. useful for NetworkTransform etc.

Definition at line 71 of file NetworkConnection.cs.

71{ get; internal set; }