예제 #1
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));
        }
예제 #2
0
 public void OnDataRead(ByteArray data)
 {
     if (data.Length == 0)
     {
         throw new SFSError("Unexpected empty packet data: no readable bytes available!");
     }
     if (bitSwarm != null && bitSwarm.Sfs.Debug)
     {
         if (data.Length > 1024)
         {
             log.Info("Data Read: Size > 1024, dump omitted");
         }
         else
         {
             log.Info("Data Read: " + DefaultObjectDumpFormatter.HexDump(data));
         }
     }
     data.Position = 0;
     while (data.Length > 0)
     {
         if (ReadState == PacketReadState.WAIT_NEW_PACKET)
         {
             data = HandleNewPacket(data);
         }
         else if (ReadState == PacketReadState.WAIT_DATA_SIZE)
         {
             data = HandleDataSize(data);
         }
         else if (ReadState == PacketReadState.WAIT_DATA_SIZE_FRAGMENT)
         {
             data = HandleDataSizeFragment(data);
         }
         else if (ReadState == PacketReadState.WAIT_DATA)
         {
             data = HandlePacketData(data);
         }
         else if (ReadState == PacketReadState.INVALID_DATA)
         {
             data = HandleInvalidData(data);
         }
     }
 }
예제 #3
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.");
     }
 }
예제 #4
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));
        }
예제 #5
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);
            }
        }
예제 #6
0
 public string GetHexDump()
 {
     return(DefaultObjectDumpFormatter.HexDump(this.ToBinary()));
 }
예제 #7
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;
                }
            }
        }