@@ -139,6 +139,17 @@ internal static void OnDestroyObject(uint networkId)
139
139
singleton . releasedNetworkObjectIds . Push ( networkId ) ;
140
140
}
141
141
142
+ internal void InvokeMessageHandlers ( string messageType , byte [ ] data , int clientId )
143
+ {
144
+ if ( ! messageTypes . ContainsKey ( messageType ) || ! messageCallbacks . ContainsKey ( messageTypes [ messageType ] ) )
145
+ return ;
146
+
147
+ foreach ( KeyValuePair < int , Action < int , byte [ ] > > pair in messageCallbacks [ messageTypes [ messageType ] ] )
148
+ {
149
+ pair . Value ( clientId , data ) ;
150
+ }
151
+ }
152
+
142
153
internal int AddIncomingMessageHandler ( string name , Action < int , byte [ ] > action )
143
154
{
144
155
if ( messageTypes . ContainsKey ( name ) )
@@ -566,6 +577,17 @@ private void HandleIncomingData(int connectonId, byte[] data)
566
577
567
578
internal void Send ( int clientId , string messageType , string channelName , byte [ ] data )
568
579
{
580
+ if ( isHost && clientId == - 1 )
581
+ {
582
+ //Host trying to send data to it's own client
583
+ InvokeMessageHandlers ( messageType , data , clientId ) ;
584
+ return ;
585
+ }
586
+ else if ( clientId == - 1 )
587
+ {
588
+ //Client trying to send data to host
589
+ clientId = serverClientId ;
590
+ }
569
591
using ( MemoryStream stream = new MemoryStream ( ) )
570
592
{
571
593
using ( BinaryWriter writer = new BinaryWriter ( stream ) )
@@ -597,7 +619,18 @@ internal void Send(int[] clientIds, string messageType, string channelName, byte
597
619
int channel = channels [ channelName ] ;
598
620
for ( int i = 0 ; i < clientIds . Length ; i ++ )
599
621
{
600
- NetworkTransport . Send ( hostId , clientIds [ i ] , channel , dataToSend , size , out error ) ;
622
+ int clientId = clientIds [ i ] ;
623
+ if ( isHost && clientId == - 1 )
624
+ {
625
+ InvokeMessageHandlers ( messageType , data , clientId ) ;
626
+ continue ;
627
+ }
628
+ else if ( clientId == - 1 )
629
+ {
630
+ //Client trying to send data to host
631
+ clientId = serverClientId ;
632
+ }
633
+ NetworkTransport . Send ( hostId , clientId , channel , dataToSend , size , out error ) ;
601
634
}
602
635
}
603
636
}
@@ -618,7 +651,18 @@ internal void Send(List<int> clientIds, string messageType, string channelName,
618
651
int channel = channels [ channelName ] ;
619
652
for ( int i = 0 ; i < clientIds . Count ; i ++ )
620
653
{
621
- NetworkTransport . Send ( hostId , clientIds [ i ] , channel , dataToSend , size , out error ) ;
654
+ int clientId = clientIds [ i ] ;
655
+ if ( isHost && clientId == - 1 )
656
+ {
657
+ InvokeMessageHandlers ( messageType , data , clientId ) ;
658
+ continue ;
659
+ }
660
+ else if ( clientId == - 1 )
661
+ {
662
+ //Client trying to send data to host
663
+ clientId = serverClientId ;
664
+ }
665
+ NetworkTransport . Send ( hostId , clientId , channel , dataToSend , size , out error ) ;
622
666
}
623
667
}
624
668
}
@@ -639,7 +683,19 @@ internal void Send(string messageType, string channelName, byte[] data)
639
683
int channel = channels [ channelName ] ;
640
684
foreach ( KeyValuePair < int , NetworkedClient > pair in connectedClients )
641
685
{
642
- NetworkTransport . Send ( hostId , pair . Key , channel , dataToSend , size , out error ) ;
686
+ int clientId = pair . Key ;
687
+ if ( isHost && pair . Key == - 1 )
688
+ {
689
+ InvokeMessageHandlers ( messageType , data , pair . Key ) ;
690
+ continue ;
691
+ }
692
+ else if ( clientId == - 1 )
693
+ {
694
+ //Client trying to send data to host
695
+ clientId = serverClientId ;
696
+ }
697
+ NetworkTransport . Send ( hostId , clientId , channel , dataToSend , size , out error ) ;
698
+
643
699
}
644
700
}
645
701
}
@@ -662,7 +718,18 @@ internal void Send(string messageType, string channelName, byte[] data, int clie
662
718
{
663
719
if ( pair . Key == clientIdToIgnore )
664
720
continue ;
665
- NetworkTransport . Send ( hostId , pair . Key , channel , dataToSend , size , out error ) ;
721
+ int clientId = pair . Key ;
722
+ if ( isHost && pair . Key == - 1 )
723
+ {
724
+ InvokeMessageHandlers ( messageType , data , clientId ) ;
725
+ continue ;
726
+ }
727
+ else if ( clientId == - 1 )
728
+ {
729
+ //Client trying to send data to host
730
+ clientId = serverClientId ;
731
+ }
732
+ NetworkTransport . Send ( hostId , clientId , channel , dataToSend , size , out error ) ;
666
733
}
667
734
}
668
735
}
0 commit comments