Skip to content

Commit

Permalink
feat: throw exception if invalid rpc (#456)
Browse files Browse the repository at this point in the history
* feat: throw exception if invalid rpc

* fix tests
  • Loading branch information
paulpach committed Nov 2, 2020
1 parent 5ba122d commit 3cef90d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
8 changes: 4 additions & 4 deletions Assets/Mirror/Runtime/NetworkBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ protected void SendServerRpcInternal(Type invokeClass, string cmdName, NetworkWr
/// <param name="reader">Parameters to pass to the ServerRpc.</param>
/// <returns>Returns true if successful.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual bool InvokeServerRpc(int cmdHash, NetworkReader reader)
public virtual void InvokeServerRpc(int cmdHash, NetworkReader reader)
{
return RemoteCallHelper.InvokeSkeleton(cmdHash, MirrorInvokeType.ServerRpc, reader, this);
RemoteCallHelper.InvokeSkeleton(cmdHash, MirrorInvokeType.ServerRpc, reader, this);
}
#endregion

Expand Down Expand Up @@ -332,9 +332,9 @@ protected internal void SendTargetRpcInternal(INetworkConnection conn, Type invo
/// <param name="reader">Parameters to pass to the RPC function.</param>
/// <returns>Returns true if successful.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual bool InvokeRpc(int rpcHash, NetworkReader reader)
public virtual void InvokeRpc(int rpcHash, NetworkReader reader)
{
return RemoteCallHelper.InvokeSkeleton(rpcHash, MirrorInvokeType.ClientRpc, reader, this);
RemoteCallHelper.InvokeSkeleton(rpcHash, MirrorInvokeType.ClientRpc, reader, this);
}
#endregion

Expand Down
5 changes: 1 addition & 4 deletions Assets/Mirror/Runtime/NetworkIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -941,10 +941,7 @@ internal void HandleRemoteCall(int componentIndex, int functionHash, MirrorInvok
if (componentIndex >= 0 && componentIndex < NetworkBehaviours.Length)
{
NetworkBehaviour invokeComponent = NetworkBehaviours[componentIndex];
if (!RemoteCallHelper.InvokeSkeleton(functionHash, invokeType, reader, invokeComponent, senderConnection))
{
logger.LogError($"Found no receiver for incoming {invokeType} [{functionHash}] on {gameObject}, the server and client should have the same NetworkBehaviour instances [netId={NetId}].");
}
RemoteCallHelper.InvokeSkeleton(functionHash, invokeType, reader, invokeComponent, senderConnection);
}
else
{
Expand Down
20 changes: 7 additions & 13 deletions Assets/Mirror/Runtime/RemoteCallHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,38 +138,32 @@ static Skeleton GetSkeleton(int cmdHash, MirrorInvokeType invokeType)
return invoker;
}

// debug message if not found, or null, or mismatched type
// (no need to throw an error, an attacker might just be trying to
// call an cmd with an rpc's hash)
if (logger.LogEnabled()) logger.Log("Skeleton for hash:" + cmdHash + " not found");

return null;
throw new MethodInvocationException($"No RPC method found for hash {cmdHash}");
}

// InvokeCmd/Rpc can all use the same function here
internal static bool InvokeSkeleton(int cmdHash, MirrorInvokeType invokeType, NetworkReader reader, NetworkBehaviour invokingType, INetworkConnection senderConnection = null)
internal static void InvokeSkeleton(int cmdHash, MirrorInvokeType invokeType, NetworkReader reader, NetworkBehaviour invokingType, INetworkConnection senderConnection = null)
{
Skeleton invoker = GetSkeleton(cmdHash, invokeType);
if (invoker != null && invoker.invokeClass.IsInstanceOfType(invokingType))
if (invoker.invokeClass.IsInstanceOfType(invokingType))
{
invoker.invokeFunction(invokingType, reader, senderConnection);

return true;
return;
}
return false;
throw new MethodInvocationException($"No RPC method found for hash {cmdHash}");
}

internal static ServerRpcInfo GetServerRpcInfo(int cmdHash, NetworkBehaviour invokingType)
{
Skeleton invoker = GetSkeleton(cmdHash, MirrorInvokeType.ServerRpc);
if (invoker != null && invoker.invokeClass.IsInstanceOfType(invokingType))
if (invoker.invokeClass.IsInstanceOfType(invokingType))
{
return new ServerRpcInfo
{
requireAuthority = invoker.cmdRequireAuthority
};
}
return default;
throw new MethodInvocationException($"No RPC method found for hash {cmdHash}");
}

/// <summary>
Expand Down

0 comments on commit 3cef90d

Please sign in to comment.