예제 #1
0
        public static void RequestAsync(SimpleProtocolStruct request, NetHandler callBack)
        {
            if (null == s_netInstace)
            {
                return;
            }
            var taskInfo = new NetTaskInfo(request.RequestID, callBack);

            lock (s_responseHandlerDic)
            {
                SAssert.MustTrue(!s_responseHandlerDic.ContainsKey(request.RequestID),
                                 string.Format("出现重复ID请求!!请求参数:{0}", request));

                s_responseHandlerDic.Add(request.RequestID, taskInfo);
            }

            s_netInstace.SendPacket(request);
        }
예제 #2
0
        private static NetHandleResult DefaultNetResponseHandler(SimpleProtocolStruct response, IPEndPoint endpoint)
        {
            var method = response.RequestMethod;

            var requestID = response.RequestID;

            /*
             * SAssert.MustTrue(s_responseMethodHandlerDic.ContainsKey(method),
             *  string.Format("Request {0} has no handler!", method));
             */

            NetTaskInfo taskInfo = null;

            lock (s_responseHandlerDic)
            {
                if (!s_responseHandlerDic.ContainsKey(requestID))
                {
                    return(new NetHandleResult(NetHandleResultCode.Failed,
                                               string.Format("请求ID = {0}没找到处理函数,可能是超时导致,请求参数为:{1}", requestID, response)));
                }

                taskInfo = s_responseHandlerDic[requestID];

                s_responseHandlerDic.Remove(requestID);
            }

            if (taskInfo != null)
            {
                var handler = taskInfo.Handler;

                return(handler.Invoke(response, endpoint));
            }
            else
            {
                return(new NetHandleResult(NetHandleResultCode.Failed,
                                           string.Format("请求ID = {0}没找到处理函数,可能是超时导致,请求参数为:{1}", requestID, response)));
            }
        }