public TcpChannel(IPipeline pipeline, BufferPool pool)
 {
     _pipeline = pipeline;
     Pipeline.SetChannel(this);
     _readBuffer = pool.PopSlice();
     _stream = new PeekableMemoryStream(_readBuffer.Buffer, _readBuffer.StartOffset, _readBuffer.Capacity);
 }
        public Sent(BufferSlice bufferSlice)
        {
            if (bufferSlice == null)
                throw new ArgumentNullException("bufferSlice");

            BufferSlice = bufferSlice;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="BufferPoolStream"/> class.
 /// </summary>
 /// <param name="slice">The slice.</param>
 public BufferPoolStream(BufferSlice slice)
     : base(slice.Buffer, slice.StartOffset, slice.Capacity, true, true)
 {
     _slize = slice;
     SetLength(slice.Count);
     Position = slice.Position;
 }
        public void ParseHeader_SkipBody()
        {
            const string HttpPost = @"POST / HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 11
Origin: http://localhost:8080
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept: */*
Referer: http://localhost:8080/ajaxPost.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: sv,en;q=0.8,en-US;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: ASP.NET_SessionId=5vkr4tfivb1ybu1sm4u4kahy; GriffinLanguageSwitcher=sv-se; __RequestVerificationToken=LiTSJATsiqh8zlcft_3gZwvY8HpcCUkirm307njxIZLdsJSYyqaV2st1tunH8sMvMwsVrj3W4dDoV8ECZRhU4s6DhTvd2F-WFkgApDBB-CA1; .ASPXAUTH=BF8BE1C246428B10B49AE867BEDF9748DB3842285BC1AF1EC44AD80281C4AE084B75F0AE13EAF1BE7F71DD26D0CE69634E83C4846625DC7E4D976CA1845914E2CC7A7CF2C522EA5586623D9B73B0AE433337FC59CF6AF665DC135491E78978EF

hello=world";
            string actual = "";
            var buffer = new BufferSlice(Encoding.ASCII.GetBytes(HttpPost), 0, HttpPost.Length);
            var stream = new SliceStream(buffer, buffer.Count);
            var parser = new HttpHeaderParser();
            parser.HeaderParsed += (sender, args) => actual = args.Value;
            parser.Parse(stream);

            Assert.Equal("ASP.NET_SessionId=5vkr4tfivb1ybu1sm4u4kahy; GriffinLanguageSwitcher=sv-se; __RequestVerificationToken=LiTSJATsiqh8zlcft_3gZwvY8HpcCUkirm307njxIZLdsJSYyqaV2st1tunH8sMvMwsVrj3W4dDoV8ECZRhU4s6DhTvd2F-WFkgApDBB-CA1; .ASPXAUTH=BF8BE1C246428B10B49AE867BEDF9748DB3842285BC1AF1EC44AD80281C4AE084B75F0AE13EAF1BE7F71DD26D0CE69634E83C4846625DC7E4D976CA1845914E2CC7A7CF2C522EA5586623D9B73B0AE433337FC59CF6AF665DC135491E78978EF", actual);
            Assert.Equal('h', (char)buffer.Buffer[stream.Position]);
        }
        public void Write_TooSmallInternalBuffer()
        {
            var slice = new BufferSlice(5);
            var stream = new SliceStream(slice);

            var mammasBullar = Encoding.UTF8.GetBytes("Mammas bullar smakar godast.");
            Assert.Throws<ArgumentOutOfRangeException>(() => stream.Write(mammasBullar, 0, 6));
        }
        public void Write_WrongOffset()
        {
            var slice = new BufferSlice(5);
            var stream = new SliceStream(slice);

            var mammasBullar = Encoding.UTF8.GetBytes("Mammas bullar smakar godast.");
            Assert.Throws<ArgumentOutOfRangeException>(() => stream.Write(mammasBullar, -1, 1));
        }
        public void Test()
        {
            var slice = new BufferSlice(65535);
            var stream = new SliceStream(slice);

            Assert.Equal(0, stream.Position);
            Assert.Equal(0, stream.Length);
            Assert.Equal(65535, ((IBufferWrapper)stream).Capacity);
        }
        public BufferSliceReaderTest()
        {
            _sourceBuffer = Encoding.ASCII.GetBytes("Hello world!\r\nSays \"SomeONe not powerful\"");
            _buffer = new byte[65535];
            Buffer.BlockCopy(_sourceBuffer, 0, _buffer, Offset, _sourceBuffer.Length);
            _slice = new BufferSlice(_buffer, Offset, Length) {Count = _sourceBuffer.Length};

            _reader = new BufferSliceReader(_slice);
        }
 /// <summary>
 /// Will serialize messages
 /// </summary>
 /// <param name="message"></param>
 public virtual void Write(object message)
 {
     var formatter = _formatterFactory.CreateSerializer();
     var buffer = new BufferSlice(65535);
     var writer = new SliceStream(buffer);
     formatter.Serialize(message, writer);
     writer.Position = 0;
     Send(buffer, (int) writer.Length);
 }
        public void InitWithWrittenBuffer()
        {
            var buffer = Encoding.ASCII.GetBytes("Hello world");
            var slice = new BufferSlice(buffer, 0, buffer.Length, buffer.Length);
            Assert.Equal(buffer.Length, slice.Count);
            Assert.Equal(buffer.Length, slice.Capacity);

            // position = 0
            Assert.Equal(buffer.Length, slice.RemainingLength);
        }
        public void TestConsumeUntilSpace()
        {
            var buffer = Encoding.ASCII.GetBytes("Hello world.!");
            var slice = new BufferSlice(buffer, 0, buffer.Length, buffer.Length);
            var reader = new BufferSliceReader(slice);

            reader.ConsumeUntil(' ');
            Assert.Equal(' ', reader.Current);
            Assert.Equal('w', reader.Peek);
        }
 public void ReadSome()
 {
     var slice = new byte[65535];
     var buffer = new BufferSlice(slice, 0, slice.Length, slice.Length);
     buffer.Position += 10;
     Assert.Equal(65535, buffer.Capacity);
     Assert.Equal(10, buffer.Position);
     Assert.Equal(0, buffer.StartOffset);
     Assert.Equal(65525, buffer.RemainingCapacity);
 }
 public void InitUsingByteArray()
 {
     var initial = "Hello world!";
     var initialBuffer = Encoding.ASCII.GetBytes(initial);
     var slice = new BufferSlice(initialBuffer, 0, initialBuffer.Length);
     var stream = new SliceStream(slice, initialBuffer.Length);
     Assert.Equal(initial.Length, stream.Length);
     Assert.Equal(initial.Length, ((IBufferWrapper) stream).Capacity);
     Assert.Equal(0, stream.Position);
 }
        public void TestContains()
        {
            var buffer = Encoding.ASCII.GetBytes("Hello  \tworld.!");
            var slice = new BufferSlice(buffer, 0, buffer.Length);
            var reader = new StringBufferSliceReader(slice, slice.Count);

            reader.Contains(' ');
            Assert.Equal('H', reader.Current);
            Assert.Equal('e', reader.Peek);
        }
Beispiel #15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TcpChannel"/> class.
 /// </summary>
 /// <param name="pipeline">The pipeline used to send messages upstream.</param>
 /// <param name="pool">Buffer pool.</param>
 public TcpChannel(IPipeline pipeline, BufferPool pool)
 {
     _pipeline = pipeline;
     _pool = pool;
     Pipeline.SetChannel(this);
     if (pool == null)
         _readBuffer = new BufferSlice(new byte[65535], 0, 65535, 0);
     else
         _readBuffer = pool.PopSlice();
     _stream = new PeekableMemoryStream(_readBuffer.Buffer, _readBuffer.StartOffset, _readBuffer.Capacity);
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="ClientExceptionEventArgs" /> class.
        /// </summary>
        /// <param name="clientContext">The client context.</param>
        /// <param name="exception">The exception.</param>
        /// <param name="buffer"></param>
        /// <exception cref="System.ArgumentNullException">clientContext</exception>
        public ClientExceptionEventArgs(IServerClientContext clientContext, Exception exception, BufferSlice buffer)
        {
            if (clientContext == null) throw new ArgumentNullException("clientContext");
            if (exception == null) throw new ArgumentNullException("exception");
            if (buffer == null) throw new ArgumentNullException("buffer");

            ClientContext = clientContext;
            Exception = exception;
            Buffer = buffer;
            CanContinue = true;
        }
        public void TestConsumeUntilSpaceAndWhiteSpaces()
        {
            var buffer = Encoding.ASCII.GetBytes("Hello  \tworld.!");
            var slice = new BufferSlice(buffer, 0, buffer.Length);
            var reader = new StringBufferSliceReader(slice, slice.Count);

            reader.ConsumeUntil(' ');
            reader.ConsumeWhiteSpaces();
            Assert.Equal('w', reader.Current);
            Assert.Equal('o', reader.Peek);
        }
        /// <summary>
        /// Send a message
        /// </summary>
        /// <param name="message">Message to send</param>
        /// <remarks>Message will be serialized using the <see cref="IMessageFormatterFactory"/> that you've specified in the constructor.</remarks>
        public void Send(object message)
        {
            if (message == null) throw new ArgumentNullException("message");

            var serializer = _formatterFactory.CreateSerializer();
            var buffer = new BufferSlice(65535);
            var writer = new BufferWriter(buffer);
            serializer.Serialize(message, writer);

            Send(buffer, writer.Count);
        }
        public void Parse()
        {
            var buffer = Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\nSERVER: LOCALHOST\r\n\r\n");
            var slice = new BufferSlice(buffer, 0, buffer.Length);
            var reader = new SliceStream(slice);


            var parser = new HttpHeaderParser();
            parser.HeaderParsed += (sender, args) => Console.WriteLine(args.Name + ": " + args.Value);
            parser.Parse(reader);
        }
Beispiel #20
0
        public BufferPoolStream2(BufferPool bufferPool)
        {
            _bufferPool = bufferPool;
            _canWrite   = true;
            _slice      = bufferPool.PopSlice();

            _buffer       = _slice.Buffer;
            _capacity     = _slice.Capacity;
            _length       = _slice.Count;
            _position     = _slice.Position;
            _initialIndex = _slice.StartOffset;
        }
        public void TestInit()
        {
            var buffer = Encoding.ASCII.GetBytes("Hello world.!");
            var slice = new BufferSlice(buffer, 0, buffer.Length);
            var reader = new StringBufferSliceReader(slice, slice.Count);

            Assert.Equal(slice.Count, reader.Length);
            Assert.Equal(slice.Offset - slice.Count, reader.RemainingLength);
            Assert.Equal('H', reader.Current);
            Assert.Equal('e', reader.Peek);
            Assert.True(reader.HasMore);
        }
        public void TestConsume()
        {
            var buffer = Encoding.ASCII.GetBytes("Hello world.!");
            var slice = new BufferSlice(buffer, 0, buffer.Length, buffer.Length); 

            var reader = new BufferSliceReader(slice);

            reader.Consume();
            Assert.Equal(slice.Count, reader.Length);
            Assert.Equal(slice.RemainingLength, reader.RemainingLength);
            Assert.Equal('e', reader.Current);
            Assert.Equal('l', reader.Peek);
            Assert.True(reader.HasMore);
        }
        public void Write_Single()
        {
            var slice = new BufferSlice(65535);
            var stream = new SliceStream(slice);

            var mammasBullar = Encoding.UTF8.GetBytes("Mammas bullar smakar godast.");
            stream.Write(mammasBullar, 0, mammasBullar.Length);

            Assert.Equal(mammasBullar.Length, stream.Position);
            Assert.Equal(mammasBullar.Length, stream.Length);

            // must be able to write after the last byte.
            stream.Position = mammasBullar.Length;
        }
        public void Create()
        {
            var slice = new byte[65535];
            var buffer = new BufferSlice(slice, 0, slice.Length, slice.Length);
            Assert.Equal(65535, buffer.Capacity);
            Assert.Equal(0, buffer.Position);
            Assert.Equal(0, buffer.StartOffset);
            Assert.Equal(65535, buffer.RemainingCapacity);

            // since offset = 0
            Assert.Equal(65535, buffer.RemainingLength);

            Assert.Equal(65535, buffer.Count);
        }
        private BufferSlice EncodeCommand(ICommand command)
        {
            /*var str = command is AuthCmd || command is SubscribeOnEvents
                          ? command.ToFreeSwitchString() + "\n\n"
                          : "bgapi " + command.ToFreeSwitchString() + "\n\n";
             * */
            var str = command.ToFreeSwitchString() + "\n\n";

            var cmd = Encoding.ASCII.GetBytes(str);
            _logger.Debug("Encoded: " + str);
            var slice = new BufferSlice(cmd, 0, cmd.Length, cmd.Length);
            _logger.Debug("Slice " + slice.Position + "/" + slice.Count);
            return slice;
        }
Beispiel #26
0
        public void Test()
        {
            var buffer = new byte[65535];
            byte[] hello = Encoding.ASCII.GetBytes("Hello world!");
            Buffer.BlockCopy(hello, 0, buffer, 1024, hello.Length);
            var slice = new BufferSlice(buffer, 1024, 1024) {Count = hello.Length};

            Assert.Equal(hello.Length, slice.RemainingCount);
            Assert.Equal(hello.Length, slice.Count);

            slice.CurrentOffset += 1;
            Assert.Equal(hello.Length - 1, slice.RemainingCount);
            Assert.Equal(hello.Length, slice.Count);
        }
        public void SlicedBuffer()
        {
            var slice = new byte[65535];
            var buffer = new BufferSlice(slice, 32768, 32768, 32768);
            Assert.Equal(32768, buffer.Capacity);
            Assert.Equal(32768, buffer.Position);
            Assert.Equal(32768, buffer.StartOffset);
            Assert.Equal(32768, buffer.RemainingCapacity);

            buffer.Position += 10;
            Assert.Equal(32768, buffer.Capacity);
            Assert.Equal(32778, buffer.Position);
            Assert.Equal(32768, buffer.StartOffset);
            Assert.Equal(32758, buffer.RemainingCapacity);
        }
Beispiel #28
0
        public virtual void Initialize(TcpChannelConfig config)
        {
            Socket = config.Socket;
            _remoteEndPoint = Socket.RemoteEndPoint;
            _localEndPoint = Socket.LocalEndPoint;
            _channelConfig = config;
            _sendArgs = config.SocketAsyncEventPool.Pull();
            _sendArgs.Completed += WriteCompleted;
            _readArgs = config.SocketAsyncEventPool.Pull();
            _readArgs.Completed += ReadCompleted;
            _readSlice = config.BufferManager.AssignBufferTo(_readArgs);
            _readSlice.Count = 0;
            //_inbufferStream = new MemoryStream(_readArgs.Buffer, _readArgs.Offset, _readArgs.Count);
            //_inbufferStream.SetLength(0);

            Initialize();
        }
Beispiel #29
0
        public IMessage Parse(BufferSlice slice)
        {
            _reader.Assign(slice);

            string read = System.Text.Encoding.UTF8.GetString(_reader.Buffer);
            _logger.Trace("Parsing method: " + _parserMethod.Method.Name);
            while (_parserMethod())
            {
                _logger.Trace("Next parsing method: " + _parserMethod.Method.Name);
            }
                

            if (_isComplete)
                return _message;

            return null;
        }
Beispiel #30
0
        public void Send(BufferSlice bufferSlice)
        {
            if (bufferSlice == null)
                throw new ArgumentNullException("bufferSlice");

            _sendQueue.Enqueue(bufferSlice);
            if (_isSending)
                return;

            lock (_sendQueue)
            {
                if (_isSending)
                    return;
                _isSending = true;
            }

            SendFirstBuffer();
        }
        public void InitUsingWrittenAndWriteMore()
        {
            var initial = "Hello world!";
            var addition = "Something more..";
            var buffer = new byte[65535];
            var text = Encoding.ASCII.GetBytes(initial);
            Buffer.BlockCopy(text, 0, buffer, 0, text.Length);
            var slice = new BufferSlice(buffer, 0, buffer.Length);
            var stream = new SliceStream(slice, text.Length);
            stream.SetLength(initial.Length);
            stream.Position = stream.Length;

            var writeable = Encoding.ASCII.GetBytes(addition);
            stream.Write(writeable, 0, writeable.Length);

            stream.Position = 0;
            var reader = new StreamReader(stream);
            var actual = reader.ReadToEnd();
            Assert.Equal(initial + addition, actual);
        }
Beispiel #32
0
 /// <summary>
 /// Assigns the slice to read from
 /// </summary>
 /// <param name="buffer">The buffer.</param>
 public void Assign(BufferSlice buffer)
 {
     _slice = buffer;
 }
Beispiel #33
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BufferSliceReader"/> class.
 /// </summary>
 /// <param name="buffer">Buffer to read from.</param>
 /// <param name="offset">Where in buffer to start reading</param>
 /// <param name="count">Number of bytes that can be read.</param>
 /// <param name="encoding">Encoding to use when converting byte array to strings.</param>
 public BufferSliceReader(byte[] buffer, int offset, int count, Encoding encoding)
 {
     _slice    = new BufferSlice(buffer, offset, count, count);
     _encoding = encoding;
 }
Beispiel #34
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BufferSliceReader"/> class.
 /// </summary>
 public BufferSliceReader(BufferSlice slice)
 {
     _slice    = slice;
     _encoding = Encoding.ASCII;
 }
Beispiel #35
0
 /// <summary>
 /// Assign a new buffer
 /// </summary>
 /// <param name="buffer">Buffer to process.</param>
 /// <param name="offset">Where to start process buffer</param>
 /// <param name="count">Buffer length</param>
 /// <exception cref="ArgumentException">Buffer needs to be a byte array</exception>
 public void Assign(byte[] buffer, int offset, int count)
 {
     _slice = new BufferSlice(buffer, offset, count, count);
 }