/// <summary>
        /// Handle an message
        /// </summary>
        /// <param name="context">Context unique for this handler instance</param>
        /// <param name="message">Message to process</param>
        /// <remarks>
        /// All messages that can't be handled MUST be send up the chain using <see cref="IPipelineHandlerContext.SendUpstream"/>.
        /// </remarks>
        public void HandleUpstream(IPipelineHandlerContext context, IPipelineMessage message)
        {
            var msg = message as Received;
            if (msg == null)
            {
                context.SendUpstream(message);
                return;
            }

            // byte + byte + int
            if (msg.BufferSlice.RemainingLength < 6)
            {
                return;
            }

            var header = new SimpleHeader
            {
                Version = msg.BufferSlice.Buffer[msg.BufferSlice.Position++],
                ContentId = msg.BufferSlice.Buffer[msg.BufferSlice.Position++],
                ContentLength = BitConverter.ToInt32(msg.BufferSlice.Buffer, msg.BufferSlice.Position)
            };
            msg.BufferSlice.Position += 4;
            context.SendUpstream(new ReceivedHeader(header));

            if (msg.BufferSlice.RemainingLength > 0)
                context.SendUpstream(msg);            
        }
        /// <summary>
        /// Handle an message
        /// </summary>
        /// <param name="context">Context unique for this handler instance</param>
        /// <param name="message">Message to process</param>
        public void HandleUpstream(IPipelineHandlerContext context, IPipelineMessage message)
        {
            var msg = message as Received;
            if (msg == null)
            {
                context.SendUpstream(message);
                return;
            }

            var bytesToCopy = Math.Min(msg.BufferReader.Count, _bytesLeft);
            msg.BufferReader.Read(_header, _position, bytesToCopy);
            _position += bytesToCopy;
            _bytesLeft -= bytesToCopy;

            if (_bytesLeft > 0)
            {
                return;
            }

            var header = new SimpleHeader
                {
                    Version = _header[0],
                    Length = BitConverter.ToInt32(_header, 1)
                };

            _bytesLeft = 5;
            _position = 0;
            context.SendUpstream(new ReceivedHeader(header));

            if (msg.BufferReader.Position < msg.BufferReader.Count)
                context.SendUpstream(msg);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Handle an message
        /// </summary>
        /// <param name="context">Context unique for this handler instance</param>
        /// <param name="message">Message to process</param>
        public void HandleUpstream(IPipelineHandlerContext context, IPipelineMessage message)
        {
            var headerMsg = message as ReceivedHeader;
            if (headerMsg != null)
            {
                _header = headerMsg.Header;
                if (_header.Length > 65535)
                {
                    var error = new ErrorResponse("-9999", new RpcError
                        {
                            Code = RpcErrorCode.InvalidRequest,
                            Message =
                                "Support requests which is at most 655355 bytes.",
                        });
                    context.SendDownstream(new SendResponse(error));
                }

                return;
            }

            var received = message as Received;
            if (received != null)
            {
                var count = Math.Min(received.BufferReader.Count, _header.Length);
                received.BufferReader.CopyTo(_stream, count);
                if (_stream.Length == _header.Length)
                {
                    _stream.Position = 0;
                    var request = DeserializeRequest(_stream);
                    context.SendUpstream(new ReceivedRequest(request));
                }

                return;
            }

            context.SendUpstream(message);
        }
Exemplo n.º 4
0
        private void HandleHeader(IPipelineHandlerContext context, IPipelineMessage message, ReceivedHeader headerMsg)
        {
            _packetType = _mapper.GetPacketType(_header.ContentId);
            if (_packetType == null)
            {
                // not supported, let the rest of the pipeline
                // handle the packet.
                context.SendUpstream(message);
            }
            else
            {
                _header = headerMsg.Header;
                var buffer = _bufferPool.PopSlice();
                if (_header.ContentLength > buffer.Capacity)
                    throw new InvalidOperationException(
                        string.Format(
                            "Buffer ({0} bytes) is less than the packet content ({1} bytes). Sorry, that's not possible in the current version.",
                            buffer.Capacity, _header.ContentLength));

                _bytesLeft = _header.ContentLength;
                _stream = new BufferPoolStream(_bufferPool, buffer);
            }
        }
Exemplo n.º 5
0
 private void Clear()
 {
     _stream.Dispose();
     _stream = null;
     _packetType = null;
     _header = null;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ReceivedHeader"/> class.
 /// </summary>
 /// <param name="header">The header.</param>
 public ReceivedHeader(SimpleHeader header)
 {
     if (header == null) throw new ArgumentNullException("header");
     Header = header;
 }