Ejemplo n.º 1
0
        // Note: *not* consuming the final FrameEnd is the correct behavior.
        public override int Read(byte[] buffer, int offset, int count)
        {
            int toRead;

tryAgain:   // label/goto vs fake while loop...
            {
                toRead = Math.Min(_currentFrameLenLeft, count);

                if (toRead == 0)
                {
                    if (_remainingTotalBody == 0)
                    {
                        return(0);                        // EOF
                    }

                    ParseFrame();                     // updates _currentFrameLenLeft and _remainingTotalBody

                    goto tryAgain;
                }
            }

            var read = _innerStream.Read(buffer, offset, toRead, fillBuffer: true);

            _position            += read;
            _currentFrameLenLeft -= read;

            return(read);
        }
Ejemplo n.º 2
0
        public static byte[] Copy(RingBufferStreamAdapter stream, int bodySize)
        {
            if (bodySize == 0) return Empty;

            var buffer = new byte[bodySize]; // more allocations. sad!
            var read = stream.Read(buffer, 0, (int)bodySize);

            if (read != bodySize) throw new Exception("Read less than body size");

            return buffer;
        }
		public void FillBufferWithLock(byte[] buffer, int count, bool reverse = true)
		{
			int totalRead = 0;
			while (totalRead < count)
			{
				totalRead += _ringBufferStream.Read(buffer, totalRead, count - totalRead, fillBuffer: true);
			}
			if (reverse && BitConverter.IsLittleEndian && count > 1)
			{
				Array.Reverse(buffer);
			}
		}
Ejemplo n.º 4
0
        public static byte[] Copy(RingBufferStreamAdapter stream, int bodySize)
        {
            if (bodySize == 0)
            {
                return(Empty);
            }

            var buffer = new byte[bodySize];             // more allocations. sad!
            var read   = stream.Read(buffer, 0, (int)bodySize);

            if (read != bodySize)
            {
                throw new Exception("Read less than body size");
            }

            return(buffer);
        }
Ejemplo n.º 5
0
        public void WritesOf31ReadsOf31_BufferOf32()
        {
            var autoResEv = new AutoResetEvent(false);

            Socket acceptedSocket = null;
            var    socketOut      = new Socket(SocketType.Stream, ProtocolType.Tcp);
            var    socketAccept   = new Socket(SocketType.Stream, ProtocolType.Tcp);

            socketAccept.Bind(new IPEndPoint(IPAddress.Any, 6868));

            var socketEvArgs = new SocketAsyncEventArgs();

            socketEvArgs.Completed += (sender, args) =>
            {
                acceptedSocket = args.AcceptSocket;
                autoResEv.Set();
            };
            socketAccept.Listen(1);
            if (!socketAccept.AcceptAsync(socketEvArgs))
            {
                acceptedSocket = socketEvArgs.AcceptSocket;
            }

            socketOut.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
            socketOut.Connect(new IPEndPoint(IPAddress.Loopback, 6868));

            autoResEv.WaitOne();

            acceptedSocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);

            var cancellationTokenSrc = new CancellationTokenSource();
            var cancellationToken    = cancellationTokenSrc.Token;

            var inputBuffer  = new ByteRingBuffer(cancellationToken);
            var outputBuffer = new ByteRingBuffer(cancellationToken);

            var inputRingBufferStream  = new RingBufferStreamAdapter(inputBuffer);
            var outputRingBufferStream = new RingBufferStreamAdapter(outputBuffer);

            // WriteLoop
            var socketConsumer = new SocketConsumer(socketOut, outputBuffer, cancellationToken, () =>
            {
            });

            // ReadLoop
            var socketProducer = new SocketProducer(acceptedSocket, inputBuffer, cancellationToken);


            var input  = new byte[1025];
            var output = new byte[1025];

            for (int j = 0; j < input.Length; j++)
            {
                input[j] = (byte)(j % 256);
            }

            for (ulong i = 0L; i < 1000000; i++)
            {
                outputRingBufferStream.Write(input, 0, input.Length);

                var read = inputRingBufferStream.Read(output, 0, output.Length);
                read.Should().Be(output.Length);

                for (int x = 0; x < output.Length; x++)
                {
                    output[x].Should().Be((byte)(x % 256), "Iteration " + i + " pos " + x);
                }

                if (i % 10000 == 0)
                {
                    Console.WriteLine("Iteration " + i);
                }
            }

            cancellationTokenSrc.Cancel();
            socketOut.Close();
            socketAccept.Close();
        }
        public void WritesOf31ReadsOf31_BufferOf32()
        {
            var autoResEv = new AutoResetEvent(false);

            Socket acceptedSocket = null;
            var socketOut = new Socket(SocketType.Stream, ProtocolType.Tcp);
            var socketAccept = new Socket(SocketType.Stream, ProtocolType.Tcp);
            socketAccept.Bind(new IPEndPoint(IPAddress.Any, 6868));

            var socketEvArgs = new SocketAsyncEventArgs();
            socketEvArgs.Completed += (sender, args) =>
            {
                acceptedSocket = args.AcceptSocket;
                autoResEv.Set();
            };
            socketAccept.Listen(1);
            if (!socketAccept.AcceptAsync(socketEvArgs))
            {
                acceptedSocket = socketEvArgs.AcceptSocket;
            }

            socketOut.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
            socketOut.Connect(new IPEndPoint(IPAddress.Loopback, 6868));

            autoResEv.WaitOne();

            acceptedSocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);

            var cancellationTokenSrc = new CancellationTokenSource();
            var cancellationToken = cancellationTokenSrc.Token;

            var inputBuffer = new ByteRingBuffer(cancellationToken);
            var outputBuffer = new ByteRingBuffer(cancellationToken);

            var inputRingBufferStream = new RingBufferStreamAdapter(inputBuffer);
            var outputRingBufferStream = new RingBufferStreamAdapter(outputBuffer);

            // WriteLoop
            var socketConsumer = new SocketConsumer(socketOut, outputBuffer, cancellationToken, () =>
            {

            });

            // ReadLoop
            var socketProducer = new SocketProducer(acceptedSocket, inputBuffer, cancellationToken);

            var input = new byte[1025];
            var output = new byte[1025];
            for (int j = 0; j < input.Length; j++)
            {
                input[j] = (byte)(j % 256);
            }

            for (ulong i = 0L; i < 1000000; i++)
            {
                outputRingBufferStream.Write(input, 0, input.Length);

                var read = inputRingBufferStream.Read(output, 0, output.Length);
                read.Should().Be(output.Length);

                for (int x = 0; x < output.Length; x++)
                {
                    output[x].Should().Be((byte)(x % 256), "Iteration " + i + " pos " + x);
                }

                if (i % 10000 == 0)
                {
                    Console.WriteLine("Iteration " + i);
                }
            }

            cancellationTokenSrc.Cancel();
            socketOut.Close();
            socketAccept.Close();
        }