/// <summary> /// byte[] 转换为 ReceiveProtocol /// </summary> /// <param name="buffer">字节数组缓冲器。</param> /// <returns></returns> public static MsgProtocol FromBytesR(byte[] buffer) { int bufferLength = buffer.Length; MsgProtocol msgProtocol = new MsgProtocol(); using (MemoryStream memoryStream = new MemoryStream(buffer)) //将字节数组填充至内存流 { BinaryReader binaryReader = new BinaryReader(memoryStream); //以二进制读取器读取该流内容 msgProtocol.MessageLength = BitConverter.ToInt32(binaryReader.ReadBytes(4).Reverse().ToArray(), 0); //读取数据长度,4字节 //如果进来的Bytes长度大于一个完整的MsgProtocol长度 if (msgProtocol.MessageLength <= 0) { Debug.WriteLine("读取消息错误,长度小于等于0 buffer: " + BitConverter.ToString(buffer)); return(null); } if ((bufferLength - 4) > msgProtocol.MessageLength) { msgProtocol.MessageContent = binaryReader.ReadBytes(msgProtocol.MessageLength); //读取实际消息内容,从第5个字节开始读 msgProtocol.ExtraBytes = binaryReader.ReadBytes(bufferLength - 4 - msgProtocol.MessageLength); //余下的数据 } //如果进来的Bytes长度等于一个完整的MessageXieYi长度 if ((bufferLength - 4) == msgProtocol.MessageLength) { msgProtocol.MessageContent = binaryReader.ReadBytes(msgProtocol.MessageLength); //读取实际消息内容,从第5个字节开始读 } if ((bufferLength - 4) >= msgProtocol.MessageLength) { byte[] commandBytes = new byte[32]; byte[] tokenBytes = new byte[4]; byte[] dataBytes = new byte[msgProtocol.MessageContent.Length - 4 - 32]; Array.Copy(msgProtocol.MessageContent, 0, commandBytes, 0, 32); //命令32字节 Array.Copy(msgProtocol.MessageContent, 32, tokenBytes, 0, 4); //token4字节 Array.Copy(msgProtocol.MessageContent, 32 + 4, dataBytes, 0, dataBytes.Length); //数据 msgProtocol.MessageCommand = Encoding.UTF8.GetString(commandBytes).Replace("\0", ""); msgProtocol.MessageToken = BitConverter.ToInt32(tokenBytes.Reverse().ToArray(), 0); msgProtocol.Data = Encoding.UTF8.GetString(Zip.DeCompress(dataBytes)); } //if((bufferLength-4)<msgProtocol.MessageLength) //{ // Debug.WriteLine("buffer.len小于消息长度." + msgProtocol.MessageLength); //} binaryReader.Close(); //关闭二进制读取器,释放资源 } return(msgProtocol); //返回消息协议对象 }
private void SendData(TcpPacket tcpPacket, string key, string key2) { //Debug.WriteLine("=============================================================================================================="); //Debug.WriteLine("发送数据:"+key); MsgProtocol msgPro = new MsgProtocol(); dataBufferDict[key] = CombineBytes.ToArray(dataBufferDict[key], 0, dataBufferDict[key].Length, tcpPacket.PayloadData, 0, tcpPacket.PayloadData.Length); if (dataBufferDict[key].Length < 4) { // Debug.WriteLine("sendBuffer.Length=" + dataBufferDict[key].Length + "< 4 \t -> \t continue"); return; } else { //取msg包头部分 msgPro = MsgProtocol.FromBytesS(dataBufferDict[key]); if (msgPro == null) { Debug.WriteLine("数据格式错误!消息为null" + BitConverter.ToString(tcpPacket.PayloadData)); dataBufferDict[key] = new byte[] { }; return; } int msgContentLength = msgPro.MessageLength; //判断去掉msg包头剩下的长度是否达到可以取包实质内容 while ((dataBufferDict[key].Length - 4) >= msgContentLength) { // Debug.WriteLine("【sendBuffer去掉包头的长度=" + (sendBuffer.Length - 4) + "】>=【" + "包实质内容长度=" + msgContentLength + "】"); msgPro = null; msgPro = MsgProtocol.FromBytesS(dataBufferDict[key]); // Debug.WriteLine("【data】=" + "command:" + msgPro.MessageCommand + " token:" + msgPro.MessageToken); // Debug.WriteLine("【param】=" + msgPro.Data); if (msgPro == null) { Debug.WriteLine("数据格式错误!消息为null" + BitConverter.ToString(tcpPacket.PayloadData)); dataBufferDict[key] = new byte[] { }; return; } CommandCacheDict[key2].UpdateData(PacketType.Send, msgPro.MessageCommand, msgPro.Data); dataBufferDict[key] = msgPro.ExtraBytes; if (dataBufferDict[key].Length >= 4) { msgPro = MsgProtocol.FromBytesS(dataBufferDict[key]); if (msgPro == null) { Debug.WriteLine("数据格式错误!消息为null" + BitConverter.ToString(tcpPacket.PayloadData)); dataBufferDict[key] = new byte[] { }; return; } msgContentLength = msgPro.MessageLength; continue; } else { break; } } } }
private void ReceivedData(TcpPacket tcpPacket, string key, string key2) { MsgProtocol msgPro = new MsgProtocol(); dataBufferDict[key] = CombineBytes.ToArray(dataBufferDict[key], 0, dataBufferDict[key].Length, tcpPacket.PayloadData, 0, tcpPacket.PayloadData.Length); if (dataBufferDict[key].Length < 4) { // Debug.WriteLine("receivedBuffer.Length=" + dataBufferDict[key].Length + "< 4 \t -> \t continue"); return; } else { //取msg包头部分 msgPro = MsgProtocol.FromBytesR(dataBufferDict[key]); if (msgPro == null) { Debug.WriteLine("数据格式错误!消息为null" + BitConverter.ToString(tcpPacket.PayloadData)); dataBufferDict[key] = new byte[] { }; return; } int msgContentLength = msgPro.MessageLength; //判断去掉msg包头剩下的长度是否达到可以取包实质内容 while ((dataBufferDict[key].Length - 4) >= msgContentLength) { // Debug.WriteLine("【receivedBuffer去掉包头的长度=" + (receivedBuffer.Length - 4) + "】>=【" + "包实质内容长度=" + msgContentLength + "】"); msgPro = null; msgPro = MsgProtocol.FromBytesR(dataBufferDict[key]); if (msgPro == null) { Debug.WriteLine("数据格式错误!消息为null" + BitConverter.ToString(tcpPacket.PayloadData)); dataBufferDict[key] = new byte[] { }; return; } string filter = "player@ltestplayer@game"; if (!msgPro.MessageCommand.Contains("push") && !filter.Contains(msgPro.MessageCommand)) { //Debug.WriteLine("=============================================================================================================="); //Debug.WriteLine("接收数据:" + key); //Debug.WriteLine("【data】=" + "command:" + msgPro.MessageCommand + " token:" + msgPro.MessageToken); // Debug.WriteLine("【json】=" + msgPro.Data); } CommandCacheDict[key2].UpdateData(PacketType.Receive, msgPro.MessageCommand, msgPro.Data); //将得到的json字符串转为对象 // var rootObj = JsonHelper.FromJson<dynamic>(msgPro.Data); var rootObj = JsonConvert.DeserializeObject <dynamic>(msgPro.Data); if (SoftContext.CommandList.ContainsKey(key + msgPro.MessageCommand)) { SoftContext.CommandList[key + msgPro.MessageCommand] = rootObj; } else { SoftContext.CommandList.Add(key + msgPro.MessageCommand, rootObj); } dataBufferDict[key] = msgPro.ExtraBytes; if (dataBufferDict[key].Length >= 4) { msgPro = MsgProtocol.FromBytesR(dataBufferDict[key]); if (msgPro == null) { Debug.WriteLine("数据格式错误!消息为null" + BitConverter.ToString(tcpPacket.PayloadData)); dataBufferDict[key] = new byte[] { }; return; } msgContentLength = msgPro.MessageLength; continue; } else { break; } } } }