Ejemplo n.º 1
0
        /// <summary>
        /// 发送消息并且等待回调
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="eventId"></param>
        /// <param name="channelId"></param>
        /// <param name="value"></param>
        /// <param name="sendType"></param>
        /// <param name="callBack"></param>
        public void Request <T>(HandlerConst.RequestId eventId, int channelId, T value, SendType sendType,
                                Action <Package> callBack, Action timeoutAct = null, float timeout = 2)
        {
            if (isConnect == false)
            {
                Debug.logger.LogError("NetPost", "服务器未连接");
                return;
            }
            Package pkg = Package.MakePakage <T>((int)eventId, channelId, value, sendType);

            if (sendType == SendType.TCP)
            {
                tcpSocket.Send(pkg.totalData);
            }
            else
            {
                udpSocket.Send(pkg.totalData, pkg.totalLength);
            }

            //如果没有回调函数则直接返回
            if (callBack == null)
            {
                return;
            }

            EventDispatcher.AddEventListener <Package>(ServerEvent.GetResponseEvent(pkg.requestId), HandleRequeset, this);
            responseHandlerPool.Add(pkg.requestId, callBack);
            //超时检查
            CoroutineTaskManager.Instance.WaitSecondTodo(() =>
            {
                if (responseHandlerPool.ContainsKey(pkg.requestId))
                {
                    EventDispatcher.RemoveEventListener <Package>(ServerEvent.GetResponseEvent(pkg.requestId), HandleRequeset, this);
                    responseHandlerPool.Remove(pkg.requestId);
                    Debug.logger.LogError("NetPost", "请求超时,请求Id为" + pkg.requestId + " 处理Id为" + EnumEx.GetValue <HandlerConst.RequestId>(pkg.eventId));
                    if (timeoutAct != null)
                    {
                        timeoutAct();
                    }
                }
            }, timeout);
            ;
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 处理包行为,分配给相应的Handler
 /// </summary>
 /// <param name="package"></param>
 /// <param name="act"></param>
 public static void HandlePackage(BaseServer server, Package package, Action act = null)
 {
     //触发响应事件(针对有响应的请求)
     HandlerConst.RequestId id = EnumEx.GetValue <HandlerConst.RequestId>(package.eventId);
     if (HandlerConst.handlerDict.ContainsKey(id))
     {
         HandlerConst.handlerDict[id].ownerServer = server;
         HandlerConst.handlerDict[id].Handle(package, act);
     }
     else
     {
         Debug.logger.Log("不存在id:" + id.ToString());
     }
     EventDispatcher.TriggerEvent <Package>(ServerEvent.GetResponseEvent(package.requestId), package, server);
     if (act != null)
     {
         act();
     }
 }
Ejemplo n.º 3
0
 //回调
 private void HandleRequeset(Package pkg)
 {
     EventDispatcher.RemoveEventListener <Package>(ServerEvent.GetResponseEvent(pkg.requestId), HandleRequeset, this);
     responseHandlerPool[pkg.requestId](pkg);
     responseHandlerPool.Remove(pkg.requestId);
 }