Skip to content

Commit 2967be5

Browse files
committed
Polished messaging with new overloads
1 parent aa8e31f commit 2967be5

File tree

2 files changed

+82
-33
lines changed

2 files changed

+82
-33
lines changed

MLAPI/Data/AttributeMessageMode.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Reflection;
3-
using System.Runtime.InteropServices.ComTypes;
43
using MLAPI.NetworkingManagerComponents.Binary;
54

65
namespace MLAPI.Data

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 82 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,8 @@ internal void HandleNetworkedVarUpdate(BitReader reader, uint clientId)
358358
#region MESSAGING_SYSTEM
359359
private static readonly Dictionary<Type, Dictionary<ulong, ClientRPC>> CachedClientRpcs = new Dictionary<Type, Dictionary<ulong, ClientRPC>>();
360360
private static readonly Dictionary<Type, Dictionary<ulong, ServerRPC>> CachedServerRpcs = new Dictionary<Type, Dictionary<ulong, ServerRPC>>();
361-
private static readonly HashSet<Type> CachedTypes = new HashSet<Type>();
362361
private static readonly Dictionary<ulong, string> HashResults = new Dictionary<ulong, string>();
362+
private static readonly HashSet<Type> CachedTypes = new HashSet<Type>();
363363

364364
private ulong HashMethodName(string name)
365365
{
@@ -562,30 +562,63 @@ internal void SendClientRPCPerformance(ulong hash, List<uint> clientIds, BitWri
562562
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Only clients and host can invoke ClientRPC");
563563
return;
564564
}
565-
566-
for (int i = 0; i < clientIds.Count; i++)
565+
566+
using (BitWriter rpcWriter = BitWriter.Get())
567567
{
568-
if (isHost && clientIds[i] == NetworkingManager.singleton.LocalClientId)
568+
writer.WriteUInt(networkId);
569+
writer.WriteUShort(networkedObject.GetOrderIndex(this));
570+
writer.WriteULong(hash);
571+
572+
writer.WriteWriter(writer);
573+
574+
for (int i = 0; i < clientIds.Count; i++)
569575
{
570-
using (BitReader reader = BitReader.Get(writer.Finalize()))
576+
if (isHost && clientIds[i] == NetworkingManager.singleton.LocalClientId)
571577
{
572-
InvokeClientRPCLocal(hash, NetworkingManager.singleton.LocalClientId, reader);
578+
using (BitReader reader = BitReader.Get(writer.Finalize()))
579+
{
580+
InvokeClientRPCLocal(hash, NetworkingManager.singleton.LocalClientId, reader);
581+
}
582+
}
583+
else
584+
{
585+
InternalMessageHandler.Send(clientIds[i], "MLAPI_CLIENT_RPC", "MLAPI_USER_CHANNEL", rpcWriter);
573586
}
574587
}
575-
else
588+
}
589+
}
590+
591+
internal void SendClientRPCPerformance(ulong hash, uint clientId, BitWriter writer)
592+
{
593+
Type type = GetType(); //This is cached by CLR
594+
595+
if (!isServer)
596+
{
597+
//We are NOT a server.
598+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Only clients and host can invoke ClientRPC");
599+
return;
600+
}
601+
602+
using (BitWriter rpcWriter = BitWriter.Get())
603+
{
604+
writer.WriteUInt(networkId);
605+
writer.WriteUShort(networkedObject.GetOrderIndex(this));
606+
writer.WriteULong(hash);
607+
608+
writer.WriteWriter(writer);
609+
610+
if (isHost && clientId == NetworkingManager.singleton.LocalClientId)
576611
{
577-
using (BitWriter rpcWriter = BitWriter.Get())
612+
using (BitReader reader = BitReader.Get(writer.Finalize()))
578613
{
579-
writer.WriteUInt(networkId);
580-
writer.WriteUShort(networkedObject.GetOrderIndex(this));
581-
writer.WriteULong(hash);
582-
583-
writer.WriteWriter(writer);
584-
585-
InternalMessageHandler.Send(clientIds[i], "MLAPI_CLIENT_RPC", "MLAPI_USER_CHANNEL", rpcWriter);
614+
InvokeClientRPCLocal(hash, NetworkingManager.singleton.LocalClientId, reader);
586615
}
587616
}
588-
}
617+
else
618+
{
619+
InternalMessageHandler.Send(clientId, "MLAPI_CLIENT_RPC", "MLAPI_USER_CHANNEL", rpcWriter);
620+
}
621+
}
589622
}
590623
#endregion
591624

