NetworkConnectionToClient.cs 3.8 KB

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