コード例 #1
0
        public SocketHolder(CancellationToken token)
        {
            _token = token;

            _inputBuffer  = new ByteRingBuffer(token);
            _outputBuffer = new ByteRingBuffer(token);

            _inputRingBufferStream  = new RingBufferStreamAdapter(_inputBuffer);
            _outputRingBufferStream = new RingBufferStreamAdapter(_outputBuffer);
        }
コード例 #2
0
		public SocketHolder(CancellationToken token)
		{
			_token = token;

			_inputBuffer = new ByteRingBuffer(token);
			_outputBuffer = new ByteRingBuffer(token);

			_inputRingBufferStream = new RingBufferStreamAdapter(_inputBuffer);
			_outputRingBufferStream = new RingBufferStreamAdapter(_outputBuffer);
		}
コード例 #3
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;
        }
コード例 #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);
        }
コード例 #5
0
        public SocketRingBuffers(Socket socket, CancellationToken cancellationToken, Action notifyWhenClosed, Action flushWrite)
        {
            _notifyWhenClosed = notifyWhenClosed;

            _inputBuffer = new ByteRingBuffer(cancellationToken);
            _outputBuffer = new ByteRingBuffer(cancellationToken);

            _inputRingBufferStream = new RingBufferStreamAdapter(_inputBuffer);
            _outputRingBufferStream = new RingBufferStreamAdapter(_outputBuffer);

            // WriteLoop
            _socketConsumer = new SocketConsumer(socket, _outputBuffer, cancellationToken, flushWrite);
            _socketConsumer.OnNotifyClosed += OnSocketClosed;

            // ReadLoop
            _socketProducer = new SocketProducer(socket, _inputBuffer, cancellationToken);
            _socketProducer.OnNotifyClosed += OnSocketClosed;

            Writer = new InternalBigEndianWriter(_outputRingBufferStream);
            Reader = new InternalBigEndianReader(_inputRingBufferStream);
        }
コード例 #6
0
        public SocketRingBuffers(Socket socket, CancellationToken cancellationToken, Action notifyWhenClosed, Action flushWrite)
        {
            _notifyWhenClosed = notifyWhenClosed;

            _inputBuffer  = new ByteRingBuffer(cancellationToken);
            _outputBuffer = new ByteRingBuffer(cancellationToken);

            _inputRingBufferStream  = new RingBufferStreamAdapter(_inputBuffer);
            _outputRingBufferStream = new RingBufferStreamAdapter(_outputBuffer);

            // WriteLoop
            _socketConsumer = new SocketConsumer(socket, _outputBuffer, cancellationToken, flushWrite);
            _socketConsumer.OnNotifyClosed += OnSocketClosed;

            // ReadLoop
            _socketProducer = new SocketProducer(socket, _inputBuffer, cancellationToken);
            _socketProducer.OnNotifyClosed += OnSocketClosed;

            Writer = new InternalBigEndianWriter(_outputRingBufferStream);
            Reader = new InternalBigEndianReader(_inputRingBufferStream);
        }
コード例 #7
0
 public RingBufferPositionMarker(RingBufferStreamAdapter ringBuffer)
 {
     _mbStreamWrapper = null;
     _ringBuffer      = ringBuffer._ringBuffer;
     _start           = _ringBuffer.GlobalReadPos;
 }
コード例 #8
0
 public SocketHolder()
 {
     _inputBuffer           = new ByteRingBuffer();
     _inputRingBufferStream = new RingBufferStreamAdapter(_inputBuffer);
 }
コード例 #9
0
		internal InternalBigEndianReader(RingBufferStreamAdapter ringBufferStream)
		{
			_ringBufferStream = ringBufferStream;
		}
コード例 #10
0
 public MultiBodyStreamWrapper(RingBufferStreamAdapter innerStream, int firstFrameLen, long totalBodyLen)
 {
     _innerStream         = innerStream;
     _currentFrameLenLeft = firstFrameLen;
     _remainingTotalBody  = totalBodyLen - firstFrameLen;
 }
コード例 #11
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();
        }
コード例 #12
0
 internal InternalBigEndianReader(RingBufferStreamAdapter ringBufferStream)
 {
     _ringBufferStream = ringBufferStream;
 }
        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();
        }