Mirror Networking
LocalConnectionToServer.cs
1using System;
2using System.Collections.Generic;
3using UnityEngine;
4
5namespace Mirror
6{
7 // a localClient's connection TO a server.
8 // send messages on this connection causes the server's handler function to be invoked directly.
10 {
11 internal LocalConnectionToClient connectionToClient;
12
13 // packet queue
14 internal readonly Queue<NetworkWriterPooled> queue = new Queue<NetworkWriterPooled>();
15
16 public override string address => "localhost";
17
18 // see caller for comments on why we need this
19 bool connectedEventPending;
20 bool disconnectedEventPending;
21 internal void QueueConnectedEvent() => connectedEventPending = true;
22 internal void QueueDisconnectedEvent() => disconnectedEventPending = true;
23
24 // Send stage two: serialized NetworkMessage as ArraySegment<byte>
25 internal override void Send(ArraySegment<byte> segment, int channelId = Channels.Reliable)
26 {
27 if (segment.Count == 0)
28 {
29 Debug.LogError("LocalConnection.SendBytes cannot send zero bytes");
30 return;
31 }
32
33 // OnTransportData assumes batching.
34 // so let's make a batch with proper timestamp prefix.
35 Batcher batcher = GetBatchForChannelId(channelId);
36 batcher.AddMessage(segment, NetworkTime.localTime);
37
38 // flush it to the server's OnTransportData immediately.
39 // local connection to server always invokes immediately.
41 {
42 // make a batch with our local time (double precision)
43 if (batcher.GetBatch(writer))
44 {
45 NetworkServer.OnTransportData(connectionId, writer.ToArraySegment(), channelId);
46 }
47 else Debug.LogError("Local connection failed to make batch. This should never happen.");
48 }
49 }
50
51 internal override void Update()
52 {
53 base.Update();
54
55 // should we still process a connected event?
56 if (connectedEventPending)
57 {
58 connectedEventPending = false;
59 NetworkClient.OnConnectedEvent?.Invoke();
60 }
61
62 // process internal messages so they are applied at the correct time
63 while (queue.Count > 0)
64 {
65 // call receive on queued writer's content, return to pool
66 NetworkWriterPooled writer = queue.Dequeue();
67 ArraySegment<byte> message = writer.ToArraySegment();
68
69 // OnTransportData assumes a proper batch with timestamp etc.
70 // let's make a proper batch and pass it to OnTransportData.
71 Batcher batcher = GetBatchForChannelId(Channels.Reliable);
72 batcher.AddMessage(message, NetworkTime.localTime);
73
74 using (NetworkWriterPooled batchWriter = NetworkWriterPool.Get())
75 {
76 // make a batch with our local time (double precision)
77 if (batcher.GetBatch(batchWriter))
78 {
79 NetworkClient.OnTransportData(batchWriter.ToArraySegment(), Channels.Reliable);
80 }
81 }
82
84 }
85
86 // should we still process a disconnected event?
87 if (disconnectedEventPending)
88 {
89 disconnectedEventPending = false;
90 NetworkClient.OnDisconnectedEvent?.Invoke();
91 }
92 }
93
95 internal void DisconnectInternal()
96 {
97 // set not ready and handle clientscene disconnect in any case
98 // (might be client or host mode here)
99 // TODO remove redundant state. have one source of truth for .ready!
100 isReady = false;
101 NetworkClient.ready = false;
102 }
103
105 public override void Disconnect()
106 {
107 connectionToClient.DisconnectInternal();
108 DisconnectInternal();
109
110 // simulate what a true remote connection would do:
111 // first, the server should remove it:
112 // TODO should probably be in connectionToClient.DisconnectInternal
113 // because that's the NetworkServer's connection!
114 NetworkServer.RemoveLocalConnection();
115
116 // then call OnTransportDisconnected for proper disconnect handling,
117 // callbacks & cleanups.
118 // => otherwise OnClientDisconnected() is never called!
119 // => see NetworkClientTests.DisconnectCallsOnClientDisconnect_HostMode()
120 NetworkClient.OnTransportDisconnected();
121 }
122
123 // true because local connections never timeout
124 internal override bool IsAlive(float timeout) => true;
125 }
126}
override void Disconnect()
Disconnects this connection.
NetworkClient with connection to server.
readonly int connectionId
Unique identifier for this connection that is assigned by the transport layer.
bool isReady
A server connection is ready after joining the game world.
NetworkServer handles remote connections and has a local connection for a local client.
Synchronizes server time to clients.
Definition: NetworkTime.cs:12
ArraySegment< byte > ToArraySegment()
Returns allocation-free ArraySegment until 'Position'.
Pool of NetworkWriters to avoid allocations.
static void Return(NetworkWriterPooled writer)
Return a writer to the pool.
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'