Exemple #1
0
        /// <summary>
        /// 字节数组转化为数据包
        /// </summary>
        public static DataPackage Parse(byte[] bytes)
        {
            DynamicBuffer buffer = new DynamicBuffer(bytes);

            int packetType = 0;

            if (buffer.TryReadInt(ref packetType))
            {
                buffer.DiscardReadBytes();
                return(new DataPackage(buffer, packetType));
            }
            else
            {
                return(null);
            }
        }
Exemple #2
0
        public static DataPackage UnPack(this DynamicBuffer dynamicBuffer)
        {
            dynamicBuffer.MarkReaderIndex();
            int messageLength = 0;

            if (dynamicBuffer.TryReadInt(ref messageLength))
            {
                int readByteLength = dynamicBuffer.ReadableBytes();
                if (messageLength > readByteLength)
                {
                    dynamicBuffer.ResetReaderIndex();
                    return(null);
                }
                else
                {
                    byte[] messageBytes = new byte[messageLength];
                    dynamicBuffer.ReadBytes(messageBytes, 0, messageLength);
                    dynamicBuffer.DiscardReadBytes();
                    return(DataPackage.Parse(messageBytes));
                }
            }
            return(null);
        }