@@ -67,8 +67,17 @@ private ConnectionConfig Init(NetworkingConfiguration netConfig)
67
67
MessageManager . messageHandlerCounter = new Dictionary < ushort , int > ( ) ;
68
68
MessageManager . releasedMessageHandlerCounters = new Dictionary < ushort , Stack < int > > ( ) ;
69
69
MessageManager . targetedMessages = new Dictionary < ushort , Dictionary < uint , List < int > > > ( ) ;
70
+ MessageManager . reverseChannels = new Dictionary < int , string > ( ) ;
71
+ MessageManager . reverseMessageTypes = new Dictionary < ushort , string > ( ) ;
70
72
SpawnManager . spawnedObjects = new Dictionary < uint , NetworkedObject > ( ) ;
71
73
SpawnManager . releasedNetworkObjectIds = new Stack < uint > ( ) ;
74
+ if ( NetworkConfig . AllowPassthroughMessages )
75
+ {
76
+ for ( int i = 0 ; i < NetworkConfig . PassthroughMessageTypes . Count ; i ++ )
77
+ {
78
+ NetworkConfig . RegisteredPassthroughMessageTypes . Add ( MessageManager . messageTypes [ NetworkConfig . PassthroughMessageTypes [ i ] ] ) ;
79
+ }
80
+ }
72
81
if ( NetworkConfig . HandleObjectSpawning )
73
82
{
74
83
NetworkedObject [ ] sceneObjects = FindObjectsOfType < NetworkedObject > ( ) ;
@@ -104,11 +113,13 @@ private ConnectionConfig Init(NetworkingConfiguration netConfig)
104
113
int channelId = cConfig . AddChannel ( pair . Value ) ;
105
114
MessageManager . channels . Add ( pair . Key , channelId ) ;
106
115
channelNames . Add ( pair . Key ) ;
116
+ MessageManager . reverseChannels . Add ( channelId , pair . Key ) ;
107
117
}
108
118
//0-32 are reserved for MLAPI messages
109
119
ushort messageId = 32 ;
110
120
for ( ushort i = 0 ; i < NetworkConfig . MessageTypes . Count ; i ++ )
111
121
{
122
+ MessageManager . reverseMessageTypes . Add ( messageId , NetworkConfig . MessageTypes [ i ] ) ;
112
123
MessageManager . messageTypes . Add ( NetworkConfig . MessageTypes [ i ] , messageId ) ;
113
124
messageId ++ ;
114
125
}
@@ -289,7 +300,7 @@ private void Update()
289
300
}
290
301
break ;
291
302
case NetworkEventType . DataEvent :
292
- HandleIncomingData ( clientId , messageBuffer ) ;
303
+ HandleIncomingData ( clientId , messageBuffer , channelId ) ;
293
304
break ;
294
305
case NetworkEventType . DisconnectEvent :
295
306
if ( isServer )
@@ -319,7 +330,7 @@ private IEnumerator ApprovalTimeout(int clientId)
319
330
}
320
331
}
321
332
322
- private void HandleIncomingData ( int connectonId , byte [ ] data )
333
+ private void HandleIncomingData ( int clientId , byte [ ] data , int channelId )
323
334
{
324
335
using ( MemoryStream readStream = new MemoryStream ( data ) )
325
336
{
@@ -330,6 +341,16 @@ private void HandleIncomingData(int connectonId, byte[] data)
330
341
uint targetNetworkId = 0 ;
331
342
if ( targeted )
332
343
targetNetworkId = reader . ReadUInt32 ( ) ;
344
+ bool isPassthrough = reader . ReadBoolean ( ) ;
345
+
346
+ int passthroughOrigin = 0 ;
347
+ int passthroughTarget = 0 ;
348
+
349
+ if ( isPassthrough && isServer )
350
+ passthroughTarget = reader . ReadInt32 ( ) ;
351
+ else if ( isPassthrough && ! isServer )
352
+ passthroughOrigin = reader . ReadInt32 ( ) ;
353
+
333
354
334
355
//Client tried to send a network message that was not the connection request before he was accepted.
335
356
if ( isServer && pendingClients . Contains ( clientId ) && messageType != 0 )
@@ -338,8 +359,30 @@ private void HandleIncomingData(int connectonId, byte[] data)
338
359
return ;
339
360
}
340
361
362
+
341
363
ushort bytesToRead = reader . ReadUInt16 ( ) ;
342
364
byte [ ] incommingData = reader . ReadBytes ( bytesToRead ) ;
365
+
366
+ if ( isServer && isPassthrough && ! NetworkConfig . RegisteredPassthroughMessageTypes . Contains ( messageType ) )
367
+ {
368
+ Debug . LogWarning ( "MLAPI: Client " + clientId + " tried to send a passthrough message for a messageType not registered as passthrough" ) ;
369
+ return ;
370
+ }
371
+ else if ( isClient && isPassthrough && ! NetworkConfig . RegisteredPassthroughMessageTypes . Contains ( messageType ) )
372
+ {
373
+ Debug . LogWarning ( "MLAPI: Server tried to send a passthrough message for a messageType not registered as passthrough" ) ;
374
+ return ;
375
+ }
376
+ else if ( isServer && NetworkConfig . AllowPassthroughMessages && connectedClients . ContainsKey ( passthroughTarget ) )
377
+ {
378
+ uint ? netIdTarget = null ;
379
+ if ( targeted )
380
+ netIdTarget = targetNetworkId ;
381
+
382
+ PassthroughSend ( passthroughTarget , clientId , messageType , channelId , incommingData , netIdTarget ) ;
383
+ return ;
384
+ }
385
+
343
386
if ( messageType >= 32 )
344
387
{
345
388
//Custom message, invoke all message handlers
@@ -348,14 +391,20 @@ private void HandleIncomingData(int connectonId, byte[] data)
348
391
List < int > handlerIds = MessageManager . targetedMessages [ messageType ] [ targetNetworkId ] ;
349
392
for ( int i = 0 ; i < handlerIds . Count ; i ++ )
350
393
{
351
- MessageManager . messageCallbacks [ messageType ] [ handlerIds [ i ] ] ( clientId , incommingData ) ;
394
+ if ( isPassthrough )
395
+ MessageManager . messageCallbacks [ messageType ] [ handlerIds [ i ] ] ( passthroughOrigin , incommingData ) ;
396
+ else
397
+ MessageManager . messageCallbacks [ messageType ] [ handlerIds [ i ] ] ( clientId , incommingData ) ;
352
398
}
353
399
}
354
400
else
355
401
{
356
402
foreach ( KeyValuePair < int , Action < int , byte [ ] > > pair in MessageManager . messageCallbacks [ messageType ] )
357
403
{
358
- pair . Value ( clientId , incommingData ) ;
404
+ if ( isPassthrough )
405
+ pair . Value ( passthroughOrigin , incommingData ) ;
406
+ else
407
+ pair . Value ( clientId , incommingData ) ;
359
408
}
360
409
}
361
410
}
@@ -500,6 +549,40 @@ private void HandleIncomingData(int connectonId, byte[] data)
500
549
}
501
550
}
502
551
552
+ internal void PassthroughSend ( int targetId , int sourceId , ushort messageType , int channelId , byte [ ] data , uint ? networkId = null )
553
+ {
554
+ if ( isHost && targetId == - 1 )
555
+ {
556
+ //Host trying to send data to it's own client
557
+ if ( networkId == null )
558
+ MessageManager . InvokeMessageHandlers ( MessageManager . reverseMessageTypes [ messageType ] , data , sourceId ) ;
559
+ else
560
+ MessageManager . InvokeTargetedMessageHandler ( MessageManager . reverseMessageTypes [ messageType ] , data , sourceId , networkId . Value ) ;
561
+ return ;
562
+ }
563
+
564
+ int sizeOfStream = 10 ;
565
+ if ( networkId != null )
566
+ sizeOfStream += 4 ;
567
+ sizeOfStream += data . Length ;
568
+
569
+ using ( MemoryStream stream = new MemoryStream ( sizeOfStream ) )
570
+ {
571
+ using ( BinaryWriter writer = new BinaryWriter ( stream ) )
572
+ {
573
+ writer . Write ( messageType ) ;
574
+ writer . Write ( networkId != null ) ;
575
+ if ( networkId != null )
576
+ writer . Write ( networkId . Value ) ;
577
+ writer . Write ( true ) ;
578
+ writer . Write ( sourceId ) ;
579
+ writer . Write ( ( ushort ) data . Length ) ;
580
+ writer . Write ( data ) ;
581
+ }
582
+ NetworkTransport . Send ( hostId , targetId , channelId , stream . GetBuffer ( ) , sizeOfStream , out error ) ;
583
+ }
584
+ }
585
+
503
586
internal void Send ( int clientId , string messageType , string channelName , byte [ ] data , uint ? networkId = null )
504
587
{
505
588
if ( isHost && clientId == - 1 )
@@ -516,10 +599,19 @@ internal void Send(int clientId, string messageType, string channelName, byte[]
516
599
//Client trying to send data to host
517
600
clientId = serverClientId ;
518
601
}
519
- //2 bytes for messageType, 2 bytes for buffer length and one byte for target bool
520
- int sizeOfStream = 5 ;
602
+
603
+ bool isPassthrough = ( ! isServer && clientId != serverClientId && NetworkConfig . AllowPassthroughMessages ) ;
604
+ if ( isPassthrough && ! NetworkConfig . RegisteredPassthroughMessageTypes . Contains ( MessageManager . messageTypes [ messageType ] ) )
605
+ {
606
+ Debug . LogWarning ( "MLAPI: The The MessageType " + messageType + " is not registered as an allowed passthrough message type." ) ;
607
+ return ;
608
+ }
609
+
610
+ int sizeOfStream = 6 ;
521
611
if ( networkId != null )
522
612
sizeOfStream += 4 ;
613
+ if ( isPassthrough )
614
+ sizeOfStream += 4 ;
523
615
sizeOfStream += data . Length ;
524
616
525
617
using ( MemoryStream stream = new MemoryStream ( sizeOfStream ) )
@@ -530,17 +622,21 @@ internal void Send(int clientId, string messageType, string channelName, byte[]
530
622
writer . Write ( networkId != null ) ;
531
623
if ( networkId != null )
532
624
writer . Write ( networkId . Value ) ;
625
+ writer . Write ( isPassthrough ) ;
626
+ if ( isPassthrough )
627
+ writer . Write ( clientId ) ;
533
628
writer . Write ( ( ushort ) data . Length ) ;
534
629
writer . Write ( data ) ;
535
630
}
631
+ if ( isPassthrough )
632
+ clientId = serverClientId ;
536
633
NetworkTransport . Send ( hostId , clientId , MessageManager . channels [ channelName ] , stream . GetBuffer ( ) , sizeOfStream , out error ) ;
537
634
}
538
635
}
539
636
540
637
internal void Send ( int [ ] clientIds , string messageType , string channelName , byte [ ] data , uint ? networkId = null )
541
638
{
542
- //2 bytes for messageType, 2 bytes for buffer length and one byte for target bool
543
- int sizeOfStream = 5 ;
639
+ int sizeOfStream = 6 ;
544
640
if ( networkId != null )
545
641
sizeOfStream += 4 ;
546
642
sizeOfStream += data . Length ;
@@ -553,6 +649,7 @@ internal void Send(int[] clientIds, string messageType, string channelName, byte
553
649
writer . Write ( networkId != null ) ;
554
650
if ( networkId != null )
555
651
writer . Write ( networkId . Value ) ;
652
+ writer . Write ( false ) ;
556
653
writer . Write ( ( ushort ) data . Length ) ;
557
654
writer . Write ( data ) ;
558
655
}
@@ -581,7 +678,7 @@ internal void Send(int[] clientIds, string messageType, string channelName, byte
581
678
internal void Send ( List < int > clientIds , string messageType , string channelName , byte [ ] data , uint ? networkId = null )
582
679
{
583
680
//2 bytes for messageType, 2 bytes for buffer length and one byte for target bool
584
- int sizeOfStream = 5 ;
681
+ int sizeOfStream = 6 ;
585
682
if ( networkId != null )
586
683
sizeOfStream += 4 ;
587
684
sizeOfStream += data . Length ;
@@ -594,6 +691,7 @@ internal void Send(List<int> clientIds, string messageType, string channelName,
594
691
writer . Write ( networkId != null ) ;
595
692
if ( networkId != null )
596
693
writer . Write ( networkId . Value ) ;
694
+ writer . Write ( false ) ;
597
695
writer . Write ( ( ushort ) data . Length ) ;
598
696
writer . Write ( data ) ;
599
697
}
@@ -622,7 +720,7 @@ internal void Send(List<int> clientIds, string messageType, string channelName,
622
720
internal void Send ( string messageType , string channelName , byte [ ] data , uint ? networkId = null )
623
721
{
624
722
//2 bytes for messageType, 2 bytes for buffer length and one byte for target bool
625
- int sizeOfStream = 5 ;
723
+ int sizeOfStream = 6 ;
626
724
if ( networkId != null )
627
725
sizeOfStream += 4 ;
628
726
sizeOfStream += data . Length ;
@@ -635,6 +733,7 @@ internal void Send(string messageType, string channelName, byte[] data, uint? ne
635
733
writer . Write ( networkId != null ) ;
636
734
if ( networkId != null )
637
735
writer . Write ( networkId . Value ) ;
736
+ writer . Write ( false ) ;
638
737
writer . Write ( ( ushort ) data . Length ) ;
639
738
writer . Write ( data ) ;
640
739
}
@@ -677,6 +776,7 @@ internal void Send(string messageType, string channelName, byte[] data, int clie
677
776
writer . Write ( networkId != null ) ;
678
777
if ( networkId != null )
679
778
writer . Write ( networkId . Value ) ;
779
+ writer . Write ( false ) ;
680
780
writer . Write ( ( ushort ) data . Length ) ;
681
781
writer . Write ( data ) ;
682
782
}
0 commit comments