/// <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(); } }
/// <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); ; }
/// <summary> /// 发送消息并且等待回调 /// </summary> /// <param name="eventId"></param> /// <param name="channelId"></param> /// <param name="sendType"></param> /// <param name="callBack"></param> /// <param name="timeout"></param> public void Request(HandlerConst.RequestId eventId, int channelId, SendType sendType, Action <Package> callBack, Action timeoutAct = null, float timeout = 2) { Request <System.Object>(eventId, channelId, null, sendType, callBack, timeoutAct, timeout); }
public void Send <T>(HandlerConst.RequestId eventId, int channelId, T value, SendType sendType) { Send <T>((int)eventId, channelId, value, sendType); }
/// <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> /// <param name="timeoutAct"></param> /// <param name="timeout"></param> public void Request <T>(HandlerConst.RequestId eventId, T value, Action <Package> callBack, SendType sendType = SendType.TCP, Action timeoutAct = null, float timeout = 2) { currentServer.Request <T>(eventId, currentSceneId, value, sendType, callBack, timeoutAct, timeout); }
/// <summary> /// 向场景服务端发送消息 /// </summary> /// <param name="eventId"></param> /// <param name="value"></param> /// <param name="sendType"></param> public void SendData <T>(HandlerConst.RequestId eventId, T value, SendType sendType = SendType.TCP) { currentServer.Send <T>(eventId, currentSceneId, value, sendType); }
/// <summary> /// 获取处理事件的名字 /// </summary> /// <param name="instanceId"></param> /// <param name="handlerId"></param> /// <returns></returns> public static string GetNetBehaviorEventName(HandlerConst.RequestId handlerId) { return("NetBehavior" + "_" + handlerId); }