public TcpNetMgr() { if (null == _NetBackMgr) { _NetBackMgr = NetBackMgr.GetInst(); _NetBackMgr.RegistNetBack(); } }
/// <summary> /// 收到消息 /// </summary> /// <param name="__msgCode">消息号</param> /// <param name="__msgContent">消息体</param> private static void AnalysisCache4RecMsg(int __msgCode, byte[] __msgContent) { if (NetBackMgr.GetInst().NetBackAnalyzerDic.ContainsKey(__msgCode)) { AbsNetBack back = NetBackMgr.GetInst().NetBackAnalyzerDic [__msgCode]; MemoryStream tstream = new MemoryStream(__msgContent); back._BinaryReader = new BinaryReader(tstream, Encoding.UTF8); Type t = back.GetType(); MethodInfo info = t.GetMethod("Recevie" + __msgCode); info.Invoke(back, null); back._BinaryReader.Close(); tstream.Close(); tstream.Dispose(); } else { Debug.LogError("无法解析Socket包:" + __msgCode); } }
//ninfo 注意这里nEnd不是buf真实size,而是buf中有效数据size //TODO 下一步,这个可以考虑移动到TcpNetMgr中 // 处理网络消息 public void ProcNetMessage(byte[] stream, int nEnd) { int EffectiveByteNumInBuffer = nEnd; MemoryStream tstream = new MemoryStream(stream); BinaryReader _BinaryReader = new BinaryReader(tstream, Encoding.UTF8); tempBinaryReaderPos = 0; while (_BinaryReader.BaseStream.Position < EffectiveByteNumInBuffer) { //ninfo 因为一次解析至少要解析 //判断剩下的字节够不够4个,如果小于4个就交给下一波来处理就行了 long leftbytes = EffectiveByteNumInBuffer - _BinaryReader.BaseStream.Position; if (leftbytes < 6) { //如果小于4个字节,那么交给下个BUFFER byte[] thisbuffer = TcpNetMgr.GetInst().GetCurrentBuffer(); byte[] nextbuffer = TcpNetMgr.GetInst().GetNextBuffer(); for (int i = 0; i < leftbytes; i++) { nextbuffer [i] = thisbuffer [_BinaryReader.BaseStream.Position + i]; } TcpNetMgr.GetInst().BytesNumWaiting4Process = (int)leftbytes; return; } int headmsg = BinaryHelper.ReadShort(_BinaryReader); if (TcpNetMgr.HEAD_MSG != headmsg) { Debug.Log("严重错误"); Debug.LogError("message is woring"); return; } int msglen = BinaryHelper.ReadInt(_BinaryReader); if (msglen <= 6) { //ninfo 解释下这里,首先按常理msglen不可能<=6 消息头+msglen两项就是6个字节 //遇到<=6的情况,说明之前判断到的消息头headmsg=BEGINTAG_MSG实际是假的消息头,是脏数据 //所以才掠过错误数据,找下一个(疑是)消息头 //肯定是出错了,跳过这个消息,挪到下一个消息 while (BinaryHelper.ReadShort(_BinaryReader) != TcpNetMgr.HEAD_MSG) { ; } _BinaryReader.BaseStream.Position -= 2; tempBinaryReaderPos = (int)_BinaryReader.BaseStream.Position; continue; } leftbytes = EffectiveByteNumInBuffer - _BinaryReader.BaseStream.Position; //ninfo msglen-6就是一条消息出去消息头和msglen的有效消息内容的长度 //这里的判断含义就是,剩余数据不够一条消息的有效数据长度 if (leftbytes < msglen - 6) { byte[] thisbuffer = TcpNetMgr.GetInst().GetCurrentBuffer(); byte[] nextbuffer = TcpNetMgr.GetInst().GetNextBuffer(); for (int i = 0; i < leftbytes + 6; i++) { nextbuffer [i] = thisbuffer [_BinaryReader.BaseStream.Position + i - 6]; } TcpNetMgr.GetInst().BytesNumWaiting4Process = (int)leftbytes + 6; return; } int function_id = BinaryHelper.ReadInt(_BinaryReader); EventID++; bool analyzeError = false; try { if (NetBackMgr.GetInst().NetBackAnalyzerDic.ContainsKey(function_id)) { Debug.LogError("收到消息Messge ID = " + function_id); //MainAnalyzer[function_id](); Byte[] content = _BinaryReader.ReadBytes(msglen - 10); //ReceivedMessage(function_id, content); if (!NetBackMgr.CheckImportantMsg(function_id, content)) { Cache4RecMsg recevieMessage = new Cache4RecMsg(); recevieMessage.id = function_id; recevieMessage.content = content; NetBackMgr.Cache4RecMsgQueue.Enqueue(recevieMessage); } } else { Debug.Log("Not Found Messge ID = " + function_id + ",msglen:" + msglen); for (int i = 10; i < msglen; i++) { BinaryHelper.ReadByte(_BinaryReader); } } } catch (Exception e) { analyzeError = true; // Debug.LogError("-------------CommandSys = " + function_id + " Read Error Start-------------"); Debug.LogException(e); // Debug.LogError("-------------CommandSys = " + function_id + " Read Error End-------------"); } finally { tempBinaryReaderPos += msglen; if (_BinaryReader.BaseStream.Position != tempBinaryReaderPos) { analyzeError = true; string ms = "消息处理函数读取数据错误!" + function_id.ToString(); Debug.LogError(ms); _BinaryReader.BaseStream.Position = tempBinaryReaderPos; } } //finally结束 } //while结束 } //函数结尾
public AbsNetBack() { _NetBackMgr = NetBackMgr.GetInst(); RegistMsgID(); }