コード例 #1
0
        public static MessageHeader ParseHeader(byte[] bytes)
        {
            if (bytes.Length != MsgHeaderLength)
            {
                throw new Exception("参数错误");
            }
            MessageHeader recieveHeader = new MessageHeader();
            var           cursor        = 0;

            var shortLength = BuildTcpProto.GetTypeLength <short>();
            var intLength   = BuildTcpProto.GetTypeLength <int>();
            var uintLength  = BuildTcpProto.GetTypeLength <uint>();

            var signBytes = bytes.Take(shortLength).ToArray();

            recieveHeader.StartSign = Util.BytesToShort(signBytes);
            cursor += signBytes.Length;

            var lengthBytes = bytes.Skip(cursor).Take(intLength).ToArray();

            recieveHeader.Length = Util.BytesToInt(lengthBytes);
            cursor += lengthBytes.Length;

            var crc32Bytes = bytes.Skip(cursor).Take(intLength).ToArray();

            recieveHeader.Crc32 = Util.BytesToUInt(crc32Bytes);
            cursor += crc32Bytes.Length;

            return(recieveHeader);
        }
コード例 #2
0
        private void DealtMessage(byte[] buffer, ref byte[] tempBytes, ref int cursor)
        {
            if (buffer == null || buffer.Length == 0)
            {
                return;
            }
            if (tempBytes == null)
            {
                tempBytes = new byte[ConfigsHelper.BufferSize];
            }
            //总字节长度
            var cacheLength = buffer.Length + cursor;
            //需要扩展的空间倍数
            var ext = (int)Math.Ceiling((cacheLength - tempBytes.Length) / (double)ConfigsHelper.BufferSize);

            if (ext > 0)
            {
                var extBytes = new byte[ext * ConfigsHelper.BufferSize];
                tempBytes = tempBytes.Concat(extBytes).ToArray();
            }
            buffer.CopyTo(tempBytes, cursor);
            cursor += buffer.Length;

            while (cursor >= BuildTcpProto.MsgHeaderLength)
            {
                var headerBytes = tempBytes.Take(BuildTcpProto.MsgHeaderLength).ToArray();
                var header      = BuildTcpProto.ParseHeader(headerBytes);
                if (cursor >= header.Length)
                {
                    var msgBytes     = tempBytes.Take(header.Length).ToArray();
                    int surplusCount = cursor - header.Length;
                    var surplusBytes = tempBytes.Skip(header.Length).Take(surplusCount).ToArray();
                    tempBytes = surplusBytes;
                    cursor   -= header.Length;
                    var msgstr = BuildTcpProto.ParseMessage(header, msgBytes);
                    Console.WriteLine(msgstr);
                }
                else
                {
                    break;
                }
            }
        }
コード例 #3
0
        public static MessageHeader SeperatePecketBuffer(byte[] pBytes, out string message)
        {
            var           msgResult     = string.Empty;
            var           headerLength  = GetStructLength <MessageHeader>();
            var           headerBytes   = pBytes.Take(headerLength).ToArray();
            var           messageBytes  = pBytes.Skip(headerLength).Take(pBytes.Length - headerLength).ToArray();
            MessageHeader recieveHeader = new MessageHeader();
            var           cursor        = 0;

            var shortLength = BuildTcpProto.GetTypeLength <short>();
            var intLength   = BuildTcpProto.GetTypeLength <int>();
            var uintLength  = BuildTcpProto.GetTypeLength <uint>();

            var signBytes = headerBytes.Take(shortLength).ToArray();

            recieveHeader.StartSign = Util.BytesToShort(signBytes);
            cursor += signBytes.Length;

            var lengthBytes = headerBytes.Skip(cursor).Take(intLength).ToArray();

            recieveHeader.Length = Util.BytesToInt(lengthBytes);
            cursor += lengthBytes.Length;

            var crc32Bytes = headerBytes.Skip(cursor).Take(intLength).ToArray();

            recieveHeader.Crc32 = Util.BytesToUInt(crc32Bytes);
            cursor += crc32Bytes.Length;

            var rmsg   = Encoding.UTF8.GetString(messageBytes);
            var rCrc32 = Crc32Algorithm.Compute(messageBytes, 0, messageBytes.Length);

            if (rCrc32 == recieveHeader.Crc32)
            {
                message = rmsg;
            }
            else
            {
                throw new Exception("wdnmd");
            }

            return(recieveHeader);
        }