Пример #1
0
 async Task OnCommandAsync(ArraySegment <byte> commandBuffer, WebSocket webSocket)
 {
     try
     {
         var listenerCommand = ListenerCommand.ReadObject(commandBuffer);
         if (listenerCommand.Accept != null)
         {
             // Don't block the pump waiting for the rendezvous
             this.OnAcceptCommandAsync(listenerCommand.Accept).Fork(this);
         }
         else if (listenerCommand.Request != null)
         {
             await HybridHttpConnection.CreateAsync(this, listenerCommand.Request, webSocket).ConfigureAwait(false);
         }
         else
         {
             string json = Encoding.UTF8.GetString(
                 commandBuffer.Array,
                 commandBuffer.Offset,
                 Math.Min(commandBuffer.Count, HybridConnectionConstants.MaxUnrecognizedJson));
             RelayEventSource.Log.Warning(this, $"Received an unknown command: {json}.");
         }
     }
     catch (Exception exception) when(!Fx.IsFatal(exception))
     {
         RelayEventSource.Log.HandledExceptionAsWarning(this, exception);
     }
 }
 public ResponseStream(HybridHttpConnection connection, RelayedHttpListenerContext context)
 {
     this.connection   = connection;
     this.context      = context;
     this.WriteTimeout = TimeoutHelper.ToMilliseconds(this.connection.OperationTimeout);
     this.asyncLock    = new AsyncLock();
 }
        public static async Task CreateAsync(HybridConnectionListener listener, ListenerCommand.RequestCommand requestCommand, WebSocket controlWebSocket)
        {
            var hybridHttpConnection = new HybridHttpConnection(listener, controlWebSocket, requestCommand.Address);

            // In this method we're holding up the listener's control connection.
            // Do only what we need to do (receive any request body from control channel) and then let this Task complete.
            bool requestOverControlConnection = requestCommand.Body.HasValue;
            var  requestAndStream             = new RequestCommandAndStream(requestCommand, null);

            if (requestOverControlConnection)
            {
                requestAndStream = await hybridHttpConnection.ReceiveRequestBodyOverControlAsync(requestCommand).ConfigureAwait(false);
            }

            // ProcessFirstRequestAsync runs without blocking the listener control connection:
            Task.Run(() => hybridHttpConnection.ProcessFirstRequestAsync(requestAndStream)).Fork(hybridHttpConnection);
        }