@@ -34,8 +34,8 @@ internal bool isHost
3434 private Dictionary < ushort , int > messageHandlerCounter ;
3535 private Dictionary < ushort , Stack < int > > releasedMessageHandlerCounters ;
3636 private int localConnectionId ;
37- public Scene PlayScene ;
38- public Scene MenuScene ;
37+ public int PlaySceneIndex ;
38+ public int MenuSceneIndex ;
3939 private Dictionary < uint , NetworkedObject > spawnedObjects ;
4040 private List < uint > spawnedObjectIds ;
4141 private Stack < uint > releasedNetworkObjectIds ;
@@ -212,7 +212,7 @@ private ConnectionConfig Init(NetworkingConfiguration netConfig)
212212 {
213213 if ( NetworkConfig . ConnectionApprovalCallback == null )
214214 {
215- Debug . LogWarning ( "MLAPI: No ConnectionApproval callback defined. Connection aproval will timeout" ) ;
215+ Debug . LogWarning ( "MLAPI: No ConnectionApproval callback defined. Connection approval will timeout" ) ;
216216 }
217217 }
218218
@@ -229,17 +229,14 @@ private ConnectionConfig Init(NetworkingConfiguration netConfig)
229229 HashSet < string > channelNames = new HashSet < string > ( ) ;
230230 foreach ( KeyValuePair < string , QosType > pair in NetworkConfig . Channels )
231231 {
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 ) )
238233 {
239234 Debug . LogWarning ( "MLAPI: Duplicate channel name: " + pair . Key ) ;
240235 continue ;
241236 }
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 ) ;
243240 }
244241 //0-32 are reserved for MLAPI messages
245242 for ( ushort i = 32 ; i < NetworkConfig . MessageTypes . Count ; i ++ )
@@ -252,37 +249,38 @@ private ConnectionConfig Init(NetworkingConfiguration netConfig)
252249
253250 public void StartServer ( NetworkingConfiguration netConfig )
254251 {
252+ SceneManager . LoadScene ( PlaySceneIndex ) ;
255253 ConnectionConfig cConfig = Init ( netConfig ) ;
256254 HostTopology hostTopology = new HostTopology ( cConfig , NetworkConfig . MaxConnections ) ;
257- hostId = NetworkTransport . AddHost ( hostTopology , NetworkConfig . Port , null ) ;
255+ hostId = NetworkTransport . AddHost ( hostTopology , NetworkConfig . Port ) ;
258256 isServer = true ;
259257 isClient = false ;
260258 isListening = true ;
261- SceneManager . LoadScene ( PlayScene . buildIndex ) ;
262259 }
263260
264261 public void StartClient ( NetworkingConfiguration netConfig )
265262 {
266263 ConnectionConfig cConfig = Init ( netConfig ) ;
267264 HostTopology hostTopology = new HostTopology ( cConfig , NetworkConfig . MaxConnections ) ;
268- hostId = NetworkTransport . AddHost ( hostTopology , 0 ) ;
265+ hostId = NetworkTransport . AddHost ( hostTopology , 0 , null ) ;
266+ //NetworkTransport.AddSceneId(PlaySceneIndex);
269267
270- localConnectionId = NetworkTransport . Connect ( hostId , NetworkConfig . Address , NetworkConfig . Port , 0 , out error ) ;
271268 isServer = false ;
272269 isClient = true ;
273270 isListening = true ;
271+ localConnectionId = NetworkTransport . Connect ( hostId , NetworkConfig . Address , NetworkConfig . Port , 0 , out error ) ;
274272 }
275273
276274 public void StartHost ( NetworkingConfiguration netConfig )
277275 {
276+ SceneManager . LoadScene ( PlaySceneIndex ) ;
278277 ConnectionConfig cConfig = Init ( netConfig ) ;
279278 HostTopology hostTopology = new HostTopology ( cConfig , NetworkConfig . MaxConnections ) ;
280279 hostId = NetworkTransport . AddHost ( hostTopology , NetworkConfig . Port , null ) ;
281280 isServer = true ;
282281 isClient = true ;
283282 isListening = true ;
284283 connectedClients . Add ( - 1 , new NetworkedClient ( ) { ClientId = - 1 } ) ;
285- SceneManager . LoadScene ( PlayScene . buildIndex ) ;
286284 }
287285
288286 private void OnEnable ( )
@@ -316,12 +314,12 @@ private void Update()
316314 do
317315 {
318316 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 ) ;
320318 NetworkError networkError = ( NetworkError ) error ;
321319 if ( networkError != NetworkError . Ok )
322320 {
323321 Debug . LogWarning ( "MLAPI: NetworkTransport receive error: " + networkError . ToString ( ) ) ;
324- return ;
322+ continue ;
325323 }
326324 switch ( eventType )
327325 {
@@ -342,12 +340,13 @@ private void Update()
342340 {
343341 writer . Write ( NetworkConfig . ConnectionData ) ;
344342 }
345- Send ( connectionId , "MLAPI_CONNECTION_REQUEST" , "MLAPI_RELIABLE_FRAGMENTED" , writeStream . ToArray ( ) ) ;
346343 }
344+ Send ( connectionId , "MLAPI_CONNECTION_REQUEST" , "MLAPI_RELIABLE_FRAGMENTED" , writeStream . ToArray ( ) ) ;
347345 }
348346 }
349347 break ;
350348 case NetworkEventType . DataEvent :
349+ Debug . Log ( "RECIEVE" ) ;
351350 HandleIncomingData ( connectionId , ref messageBuffer ) ;
352351 break ;
353352 }
@@ -405,7 +404,7 @@ private void HandleIncomingData(int connectonId, ref byte[] data)
405404 using ( BinaryReader messageReader = new BinaryReader ( messageReadStream ) )
406405 {
407406 byte [ ] configHash = messageReader . ReadBytes ( 32 ) ;
408- if ( NetworkConfig . CompareConfig ( configHash ) == false )
407+ if ( ! NetworkConfig . CompareConfig ( configHash ) )
409408 {
410409 Debug . LogWarning ( "MLAPI: NetworkConfiguration missmatch. The configuration between the server and client does not match." ) ;
411410 DisconnectClient ( connectionId ) ;
@@ -427,7 +426,7 @@ private void HandleIncomingData(int connectonId, ref byte[] data)
427426 case 1 : //Server informs client it has been approved:
428427 if ( isClient )
429428 {
430- SceneManager . LoadScene ( PlayScene . buildIndex ) ;
429+ SceneManager . LoadScene ( PlaySceneIndex ) ;
431430 using ( MemoryStream messageReadStream = new MemoryStream ( reader . ReadBytes ( int . MaxValue ) ) )
432431 {
433432 using ( BinaryReader messageReader = new BinaryReader ( messageReadStream ) )
@@ -477,7 +476,9 @@ internal void Send(int connectionId, string messageType, string channelName, byt
477476 {
478477 writer . Write ( messageTypes [ messageType ] ) ;
479478 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 ) ;
481482 }
482483 }
483484 }
@@ -549,9 +550,8 @@ private void HandleApproval(int connectionId, bool approved)
549550 writer . Write ( spawnedObjects [ spawnedObjectIds [ i ] ] . SpawnablePrefabId ) ;
550551 }
551552 }
552-
553- Send ( connectionId , "MLAPI_CLIENT_LIST" , "MLAPI_RELIABLE_FRAGMENTED" , writeStream . ToArray ( ) ) ;
554553 }
554+ Send ( connectionId , "MLAPI_CLIENT_LIST" , "MLAPI_RELIABLE_FRAGMENTED" , writeStream . ToArray ( ) ) ;
555555 }
556556 }
557557 else
0 commit comments