Skip to content

Commit bde2f0f

Browse files
committed
Added host support
1 parent db1e200 commit bde2f0f

File tree

3 files changed

+78
-8
lines changed

3 files changed

+78
-8
lines changed

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,14 @@ private void OnDestroy()
8282

8383
public void SendToServer(string messageType, string channelName, byte[] data)
8484
{
85-
if(isServer)
85+
if (isServer)
8686
{
87-
Debug.LogWarning("MLAPI: Sending messages from server to server is not yet supported");
88-
return;
87+
NetworkingManager.singleton.InvokeMessageHandlers(messageType, data, -1);
88+
}
89+
else
90+
{
91+
NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data);
8992
}
90-
NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data);
9193
}
9294

9395
public void SendToLocalClient(string messageType, string channelName, byte[] data)

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ internal static void OnDestroyObject(uint networkId)
139139
singleton.releasedNetworkObjectIds.Push(networkId);
140140
}
141141

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+
142153
internal int AddIncomingMessageHandler(string name, Action<int, byte[]> action)
143154
{
144155
if(messageTypes.ContainsKey(name))
@@ -566,6 +577,17 @@ private void HandleIncomingData(int connectonId, byte[] data)
566577

567578
internal void Send(int clientId, string messageType, string channelName, byte[] data)
568579
{
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+
}
569591
using (MemoryStream stream = new MemoryStream())
570592
{
571593
using (BinaryWriter writer = new BinaryWriter(stream))
@@ -597,7 +619,18 @@ internal void Send(int[] clientIds, string messageType, string channelName, byte
597619
int channel = channels[channelName];
598620
for (int i = 0; i < clientIds.Length; i++)
599621
{
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);
601634
}
602635
}
603636
}
@@ -618,7 +651,18 @@ internal void Send(List<int> clientIds, string messageType, string channelName,
618651
int channel = channels[channelName];
619652
for (int i = 0; i < clientIds.Count; i++)
620653
{
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);
622666
}
623667
}
624668
}
@@ -639,7 +683,19 @@ internal void Send(string messageType, string channelName, byte[] data)
639683
int channel = channels[channelName];
640684
foreach (KeyValuePair<int, NetworkedClient> pair in connectedClients)
641685
{
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+
643699
}
644700
}
645701
}
@@ -662,7 +718,18 @@ internal void Send(string messageType, string channelName, byte[] data, int clie
662718
{
663719
if (pair.Key == clientIdToIgnore)
664720
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);
666733
}
667734
}
668735
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ It's licenced under the MIT licence :D
2020
* Serializer (both for the library to speed up and to allow structs to be sent easily)
2121
* SyncVars (allow variables to automatically be synced to new clients and current clients when it's changed)
2222
* Message compression
23+
* Host support (Client hosts the server) (done)
2324

2425
That's all I can think of right now. But there is more to come, especially if people show intrest in the project.
2526

0 commit comments

Comments
 (0)