Mirror Networking
NetworkConnectionToClient.cs
1using System;
2using System.Collections.Generic;
3using System.Runtime.CompilerServices;
4
5namespace Mirror
6{
8 {
9 public override string address =>
11
13 // TODO move to server's NetworkConnectionToClient?
14 public new readonly HashSet<NetworkIdentity> observing = new HashSet<NetworkIdentity>();
15
17 // IMPORTANT: this needs to be <NetworkIdentity>, not <uint netId>.
18 // fixes a bug where DestroyOwnedObjects wouldn't find the
19 // netId anymore: https://github.com/vis2k/Mirror/issues/1380
20 // Works fine with NetworkIdentity pointers though.
21 public new readonly HashSet<NetworkIdentity> clientOwnedObjects = new HashSet<NetworkIdentity>();
22
23 // unbatcher
24 public Unbatcher unbatcher = new Unbatcher();
25
26 public NetworkConnectionToClient(int networkConnectionId)
27 : base(networkConnectionId) {}
28
29 // Send stage three: hand off to transport
30 [MethodImpl(MethodImplOptions.AggressiveInlining)]
31 protected override void SendToTransport(ArraySegment<byte> segment, int channelId = Channels.Reliable) =>
33
35 public override void Disconnect()
36 {
37 // set not ready and handle clientscene disconnect in any case
38 // (might be client or host mode here)
39 isReady = false;
41
42 // IMPORTANT: NetworkConnection.Disconnect() is NOT called for
43 // voluntary disconnects from the other end.
44 // -> so all 'on disconnect' cleanup code needs to be in
45 // OnTransportDisconnect, where it's called for both voluntary
46 // and involuntary disconnects!
47 }
48
49 internal void AddToObserving(NetworkIdentity netIdentity)
50 {
51 observing.Add(netIdentity);
52
53 // spawn identity for this conn
54 NetworkServer.ShowForConnection(netIdentity, this);
55 }
56
57 internal void RemoveFromObserving(NetworkIdentity netIdentity, bool isDestroyed)
58 {
59 observing.Remove(netIdentity);
60
61 if (!isDestroyed)
62 {
63 // hide identity for this conn
64 NetworkServer.HideForConnection(netIdentity, this);
65 }
66 }
67
68 internal void RemoveFromObservingsObservers()
69 {
70 foreach (NetworkIdentity netIdentity in observing)
71 {
72 netIdentity.RemoveObserver(this);
73 }
74 observing.Clear();
75 }
76
77 internal void AddOwnedObject(NetworkIdentity obj)
78 {
79 clientOwnedObjects.Add(obj);
80 }
81
82 internal void RemoveOwnedObject(NetworkIdentity obj)
83 {
84 clientOwnedObjects.Remove(obj);
85 }
86
87 internal void DestroyOwnedObjects()
88 {
89 // create a copy because the list might be modified when destroying
90 HashSet<NetworkIdentity> tmp = new HashSet<NetworkIdentity>(clientOwnedObjects);
91 foreach (NetworkIdentity netIdentity in tmp)
92 {
93 if (netIdentity != null)
94 {
95 NetworkServer.Destroy(netIdentity.gameObject);
96 }
97 }
98
99 // clear the hashset because we destroyed them all
100 clientOwnedObjects.Clear();
101 }
102 }
103}
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.
bool isReady
A server connection is ready after joining the game world.
new readonly HashSet< NetworkIdentity > clientOwnedObjects
All NetworkIdentities owned by this connection. Can be main player, pets, etc.
override void Disconnect()
Disconnects this connection.
new readonly HashSet< NetworkIdentity > observing
NetworkIdentities that this connection can see
NetworkIdentity identifies objects across the network.
NetworkServer handles remote connections and has a local connection for a local client.
Abstract transport layer component
Definition: Transport.cs:32
static Transport activeTransport
The current transport used by Mirror.
Definition: Transport.cs:35
abstract void ServerSend(int connectionId, ArraySegment< byte > segment, int channelId=Channels.Reliable)
Send a message to a client over the given channel.
abstract string ServerGetClientAddress(int connectionId)
Get a client's address on the server.
abstract void ServerDisconnect(int connectionId)
Disconnect a client from the server.