示例#1
0
        public static HttpResponseMessage DecodeResponse(string commandResultBase64)
        {
            var binary = Convert.FromBase64String(commandResultBase64);

            var length = HttpFormatter.ParseResponse(new ArraySegment <byte>(binary), out var response, out var contentHeaders);

            if (binary.Length > length)
            {
                response.Content = new ByteArrayContent(binary, length, binary.Length - length);
                foreach (var contentHeader in contentHeaders)
                {
                    response.Content.Headers.Add(contentHeader.Key, (IEnumerable <string>)contentHeader.Value);
                }
            }

            return(response);
        }
示例#2
0
        private void ProcessResponse(BufferSegment buffer, bool isCompleted)
        {
            Logger.LogDataPackage("Received Response", buffer.Buffer, buffer.Offset, buffer.Length);

            var headerLength  = HttpFormatter.ParseResponse(buffer, out var response, out var contentHeaders);
            var requestId     = int.Parse(response.Headers.GetValues(MazeHeaders.MazeSocketRequestIdHeader).Single());
            var bufferSegment =
                new ArraySegment <byte>(buffer.Buffer, buffer.Offset + headerLength, buffer.Length - headerLength);

            if (isCompleted)
            {
                response.Content = new RawStreamContent(new ArrayPoolMemoryStream(bufferSegment, _socket.BufferPool));
            }
            else
            {
                var stream = new BufferQueueStream(_socket.BufferPool);
                stream.PushBuffer(bufferSegment);
                if (_activeResponses.TryAdd(requestId, stream))
                {
                    Logger.Debug("Added response {requestId} to active responses", requestId);
                }
                else
                {
                    Logger.Error(
                        "Adding the response {requestId} to the active response failed because it already exists.",
                        requestId);
                    throw new InvalidOperationException("The response already exists, duplicate response id deteceted.");
                }

                response.Content = new RawStreamContent(stream);
            }

            foreach (var contentHeader in contentHeaders)
            {
                response.Content.Headers.Add(contentHeader.Key, (IEnumerable <string>)contentHeader.Value);
            }

            if (!_waitingRequests.TryRemove(requestId, out var taskCompletionSource))
            {
                Logger.Error("No TaskCompletionSource for request {requestId} found.", requestId);
                response.Dispose();
                return;
            }

            taskCompletionSource.SetResult(response);
        }