예제 #1
0
        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;
                    }
                }
            }
        }
예제 #2
0
        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;
                    }
                }
            }
        }