/// <summary> /// 发送消息给Server /// </summary> /// <param name="netMessage">对应的消息类</param> /// <returns></returns> protected bool _SendMessage(CSLib.Framework.CNetMessage netMessage) { if (netMessage == null) { UDLib.Utility.CDebugOut.LogError("SendMessage : netMessage == null"); return(false); } if (m_tcpClient == null) { UDLib.Utility.CDebugOut.LogError("SendMessage : m_tcpClient == null"); return(false); } if (m_tcpClient.UseEchoID) { CSLib.Framework.CEchoMessage echoMessage = (CSLib.Framework.CEchoMessage)netMessage; echoMessage.EchoID = m_uEchoID; } //UnityEngine.Profiling.Profiler.BeginSample("CMoniBehaviour > _SendMessage 1"); CSLib.Utility.CStream msgStream = new CSLib.Utility.CStream(); netMessage.Serialize(msgStream); //UnityEngine.Profiling.Profiler.EndSample(); //UnityEngine.Profiling.Profiler.BeginSample("CMoniBehaviour > _SendMessage 2"); bool result = m_tcpClient.SendAsync(msgStream); //UnityEngine.Profiling.Profiler.EndSample(); if (result) { // 禁止操作 //UDLib.Utility.CDebugOut.LogError("<Trace> 有消息还没处理完,等待ing"); } return(result); }
/// <summary> /// 处理消息 /// 逻辑层需要在Update里调用这个接口,以处理接收消息 /// </summary> public void ExecuteMessages() { if (m_tcpClient == null) { return; } //UnityEngine.Profiling.Profiler.BeginSample("CMoniBehaviour > ExecuteMessages"); CSLib.Framework.CMsgBuffInfoQueue msgBuffInfoQueue = m_tcpClient.GetMsgBuffInfoQueue(m_uEchoID); while (msgBuffInfoQueue.Count > 0) { CSLib.Framework.CMessageLabel msgLabel = new CSLib.Framework.CMessageLabel(); CSLib.Framework.CMsgBuffInfo msgBuffInfo = msgBuffInfoQueue.Dequeue(); // 执行后 msgLabel.Id 才会被赋值 if (!ExecuteMessage(msgLabel, msgBuffInfo.MsgBuff, msgBuffInfo.MsgSize)) { UDLib.Utility.CDebugOut.LogError("ExecuteMessages : 消息执行错误"); } #if DEBUG CSLib.Framework.CNetMessage msgResult = (CSLib.Framework.CNetMessage)msgLabel.MsgObject; byte tmpServer = CSLib.Utility.CBitHelper.GetHighUInt8(msgResult.MsgType); byte tmpFunc = CSLib.Utility.CBitHelper.GetLowUInt8(msgResult.MsgType); m_recvStatisticsNum.AddNum(tmpServer.ToString() + " -> " + tmpFunc.ToString()); #endif } //UnityEngine.Profiling.Profiler.EndSample(); }
// 发送request消息的时候把消息加入队列 public bool InsertCacheMessage(CSLib.Framework.CNetMessage message) { lock (msgLock) { #if DEBUG UDLib.Utility.CDebugOut.Log("发送消息时本地缓存消息以便超时重发 > reqIndex : " + message.GetReqIndex()); #endif ushort reqIndex = message.GetReqIndex(); if (reqIndex == 0) { return(false); } if (msgDictCache.ContainsKey(reqIndex)) { msgDictCache.Remove(reqIndex); } var obj = m_messagePool.Obtain(); obj.timeStamp = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; obj.netMessage = message; obj.sentCount = 0; msgDictCache.Add(reqIndex, obj); return(true); } }
// 发送request消息的时候把消息加入队列 public bool InsertCacheMessage(CSLib.Framework.CNetMessage message) { if (msgDict.ContainsKey(message)) { msgDict.Remove(message); } msgDict.Add(message, DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond); return(true); }
/// <summary> /// 发送消息给Server /// </summary> /// <param name="netMessage">对应的消息类</param> /// <returns></returns> public virtual bool SendMessage(CSLib.Framework.CNetMessage msgRequest) { if (CReconnectMgr.Instance.SimulateSocketClose) { UDLib.Utility.CDebugOut.Log("客户端模拟断网"); m_tcpClient.Close(); m_tcpClient.CbTerminate(); return(false); } if (msgRequest == null) { UDLib.Utility.CDebugOut.LogError("SendMessage : msgRquest == null"); return(false); } if (m_tcpClient == null) { UDLib.Utility.CDebugOut.LogError("SendMessage : m_tcpClient == null"); return(false); } //UnityEngine.Profiling.Profiler.BeginSample("CMoniBehaviour > SendMessage"); { byte tmpServer = CSLib.Utility.CBitHelper.GetHighUInt8(msgRequest.MsgType); byte tmpFunc = CSLib.Utility.CBitHelper.GetLowUInt8(msgRequest.MsgType); if (!CSLib.Framework.CMsgFactory.Instance.IsIgnoreTrace(tmpServer, tmpFunc, msgRequest.Id)) { //发送消息前加入本地缓存,在这里做可以过滤掉心跳 UDLib.Utility.CDebugOut.Log("SendMessage : uServer = " + tmpServer.ToString() + "; uFunc = " + tmpFunc.ToString() + "; uID = " + msgRequest.Id.ToString() + ", reqIndex" + msgRequest.GetReqIndex()); // 超时重发机制, 非ack消息,缓存序列好,待收到ack返回从缓存移除 m_tcpClient.CacheMessage(msgRequest); } #if DEBUG m_sendStatisticsNum.AddNum(tmpServer.ToString() + " -> " + tmpFunc.ToString()); #endif } //UnityEngine.Profiling.Profiler.EndSample(); // 这个Cache后检测,保证断网后仍然可以缓存消息,在网络恢复后不会丢失消息(剧情过程中断网如不缓存会丢失消息导致黑屏) if (!m_tcpClient.IsValid()) { //UDLib.Utility.CDebugOut.LogError("SendMessage : !m_tcpClient.IsValid()"); return(false); } return(_SendMessage(msgRequest)); }
protected override Boolean _CbParseMsg(Byte[] msgBuff, Int32 msgSize) { CSLib.Framework.CMessageLabel msgLabel = CSLib.Framework.CMsgFactory.Instance.CreateMsg(msgBuff, msgSize); if (msgLabel == null) { return(true); } CSLib.Framework.CNetMessage theMsg = (CSLib.Framework.CNetMessage)(msgLabel.MsgObject); // 删除发送时做的消息缓存,判空应对一些不规范的返回(GM命令系统返回是空) if (theMsg != null) { m_dicDelayedMsg.DelObject(theMsg.UniqueID); } // ack 的 response不做解析和向服务器发送ack if (theMsg != null && theMsg.Func == CReconnectMgr.EFUNC_MESSAGESYSTEM) { #if DEBUG UDLib.Utility.CDebugOut.Log("收到ack消息, 从消息重发队列移除消息 > reqIndex : " + theMsg.GetReqIndex()); #endif m_cacheMessage.RemoveCacheMessageByReqIndex(msgLabel.ReqIndex); return(true); } // 根据这边的 Index 数据,发送 Ack 信息, 排除心跳为0 if (msgLabel.ResIndex != 0) { // 10 Gateway, 5 ACK系统号, 2 客户端发送给服务的消息号,在逻辑层的proto定义,底层无法获取,这里用直接用数字了 UDLib.Network.CBufMessage netMessage = new UDLib.Network.CBufMessage(10, 5, 2); netMessage.SetReqIndex(0); netMessage.SetResIndex(msgLabel.ResIndex); #if DEBUG UDLib.Utility.CDebugOut.Log("收到系统消息消息, 给服务器发送ACK确认 : " + msgLabel.ResIndex); #endif CSLib.Utility.CStream msgStream = new CSLib.Utility.CStream();; netMessage.Serialize(msgStream); SendAsync(msgStream); } base._CbParseMsg(msgBuff, msgSize); return(true); }
public bool SendMessage(CSLib.Framework.CNetMessage msgRequest, CSLib.Framework.CMessage msgResponse, Int32 delaySecond = 3) { if (CReconnectMgr.Instance.SimulateSocketClose) { UDLib.Utility.CDebugOut.Log("客户端模拟断网"); m_tcpClient.Close(); m_tcpClient.CbTerminate(); return(false); } if (msgRequest == null) { UDLib.Utility.CDebugOut.LogError("SendMessage : msgRquest == null"); return(false); } if (msgResponse == null) { UDLib.Utility.CDebugOut.LogError("SendMessage : msgResponse == null"); return(false); } if (m_tcpClient == null) { UDLib.Utility.CDebugOut.LogError("SendMessage : m_tcpClient == null"); return(false); } if (!m_tcpClient.IsValid()) { return(false); } Int64 curTicks = DateTime.Now.Ticks / 10000; // 转成毫秒 if (m_tcpClient.DelayedMsg.ContainsKey(msgResponse.UniqueID)) { if (curTicks < m_tcpClient.DelayedMsg.GetObject(msgResponse.UniqueID)) { return(false); } else { m_tcpClient.DelayedMsg.DelObject(msgResponse.UniqueID); } } //UnityEngine.Profiling.Profiler.BeginSample("CMoniBehaviour > SendMessage"); { byte tmpServer = CSLib.Utility.CBitHelper.GetHighUInt8(msgRequest.MsgType); byte tmpFunc = CSLib.Utility.CBitHelper.GetLowUInt8(msgRequest.MsgType); if (!CSLib.Framework.CMsgFactory.Instance.IsIgnoreTrace(tmpServer, tmpFunc, msgRequest.Id)) { //发送消息前加入本地缓存,在这里做可以过滤掉心跳 UDLib.Utility.CDebugOut.Log("SendMessage : uServer = " + tmpServer.ToString() + "; uFunc = " + tmpFunc.ToString() + "; uID = " + msgRequest.Id.ToString() + ", reqIndex" + msgRequest.GetReqIndex()); // 超时重发机制, 非ack消息,缓存序列号,待收到ack返回从缓存移除 m_tcpClient.CacheMessage(msgRequest); m_tcpClient.DelayedMsg.AddObject(msgResponse.UniqueID, curTicks + delaySecond * 1000); // 第二个参数转成毫秒 #if DEBUG m_sendStatisticsNum.AddNum(tmpServer.ToString() + " -> " + tmpFunc.ToString()); #endif } } //UnityEngine.Profiling.Profiler.EndSample(); return(_SendMessage(msgRequest)); }
public void Reset() { netMessage = null; timeStamp = 0; sentCount = 0; }
public void CacheMessage(CSLib.Framework.CNetMessage msg) { m_cacheMessage.InsertCacheMessage(msg); }