示例#1
0
        public bool Request <TRequest, TResponse>(NetworkConnection conn, TRequest request, Action <NetworkConnection, TResponse> callback, Action <NetworkConnection, ErrorMessage> errorCallback, double timeOut) where TRequest : IRequestMessage where TResponse : IResponseMessage
        {
            if (callback != null || errorCallback != null)
            {
                QueryResponsePublisher pub;

                if (!queryHandlers.TryGetValue(request.GetId, out pub))
                {
                    pub = new QueryResponsePublisher();

                    Manager.AddListener <TResponse>((c, r) =>
                    {
                        pub.PublishQueryResponse(c, r.QueryId, r);
                    });

                    if (!queryHandlers.TryAdd(request.GetId, pub))
                    {
                        Logger.Error("Can't add response publisher to queryHandlers : " + request.GetId);
                    }
                }

                request.QueryId = TypeIdProvider.GetNextQueryId();

                var queryCallback = new QueryCallback <IResponseMessage>((c, m) => callback.Invoke(c, (TResponse)m), errorCallback, Time.time + timeOut);

                pub.AddCallback(request.QueryId, queryCallback);
            }
            else
            {
                request.QueryId = 0;
            }

            return(Manager.Send(conn, request));
        }
示例#2
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;
                }
            }
        }
示例#3
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;
                }
            }
        }