예제 #1
0
        public void EnsureCapacity(int size)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("this", "Dispose() has already been called on this object.");
            }

            if (buffer.Array.Length >= size)
            {
                return;
            }

            buffer.Dispose();
            buffer = new Rent <byte>(size);
        }
예제 #2
0
        async ValueTask <Rent <byte> > ReceivePacketAsync(CancellationToken token)
        {
            byte[] lengthBuffer = new byte[4];
            if (!await tcpClient.ReadChunkAsync(lengthBuffer, 4, token))
            {
                throw new IOException("Partner closed connection");
            }

            int length = BitConverter.ToInt32(lengthBuffer, 0);

            if (length == 0)
            {
                return(Rent.Empty <byte>());
            }

            var readBuffer = new Rent <byte>(length);

            try
            {
                if (!await tcpClient.ReadChunkAsync(readBuffer.Array, length, token))
                {
                    throw new IOException("Partner closed connection");
                }
            }
            catch (Exception)
            {
                readBuffer.Dispose();
                throw;
            }

            return(readBuffer);
        }