Example #1
0
    //将大缓冲的字节转移到此包
    public void Swap()
    {
        m_ReadBytes = PooledClassManager <DataBytes> .CreateClass();

        m_ReadBytes.SetBytes(m_Offset);
        Array.Copy(s_WriteBytes, 0, m_ReadBytes.GetBytes(), 0, m_Offset);
    }
        private void Process()
        {
            if (DataBytes.Length >= 8)
            {
                UncompressedSize = BinaryPrimitives.ReadInt64LittleEndian(DataBytes);
            }

            if (DataBytes.Length >= 16)
            {
                CompressedSize = BinaryPrimitives.ReadInt64LittleEndian(DataBytes.AsSpan(8));
            }

            if (DataBytes.Length >= 24)
            {
                RelativeOffsetOfEntryHeader = BinaryPrimitives.ReadInt64LittleEndian(DataBytes.AsSpan(16));
            }

            if (DataBytes.Length >= 28)
            {
                VolumeNumber = BinaryPrimitives.ReadUInt32LittleEndian(DataBytes.AsSpan(24));
            }

            switch (DataBytes.Length)
            {
            case 8:
            case 16:
            case 24:
            case 28:
                break;

            default:
                throw new ArchiveException($"Unexpected size of of Zip64 extended information extra field: {DataBytes.Length}");
            }
        }
Example #3
0
        protected override void ReadPayload()
        {
            var reader = new BinaryReader(new MemoryStream(DataBytes.Skip((sizeof(int))).ToArray()));

            LineNumber  = reader.ReadInt32();
            QueueLength = reader.ReadInt32();
            EngineState = (EngineState)reader.ReadInt32();
        }
Example #4
0
 public override void DestroyClass()
 {
     if (m_ReadBytes != null)
     {
         m_ReadBytes.DestroyClass();
         m_ReadBytes = null;
     }
     m_Offset = 0;
     PooledClassManager <WfPacket> .DeleteClass(this);
 }