@@ -596,48 +629,65 @@ internal void SendClientRPCPerformance(ulong hash, List<uint> clientIds, BitWri
596629
public delegate void Action<T1, T2, T3, T4, T5, T6, T7, T8>(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8);
597630
public delegate void Action<T1, T2, T3, T4, T5, T6, T7, T8, T9>(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9);
598631
public delegate void Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10);
599-
600-
//BOXED
601-
public void InvokeServerRPC(Action method)
632+
633+
//BOXED SERVER RPC
634+
public void InvokeServerRpc(string methodName, params object[] parameters)
635+
{
636+
SendServerRPCBoxed(HashMethodName(methodName), parameters);
637+
}
638+
639+
public void InvokeServerRpc(Action method)
602640
{
603641
SendServerRPCBoxed(HashMethodName(method.Method.Name));
604642
}
605643

606-
public void InvokeServerRPC<T1>(Action<T1> method, T1 t1)
644+
public void InvokeServerRpc<T1>(Action<T1> method, T1 t1)
607645
{
608646
SendServerRPCBoxed(HashMethodName(method.Method.Name), t1);
609647
}
610-
611-
public void InvokeClientRPC(Action method, List<uint> clientIds)
648+
649+
public void InvokeServerRpc<T1>(Action<T1> method, T1 t1)
650+
{
651+
SendServerRPCBoxed(HashMethodName(method.Method.Name), t1);
652+
}
653+
654+
//BOXED CLIENT RPC
655+
public void InvokeClientRpc(Action method, List<uint> clientIds)
612656
{
613657
SendClientRPCBoxed(HashMethodName(method.Method.Name), clientIds);
614658
}
615659

616-
public void InvokeClientRPC<T1>(Action<T1> method, List<uint> clientIds, T1 t1)
660+
public void InvokeClientRpc<T1>(Action<T1> method, List<uint> clientIds, T1 t1)
617661
{
618662
SendClientRPCBoxed(HashMethodName(method.Method.Name), clientIds, t1);
619663
}
620664

621-
//Performance
622-
public void InvokeServerRPC(RpcDelegate method, BitWriter writer)
665+
//PERFORMANCE SERVER RPC
666+
public void InvokeServerRpc(RpcDelegate method, BitWriter writer)
623667
{
624668
SendServerRPCPerformance(HashMethodName(method.Method.Name), writer);
625669
}
626-
627-
public void InvokeClientRPC(RpcDelegate method, BitWriter writer)
670+
671+
public void InvokeServerRpc(string methodName, BitWriter writer)
628672
{
629-
SendServerRPCPerformance(HashMethodName(method.Method.Name), writer);
673+
SendServerRPCPerformance(HashMethodName(methodName), writer);
630674
}
631-
632-
public void InvokeServerRPC(string methodName, BitWriter writer)
675+
676+
//PERFORMANCE CLIENT RPC
677+
public void InvokeClientRpc(RpcDelegate method, BitWriter writer)
633678
{
634-
SendServerRPCPerformance(HashMethodName(methodName), writer);
679+
SendServerRPCPerformance(HashMethodName(method.Method.Name), writer);
635680
}
636681

637-
public void InvokeClientRPC(string methodName, List<uint> clientIds, BitWriter writer)
682+
public void InvokeClientRpc(string methodName, List<uint> clientIds, BitWriter writer)
638683
{
639684
SendClientRPCPerformance(HashMethodName(methodName), clientIds, writer);
640685
}
686+
687+
public void InvokeClientRpcOnOwner(string methodName, BitWriter writer)
688+
{
689+
SendClientRPCPerformance(HashMethodName(methodName), OwnerClientId, writer);
690+
}
641691
#endregion
642692

643693
/// <summary>

0 commit comments

Comments
 (0)