Пример #1
0
        public void RemoveListener <T>(Action <NetworkConnection, T> listener) where T : IMessage
        {
            ushort msgId = TypeIdProvider.GetId <T>();

            //We need to lock because we will do multiple actions with the dict
            lock (_lock)
            {
                IMessageDeserializer handler;
                if (Handlers.TryGetValue(msgId, out handler))
                {
                    (handler as IMessagePublisher <T>).Listeners -= listener;
                }
            }
        }
Пример #2
0
        public void AddListener <T>(Action <NetworkConnection, T> listener) where T : IMessage
        {
            ushort msgId = TypeIdProvider.GetId <T>();

            //We need to lock because we will do multiple actions with the dict
            lock (_lock)
            {
                IMessageDeserializer handler;
                if (!Handlers.TryGetValue(msgId, out handler))
                {
                    //No message message registered yet, adding a new invoker
                    handler         = new MessageHandler <T>(Serializer, listener);
                    Handlers[msgId] = handler;
                }
                else
                {
                    (handler as IMessagePublisher <T>).Listeners += listener;
                }
            }
        }