Example #5
0
        //From the spec values are only in the extradata if the standard
        //value is set to 0xFFFF, but if one of the sizes are present, both are.
        //Hence if length == 4 volume only
        //      if length == 8 offset only
        //      if length == 12 offset + volume
        //      if length == 16 sizes only
        //      if length == 20 sizes + volume
        //      if length == 24 sizes + offset
        //      if length == 28 everything.
        //It is unclear how many of these are used in the wild.

        private void Process()
        {
            switch (DataBytes.Length)
            {
            case 4:
                VolumeNumber = BinaryPrimitives.ReadUInt32LittleEndian(DataBytes);
                return;

            case 8:
                RelativeOffsetOfEntryHeader = BinaryPrimitives.ReadInt64LittleEndian(DataBytes);
                return;

            case 12:
                RelativeOffsetOfEntryHeader = BinaryPrimitives.ReadInt64LittleEndian(DataBytes);
                VolumeNumber = BinaryPrimitives.ReadUInt32LittleEndian(DataBytes.AsSpan(8));
                return;

            case 16:
                UncompressedSize = BinaryPrimitives.ReadInt64LittleEndian(DataBytes);
                CompressedSize   = BinaryPrimitives.ReadInt64LittleEndian(DataBytes.AsSpan(8));
                return;

            case 20:
                UncompressedSize = BinaryPrimitives.ReadInt64LittleEndian(DataBytes);
                CompressedSize   = BinaryPrimitives.ReadInt64LittleEndian(DataBytes.AsSpan(8));
                VolumeNumber     = BinaryPrimitives.ReadUInt32LittleEndian(DataBytes.AsSpan(16));
                return;

            case 24:
                UncompressedSize            = BinaryPrimitives.ReadInt64LittleEndian(DataBytes);
                CompressedSize              = BinaryPrimitives.ReadInt64LittleEndian(DataBytes.AsSpan(8));
                RelativeOffsetOfEntryHeader = BinaryPrimitives.ReadInt64LittleEndian(DataBytes.AsSpan(16));
                return;

            case 28:
                UncompressedSize            = BinaryPrimitives.ReadInt64LittleEndian(DataBytes);
                CompressedSize              = BinaryPrimitives.ReadInt64LittleEndian(DataBytes.AsSpan(8));
                RelativeOffsetOfEntryHeader = BinaryPrimitives.ReadInt64LittleEndian(DataBytes.AsSpan(16));
                VolumeNumber = BinaryPrimitives.ReadUInt32LittleEndian(DataBytes.AsSpan(24));
                return;

            default:
                throw new ArchiveException("Unexpected size of of Zip64 extended information extra field");
            }
        }
        //public int ADestinationSteps { get; set; }

        //public int BDestinationSteps { get; set; }

        //public int CDestinationSteps { get; set; }

        protected override void ReadPayload()
        {
            var reader = new BinaryReader(new MemoryStream(DataBytes.Skip((sizeof(int) * 1)).ToArray()));

            XSteps = reader.ReadSingle(); //.ReadInt32();
            YSteps = reader.ReadSingle();
            ZSteps = reader.ReadSingle();
            //ASteps = reader.ReadInt32();
            //BSteps = reader.ReadInt32();
            //CSteps = reader.ReadInt32();

            XDestinationSteps = reader.ReadSingle();
            YDestinationSteps = reader.ReadSingle();
            ZDestinationSteps = reader.ReadSingle();
            //ADestinationSteps = reader.ReadInt32();
            //BDestinationSteps = reader.ReadInt32();
            //CDestinationSteps = reader.ReadInt32();
        }
        // From the spec, values are only in the extradata if the standard
        // value is set to 0xFFFFFFFF (or 0xFFFF for the Disk Start Number).
        // Values, if present, must appear in the following order:
        // - Original Size
        // - Compressed Size
        // - Relative Header Offset
        // - Disk Start Number
        public void Process(long uncompressedFileSize, long compressedFileSize, long relativeHeaderOffset, ushort diskNumber)
        {
            var bytesRequired = ((uncompressedFileSize == uint.MaxValue) ? 8 : 0)
                                + ((compressedFileSize == uint.MaxValue) ? 8 : 0)
                                + ((relativeHeaderOffset == uint.MaxValue) ? 8 : 0)
                                + ((diskNumber == ushort.MaxValue) ? 4 : 0);
            var currentIndex = 0;

            if (bytesRequired > DataBytes.Length)
            {
                throw new ArchiveException("Zip64 extended information extra field is not large enough for the required information");
            }

            if (uncompressedFileSize == uint.MaxValue)
            {
                UncompressedSize = BinaryPrimitives.ReadInt64LittleEndian(DataBytes.AsSpan(currentIndex));
                currentIndex    += 8;
            }

            if (compressedFileSize == uint.MaxValue)
            {
                CompressedSize = BinaryPrimitives.ReadInt64LittleEndian(DataBytes.AsSpan(currentIndex));
                currentIndex  += 8;
            }

            if (relativeHeaderOffset == uint.MaxValue)
            {
                RelativeOffsetOfEntryHeader = BinaryPrimitives.ReadInt64LittleEndian(DataBytes.AsSpan(currentIndex));
                currentIndex += 8;
            }

            if (diskNumber == ushort.MaxValue)
            {
                VolumeNumber = BinaryPrimitives.ReadUInt32LittleEndian(DataBytes.AsSpan(currentIndex));
            }
        }
        private void Process()
        {
            if (DataBytes.Length >= 8)
            {
                UncompressedSize = BinaryPrimitives.ReadInt64LittleEndian(DataBytes);
            }
            if (DataBytes.Length >= 16)
            {
                CompressedSize = BinaryPrimitives.ReadInt64LittleEndian(DataBytes.AsSpan(8));
            }
            if (DataBytes.Length >= 24)
            {
                RelativeOffsetOfEntryHeader = BinaryPrimitives.ReadInt64LittleEndian(DataBytes.AsSpan(16));
            }
            if (DataBytes.Length >= 28)
            {
                VolumeNumber = BinaryPrimitives.ReadUInt32LittleEndian(DataBytes.AsSpan(24));
            }

            if (DataBytes.Length > 28)
            {
                throw new ArchiveException("Unexpected size of of Zip64 extended information extra field");
            }
        }
