示例#1
0
        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);
     }
 }
示例#3
0
        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));
        }
示例#4
0
        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);
        }
示例#6
0
 protected override void SendError(DuplexCallbackId id, Exception error)
 {
     protocol.SendDuplexMessageAsync(DuplexQbservableMessage.CreateErrorResponse(id, error, protocol));
 }
示例#7
0
 public override void DisposeEnumerator(int enumeratorId)
 {
     protocol.SendDuplexMessageAsync(DuplexQbservableMessage.CreateDisposeEnumerator(enumeratorId, protocol));
 }
示例#8
0
 public override void ResetEnumerator(int enumeratorId)
 {
     protocol.ServerSendEnumeratorDuplexMessage(enumeratorId, id => DuplexQbservableMessage.CreateResetEnumerator(id, protocol));
 }
示例#9
0
 public override Tuple <bool, object> MoveNext(int enumeratorId)
 {
     return((Tuple <bool, object>)protocol.ServerSendEnumeratorDuplexMessage(enumeratorId, id => DuplexQbservableMessage.CreateMoveNext(id, protocol)));
 }
示例#10
0
 public override int GetEnumerator(int clientId)
 {
     return((int)protocol.ServerSendDuplexMessage(clientId, id => DuplexQbservableMessage.CreateGetEnumerator(id, protocol)));
 }
示例#11
0
 public override void SendOnError(DuplexCallbackId id, Exception error)
 {
     protocol.SendDuplexMessageAsync(DuplexQbservableMessage.CreateOnError(id, error, protocol));
 }
示例#12
0
 public override void SendOnNext(DuplexCallbackId id, object value)
 {
     protocol.SendDuplexMessageAsync(DuplexQbservableMessage.CreateOnNext(id, value, protocol));
 }
示例#13
0
 public override object Invoke(int clientId, object[] arguments)
 {
     return(protocol.ServerSendDuplexMessage(clientId, id => DuplexQbservableMessage.CreateInvoke(id, arguments, protocol)));
 }
示例#14
0
 protected override void SendResponse(DuplexCallbackId id, object result)
 {
     protocol.SendDuplexMessageAsync(DuplexQbservableMessage.CreateResponse(id, result, protocol));
 }
示例#15
0
 public override void SendOnCompleted(DuplexCallbackId id)
 {
     protocol.SendDuplexMessageAsync(DuplexQbservableMessage.CreateOnCompleted(id, protocol));
 }
示例#16
0
 protected override void SendEnumeratorResponse(DuplexCallbackId id, bool result, object current)
 {
     protocol.SendDuplexMessageAsync(DuplexQbservableMessage.CreateEnumeratorResponse(id, result, current, protocol));
 }
示例#17
0
 protected override void SendGetEnumeratorError(DuplexCallbackId id, Exception error)
 {
     protocol.SendDuplexMessageAsync(DuplexQbservableMessage.CreateGetEnumeratorError(id, error, protocol));
 }
示例#18
0
 protected override void SendGetEnumeratorResponse(DuplexCallbackId id, int clientEnumeratorId)
 {
     protocol.SendDuplexMessageAsync(DuplexQbservableMessage.CreateGetEnumeratorResponse(id, clientEnumeratorId, protocol));
 }
示例#19
0
 protected override void SendSubscribeResponse(DuplexCallbackId id, int clientSubscriptionId)
 {
     protocol.SendDuplexMessageAsync(DuplexQbservableMessage.CreateSubscribeResponse(id, clientSubscriptionId, protocol));
 }
 private void SendDuplexMessage(DuplexQbservableMessage message)
 {
     SendMessageAsync(message).Wait(Cancel);
 }