2using System.Collections.Generic;
3using System.Runtime.CompilerServices;
11 public const int LocalConnectionId = 0;
15 [Obsolete(
"Cast to NetworkConnectionToClient to access .observing")]
50 [Obsolete(
"Cast to NetworkConnectionToClient to access .clientOwnedObjects")]
62 protected Dictionary<int, Batcher> batches =
new Dictionary<int, Batcher>();
80 internal NetworkConnection(
int networkConnectionId) : this()
87 protected Batcher GetBatchForChannelId(
int channelId)
91 if (!batches.TryGetValue(channelId, out batch))
94 int threshold = Transport.activeTransport.GetBatchThreshold(channelId);
97 batch =
new Batcher(threshold);
98 batches[channelId] = batch;
108 protected static bool ValidatePacketSize(ArraySegment<byte> segment,
int channelId)
110 int max = Transport.activeTransport.GetMaxPacketSize(channelId);
111 if (segment.Count > max)
113 Debug.LogError($
"NetworkConnection.ValidatePacketSize: cannot send packet larger than {max} bytes, was {segment.Count} bytes");
117 if (segment.Count == 0)
120 Debug.LogError(
"NetworkConnection.ValidatePacketSize: cannot send zero bytes");
130 [MethodImpl(MethodImplOptions.AggressiveInlining)]
146 [MethodImpl(MethodImplOptions.AggressiveInlining)]
147 internal virtual void Send(ArraySegment<byte> segment,
int channelId =
Channels.Reliable)
167 GetBatchForChannelId(channelId).AddMessage(segment,
NetworkTime.localTime);
171 [MethodImpl(MethodImplOptions.AggressiveInlining)]
172 protected abstract void SendToTransport(ArraySegment<byte> segment,
int channelId = Channels.Reliable);
175 internal virtual void Update()
178 foreach (KeyValuePair<int, Batcher> kvp
in batches)
182 Batcher batcher = kvp.Value;
183 using (NetworkWriterPooled writer = NetworkWriterPool.Get())
186 while (batcher.GetBatch(writer))
193 ArraySegment<byte> segment = writer.ToArraySegment();
194 if (ValidatePacketSize(segment, kvp.Key))
197 SendToTransport(segment, kvp.Key);
209 internal virtual bool IsAlive(
float timeout) => Time.time -
lastMessageTime < timeout;
231 public override string ToString() => $
"connection({connectionId})";
Base NetworkConnection class for server-to-client and client-to-server connection.
readonly int connectionId
Unique identifier for this connection that is assigned by the transport layer.
NetworkIdentity identity
This connection's main object (usually the player object).
HashSet< NetworkIdentity > observing
NetworkIdentities that this connection can see
abstract string address
IP address of the connection. Can be useful for game master IP bans etc.
double remoteTimeStamp
last batch's remote timestamp. not interpolated. useful for NetworkTransform etc.
abstract void Disconnect()
Disconnects this connection.
bool isAuthenticated
Flag that indicates the client has been authenticated.
void Send< T >(T message, int channelId=Channels.Reliable)
Send a NetworkMessage to this connection over the given channel.
object authenticationData
General purpose object to hold authentication data, character selection, tokens, etc.
bool isReady
A server connection is ready after joining the game world.
float lastMessageTime
Last time a message was received for this connection. Includes system and user messages.
HashSet< NetworkIdentity > clientOwnedObjects
All NetworkIdentities owned by this connection. Can be main player, pets, etc.
Profiling statistics for tool to subscribe to (profiler etc.)
NetworkIdentity identifies objects across the network.
Synchronizes server time to clients.
ArraySegment< byte > ToArraySegment()
Returns allocation-free ArraySegment until 'Position'.
int Position
Next position to write to the buffer
Pool of NetworkWriters to avoid allocations.
static NetworkWriterPooled Get()
Get a writer from the pool. Creates new one if pool is empty.
Pooled NetworkWriter, automatically returned to pool when using 'using'