Example #9
0
    private void WfNetworkThread()
    {
        HeaderBytes headbytes = PooledClassManager <HeaderBytes> .CreateClass();

        DataBytes databytes  = null;
        int       recvedNum  = 0;
        WfPacket  sendPacket = null;
        int       sendedNum  = 0;

        while (m_Thread.IsAlive)
        {
            try
            {
                Thread.Sleep(10);
                if (m_ConnectState == EConnectState.PostConnecting)
                {
                    databytes = null;
                    recvedNum = 0;
                    if (sendPacket != null)
                    {
                        sendPacket.DestroyClass();
                        sendPacket = null;
                    }
                    sendedNum = 0;
                    ConnectServer();
                }
                if (m_ConnectState == EConnectState.Connected)
                {
                    string errorText = null;
                    bool   bWaiting  = false;
                    do
                    {
                        errorText = RecvAll(ref headbytes, ref databytes, ref recvedNum, ref bWaiting);
                    }while (errorText == null && !bWaiting);
                    if (errorText == null)
                    {
                        object privateLockObject;
                        Monitor.Enter(privateLockObject = m_PrivateLockObject);
                        try
                        {
                            while (m_SendQueue.Count > 0 || sendPacket != null)
                            {
                                if (sendPacket != null)
                                {
                                    errorText = SendAll(sendPacket, ref sendedNum);
                                    if (sendedNum == sendPacket.GetOffset())
                                    {
                                        sendPacket.DestroyClass();
                                        sendPacket = null;
                                    }
                                }
                                if (errorText != null || sendPacket != null || m_SendQueue.Count <= 0)
                                {
                                    break;
                                }
                                sendedNum  = 0;
                                sendPacket = m_SendQueue.Dequeue();
                                sendPacket.SetHeadLength();
                            }
                        }
                        finally
                        {
                            Monitor.Exit(privateLockObject);
                        }
                    }
                    if (errorText != null)
                    {
                        Debug.LogError(errorText);
                        object privateLockObject;
                        Monitor.Enter(privateLockObject = m_PrivateLockObject);
                        try
                        {
                            if (m_ConnectState != EConnectState.PostConnecting)
                            {
                                m_ConnectState     = EConnectState.Disconnected;
                                m_bNetStateChanged = true;
                                //GameDll.CGameProcedure.s_EventManager.OnNetStateChanged.SafeInvoke((int)NetworkProtol.Tcp, (int)m_ConnectState);
                            }
                        }
                        finally
                        {
                            Monitor.Exit(privateLockObject);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.LogError(ex.Message + ex.StackTrace);
                object privateLockObject;
                Monitor.Enter(privateLockObject = m_PrivateLockObject);
                try
                {
                    if (m_ConnectState != EConnectState.PostConnecting)
                    {
                        m_ConnectState     = EConnectState.Disconnected;
                        m_bNetStateChanged = true;
                        //GameDll.CGameProcedure.s_EventManager.OnNetStateChanged.SafeInvoke((int)NetworkProtol.Tcp, (int)m_ConnectState);
                    }
                }
                finally
                {
                    Monitor.Exit(privateLockObject);
                }
            }
        }
        m_Thread.Abort();
        m_Thread.Join();
    }
Example #10
0
    private string RecvAll(ref HeaderBytes msgHeader, ref DataBytes msgData, ref int nRecved, ref bool bWaiting)
    {
        string result;

        if (msgData == null)
        {
            SocketError socketError;
            int         recveNum = m_Socket.Receive(msgHeader.Bytes, nRecved, msgHeader.Bytes.Length - nRecved, SocketFlags.None, out socketError);
            if (recveNum < 0)
            {
                result = "消息头小于0";
                return(result);
            }
            if (socketError != SocketError.Success && socketError != SocketError.WouldBlock)
            {
                result             = "网络错误:" + socketError.ToString();
                m_ConnectState     = EConnectState.PostConnecting;
                m_bNetStateChanged = true;
                //GameDll.CGameProcedure.s_EventManager.OnNetStateChanged.SafeInvoke((int)NetworkProtol.Tcp, (int)m_ConnectState);
                return(result);
            }
            if (recveNum == 0)
            {
                bWaiting = true;
                result   = null;
                return(result);
            }
            nRecved += recveNum;
            if (nRecved == msgHeader.Bytes.Length)
            {
                ushort msgType   = 0;
                ushort msgLength = 0;
                if (!WfPacket.ParseHeader(msgHeader.Bytes, ref msgType, ref msgLength))
                {
                    result = string.Concat(new object[]
                    {
                        "error ParseHeader type:",
                        msgType,
                        "len:",
                        msgLength
                    });
                    return(result);
                }
                if (msgLength < msgHeader.Bytes.Length)
                {
                    result = string.Concat(new object[]
                    {
                        "error ParseHeader < msglen:", msgLength, "headerLength:", msgHeader.Bytes.Length
                    });
                    return(result);
                }
                if (msgLength == msgHeader.Bytes.Length)
                {
                    //这里虽然只有一个是一个很简单的只带有消息头的消息,例如心跳,但是我依然用了完整消息大小
                    msgData = PooledClassManager <DataBytes> .CreateClass();

                    msgData.SetBytes(msgLength + 1);
                    Array.Copy(msgHeader.Bytes, 0, msgData.GetBytes(), 0, msgHeader.Bytes.Length);
                    WfPacket item = PooledClassManager <WfPacket> .CreateClass();

                    item.InitRead(msgData);
                    msgData = null;
                    nRecved = 0;
                    object privateLockObject;
                    Monitor.Enter(privateLockObject = m_PrivateLockObject);
                    try
                    {
                        m_RecvQueue.Enqueue(item);
                    }
                    finally
                    {
                        Monitor.Exit(privateLockObject);
                    }
                }
                else
                {
                    msgData = PooledClassManager <DataBytes> .CreateClass();

                    msgData.SetBytes(msgLength);
                    Array.Copy(msgHeader.Bytes, 0, msgData.GetBytes(), 0, msgHeader.Bytes.Length);
                    nRecved = msgHeader.Bytes.Length;
                }
            }
        }
        if (msgData != null)
        {
            SocketError socketError;
            int         recveNum = m_Socket.Receive(msgData.GetBytes(), nRecved, msgData.GetLength() - nRecved, SocketFlags.None, out socketError);
            //Debug.Log("底层函数接收数据:" + socketError.ToString());
            if (recveNum < 0)
            {
                result = "ReceiveData < 0";
                return(result);
            }
            if (socketError != SocketError.Success && socketError != SocketError.WouldBlock)
            {
                result = "ReceiveData Failed";
                return(result);
            }
            if (recveNum == 0)
            {
                bWaiting = true;
                result   = null;
                return(result);
            }
            nRecved += recveNum;
            if (nRecved > msgData.GetLength())
            {
                result = "ReceiveData IO error";
                return(result);
            }
            if (nRecved == msgData.GetLength())
            {
                WfPacket item = PooledClassManager <WfPacket> .CreateClass();

                item.InitRead(msgData);
                msgData = null;
                nRecved = 0;
                object privateLockObject;
                Monitor.Enter(privateLockObject = m_PrivateLockObject);
                try
                {
                    m_RecvQueue.Enqueue(item);
                }
                finally
                {
                    Monitor.Exit(privateLockObject);
                }
            }
            else
            {
                bWaiting = true;
            }
        }
        result = null;
        return(result);
    }
Example #11
0
 public void InitRead(DataBytes data)
 {
     m_Offset    = 0;
     m_ReadBytes = data;
 }