/// <summary> /// 发送协议且侦听回调 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="cmdId"></param> /// <param name="message"></param> /// <param name="callback"></param> /// <param name="displayTip"></param> /// <param name="timeoutCallback"></param> /// <param name="timeout"></param> /// <param name="connID"></param> /// <returns></returns> public bool Send <T>(uint cmdId , T message , NetworkMessageHandle callback , bool displayTip = false , TimerEntity.TimerEntityCallBack timeoutCallback = null , double timeout = 10 , int connID = -1 , string tag = "None") where T : IExtensible { #if UNITY_EDITOR && KHNET_DEBUG if (typeProvider_D.ContainsKey(cmdId)) { if (callback != null) { object mockresp = getRespByType(typeProvider_D[cmdId]); if (mockresp != null) { callback(mockresp); return(false); //模拟回包 } else { UnityEngine.Debuger.LogError("数据实体未定义"); } } } #endif return(SendMessage(cmdId, message, callback, displayTip, timeoutCallback, timeout, connID, tag)); }
public bool SendWithSerial(uint cmdId , IExtensible message , NetworkMessageHandle callback , bool displayTip = false , TimerEntity.TimerEntityCallBack timeoutCallback = null , double timeout = 10 , int connID = -1 , uint serial = 0 , string tag = "None") { bool result = proxy.Write(cmdId, message, serial, connID); if (!result) { ///发送失败直接超时回调 if (timeoutCallback != null) { DelayTimeoutCallbackData timeoutData = new DelayTimeoutCallbackData(timeoutCallback, serial); NetworkManager.timeoutCallbackDataList.Add(timeoutData); router.OnTimeoutConnectCheck(); } } else { router.AddUnicastCallback(cmdId, serial, callback , displayTip, timeoutCallback, timeout , connID, tag, PBTYPE.CSharpPB); } return(result); }
public void SendFake <T, TRsp>(uint cmdId, T message, NetworkMessageHandle callback, TRsp messageRsp , int connID = -1 ) where T : IExtensible { //Send<T>(cmdId, message, callback, true , _default_timeout_func,10,connID); callback(messageRsp); }
public bool SendMessage(uint cmdId , IExtensible message , NetworkMessageHandle callback , bool displayTip = false , TimerEntity.TimerEntityCallBack timeoutCallback = null , double timeout = 10 , int connID = -1 , string tag = "None") { uint serial = router.NextSerial(cmdId, tag); bool result = true; // Update by Chicheng MessageManager msgManager = MessageManager.Instance; // 模拟服务器,从本地读包 if (msgManager.IsActivate && !msgManager.IsSerializeToLocal) { // 反序列化之后的结果 List <object> messagesBody = msgManager.deserializeFromLocal(cmdId); try { __Proxy.__QueueAddMessage(cmdId, serial, messagesBody); } catch (NullReferenceException) { return(false); } } else { result = proxy.Write(cmdId, message, serial, connID); } if (!result) { ///发送失败直接超时回调 if (timeoutCallback != null) { DelayTimeoutCallbackData timeoutData = new DelayTimeoutCallbackData(timeoutCallback, serial); NetworkManager.timeoutCallbackDataList.Add(timeoutData); router.OnTimeoutConnectCheck(); } } else { router.AddUnicastCallback(cmdId, serial, callback , displayTip, timeoutCallback, timeout , connID, tag, PBTYPE.CSharpPB); } return(result); }
/// <summary> /// 共Lua使用的非泛型方法 /// 返回值为 0 说明是发送失败,否则返回 serial /// </summary> public SendUnicastRetInfo SendUnicast(uint cmdId, object message, NetworkMessageHandle callback, bool displayTip = false, TimerEntity.TimerEntityCallBack timeoutCallback = null, double timeout = 10, int connID = -1, string tag = "None") { uint serial = router.NextSerial(cmdId, tag); bool result = true; // Update by Chicheng MessageManager msgManager = MessageManager.Instance; // 模拟服务器,从本地读包 if (msgManager.IsActivate && !msgManager.IsSerializeToLocal) { // Debug.LogWarning("模拟服务器,从本地读包Lua"); // Lua反序列化之后的结果 try { List <object> messagesBody = msgManager.deserializeFromLocal(cmdId); if (messagesBody != null) { __Proxy.__QueueAddMessage(cmdId, serial, messagesBody); //__Proxy.AddMessage(cmdId, serial, messagesBody[0]); } } catch (Exception) { Debug.LogWarning("Lua这里出错"); } } else { result = proxy.Write(cmdId, message, serial, connID); } if (result) { PBTYPE pbtype = PBTYPE.None; if (message is byte[]) { pbtype = PBTYPE.LuaPB; } else { pbtype = PBTYPE.CSharpPB; } router.AddUnicastCallback(cmdId, serial, callback , displayTip , timeoutCallback , timeout , connID , tag , pbtype); m_retInfo.SetData(true, serial); } else { if (timeoutCallback != null) { DelayTimeoutCallbackData timeoutData = new DelayTimeoutCallbackData(timeoutCallback, serial); NetworkManager.timeoutCallbackDataList.Add(timeoutData); router.OnTimeoutConnectCheck(); } m_retInfo.SetData(false, serial); } return(m_retInfo); }
/// <summary> /// 去除一个回调 /// </summary> /// <param name="cmdId">触发回调的CmdId</param> /// <param name="handle">被触发的回调</param> /// <param name="inMainThread">回调是否应当在主线程调用</param> public void RemoveMessageCallback(uint cmdId, NetworkMessageHandle handle) { router.RemoveMessageCallback(cmdId, handle); }
/// <summary> /// 添加一个回调 /// </summary> /// <param name="cmdId">触发回调的CmdId</param> /// <param name="handle">被触发的回调</param> /// <param name="inMainThread">回调是否应当在主线程调用</param> public void AddMessageCallback(uint cmdId, NetworkMessageHandle handle) { router.AddMessageCallback(cmdId, handle); }