Ejemplo n.º 1
0
 public void OnDataRead(ByteArray data)
 {
     if (data.Length == 0)
     {
         throw new AWError("Unexpected empty packet data: no readable bytes available!");
     }
     if (this.bitSwarm != null && this.bitSwarm.Sfs.Debug)
     {
         if (data.Length > 1024)
         {
             this.log.Info(new string[]
             {
                 "Data Read: Size > 1024, dump omitted"
             });
         }
         else
         {
             this.log.Info(new string[]
             {
                 "Data Read: " + DefaultObjectDumpFormatter.HexDump(data)
             });
         }
     }
     data.Position = 0;
     while (data.Length > 0)
     {
         if (this.ReadState == PacketReadState.WAIT_NEW_PACKET)
         {
             data = this.HandleNewPacket(data);
         }
         else
         {
             if (this.ReadState == PacketReadState.WAIT_DATA_SIZE)
             {
                 data = this.HandleDataSize(data);
             }
             else
             {
                 if (this.ReadState == PacketReadState.WAIT_DATA_SIZE_FRAGMENT)
                 {
                     data = this.HandleDataSizeFragment(data);
                 }
                 else
                 {
                     if (this.ReadState == PacketReadState.WAIT_DATA)
                     {
                         data = this.HandlePacketData(data);
                     }
                     else
                     {
                         if (this.ReadState == PacketReadState.INVALID_DATA)
                         {
                             data = this.HandleInvalidData(data);
                         }
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 2
0
 public string GetDump(bool format)
 {
     if (!format)
     {
         return(Dump());
     }
     return(DefaultObjectDumpFormatter.PrettyPrintDump(Dump()));
 }
Ejemplo n.º 3
0
        private void PrintRecvHexData(byte[] msg)
        {
            ByteArray byteArray = new ByteArray(msg);

            UnityEngine.Debug.Log("<----------------TcpSocketLayer--begin--------------->");
            UnityEngine.Debug.Log("Message Buffer-->" + DefaultObjectDumpFormatter.HexDump(byteArray));
            UnityEngine.Debug.Log("<----------------TcpSocketLayer--end----------------->");
        }
Ejemplo n.º 4
0
        public string GetDump(bool format)
        {
            string result;

            if (!format)
            {
                result = this.Dump();
            }
            else
            {
                result = DefaultObjectDumpFormatter.PrettyPrintDump(this.Dump());
            }
            return(result);
        }
Ejemplo n.º 5
0
 public void OnDataRead(ByteArray data, int msgNameLen)
 {
     if (data.Length == 0)
     {
         throw new AWError("Unexpected empty packet data: no readable bytes available!");
     }
     // UnityEngine.Debug.Log("AWIOHandler--call OnDataProtoBufRead --" + DefaultObjectDumpFormatter.HexDump(data));
     UnityEngine.Debug.Log("反序列化消息名字长度:  " + msgNameLen);
     UnityEngine.Debug.Log("反序列化消息参数据度:  " + data.Bytes.Length);
     UnityEngine.Debug.Log("反序列化消息内容:   " + DefaultObjectDumpFormatter.HexDump(data));
     ProtoBuf.IExtensible message = this.protoBufserializer.Deserialize(msgNameLen, data.Bytes, 0, data.Bytes.Length);
     UnityEngine.Debug.Log("反序列化消息:  " + message.GetType().FullName.ToString(null) + "   OK!");
     HandleProtoBufPacketData(message);
 }
Ejemplo n.º 6
0
        private ByteArray HandleNewPacket(ByteArray data)
        {
            log.Debug("Handling New Packet of size " + data.Length);
            byte b = data.ReadByte();

            if (~(b & 0x80) > 0)
            {
                throw new SFSError("Unexpected header byte: " + b + "\n" + DefaultObjectDumpFormatter.HexDump(data));
            }
            PacketHeader header = PacketHeader.FromBinary(b);

            pendingPacket = new PendingPacket(header);
            fsm.ApplyTransition(PacketReadTransition.HeaderReceived);
            return(ResizeByteArray(data, 1, data.Length - 1));
        }
Ejemplo n.º 7
0
 public void Send(ByteArray binaryData)
 {
     if (initSuccess)
     {
         try
         {
             udpSocket.Write(binaryData.Bytes);
             if (sfs.Debug)
             {
                 log.Info("UDP Data written: " + DefaultObjectDumpFormatter.HexDump(binaryData));
             }
         }
         catch (Exception ex)
         {
             log.Warn("WriteUDP operation failed due to error: " + ex.Message + " " + ex.StackTrace);
         }
     }
     else
     {
         log.Warn("UDP protocol is not initialized yet. Please use the initUDP() method.");
     }
 }
Ejemplo n.º 8
0
        private ByteArray HandleNewPacket(ByteArray data)
        {
            this.log.Debug(new string[]
            {
                "Handling New Packet of size " + data.Length
            });
            byte b = data.ReadByte();

            if (~(b & 128) > 0)
            {
                throw new AWError(string.Concat(new object[]
                {
                    "Unexpected header byte: ",
                    b,
                    "\n",
                    DefaultObjectDumpFormatter.HexDump(data)
                }));
            }
            PacketHeader header = PacketHeader.FromBinary((int)b);

            this.pendingPacket = new PendingPacket(header);
            this.fsm.ApplyTransition(PacketReadTransition.HeaderReceived);
            return(this.ResizeByteArray(data, 1, data.Length - 1));
        }
Ejemplo n.º 9
0
        private void OnUDPData(byte[] bt)
        {
            //Discarded unreachable code: IL_014c
            ByteArray byteArray = new ByteArray(bt);

            if (byteArray.BytesAvailable < 4)
            {
                log.Warn("Too small UDP packet. Len: " + byteArray.Length);
                return;
            }
            if (sfs.Debug)
            {
                log.Info("UDP Data Read: " + DefaultObjectDumpFormatter.HexDump(byteArray));
            }
            byte  b     = byteArray.ReadByte();
            bool  flag  = (b & 0x20) > 0;
            bool  flag2 = (b & 0x40) > 0;
            short num   = byteArray.ReadShort();

            if (num != byteArray.BytesAvailable)
            {
                log.Warn("Insufficient UDP data. Expected: " + num + ", got: " + byteArray.BytesAvailable);
                return;
            }
            byte[]    buf        = byteArray.ReadBytes(byteArray.BytesAvailable);
            ByteArray byteArray2 = new ByteArray(buf);

            if (flag2)
            {
                try
                {
                    packetEncrypter.Decrypt(byteArray2);
                }
                catch (Exception ex)
                {
                    log.Warn("UDP data decryption failed due to error: " + ex.Message + " " + ex.StackTrace);
                    return;
                }
            }
            if (flag)
            {
                byteArray2.Uncompress();
            }
            ISFSObject iSFSObject = SFSObject.NewFromBinaryData(byteArray2);

            if (iSFSObject.ContainsKey("h"))
            {
                if (!initSuccess)
                {
                    StopTimer();
                    locked      = false;
                    initSuccess = true;
                    Hashtable hashtable = new Hashtable();
                    hashtable["success"] = true;
                    sfs.DispatchEvent(new SFSEvent(SFSEvent.UDP_INIT, hashtable));
                }
            }
            else
            {
                sfs.GetSocketEngine().IoHandler.Codec.OnPacketRead(iSFSObject);
            }
        }
Ejemplo n.º 10
0
 public string GetHexDump()
 {
     return(DefaultObjectDumpFormatter.HexDump(this.ToBinary()));
 }
Ejemplo n.º 11
0
        private void Read()
        {
            int msglength  = 0;
            int msgNameLen = 0;

            while (true)
            {
                try
                {
                    if (this.State != TCPSocketLayer.States.Connected)
                    {
                        UnityEngine.Debug.Log("Recive Thread DisConnect-->");
                        break;
                    }

                    if (this.socketPollSleep > 0)
                    {
                        TCPSocketLayer.Sleep(this.socketPollSleep);
                    }
                    if (this.connection.Client.Poll(-1, SelectMode.SelectRead) || this.connection.Client.Poll(-1, SelectMode.SelectError))
                    {
                        byte[] headerBytes = new byte[9];
                        int    i           = this.connection.Client.Receive(headerBytes);
                        if (i <= 0)
                        {
                            this.Disconnect();
                            break;
                        }

                        ProtoBufPackageHeader header = new ProtoBufPackageHeader();
                        int iHeaderLen = header.ReturnHeaderLen();
                        header.ReadFrom(headerBytes, 0);
                        msglength  = header.MessageLength - iHeaderLen;
                        msgNameLen = header.MessageTypeLength;
                        if (0 == msglength)
                        {
                            ResponsePing();
                            continue;
                        }
                    }
                    if (this.connection.Client.Poll(-1, SelectMode.SelectRead) || this.connection.Client.Poll(-1, SelectMode.SelectError))
                    {
                        byte[] msgbytes = new byte[msglength];
                        int    i        = this.connection.Client.Receive(msgbytes);
                        if (i <= 0)
                        {
                            this.Disconnect();
                            break;
                        }
                        ByteArray byteArray = new ByteArray();
                        byteArray.WriteBytes(msgbytes);
                        UnityEngine.Debug.Log("TCP Socket 收到的字节:  " + DefaultObjectDumpFormatter.HexDump(byteArray));
                        this.HandleBinaryProtoBufData(byteArray.Bytes, byteArray.Bytes.Length, msgNameLen);
                    }
                }
                catch (Exception ex)
                {
                    this.HandleError("General error reading data from socket: " + ex.Message + " " + ex.StackTrace);
                    break;
                }
            }
        }