Skip to content

Commit 39e3a70

Browse files
committed
Added custom message support
1 parent cf7fbe7 commit 39e3a70

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

MLAPI/Data/MLAPIConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ public static class MLAPIConstants
1919
public const ushort MLAPI_NETWORKED_VAR_UPDATE = 12;
2020
public const ushort MLAPI_SERVER_RPC = 13;
2121
public const ushort MLAPI_CLIENT_RPC = 14;
22+
public const ushort MLAPI_CUSTOM_MESSAGE = 15;
2223
}
2324
}

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public class NetworkingManager : MonoBehaviour
4545
/// </summary>
4646
public static NetworkingManager singleton { get; private set; }
4747
/// <summary>
48+
/// Gets the networkId of the server
49+
/// </summary>
50+
public uint ServerNetId => NetworkConfig.NetworkTransport.ServerNetId;
51+
/// <summary>
4852
/// The clientId the server calls the local client by, only valid for clients
4953
/// </summary>
5054
public uint LocalClientId
@@ -123,6 +127,62 @@ internal set
123127
/// The current NetworkingConfiguration
124128
/// </summary>
125129
public NetworkConfig NetworkConfig;
130+
/// <summary>
131+
/// Delegate used for incomming custom messages
132+
/// </summary>
133+
/// <param name="clientId">The clientId that sent the message</param>
134+
/// <param name="reader">The reader containing the message data</param>
135+
public delegate void CustomMessageDelegete(uint clientId, BitReader reader);
136+
/// <summary>
137+
/// Event invoked when custom messages arrive
138+
/// </summary>
139+
public event CustomMessageDelegete OnIncommingCustomMessage;
140+
141+
internal void InvokeOnIncommingCustomMessage(uint clientId, BitReader reader)
142+
{
143+
if (OnIncommingCustomMessage != null) OnIncommingCustomMessage(clientId, reader);
144+
}
145+
146+
/// <summary>
147+
/// Sends custom message to a list of clients
148+
/// </summary>
149+
/// <param name="clientIds">The clients to send to, sends to everyone if null</param>
150+
/// <param name="writer">The message writer containing the data</param>
151+
/// <param name="channel">The channel to send the data on</param>
152+
public void SendCustomMessage(List<uint> clientIds, BitWriter writer, string channel = "MLAPI_DEFAULT_MESSAGE")
153+
{
154+
if (!isServer)
155+
{
156+
if (LogHelper.CurrentLogLevel <= LogLevel.Error) LogHelper.LogWarning("Can not send custom message to multiple users as a client");
157+
return;
158+
}
159+
if (clientIds == null)
160+
{
161+
for (int i = 0; i < ConnectedClientsList.Count; i++)
162+
{
163+
InternalMessageHandler.Send(ConnectedClientsList[i].ClientId, "MLAPI_CUSTOM_MESSAGE", channel, writer);
164+
}
165+
}
166+
else
167+
{
168+
for (int i = 0; i < clientIds.Count; i++)
169+
{
170+
InternalMessageHandler.Send(clientIds[i], "MLAPI_CUSTOM_MESSAGE", channel, writer);
171+
}
172+
}
173+
}
174+
175+
/// <summary>
176+
/// Sends a custom message to a specific client
177+
/// </summary>
178+
/// <param name="clientId">The client to send the message to</param>
179+
/// <param name="writer">The message writer containing the data</param>
180+
/// <param name="channel">The channel tos end the data on</param>
181+
public void SendCustomMessage(uint clientId, BitWriter writer, string channel = "MLAPI_DEFAULT_MESSAGE")
182+
{
183+
InternalMessageHandler.Send(clientId, "MLAPI_CUSTOM_MESSAGE", channel, writer);
184+
}
185+
126186

127187
#if !DISABLE_CRYPTOGRAPHY
128188
internal EllipticDiffieHellman clientDiffieHellman;
@@ -363,6 +423,7 @@ private object Init(bool server)
363423
MessageManager.messageTypes.Add("MLAPI_NETWORKED_VAR_UPDATE", MLAPIConstants.MLAPI_NETWORKED_VAR_UPDATE);
364424
MessageManager.messageTypes.Add("MLAPI_SERVER_RPC", MLAPIConstants.MLAPI_SERVER_RPC);
365425
MessageManager.messageTypes.Add("MLAPI_CLIENT_RPC", MLAPIConstants.MLAPI_CLIENT_RPC);
426+
MessageManager.messageTypes.Add("MLAPI_CUSTOM_MESSAGE", MLAPIConstants.MLAPI_CUSTOM_MESSAGE);
366427

367428
return settings;
368429
}
@@ -755,7 +816,6 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId, uint
755816
{
756817
#region INTERNAL MESSAGE
757818

758-
//MLAPI message
759819
switch (messageType)
760820
{
761821
case MLAPIConstants.MLAPI_CONNECTION_REQUEST:
@@ -803,6 +863,9 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId, uint
803863
case MLAPIConstants.MLAPI_CLIENT_RPC:
804864
if (isClient) InternalMessageHandler.HandleClientRPC(clientId, reader, channelId);
805865
break;
866+
case MLAPIConstants.MLAPI_CUSTOM_MESSAGE:
867+
InternalMessageHandler.HandleCustomMessage(clientId, reader, channelId);
868+
break;
806869
}
807870

808871
#endregion

MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Receive.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,5 +315,10 @@ internal static void HandleClientRPC(uint clientId, BitReader reader, int channe
315315
NetworkedBehaviour behaviour = SpawnManager.SpawnedObjects[networkId].GetBehaviourAtOrderIndex(behaviourId);
316316
behaviour.OnRemoteClientRPC(hash, clientId, reader);
317317
}
318+
319+
internal static void HandleCustomMessage(uint clientId, BitReader reader, int channelId)
320+
{
321+
NetworkingManager.singleton.InvokeOnIncommingCustomMessage(clientId, reader);
322+
}
318323
}
319324
}

0 commit comments

Comments
 (0)