@@ -34,8 +34,8 @@ internal bool isHost
34
34
private Dictionary < ushort , int > messageHandlerCounter ;
35
35
private Dictionary < ushort , Stack < int > > releasedMessageHandlerCounters ;
36
36
private int localConnectionId ;
37
- public Scene PlayScene ;
38
- public Scene MenuScene ;
37
+ public int PlaySceneIndex ;
38
+ public int MenuSceneIndex ;
39
39
private Dictionary < uint , NetworkedObject > spawnedObjects ;
40
40
private List < uint > spawnedObjectIds ;
41
41
private Stack < uint > releasedNetworkObjectIds ;
@@ -212,7 +212,7 @@ private ConnectionConfig Init(NetworkingConfiguration netConfig)
212
212
{
213
213
if ( NetworkConfig . ConnectionApprovalCallback == null )
214
214
{
215
- Debug . LogWarning ( "MLAPI: No ConnectionApproval callback defined. Connection aproval will timeout" ) ;
215
+ Debug . LogWarning ( "MLAPI: No ConnectionApproval callback defined. Connection approval will timeout" ) ;
216
216
}
217
217
}
218
218
@@ -229,17 +229,14 @@ private ConnectionConfig Init(NetworkingConfiguration netConfig)
229
229
HashSet < string > channelNames = new HashSet < string > ( ) ;
230
230
foreach ( KeyValuePair < string , QosType > pair in NetworkConfig . Channels )
231
231
{
232
- if ( pair . Key . StartsWith ( "MLAPI_" ) )
233
- {
234
- Debug . LogWarning ( "MLAPI: Channel names are not allowed to start with MLAPI_. This is to prevent name conflicts" ) ;
235
- continue ;
236
- }
237
- else if ( channelNames . Contains ( pair . Key ) )
232
+ if ( channelNames . Contains ( pair . Key ) )
238
233
{
239
234
Debug . LogWarning ( "MLAPI: Duplicate channel name: " + pair . Key ) ;
240
235
continue ;
241
236
}
242
- channels . Add ( pair . Key , cConfig . AddChannel ( pair . Value ) ) ;
237
+ int channelId = cConfig . AddChannel ( pair . Value ) ;
238
+ channels . Add ( pair . Key , channelId ) ;
239
+ channelNames . Add ( pair . Key ) ;
243
240
}
244
241
//0-32 are reserved for MLAPI messages
245
242
for ( ushort i = 32 ; i < NetworkConfig . MessageTypes . Count ; i ++ )
@@ -252,37 +249,38 @@ private ConnectionConfig Init(NetworkingConfiguration netConfig)
252
249
253
250
public void StartServer ( NetworkingConfiguration netConfig )
254
251
{
252
+ SceneManager . LoadScene ( PlaySceneIndex ) ;
255
253
ConnectionConfig cConfig = Init ( netConfig ) ;
256
254
HostTopology hostTopology = new HostTopology ( cConfig , NetworkConfig . MaxConnections ) ;
257
- hostId = NetworkTransport . AddHost ( hostTopology , NetworkConfig . Port , null ) ;
255
+ hostId = NetworkTransport . AddHost ( hostTopology , NetworkConfig . Port ) ;
258
256
isServer = true ;
259
257
isClient = false ;
260
258
isListening = true ;
261
- SceneManager . LoadScene ( PlayScene . buildIndex ) ;
262
259
}
263
260
264
261
public void StartClient ( NetworkingConfiguration netConfig )
265
262
{
266
263
ConnectionConfig cConfig = Init ( netConfig ) ;
267
264
HostTopology hostTopology = new HostTopology ( cConfig , NetworkConfig . MaxConnections ) ;
268
- hostId = NetworkTransport . AddHost ( hostTopology , 0 ) ;
265
+ hostId = NetworkTransport . AddHost ( hostTopology , 0 , null ) ;
266
+ //NetworkTransport.AddSceneId(PlaySceneIndex);
269
267
270
- localConnectionId = NetworkTransport . Connect ( hostId , NetworkConfig . Address , NetworkConfig . Port , 0 , out error ) ;
271
268
isServer = false ;
272
269
isClient = true ;
273
270
isListening = true ;
271
+ localConnectionId = NetworkTransport . Connect ( hostId , NetworkConfig . Address , NetworkConfig . Port , 0 , out error ) ;
274
272
}
275
273
276
274
public void StartHost ( NetworkingConfiguration netConfig )
277
275
{
276
+ SceneManager . LoadScene ( PlaySceneIndex ) ;
278
277
ConnectionConfig cConfig = Init ( netConfig ) ;
279
278
HostTopology hostTopology = new HostTopology ( cConfig , NetworkConfig . MaxConnections ) ;
280
279
hostId = NetworkTransport . AddHost ( hostTopology , NetworkConfig . Port , null ) ;
281
280
isServer = true ;
282
281
isClient = true ;
283
282
isListening = true ;
284
283
connectedClients . Add ( - 1 , new NetworkedClient ( ) { ClientId = - 1 } ) ;
285
- SceneManager . LoadScene ( PlayScene . buildIndex ) ;
286
284
}
287
285
288
286
private void OnEnable ( )
@@ -316,12 +314,12 @@ private void Update()
316
314
do
317
315
{
318
316
messagesProcessed ++ ;
319
- eventType = NetworkTransport . Receive ( out hostId , out connectionId , out channelId , messageBuffer , NetworkConfig . MessageBufferSize , out receivedSize , out error ) ;
317
+ eventType = NetworkTransport . Receive ( out hostId , out connectionId , out channelId , messageBuffer , messageBuffer . Length , out receivedSize , out error ) ;
320
318
NetworkError networkError = ( NetworkError ) error ;
321
319
if ( networkError != NetworkError . Ok )
322
320
{
323
321
Debug . LogWarning ( "MLAPI: NetworkTransport receive error: " + networkError . ToString ( ) ) ;
324
- return ;
322
+ continue ;
325
323
}
326
324
switch ( eventType )
327
325
{
@@ -342,12 +340,13 @@ private void Update()
342
340
{
343
341
writer . Write ( NetworkConfig . ConnectionData ) ;
344
342
}
345
- Send ( connectionId , "MLAPI_CONNECTION_REQUEST" , "MLAPI_RELIABLE_FRAGMENTED" , writeStream . ToArray ( ) ) ;
346
343
}
344
+ Send ( connectionId , "MLAPI_CONNECTION_REQUEST" , "MLAPI_RELIABLE_FRAGMENTED" , writeStream . ToArray ( ) ) ;
347
345
}
348
346
}
349
347
break ;
350
348
case NetworkEventType . DataEvent :
349
+ Debug . Log ( "RECIEVE" ) ;
351
350
HandleIncomingData ( connectionId , ref messageBuffer ) ;
352
351
break ;
353
352
}
@@ -405,7 +404,7 @@ private void HandleIncomingData(int connectonId, ref byte[] data)
405
404
using ( BinaryReader messageReader = new BinaryReader ( messageReadStream ) )
406
405
{
407
406
byte [ ] configHash = messageReader . ReadBytes ( 32 ) ;
408
- if ( NetworkConfig . CompareConfig ( configHash ) == false )
407
+ if ( ! NetworkConfig . CompareConfig ( configHash ) )
409
408
{
410
409
Debug . LogWarning ( "MLAPI: NetworkConfiguration missmatch. The configuration between the server and client does not match." ) ;
411
410
DisconnectClient ( connectionId ) ;
@@ -427,7 +426,7 @@ private void HandleIncomingData(int connectonId, ref byte[] data)
427
426
case 1 : //Server informs client it has been approved:
428
427
if ( isClient )
429
428
{
430
- SceneManager . LoadScene ( PlayScene . buildIndex ) ;
429
+ SceneManager . LoadScene ( PlaySceneIndex ) ;
431
430
using ( MemoryStream messageReadStream = new MemoryStream ( reader . ReadBytes ( int . MaxValue ) ) )
432
431
{
433
432
using ( BinaryReader messageReader = new BinaryReader ( messageReadStream ) )
@@ -477,7 +476,9 @@ internal void Send(int connectionId, string messageType, string channelName, byt
477
476
{
478
477
writer . Write ( messageTypes [ messageType ] ) ;
479
478
writer . Write ( data ) ;
480
- NetworkTransport . Send ( hostId , connectionId , channels [ channelName ] , data , data . Length , out error ) ;
479
+ //2 bytes for message type
480
+ int size = data . Length + 2 ;
481
+ NetworkTransport . Send ( hostId , connectionId , channels [ channelName ] , stream . ToArray ( ) , size , out error ) ;
481
482
}
482
483
}
483
484
}
@@ -549,9 +550,8 @@ private void HandleApproval(int connectionId, bool approved)
549
550
writer . Write ( spawnedObjects [ spawnedObjectIds [ i ] ] . SpawnablePrefabId ) ;
550
551
}
551
552
}
552
-
553
- Send ( connectionId , "MLAPI_CLIENT_LIST" , "MLAPI_RELIABLE_FRAGMENTED" , writeStream . ToArray ( ) ) ;
554
553
}
554
+ Send ( connectionId , "MLAPI_CLIENT_LIST" , "MLAPI_RELIABLE_FRAGMENTED" , writeStream . ToArray ( ) ) ;
555
555
}
556
556
}
557
557
else
0 commit comments