public override Task <QbservableMessage> ReceivingAsync(QbservableMessage message, CancellationToken cancel) { DuplexQbservableMessage duplexMessage; if (DuplexQbservableMessage.TryParse(message, protocol, out duplexMessage)) { message = duplexMessage; switch (duplexMessage.Kind) { case QbservableProtocolMessageKind.DuplexResponse: HandleResponse(duplexMessage.Id, duplexMessage.Value); break; case QbservableProtocolMessageKind.DuplexErrorResponse: HandleErrorResponse(duplexMessage.Id, duplexMessage.Error); break; case QbservableProtocolMessageKind.DuplexSubscribeResponse: HandleSubscribeResponse(duplexMessage.Id, (int)duplexMessage.Value); break; case QbservableProtocolMessageKind.DuplexGetEnumeratorResponse: HandleGetEnumeratorResponse(duplexMessage.Id, (int)duplexMessage.Value); break; case QbservableProtocolMessageKind.DuplexGetEnumeratorErrorResponse: HandleGetEnumeratorErrorResponse(duplexMessage.Id, duplexMessage.Error); break; case QbservableProtocolMessageKind.DuplexEnumeratorResponse: HandleEnumeratorResponse(duplexMessage.Id, (Tuple <bool, object>)duplexMessage.Value); break; case QbservableProtocolMessageKind.DuplexEnumeratorErrorResponse: HandleEnumeratorErrorResponse(duplexMessage.Id, duplexMessage.Error); break; case QbservableProtocolMessageKind.DuplexOnNext: HandleOnNext(duplexMessage.Id, duplexMessage.Value); break; case QbservableProtocolMessageKind.DuplexOnCompleted: HandleOnCompleted(duplexMessage.Id); break; case QbservableProtocolMessageKind.DuplexOnError: HandleOnError(duplexMessage.Id, duplexMessage.Error); break; default: throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Errors.ProtocolUnknownMessageKindFormat, duplexMessage.Kind)); } duplexMessage.Handled = true; } return(Task.FromResult(message)); }
internal async void SendDuplexMessageAsync(DuplexQbservableMessage message) { try { await SendMessageAsync(message).ConfigureAwait(false); } catch (OperationCanceledException) { } catch (Exception ex) { CancelAllCommunication(ex); } }
public override Task <QbservableMessage> ReceivingAsync(QbservableMessage message, CancellationToken cancel) { DuplexQbservableMessage duplexMessage; if (DuplexQbservableMessage.TryParse(message, protocol, out duplexMessage)) { message = duplexMessage; switch (duplexMessage.Kind) { case QbservableProtocolMessageKind.DuplexInvoke: Invoke(duplexMessage.Id, (object[])duplexMessage.Value); break; case QbservableProtocolMessageKind.DuplexSubscribe: Subscribe(duplexMessage.Id); break; case QbservableProtocolMessageKind.DuplexDisposeSubscription: DisposeSubscription(duplexMessage.Id.ClientId); break; case QbservableProtocolMessageKind.DuplexGetEnumerator: GetEnumerator(duplexMessage.Id); break; case QbservableProtocolMessageKind.DuplexMoveNext: MoveNext(duplexMessage.Id); break; case QbservableProtocolMessageKind.DuplexResetEnumerator: ResetEnumerator(duplexMessage.Id); break; case QbservableProtocolMessageKind.DuplexDisposeEnumerator: DisposeEnumerator(duplexMessage.Id.ClientId); break; default: throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Errors.ProtocolUnknownMessageKindFormat, duplexMessage.Kind)); } duplexMessage.Handled = true; } return(Task.FromResult(message)); }
public static bool TryParse(QbservableMessage message, QbservableProtocol protocol, out DuplexQbservableMessage duplexMessage) { switch (message.Kind) { case QbservableProtocolMessageKind.DuplexInvoke: case QbservableProtocolMessageKind.DuplexResponse: case QbservableProtocolMessageKind.DuplexSubscribeResponse: case QbservableProtocolMessageKind.DuplexGetEnumeratorResponse: case QbservableProtocolMessageKind.DuplexEnumeratorResponse: case QbservableProtocolMessageKind.DuplexOnNext: // The following cases are handled the same as the above cases to ensure that extra data is read, though it's unexpected. case QbservableProtocolMessageKind.DuplexOnCompleted: case QbservableProtocolMessageKind.DuplexSubscribe: case QbservableProtocolMessageKind.DuplexDisposeSubscription: case QbservableProtocolMessageKind.DuplexGetEnumerator: case QbservableProtocolMessageKind.DuplexMoveNext: case QbservableProtocolMessageKind.DuplexResetEnumerator: case QbservableProtocolMessageKind.DuplexDisposeEnumerator: duplexMessage = new DuplexQbservableMessage( message.Kind, BitConverter.ToInt64(message.Data, 0), protocol.Deserialize <object>(message.Data, offset: DuplexCallbackId.Size), message.Data, message.Length); return(true); case QbservableProtocolMessageKind.DuplexErrorResponse: case QbservableProtocolMessageKind.DuplexGetEnumeratorErrorResponse: case QbservableProtocolMessageKind.DuplexEnumeratorErrorResponse: case QbservableProtocolMessageKind.DuplexOnError: duplexMessage = new DuplexQbservableMessage( message.Kind, BitConverter.ToInt64(message.Data, 0), protocol.Deserialize <Exception>(message.Data, offset: DuplexCallbackId.Size), message.Data, message.Length); return(true); default: duplexMessage = null; return(false); } }
internal IDisposable ServerSendSubscribeDuplexMessage( int clientId, Action <object> onNext, Action <Exception> onError, Action onCompleted) { var duplexSink = FindSink <IServerDuplexQbservableProtocolSink>(); var registration = duplexSink.RegisterObservableCallbacks( clientId, onNext, onError, onCompleted, subscriptionId => SendDuplexMessageAsync(DuplexQbservableMessage.CreateDisposeSubscription(subscriptionId, this))); var id = registration.Item1; var subscription = registration.Item2; var message = DuplexQbservableMessage.CreateSubscribe(id, this); SendDuplexMessage(message); return(subscription); }
protected override void SendError(DuplexCallbackId id, Exception error) { protocol.SendDuplexMessageAsync(DuplexQbservableMessage.CreateErrorResponse(id, error, protocol)); }
public override void DisposeEnumerator(int enumeratorId) { protocol.SendDuplexMessageAsync(DuplexQbservableMessage.CreateDisposeEnumerator(enumeratorId, protocol)); }
public override void ResetEnumerator(int enumeratorId) { protocol.ServerSendEnumeratorDuplexMessage(enumeratorId, id => DuplexQbservableMessage.CreateResetEnumerator(id, protocol)); }
public override Tuple <bool, object> MoveNext(int enumeratorId) { return((Tuple <bool, object>)protocol.ServerSendEnumeratorDuplexMessage(enumeratorId, id => DuplexQbservableMessage.CreateMoveNext(id, protocol))); }
public override int GetEnumerator(int clientId) { return((int)protocol.ServerSendDuplexMessage(clientId, id => DuplexQbservableMessage.CreateGetEnumerator(id, protocol))); }
public override void SendOnError(DuplexCallbackId id, Exception error) { protocol.SendDuplexMessageAsync(DuplexQbservableMessage.CreateOnError(id, error, protocol)); }
public override void SendOnNext(DuplexCallbackId id, object value) { protocol.SendDuplexMessageAsync(DuplexQbservableMessage.CreateOnNext(id, value, protocol)); }
public override object Invoke(int clientId, object[] arguments) { return(protocol.ServerSendDuplexMessage(clientId, id => DuplexQbservableMessage.CreateInvoke(id, arguments, protocol))); }
protected override void SendResponse(DuplexCallbackId id, object result) { protocol.SendDuplexMessageAsync(DuplexQbservableMessage.CreateResponse(id, result, protocol)); }
public override void SendOnCompleted(DuplexCallbackId id) { protocol.SendDuplexMessageAsync(DuplexQbservableMessage.CreateOnCompleted(id, protocol)); }
protected override void SendEnumeratorResponse(DuplexCallbackId id, bool result, object current) { protocol.SendDuplexMessageAsync(DuplexQbservableMessage.CreateEnumeratorResponse(id, result, current, protocol)); }
protected override void SendGetEnumeratorError(DuplexCallbackId id, Exception error) { protocol.SendDuplexMessageAsync(DuplexQbservableMessage.CreateGetEnumeratorError(id, error, protocol)); }
protected override void SendGetEnumeratorResponse(DuplexCallbackId id, int clientEnumeratorId) { protocol.SendDuplexMessageAsync(DuplexQbservableMessage.CreateGetEnumeratorResponse(id, clientEnumeratorId, protocol)); }
protected override void SendSubscribeResponse(DuplexCallbackId id, int clientSubscriptionId) { protocol.SendDuplexMessageAsync(DuplexQbservableMessage.CreateSubscribeResponse(id, clientSubscriptionId, protocol)); }
private void SendDuplexMessage(DuplexQbservableMessage message) { SendMessageAsync(message).Wait(Cancel); }