/// <summary> /// Replaces a handler for a particular message type. /// <para>See also <see cref="RegisterHandler{T}(Action{NetworkConnection, T}, bool)">RegisterHandler(T)(Action(NetworkConnection, T), bool)</see></para> /// </summary> /// <typeparam name="T">Message type</typeparam> /// <param name="handler">Function handler which will be invoked when this message type is received.</param> /// <param name="requireAuthentication">True if the message requires an authenticated connection</param> public static void ReplaceHandler <T>(Action <NetworkConnection, T> handler, bool requireAuthentication = true) where T : struct, NetworkMessage { int msgType = MessagePacking.GetId <T>(); handlers[msgType] = MessagePacking.WrapHandler(handler, requireAuthentication); }
/// <summary> /// Unregisters a network message handler. /// </summary> /// <typeparam name="T">The message type to unregister.</typeparam> public static bool UnregisterHandler <T>() where T : struct, NetworkMessage { // use int to minimize collisions int msgType = MessagePacking.GetId <T>(); return(handlers.Remove(msgType)); }
/// <summary> /// Register a handler for a particular message type. /// <para>There are several system message types which you can add handlers for. You can also add your own message types.</para> /// </summary> /// <typeparam name="T">Message type</typeparam> /// <param name="handler">Function handler which will be invoked when this message type is received.</param> /// <param name="requireAuthentication">True if the message requires an authenticated connection</param> public static void RegisterHandler <T>(Action <NetworkConnection, T> handler, bool requireAuthentication = true) where T : struct, NetworkMessage { int msgType = MessagePacking.GetId <T>(); if (handlers.ContainsKey(msgType)) { Debug.LogWarning($"NetworkClient.RegisterHandler replacing handler for {typeof(T).FullName}, id={msgType}. If replacement is intentional, use ReplaceHandler instead to avoid this warning."); } handlers[msgType] = MessagePacking.WrapHandler(handler, requireAuthentication); }