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); } }
void OnCommand(ArraySegment <byte> buffer) { try { ListenerCommand listenerCommand; using (var stream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count)) { listenerCommand = ListenerCommand.ReadObject(stream); } if (listenerCommand.Accept != null) { this.OnAcceptClientCommand(listenerCommand.Accept).Fork(this); } else { string json = Encoding.UTF8.GetString(buffer.Array, buffer.Offset, buffer.Count); RelayEventSource.Log.RelayListenerUnknownCommand(this.ToString(), json); } } catch (Exception exception) when(!Fx.IsFatal(exception)) { RelayEventSource.Log.HandledExceptionAsWarning(this, exception); } }
async Task OnCommandAsync(ArraySegment <byte> buffer) { try { ListenerCommand listenerCommand; using (var stream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count)) { listenerCommand = ListenerCommand.ReadObject(stream); } if (listenerCommand.Accept != null) { await this.OnAcceptClientCommandAsync(listenerCommand.Accept).ConfigureAwait(false); } else { string json = Encoding.UTF8.GetString(buffer.Array, buffer.Offset, buffer.Count); RelayEventSource.Log.RelayListenerUnknownCommand(this.ToString(), json); } } catch (Exception exception) { if (Fx.IsFatal(exception)) { throw; } RelayEventSource.Log.HandledExceptionAsWarning(this, exception); } }
public static ListenerCommand ReadObject(ArraySegment <byte> buffer) { using (var stream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count)) { return(ListenerCommand.ReadObject(stream)); } }
async Task <RequestCommandAndStream> ReceiveRequestOverRendezvousAsync() { using (var cancelSource = new CancellationTokenSource(this.OperationTimeout)) { // A Rendezvous is required to get full request await this.EnsureRendezvousAsync(cancelSource.Token).ConfigureAwait(false); } RelayEventSource.Log.HybridHttpReadRendezvousValue(this, "request command"); ListenerCommand.RequestCommand requestCommand; using (var rendezvousCommandStream = new WebSocketMessageStream(this.rendezvousWebSocket, this.OperationTimeout)) { // Deserializing the object from stream makes a sync-over-async call which can deadlock // if performed on the websocket transport's callback thread. requestCommand = await Task.Run(() => ListenerCommand.ReadObject(rendezvousCommandStream).Request).ConfigureAwait(false); if (rendezvousCommandStream.MessageType == WebSocketMessageType.Close) { throw RelayEventSource.Log.ThrowingException(new InvalidOperationException(SR.EntityClosedOrAborted), this); } else if (rendezvousCommandStream.MessageType != WebSocketMessageType.Text) { throw RelayEventSource.Log.ThrowingException( new ProtocolViolationException(SR.GetString(SR.InvalidType, WebSocketMessageType.Text, rendezvousCommandStream.MessageType)), this); } else if (requestCommand == null) { throw RelayEventSource.Log.ThrowingException(new ProtocolViolationException(SR.GetString(SR.InvalidType, "request", "{unknown}")), this); } } Stream requestStream = null; if (requestCommand.Body.HasValue && requestCommand.Body.Value) { RelayEventSource.Log.HybridHttpReadRendezvousValue(this, "request body"); requestStream = new WebSocketMessageStream(this.rendezvousWebSocket, this.OperationTimeout); } return(new RequestCommandAndStream(requestCommand, requestStream)); }