Beispiel #1
0
        // ######################## FUNCTIONALITY ######################## //
        /// <summary>
        /// Register a Handler
        /// </summary>
        /// <typeparam name="T">A Network Message Type. Needs to be derived from MessageBase</typeparam>
        /// <param name="server">Register on server or on client?</param>
        /// <param name="msgType">Message type number</param>
        /// <param name="handler">Function handler which will be invoked when this type of message is recieved</param>
        /// <param name="client">If registering on a client, this is the client to register to</param>
        private static void RegisterHandler <T>(bool server, short msgType, ReceivedNetworkMessageDelegate <T> handler, NetworkClient client = null) where T : MessageBase, new()
        {
            // go through all handler containers and see if we allready have one for this msgType
            foreach (HandlerContainerBase container in _handlerContainers)
            {
                if (container.MessageId == msgType) // If we found one, register the handler function
                {
                    if (!server && container.Client != client)
                    {
                        continue;
                    }

                    HandlerContainer <T> c = (HandlerContainer <T>)container;
                    c.OnMessageReceived += handler;
                    return;
                }
            }

            // if we reach this it means there is no container for this type of message yet, so we need to add one
            HandlerContainer <T> newContainer = new HandlerContainer <T>(msgType, server, client);

            newContainer.OnMessageReceived += handler;

            _handlerContainers.Add(newContainer);
        }
Beispiel #2
0
            /// <summary>
            /// Removes the provided delegate from the OnMessageReceived Event
            /// </summary>
            /// <param name="del">Delegate to remove</param>
            public bool UnregisterDelegate(ReceivedNetworkMessageDelegate <T> del)
            {
                if (OnMessageReceived.GetInvocationList().Search(del) != -1)
                {
                    OnMessageReceived -= del;
                    return(true);
                }

                return(false);
            }
Beispiel #3
0
        /// <summary>
        /// Register a Handler for messages send to the client
        /// </summary>
        /// <typeparam name="T">A Network Message Type. Needs to be derived from MessageBase</typeparam>
        /// <param name="msgType">Message type number</param>
        /// <param name="handler">unction handler which will be invoked when this type of message is recieved</param>
        public static void RegisterHandlerClient <T>(short msgType, ReceivedNetworkMessageDelegate <T> handler) where T : MessageBase, new()
        {
            if (!NetworkClient.active)
            {
                Debug.LogError("Cannot Register a Handler on the Client because the this is not a Client");
                return;
            }
            //===================== code after this is only run on client ====================

            RegisterHandler <T>(false, msgType, handler, NetworkClient.allClients[0]);
        }
Beispiel #4
0
        /// <summary>
        /// Register a Handler for messages send to the server
        /// </summary>
        /// <typeparam name="T">A Network Message Type. Needs to be derived from MessageBase</typeparam>
        /// <param name="msgType">Message type number</param>
        /// <param name="handler">unction handler which will be invoked when this type of message is recieved</param>
        public static void RegisterHandlerServer <T>(short msgType, ReceivedNetworkMessageDelegate <T> handler) where T : MessageBase, new()
        {
            if (!NetworkServer.active)
            {
                Debug.LogError("Cannot Register a Handler on the Server because the this is not a Server");
                return;
            }
            //===================== code after this is only run on server ====================

            RegisterHandler <T>(true, msgType, handler);
        }
Beispiel #5
0
        /// <summary>
        /// Unregister a Handler
        /// </summary>
        /// <typeparam name="T">A Network Message Type. Needs to be derived from MessageBase</typeparam>
        /// <param name="msgType">Message type number</param>
        /// <param name="handler">Delegate to unregister</param>
        public static void UnregisterHandler <T>(short msgType, ReceivedNetworkMessageDelegate <T> handler) where T : MessageBase, new()
        {
            // go through all handler containers and look for the handler
            foreach (HandlerContainerBase container in _handlerContainers)
            {
                if (container.MessageId == msgType) // if we found the right handler container, unregister the delegate
                {
                    HandlerContainer <T> c = (HandlerContainer <T>)container;
                    if (!c.UnregisterDelegate(handler))
                    {
                        continue;
                    }
                    return;
                }
            }

            Debug.LogWarningFormat("Could not find {0} in registered handlers", handler.Method.Name);
        }