/// <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); }
// private void _OnMsgTestReqSay(CSLib.Framework.CMessageLabel msgLabel, CSLib.Framework.CMessage msg) { Message.CMsgSampleReqSay reqSay = (Message.CMsgSampleReqSay)msg; Console.WriteLine("收到:" + reqSay.m_string + "{" + m_netStub.LocalIPEndPoint.ToString() + "<---" + m_netStub.PeerIPEndPoint.ToString() + "}"); Message.CMsgSampleResSay resSay = new Message.CMsgSampleResSay(); resSay.m_string = "Sever No.["; resSay.m_string += m_number.ToString(); resSay.m_string += "]"; resSay.m_bool = reqSay.m_bool; resSay.m_byte = reqSay.m_byte; resSay.m_bytes = reqSay.m_bytes; resSay.m_double = reqSay.m_double; resSay.m_float = reqSay.m_float; resSay.m_int = reqSay.m_int; resSay.m_long = reqSay.m_long; resSay.m_short = reqSay.m_short; resSay.m_uint = reqSay.m_uint; resSay.m_ulong = reqSay.m_ulong; resSay.m_ushort = reqSay.m_ushort; m_number++; CSLib.Utility.CStream streamRes = new CSLib.Utility.CStream(); resSay.Serialize(streamRes); m_netStub.SendAsync(streamRes); }
protected override bool _Serialize(CSLib.Utility.CStream stream) { base._Serialize(stream); stream.Write(m_Buf); stream.Write(m_Result); return(true); }
// protected override bool _Deserialize(CSLib.Utility.CStream stream) { base._Deserialize(stream); stream.Read(ref m_Buf); stream.Read(ref m_Result); return(true); }
public static bool ParseMsg(byte[] msgBuff, int msgSize) { CSLib.Utility.CStream stream = new CSLib.Utility.CStream(); stream.Write(msgBuff, 0, msgSize); Console.WriteLine(stream.ReadString()); return(true); }
protected override bool _Serialize(CSLib.Utility.CStream stream) { stream.Write(m_bool); stream.Write(m_byte); stream.Write(m_bytes); stream.Write(m_double); stream.Write(m_float); stream.Write(m_int); stream.Write(m_long); stream.Write(m_short); stream.Write(m_string); stream.Write(m_uint); stream.Write(m_ulong); stream.Write(m_ushort); return(true); }
// 超时重新发送消息 private void CheckTimeout() { if (msgDictCache == null || msgDictCache.Count == 0) { return; } lock (msgLock) { // 重发超时消息,并且更新超时的时间戳和重发次数 m_keys.Clear(); m_keys.AddRange(msgDictCache.Keys); foreach (ushort reqIndex in m_keys) { var messageObject = msgDictCache[reqIndex]; if (messageObject.sentCount > CReconnectMgr.Instance.TimeOutRetryCount) { ////TODO, 超时10次判定为断线逻辑,暂不启用 //CDebugOut.LogWarning(string.Format("消息reqIndex {0}, id : {1} 已发送{2}次,网络太差,判定为断线,关闭socket", key, messageObject.netMessage.Id, // CReconnectMgr.Instance.TimeOutRetryCount)); //m_tcpClient.Close(); //ClearCacheMessage(); //break; } var timePassed = Math.Abs(DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - messageObject.timeStamp); #if DEBUG CSLib.Utility.CDebugOut.Log(string.Format("time passed :{0} ms", timePassed)); #endif if (timePassed > CReconnectMgr.Instance.TimeOutDuration) { CSLib.Utility.CStream msgStream = new CSLib.Utility.CStream(); messageObject.sentCount++; messageObject.timeStamp = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; messageObject.netMessage.Serialize(msgStream); SendMessage(msgStream); if (CReconnectMgr.Instance.timeoutCallBack != null) { CReconnectMgr.Instance.timeoutCallBack(reqIndex, messageObject.netMessage.MsgType, messageObject.netMessage.Id); } } } } }
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); }
static void Main(string[] args) { CSLib.Network.CTcpClient tcpClient = new CSLib.Network.CTcpClient(); tcpClient.CbParsMsg = CClientMsg.ParseMsg; tcpClient.CbTerminateMsg = CClientMsg.TerminateMsg; if (!tcpClient.Connect("127.0.0.1", 9000)) { Console.WriteLine("连接服务器失败"); return; } CSLib.Utility.CStream stream = new CSLib.Utility.CStream(); Console.WriteLine(tcpClient.LocalIPEndPoint.ToString() + "--->" + tcpClient.PeerIPEndPoint.ToString()); uint i = 1; while (true) { string data = "Client No.["; data += i.ToString(); data += "]"; stream.Write(data); i++; Console.WriteLine(data.ToString() + "{" + tcpClient.LocalIPEndPoint.ToString() + "--->" + tcpClient.PeerIPEndPoint.ToString() + "}"); if (!tcpClient.SendAsync(stream)) { break; } stream.Reset(); System.Threading.Thread.Sleep(1000); } }
static void Main(string[] args) { CSLib.Network.CTcpClient tcpClient = new CSLib.Network.CTcpClient(); CClientMsg clientMsg = new CClientMsg(tcpClient); tcpClient.CbParsMsg = clientMsg.ParseMsg; tcpClient.CbTerminateMsg = CClientMsg.TerminateMsg; if (!tcpClient.Connect("127.0.0.1", 9500)) { Console.WriteLine("连接服务器失败"); return; } Message.CMsgSampleNtfOk ntfOk = new Message.CMsgSampleNtfOk(); CSLib.Utility.CStream stream = new CSLib.Utility.CStream(); ntfOk.Serialize(stream); if (!tcpClient.SendAsync(stream)) { return; } stream.Reset(); Console.WriteLine(tcpClient.LocalIPEndPoint.ToString() + "--->" + tcpClient.PeerIPEndPoint.ToString()); uint i = 1; //DateTime dt = new DateTime(2013,3,4); // decimal dec = 12.12M; while (true) { stream.Reset(); Message.CMsgSampleReqSay reqSay = new Message.CMsgSampleReqSay(); reqSay.m_string = "Client No.["; reqSay.m_string += i.ToString(); reqSay.m_string += "]"; byte[] vv; vv = new byte[5]; vv[1] = 1; vv[2] = 2; vv[3] = 3; vv[4] = 4; vv[0] = 5; reqSay.m_bool = true; reqSay.m_byte = (byte)i; reqSay.m_bytes = vv; reqSay.m_double = 1213.43435; reqSay.m_float = 123.234f; reqSay.m_int = 34534534; reqSay.m_long = 3245675756675L; reqSay.m_short = 32000; reqSay.m_uint = 2100000000; reqSay.m_ulong = 94242424242342L; reqSay.m_ushort = 65000; reqSay.Serialize(stream); i++; Console.WriteLine(reqSay.m_string + reqSay.m_bool + " " + reqSay.m_byte + " " + reqSay.m_bytes[0] + " " + reqSay.m_bytes[1] + " " + reqSay.m_bytes[2] + " " + reqSay.m_bytes[3] + " " + reqSay.m_bytes[4] + " " + reqSay.m_double + " " + reqSay.m_float + " " + reqSay.m_int + " " + reqSay.m_long + " " + reqSay.m_short + " " + reqSay.m_uint + " " + reqSay.m_ulong + " " + reqSay.m_ushort); if (!tcpClient.SendAsync(stream)) { break; } stream.Reset(); System.Threading.Thread.Sleep(1000); } }
protected override bool _Serialize(CSLib.Utility.CStream stream) { return(true); }
private void SendMessage(CSLib.Utility.CStream msgStream) { m_tcpClient.SendAsync(msgStream); }