|
| 1 | +using MLAPI.MonoBehaviours.Core; |
| 2 | +using System; |
| 3 | + |
| 4 | +namespace MLAPI.Data |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Represents a ClientId structure |
| 8 | + /// </summary> |
| 9 | + public struct NetId |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// The hostId this client is on |
| 13 | + /// </summary> |
| 14 | + public byte HostId; |
| 15 | + /// <summary> |
| 16 | + /// The connectionId this client is assigned |
| 17 | + /// </summary> |
| 18 | + public ushort ConnectionId; |
| 19 | + /// <summary> |
| 20 | + /// Meta data about hte client |
| 21 | + /// </summary> |
| 22 | + public byte Meta; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Returns wheter or not the clientId represents a -1 |
| 26 | + /// </summary> |
| 27 | + /// <returns><c>true</c>, if host, <c>false</c> otherwise.</returns> |
| 28 | + public bool IsHost() |
| 29 | + { |
| 30 | + return Meta == 1; |
| 31 | + } |
| 32 | + /// <summary> |
| 33 | + /// Returns if this is a invalid clientId, (-2) |
| 34 | + /// </summary> |
| 35 | + /// <returns><c>true</c>, if invalid, <c>false</c> otherwise.</returns> |
| 36 | + public bool IsInvalid() |
| 37 | + { |
| 38 | + return Meta == 2; |
| 39 | + } |
| 40 | + /// <summary> |
| 41 | + /// Static ServerNetId for comparison |
| 42 | + /// </summary> |
| 43 | + /// <value>The server net identifier.</value> |
| 44 | + public static NetId ServerNetId |
| 45 | + { |
| 46 | + get |
| 47 | + { |
| 48 | + return new NetId((byte)NetworkingManager.singleton.serverHostId, (ushort)NetworkingManager.singleton.serverConnectionId, false, false); |
| 49 | + } |
| 50 | + } |
| 51 | + /// <summary> |
| 52 | + /// Initializes a new instance of the netId struct from transport values |
| 53 | + /// </summary> |
| 54 | + /// <param name="hostId">Host identifier.</param> |
| 55 | + /// <param name="connectionId">Connection identifier.</param> |
| 56 | + /// <param name="isHost">If set to <c>true</c> is host.</param> |
| 57 | + /// <param name="isInvalid">If set to <c>true</c> is invalid.</param> |
| 58 | + public NetId(byte hostId, ushort connectionId, bool isHost, bool isInvalid) |
| 59 | + { |
| 60 | + HostId = hostId; |
| 61 | + ConnectionId = connectionId; |
| 62 | + if (isHost) |
| 63 | + Meta = 1; |
| 64 | + else if (isInvalid) |
| 65 | + Meta = 2; |
| 66 | + else |
| 67 | + Meta = 0; |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + private static byte[] tempUIntBytes = new byte[4]; |
| 72 | + private static byte[] tempUShortBytes = new byte[2]; |
| 73 | + /// <summary> |
| 74 | + /// Initializes a new instance of the netId struct from a clientId |
| 75 | + /// </summary> |
| 76 | + /// <param name="clientId">Client identifier.</param> |
| 77 | + public NetId(uint clientId) |
| 78 | + { |
| 79 | + tempUIntBytes = BitConverter.GetBytes(clientId); |
| 80 | + HostId = tempUIntBytes[0]; |
| 81 | + ConnectionId = BitConverter.ToUInt16(tempUIntBytes, 1); |
| 82 | + Meta = tempUIntBytes[3]; |
| 83 | + } |
| 84 | + /// <summary> |
| 85 | + /// Gets the clientId. |
| 86 | + /// </summary> |
| 87 | + /// <returns>The client identifier.</returns> |
| 88 | + public uint GetClientId() |
| 89 | + { |
| 90 | + tempUShortBytes = BitConverter.GetBytes(ConnectionId); |
| 91 | + tempUIntBytes[0] = HostId; |
| 92 | + tempUIntBytes[1] = tempUShortBytes[0]; |
| 93 | + tempUIntBytes[2] = tempUShortBytes[1]; |
| 94 | + tempUIntBytes[3] = Meta; |
| 95 | + return BitConverter.ToUInt32(tempUIntBytes, 0); |
| 96 | + } |
| 97 | + // Rider generated vvv |
| 98 | + /// <summary> |
| 99 | + /// Determines whether the specified <see cref="object"/> is equal to the current <see cref="T:MLAPI.Data.NetId"/>. |
| 100 | + /// </summary> |
| 101 | + /// <param name="obj">The <see cref="object"/> to compare with the current <see cref="T:MLAPI.Data.NetId"/>.</param> |
| 102 | + /// <returns><c>true</c> if the specified <see cref="object"/> is equal to the current <see cref="T:MLAPI.Data.NetId"/>; |
| 103 | + /// otherwise, <c>false</c>.</returns> |
| 104 | + public override bool Equals (object obj) |
| 105 | + { |
| 106 | + if (obj == null || GetType() != obj.GetType()) |
| 107 | + return false; |
| 108 | + |
| 109 | + NetId key = (NetId)obj; |
| 110 | + return (HostId == key.HostId) && (ConnectionId == key.ConnectionId); |
| 111 | + } |
| 112 | + // Rider generated vvv |
| 113 | + /// <summary> |
| 114 | + /// Serves as a hash function for a <see cref="T:MLAPI.Data.NetId"/> object. |
| 115 | + /// </summary> |
| 116 | + /// <returns>A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a |
| 117 | + /// hash table.</returns> |
| 118 | + public override int GetHashCode() |
| 119 | + { |
| 120 | + return (int)GetClientId(); |
| 121 | + } |
| 122 | + // Rider generated vvv |
| 123 | + /// <summary> |
| 124 | + /// Determines whether a specified instance of <see cref="MLAPI.Data.NetId"/> is equal to another specified <see cref="MLAPI.Data.NetId"/>. |
| 125 | + /// </summary> |
| 126 | + /// <param name="client1">The first <see cref="MLAPI.Data.NetId"/> to compare.</param> |
| 127 | + /// <param name="client2">The second <see cref="MLAPI.Data.NetId"/> to compare.</param> |
| 128 | + /// <returns><c>true</c> if <c>client1</c> and <c>client2</c> are equal; otherwise, <c>false</c>.</returns> |
| 129 | + public static bool operator ==(NetId client1, NetId client2) |
| 130 | + { |
| 131 | + return (client1.HostId == client2.HostId && client1.ConnectionId == client2.ConnectionId) || (client1.IsHost() == client2.IsHost()); |
| 132 | + } |
| 133 | + // Rider generated vvv |
| 134 | + /// <summary> |
| 135 | + /// Determines whether a specified instance of <see cref="MLAPI.Data.NetId"/> is not equal to another specified <see cref="MLAPI.Data.NetId"/>. |
| 136 | + /// </summary> |
| 137 | + /// <param name="client1">The first <see cref="MLAPI.Data.NetId"/> to compare.</param> |
| 138 | + /// <param name="client2">The second <see cref="MLAPI.Data.NetId"/> to compare.</param> |
| 139 | + /// <returns><c>true</c> if <c>client1</c> and <c>client2</c> are not equal; otherwise, <c>false</c>.</returns> |
| 140 | + public static bool operator !=(NetId client1, NetId client2) |
| 141 | + { |
| 142 | + return !(client1 == client2); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments