Exemplo n.º 1
0
        public async Task <byte[]> ReceiveAsync(SocketFlags flags, int length, bool isStrict)
        {
            try
            {
                var buffer     = new byte[length];
                int readLength = await ReceiveAsync(buffer,
                                                    0, length, flags).ConfigureAwait(false);

                if (!isStrict)
                {
                    var readData = new byte[readLength];
                    Buffer.BlockCopy(buffer, 0, readData, 0, readLength);

                    buffer = readData;
                }
                while (isStrict && (readLength < length))
                {
                    int bytesLeft = (length - readLength);

                    readLength += await ReceiveAsync(buffer,
                                                     readLength, bytesLeft, flags).ConfigureAwait(false);
                }

                Decrypter?.Parse(buffer);
                return(buffer);
            }
            catch { return(null); }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Receives an array of type <see cref="byte"/> that contains data convertible to an <see cref="HMessage"/> in an asynchronous operation.
        /// </summary>
        /// <returns></returns>
        public async Task <byte[]> ReceiveAsync()
        {
            byte[] lengthBlock = new byte[4];
            await ReceiveAsync(lengthBlock, 0, 4).ConfigureAwait(false);

            Decrypter?.Parse(lengthBlock);
            int bodyLength = BigEndian.DecypherInt(lengthBlock);

            int bytesRead      = 0;
            int totalBytesRead = 0;

            byte[] body = new byte[bodyLength];
            while (totalBytesRead != body.Length)
            {
                byte[] block = new byte[bodyLength - totalBytesRead];

                bytesRead = await ReceiveAsync(block, 0, block.Length)
                            .ConfigureAwait(false);

                Buffer.BlockCopy(block, 0, body, totalBytesRead, bytesRead);
                totalBytesRead += bytesRead;
            }
            Decrypter?.Parse(body);

            byte[] packet = new byte[4 + body.Length];
            Buffer.BlockCopy(lengthBlock, 0, packet, 0, 4);
            Buffer.BlockCopy(body, 0, packet, 4, body.Length);

            return(packet);
        }
Exemplo n.º 3
0
        public async Task <byte[]> ReceiveAsync(SocketFlags flags, int length, bool isStrict)
        {
            var buffer = new byte[length];

            try
            {
                int readLength = await ReceiveAsync(
                    buffer, 0, length, flags).ConfigureAwait(false);

                if (!isStrict)
                {
                    var readData = new byte[readLength];
                    Buffer.BlockCopy(buffer, 0, readData, 0, readLength);
                    buffer = readData;
                }
                while (isStrict && (readLength < length))
                {
                    int bytesLeft = (length - readLength);

                    readLength += await ReceiveAsync(buffer,
                                                     readLength, bytesLeft, flags).ConfigureAwait(false);

                    if (readLength == 0 && bytesLeft > 0)
                    {
                        buffer = null;
                        break;
                    }
                }

                if (buffer != null)
                {
                    Decrypter?.Parse(buffer);
                }
            }
            catch { buffer = null; }
            finally
            {
                if (buffer == null)
                {
                    Disconnect();
                }
            }
            return(buffer);
        }