public void Consume(IConsumerByteBuffer buffer)
        {
            if (_parser == null)
            {
                _parser = ResponseParser.ParserFor(buffer.ToArray());
            }
            else
            {
                _parser.ParseNext(buffer.ToArray());
            }
            buffer.Release();

            // don't disperse stowed messages unless a full response has arrived
            if (_parser.HasFullResponse())
            {
                var response = _parser.FullResponse();

                if (_testId != EmptyTestId)
                {
                    response.Headers.Add(ResponseHeader.Of(Client.ClientIdCustomHeader, _testId));
                }

                _completesEventually?.With(response);
                _completesEventually = null;
                DisperseStowedMessages();
            }

            if (!_parser.IsMissingContent)
            {
                _parser = null;
            }
        }
Example #2
0
            public void Consume(RequestResponseContext requestResponseContext, IConsumerByteBuffer buffer)
            {
                try
                {
                    RequestParser parser;
                    var           wasIncompleteContent = false;

                    if (!requestResponseContext.HasConsumerData)
                    {
                        parser = RequestParser.ParserFor(buffer.ToArray());
                        requestResponseContext.ConsumerData(parser);
                    }
                    else
                    {
                        parser = requestResponseContext.ConsumerData <RequestParser>();
                        wasIncompleteContent = parser.IsMissingContent;
                        parser.ParseNext(buffer.ToArray());
                    }

                    Context?context = null;

                    while (parser.HasFullRequest())
                    {
                        var unfilteredRequest = parser.FullRequest();
                        DetermineKeepAlive(requestResponseContext, unfilteredRequest);
                        var request   = _serverActor._filters.Process(unfilteredRequest);
                        var completes = new ResponseCompletes(_serverActor, requestResponseContext, request.Headers.HeaderOf(RequestHeader.XCorrelationID));
                        context = new Context(requestResponseContext, request, _serverActor._world.CompletesFor(completes));
                        _dispatcher.DispatchFor(context);
                        if (wasIncompleteContent)
                        {
                            _serverActor._requestsMissingContent.Remove(requestResponseContext.Id);
                        }
                    }

                    if (parser.IsMissingContent && !_serverActor._requestsMissingContent.ContainsKey(requestResponseContext.Id))
                    {
                        if (context == null)
                        {
                            var completes = new ResponseCompletes(_serverActor, requestResponseContext);
                            context = new Context(_serverActor._world.CompletesFor(completes));
                        }
                        _serverActor._requestsMissingContent.Add(requestResponseContext.Id, new RequestResponseHttpContext(requestResponseContext, context));
                    }
                }
                catch (Exception e)
                {
                    _serverActor.Logger.Error("Request parsing failed.", e);
                    new ResponseCompletes(_serverActor, requestResponseContext, null).With(Response.Of(ResponseStatus.BadRequest, e.Message));
                }
                finally
                {
                    buffer.Release();
                }
            }
        public void Consume(IConsumerByteBuffer buffer)
        {
            try
            {
                if (_state.Parser == null)
                {
                    _state.Parser = ResponseParser.ParserFor(buffer.ToArray());
                }
                else
                {
                    _state.Parser.ParseNext(buffer.ToArray());
                }

                while (_state.Parser.HasFullResponse())
                {
                    var response      = _state.Parser.FullResponse();
                    var correlationId = response.Headers.HeaderOf(ResponseHeader.XCorrelationID);
                    if (correlationId == null)
                    {
                        Logger.Warn("Client Consumer: Cannot complete response because no correlation id.");
                        _state.Configuration.ConsumerOfUnknownResponses.Consume(response);
                    }
                    else
                    {
                        ICompletesEventually?completes;
                        if (_completables.TryGetValue(correlationId?.Value !, out completes))
                        {
                            if (!_state.Configuration.KeepAlive)
                            {
                                _completables.Remove(correlationId?.Value !);
                            }
                        }

                        if (completes == null)
                        {
                            _state.Configuration.Stage.World.DefaultLogger.Warn(
                                $"Client Consumer: Cannot complete response because mismatched correlation id: {correlationId?.Value}");
                            _state.Configuration.ConsumerOfUnknownResponses.Consume(response);
                        }
                        else
                        {
                            completes.With(response);
                        }
                    }
                }
            }
            finally
            {
                buffer.Release();
            }
        }
Example #4
0
        public void Consume(IConsumerByteBuffer buffer)
        {
            var responsePart = buffer.ToArray().BytesToText(0, (int)buffer.Limit());

            buffer.Release();
            _access.WriteUsing("responses", responsePart);
        }
        public void Consume(IConsumerByteBuffer buffer)
        {
            var bytes = buffer.ToArray();

            buffer.Release();
            Consume(new ReadOnlySequence <byte>(bytes));
        }
    public void Consume(RequestResponseContext context, IConsumerByteBuffer buffer)
    {
        var bytes = buffer.ToArray();

        buffer.Release();
        Consume(context, new ReadOnlySequence <byte>(bytes));
    }
        public void RespondWith(RequestResponseContext context, IConsumerByteBuffer buffer)
        {
            var parser = _receivedStatus ?
                         ResponseParser.ParserForBodyOnly(buffer.ToArray()) :
                         ResponseParser.ParserFor(buffer.ToArray());

            if (!_receivedStatus)
            {
                Response.Set(parser.FullResponse());
            }
            else
            {
                _respondWithSafely.WriteUsing("events", parser.FullResponse());
            }

            _receivedStatus = true;
        }
Example #8
0
        public void Consume(IConsumerByteBuffer buffer)
        {
            if (_parser == null)
            {
                _parser = ResponseParser.ParserFor(buffer.ToArray());
            }
            else
            {
                _parser.ParseNext(buffer.ToArray());
            }

            buffer.Release();

            while (_parser.HasFullResponse())
            {
                var response = _parser.FullResponse();
                _progress.ConsumeCalls.WriteUsing("consume", response);
            }
        }
Example #9
0
            internal VirtualStateParser Includes(IConsumerByteBuffer responseContent)
            {
                _outOfContentTime = 0;
                var responseContentText = Converters.BytesToText(responseContent.ToArray());
                var utf8ExtraLength     = responseContent.Remaining - responseContentText.Length;

                if (_contentQueue.Count == 0)
                {
                    _contentExtraLength       += (int)utf8ExtraLength;
                    _responseText              = _responseText + responseContentText;
                    _currentResponseTextLength = _responseText.Length;
                }
                else
                {
                    _contentQueue.Enqueue(new ContentPacket(responseContentText, (int)utf8ExtraLength));
                }
                return(this);
            }
Example #10
0
 private void WriteWithCachedData(Context context, Socket clientChannel, IConsumerByteBuffer buffer)
 {
     try
     {
         var responseBuffer = buffer.ToArray();
         var stateObject    = new StateObject(clientChannel, context, null, buffer);
         // Begin sending the data to the remote device.
         clientChannel.BeginSend(responseBuffer, 0, responseBuffer.Length, 0, SendCallback, stateObject);
     }
     catch (Exception e)
     {
         Logger.Error($"Failed to write buffer for {_name} with channel {clientChannel.RemoteEndPoint} because: {e.Message}", e);
     }
     finally
     {
         buffer.Release();
     }
 }