feat(NetworkClient): Add ReplaceHandler with channel param (#3747)

* breaking(NetworkClient): Remove NetworkConnection parameter from ReplaceHandler
There is only one connection on client.
Aligns with RegisterHandler that takes no NetworkConnection parameter.

* feat(NetworkClient): Add ReplaceHandler with channel param
This commit is contained in:
MrGadget 2024-01-22 04:45:32 -05:00 committed by GitHub
parent 4c0d979d4d
commit 50d0008d03
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -552,14 +552,17 @@ public static void RegisterHandler<T>(Action<T, int> handler, bool requireAuthen
handlers[msgType] = NetworkMessages.WrapHandler((Action<NetworkConnection, T, int>)HandlerWrapped, requireAuthentication, exceptionsDisconnect);
}
/// <summary>Replace a handler for a particular message type. Should require authentication by default.</summary>
// RegisterHandler throws a warning (as it should) if a handler is assigned twice
// Use of ReplaceHandler makes it clear the user intended to replace the handler
// Deprecated 2024-01-21
[Obsolete("Use ReplaceHandler without the NetworkConnection parameter instead. This version is obsolete and will be removed soon.")]
public static void ReplaceHandler<T>(Action<NetworkConnection, T> handler, bool requireAuthentication = true)
where T : struct, NetworkMessage
{
// we use the same WrapHandler function for server and client.
// so let's wrap it to ignore the NetworkConnection parameter.
// it's not needed on client. it's always NetworkClient.connection.
ushort msgType = NetworkMessageId<T>.Id;
handlers[msgType] = NetworkMessages.WrapHandler(handler, requireAuthentication, exceptionsDisconnect);
void HandlerWrapped(NetworkConnection _, T value) => handler(_, value);
handlers[msgType] = NetworkMessages.WrapHandler((Action<NetworkConnection, T>)HandlerWrapped, requireAuthentication, exceptionsDisconnect);
}
/// <summary>Replace a handler for a particular message type. Should require authentication by default.</summary>
@ -568,7 +571,26 @@ public static void ReplaceHandler<T>(Action<NetworkConnection, T> handler, bool
public static void ReplaceHandler<T>(Action<T> handler, bool requireAuthentication = true)
where T : struct, NetworkMessage
{
ReplaceHandler((NetworkConnection _, T value) => { handler(value); }, requireAuthentication);
// we use the same WrapHandler function for server and client.
// so let's wrap it to ignore the NetworkConnection parameter.
// it's not needed on client. it's always NetworkClient.connection.
ushort msgType = NetworkMessageId<T>.Id;
void HandlerWrapped(NetworkConnection _, T value) => handler(value);
handlers[msgType] = NetworkMessages.WrapHandler((Action<NetworkConnection, T>)HandlerWrapped, requireAuthentication, exceptionsDisconnect);
}
/// <summary>Replace a handler for a particular message type. Should require authentication by default. This version passes channelId to the handler.</summary>
// RegisterHandler throws a warning (as it should) if a handler is assigned twice
// Use of ReplaceHandler makes it clear the user intended to replace the handler
public static void ReplaceHandler<T>(Action<T, int> handler, bool requireAuthentication = true)
where T : struct, NetworkMessage
{
// we use the same WrapHandler function for server and client.
// so let's wrap it to ignore the NetworkConnection parameter.
// it's not needed on client. it's always NetworkClient.connection.
ushort msgType = NetworkMessageId<T>.Id;
void HandlerWrapped(NetworkConnection _, T value, int channelId) => handler(value, channelId);
handlers[msgType] = NetworkMessages.WrapHandler((Action<NetworkConnection, T, int>)HandlerWrapped, requireAuthentication, exceptionsDisconnect);
}
/// <summary>Unregister a message handler of type T.</summary>