@@ -151,7 +151,7 @@ public void Send(ulong clientId, NetworkDelivery delivery, FastBufferWriter batc
151
151
{
152
152
var sendBuffer = batchData . ToTempByteArray ( ) ;
153
153
154
- m_NetworkManager . NetworkConfig . NetworkTransport . Send ( clientId , sendBuffer , delivery ) ;
154
+ m_NetworkManager . NetworkConfig . NetworkTransport . Send ( m_NetworkManager . ClientIdToTransportId ( clientId ) , sendBuffer , delivery ) ;
155
155
}
156
156
}
157
157
@@ -223,10 +223,12 @@ public GameObject GetNetworkPrefabOverride(GameObject gameObject)
223
223
224
224
public NetworkSceneManager SceneManager { get ; private set ; }
225
225
226
+ public readonly ulong ServerClientId = 0 ;
227
+
226
228
/// <summary>
227
229
/// Gets the networkId of the server
228
230
/// </summary>
229
- public ulong ServerClientId => NetworkConfig . NetworkTransport ? . ServerClientId ??
231
+ private ulong m_ServerTransportId => NetworkConfig . NetworkTransport ? . ServerClientId ??
230
232
throw new NullReferenceException (
231
233
$ "The transport in the active { nameof ( NetworkConfig ) } is null") ;
232
234
@@ -243,6 +245,10 @@ public ulong LocalClientId
243
245
244
246
private Dictionary < ulong , NetworkClient > m_ConnectedClients = new Dictionary < ulong , NetworkClient > ( ) ;
245
247
248
+ private ulong m_NextClientId = 1 ;
249
+ private Dictionary < ulong , ulong > m_ClientIdToTransportIdMap = new Dictionary < ulong , ulong > ( ) ;
250
+ private Dictionary < ulong , ulong > m_TransportIdToClientIdMap = new Dictionary < ulong , ulong > ( ) ;
251
+
246
252
private List < NetworkClient > m_ConnectedClientsList = new List < NetworkClient > ( ) ;
247
253
248
254
private List < ulong > m_ConnectedClientIds = new List < ulong > ( ) ;
@@ -980,6 +986,12 @@ private void OnDestroy()
980
986
}
981
987
}
982
988
989
+ private void DisconnectRemoteClient ( ulong clientId )
990
+ {
991
+ var transportId = ClientIdToTransportId ( clientId ) ;
992
+ NetworkConfig . NetworkTransport . DisconnectRemoteClient ( transportId ) ;
993
+ }
994
+
983
995
/// <summary>
984
996
/// Globally shuts down the library.
985
997
/// Disconnects clients if connected and stops server if running.
@@ -1014,7 +1026,7 @@ public void Shutdown()
1014
1026
continue ;
1015
1027
}
1016
1028
1017
- NetworkConfig . NetworkTransport . DisconnectRemoteClient ( pair . Key ) ;
1029
+ DisconnectRemoteClient ( pair . Key ) ;
1018
1030
}
1019
1031
}
1020
1032
@@ -1028,7 +1040,7 @@ public void Shutdown()
1028
1040
continue ;
1029
1041
}
1030
1042
1031
- NetworkConfig . NetworkTransport . DisconnectRemoteClient ( pair . Key ) ;
1043
+ DisconnectRemoteClient ( pair . Key ) ;
1032
1044
}
1033
1045
}
1034
1046
}
@@ -1098,6 +1110,9 @@ public void Shutdown()
1098
1110
NetworkConfig ? . NetworkTransport ? . Shutdown ( ) ;
1099
1111
}
1100
1112
1113
+ m_ClientIdToTransportIdMap . Clear ( ) ;
1114
+ m_TransportIdToClientIdMap . Clear ( ) ;
1115
+
1101
1116
IsListening = false ;
1102
1117
}
1103
1118
@@ -1224,14 +1239,30 @@ private IEnumerator ApprovalTimeout(ulong clientId)
1224
1239
}
1225
1240
}
1226
1241
1242
+ private ulong TransportIdToClientId ( ulong transportId )
1243
+ {
1244
+ return transportId == m_ServerTransportId ? ServerClientId : m_TransportIdToClientIdMap [ transportId ] ;
1245
+ }
1246
+
1247
+ private ulong ClientIdToTransportId ( ulong clientId )
1248
+ {
1249
+ return clientId == ServerClientId ? m_ServerTransportId : m_ClientIdToTransportIdMap [ clientId ] ;
1250
+ }
1251
+
1227
1252
private void HandleRawTransportPoll ( NetworkEvent networkEvent , ulong clientId , ArraySegment < byte > payload , float receiveTime )
1228
1253
{
1254
+ var transportId = clientId ;
1229
1255
switch ( networkEvent )
1230
1256
{
1231
1257
case NetworkEvent . Connect :
1232
1258
#if DEVELOPMENT_BUILD || UNITY_EDITOR
1233
1259
s_TransportConnect . Begin ( ) ;
1234
1260
#endif
1261
+
1262
+ clientId = m_NextClientId ++ ;
1263
+ m_ClientIdToTransportIdMap [ clientId ] = transportId ;
1264
+ m_TransportIdToClientIdMap [ transportId ] = clientId ;
1265
+
1235
1266
MessagingSystem . ClientConnected ( clientId ) ;
1236
1267
if ( IsServer )
1237
1268
{
@@ -1270,13 +1301,19 @@ private void HandleRawTransportPoll(NetworkEvent networkEvent, ulong clientId, A
1270
1301
NetworkLog . LogInfo ( $ "Incoming Data From { clientId } : { payload . Count } bytes") ;
1271
1302
}
1272
1303
1304
+ clientId = TransportIdToClientId ( clientId ) ;
1305
+
1273
1306
HandleIncomingData ( clientId , payload , receiveTime ) ;
1274
1307
break ;
1275
1308
}
1276
1309
case NetworkEvent . Disconnect :
1277
1310
#if DEVELOPMENT_BUILD || UNITY_EDITOR
1278
1311
s_TransportDisconnect . Begin ( ) ;
1279
1312
#endif
1313
+ clientId = TransportIdToClientId ( clientId ) ;
1314
+
1315
+ m_TransportIdToClientIdMap . Remove ( transportId ) ;
1316
+ m_ClientIdToTransportIdMap . Remove ( clientId ) ;
1280
1317
1281
1318
if ( NetworkLog . CurrentLogLevel <= LogLevel . Developer )
1282
1319
{
@@ -1400,8 +1437,7 @@ public void DisconnectClient(ulong clientId)
1400
1437
}
1401
1438
1402
1439
OnClientDisconnectFromServer ( clientId ) ;
1403
-
1404
- NetworkConfig . NetworkTransport . DisconnectRemoteClient ( clientId ) ;
1440
+ DisconnectRemoteClient ( clientId ) ;
1405
1441
}
1406
1442
1407
1443
private void OnClientDisconnectFromServer ( ulong clientId )
@@ -1575,7 +1611,7 @@ internal void HandleApproval(ulong ownerClientId, bool createPlayerObject, uint?
1575
1611
else
1576
1612
{
1577
1613
PendingClients . Remove ( ownerClientId ) ;
1578
- NetworkConfig . NetworkTransport . DisconnectRemoteClient ( ownerClientId ) ;
1614
+ DisconnectRemoteClient ( ownerClientId ) ;
1579
1615
}
1580
1616
}
1581
1617
0 commit comments