async Task SendResponseAsync(ListenerCommand.ResponseCommand responseCommand, Stream responseBodyStream, CancellationToken cancelToken) { if (this.rendezvousWebSocket == null) { RelayEventSource.Log.HybridHttpConnectionSendResponse(this.TrackingContext, "control", responseCommand.StatusCode); var listenerCommand = new ListenerCommand { Response = responseCommand }; await this.listener.SendControlCommandAndStreamAsync(listenerCommand, responseBodyStream, cancelToken).ConfigureAwait(false); } else { RelayEventSource.Log.HybridHttpConnectionSendResponse(this.TrackingContext, "rendezvous", responseCommand.StatusCode); await this.EnsureRendezvousAsync(cancelToken).ConfigureAwait(false); using (var memoryStream = new MemoryStream(this.listener.ConnectionBufferSize)) { new ListenerCommand { Response = responseCommand }.WriteObject(memoryStream); memoryStream.Position = 0; ArraySegment <byte> buffer = memoryStream.GetArraySegment(); // We need to respond over the rendezvous connection await this.rendezvousWebSocket.SendAsync(buffer, WebSocketMessageType.Text, true, cancelToken).ConfigureAwait(false); if (responseCommand.Body && responseBodyStream != null) { buffer = new ArraySegment <byte>(buffer.Array, 0, buffer.Array.Length); await this.rendezvousWebSocket.SendStreamAsync(responseBodyStream, WebSocketMessageType.Binary, buffer, cancelToken).ConfigureAwait(false); } } } }
static ListenerCommand.ResponseCommand CreateResponseCommand(RelayedHttpListenerContext listenerContext) { var responseCommand = new ListenerCommand.ResponseCommand(); responseCommand.StatusCode = (int)listenerContext.Response.StatusCode; responseCommand.StatusDescription = listenerContext.Response.StatusDescription; responseCommand.RequestId = listenerContext.TrackingContext.TrackingId; foreach (string headerName in listenerContext.Response.Headers.AllKeys) { responseCommand.ResponseHeaders[headerName] = listenerContext.Response.Headers[headerName]; } return(responseCommand); }