Ejemplo n.º 1
0
        /// <summary>
        /// Joins together the results from several patterns.
        /// </summary>
        /// <typeparam name="TResult">The type of the elements in the result sequence, obtained from the specified patterns.</typeparam>
        /// <param name="provider">Query provider used to construct the IQbservable&lt;T&gt; data source.</param>
        /// <param name="plans">A series of plans created by use of the Then operator on patterns.</param>
        /// <returns>An observable sequence with the results from matching several patterns.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="provider"/> or <paramref name="plans"/> is null.</exception>
        public static IQbservable <TResult> When <TResult>(this IQbservableProvider provider, params QueryablePlan <TResult>[] plans)
        {
            if (provider == null)
            {
                throw new ArgumentNullException(nameof(provider));
            }
            if (plans == null)
            {
                throw new ArgumentNullException(nameof(plans));
            }

            return(provider.CreateQuery <TResult>(
                       Expression.Call(
                           null,
#if CRIPPLED_REFLECTION
                           InfoOf(() => Qbservable.When <TResult>(default(IQbservableProvider), default(QueryablePlan <TResult>[]))),
#else
                           ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
#endif
                           Expression.Constant(provider, typeof(IQbservableProvider)),
                           Expression.NewArrayInit(
                               typeof(QueryablePlan <TResult>),
                               plans.Select(p => p.Expression)
                               )
                           )
                       ));
        }
Ejemplo n.º 2
0
 public Task ExecuteServerAsync(IQbservableProvider provider)
 {
     Contract.Requires(!IsClient);
     Contract.Requires(provider != null);
     Contract.Ensures(Contract.Result <Task>() != null);
     return(null);
 }
Ejemplo n.º 3
0
        private Expression PrepareExpression(out IQbservableProvider realProvider)
        {
            Contract.Ensures(Contract.ValueAtReturn(out realProvider) != null);
            Contract.Ensures(Contract.Result <Expression>() != null);

            Log.ServerReceivingExpression(clientId, Expression);

            var source = Provider.GetSource(argument);

            realProvider = source.Provider;

            ExpressionVisitor visitor;
            Expression        preparedExpression = null;

            if (!Provider.Options.AllowExpressionsUnrestricted)
            {
                visitor = new SecurityExpressionVisitor(Provider.Options);

                preparedExpression = visitor.Visit(Expression);
            }

            visitor = ReplaceConstantsVisitor.Create(
                typeof(QbservableSourcePlaceholder <TSource>),
                source,
                typeof(IQbservable <TSource>),
                (actualTypeInQuery, actualTypeInServer) =>
            {
                throw new InvalidOperationException("The client specified the wrong data type for the query." + Environment.NewLine
                                                    + "Client data type: " + actualTypeInQuery.FullName + Environment.NewLine
                                                    + "Actual data type: " + actualTypeInServer.FullName);
            });

            preparedExpression = visitor.Visit(preparedExpression ?? Expression);

            visitor = ReplaceConstantsVisitor.Create(
                typeof(DuplexCallback),
                (value, _) =>
            {
                var callback = (DuplexCallback)value;

                callback.SetServerProtocol(Provider.Protocol);

                return(callback);
            },
                type => type);

            preparedExpression = visitor.Visit(preparedExpression);

            foreach (var customVisitorFactory in Provider.Options.VisitorFactories)
            {
                var customVisitor = customVisitorFactory();

                preparedExpression = customVisitor.Visit(preparedExpression);
            }

            Log.ServerRewrittenExpression(clientId, preparedExpression);

            return(preparedExpression);
        }
Ejemplo n.º 4
0
        public Stream(StreamDbContextOptions options, string[] streamPatterns)
        {
            _options        = options;
            _streamPatterns = streamPatterns;

            ElementType = typeof(T);
            Provider    = new StreamDbProvider <T>(options, _streamPatterns);
            Expression  = Expression.Parameter(typeof(IQbservable <T>), "o");
        }
Ejemplo n.º 5
0
 public QueryStream(
     IQbservableProvider provider,
     Expression expression,
     StreamDbContextOptions options,
     string[] streamPatterns)
 {
     ElementType     = typeof(TIn);
     Provider        = provider;
     Expression      = expression;
     _options        = options;
     _streamPatterns = streamPatterns;
 }
        private async Task ExecuteServerQueryAsync(Tuple <Expression, object> input, IQbservableProvider provider)
        {
            var shutDownReason = QbservableProtocolShutDownReason.ObservableTerminated;

            ExceptionDispatchInfo createQueryError = null;

            if (input == null)
            {
                shutDownReason = QbservableProtocolShutDownReason.ProtocolTerminated;
            }
            else
            {
                var expression = input.Item1;
                var argument   = input.Item2;

                if (expression == null)
                {
                    shutDownReason = QbservableProtocolShutDownReason.ProtocolTerminated;
                }
                else
                {
                    Type   dataType   = null;
                    object observable = null;

                    try
                    {
                        observable = CreateQuery(provider, expression, argument, out dataType);
                    }
                    catch (Exception ex)
                    {
                        shutDownReason   = QbservableProtocolShutDownReason.ServerError;
                        createQueryError = ExceptionDispatchInfo.Capture(ex);
                    }

                    if (createQueryError == null)
                    {
                        await SendObservableAsync(observable, dataType, serviceOptions.SendServerErrorsToClients, cancel).ConfigureAwait(false);
                    }
                    else if (serviceOptions.SendServerErrorsToClients)
                    {
                        await ServerSendAsync(NotificationKind.OnError, createQueryError.SourceException).ConfigureAwait(false);
                    }
                }
            }

            await ShutDownAsync(shutDownReason).ConfigureAwait(false);

            if (createQueryError != null)
            {
                createQueryError.Throw();
            }
        }
        private Expression PrepareExpression(out IQbservableProvider realProvider)
        {
            QbservableProviderDiagnostics.DebugPrint(Expression, "TcpServerQuery Received Expression");

            var source = Provider.GetSource(argument);

            realProvider = source.Provider;

            ExpressionVisitor visitor;
            Expression        preparedExpression = null;

            if (!Provider.Options.AllowExpressionsUnrestricted)
            {
                visitor = new SecurityExpressionVisitor(Provider.Options);

                preparedExpression = visitor.Visit(Expression);
            }

            visitor = ReplaceConstantsVisitor.Create(
                typeof(QbservableSourcePlaceholder <TSource>),
                source,
                typeof(IQbservable <TSource>),
                (actualTypeInQuery, actualTypeInServer) =>
            {
                throw new InvalidOperationException("The client specified the wrong data type for the query." + Environment.NewLine
                                                    + "Client data type: " + actualTypeInQuery.FullName + Environment.NewLine
                                                    + "Actual data type: " + actualTypeInServer.FullName);
            });

            preparedExpression = visitor.Visit(preparedExpression ?? Expression);

            visitor = ReplaceConstantsVisitor.Create(
                typeof(DuplexCallback),
                (value, _) =>
            {
                var callback = (DuplexCallback)value;

                callback.SetServerProtocol(Provider.Protocol);

                return(callback);
            },
                type => type);

            preparedExpression = visitor.Visit(preparedExpression);

            QbservableProviderDiagnostics.DebugPrint(preparedExpression, "TcpServerQuery Rewritten Expression");

            return(preparedExpression);
        }
Ejemplo n.º 8
0
        private static object CreateQuery(IQbservableProvider provider, Expression expression, object argument, out Type type)
        {
            Contract.Requires(provider != null);
            Contract.Requires(expression != null);
            Contract.Requires(expression.Type.GetIsGenericType());
            Contract.Requires(!expression.Type.GetIsGenericTypeDefinition());
            Contract.Requires(expression.Type.GetGenericTypeDefinition() == typeof(IQbservable <>));
            Contract.Ensures(Contract.Result <object>() != null);
            Contract.Ensures(Contract.ValueAtReturn(out type) != null);

            type = expression.Type.GetGenericArguments()[0];

            var parameterized = provider as IParameterizedQbservableProvider;

#if CAS
            new PermissionSet(PermissionState.Unrestricted).Assert();
#endif

            try
            {
                if (parameterized != null)
                {
                    return(provider.GetType()
                           .GetInterfaceMap(typeof(IParameterizedQbservableProvider))
                           .TargetMethods
                           .First()
                           .MakeGenericMethod(type)
                           .Invoke(provider, new[] { expression, argument }));
                }
                else
                {
                    return(provider.GetType()
                           .GetInterfaceMap(typeof(IQbservableProvider))
                           .TargetMethods
                           .First()
                           .MakeGenericMethod(type)
                           .Invoke(provider, new[] { expression }));
                }
            }
            finally
            {
#if CAS
                PermissionSet.RevertAssert();
#endif
            }
        }
Ejemplo n.º 9
0
        internal TriggeredRestObservable(
            IObservable <Unit> trigger,
            IAsyncRestClientFactory restClient,
            ISerializerFactory serializerFactory,
            IMemberNameResolver memberNameResolver,
            IEnumerable <IValueWriter> valueWriters,
            Expression expression,
            IScheduler subscriberScheduler,
            IScheduler observerScheduler)
            : base(restClient, serializerFactory, memberNameResolver, valueWriters, expression, subscriberScheduler, observerScheduler)
        {
            Contract.Requires(restClient != null);
            Contract.Requires(trigger != null);
            Contract.Requires(serializerFactory != null);
            Contract.Requires(memberNameResolver != null);
            Contract.Requires(valueWriters != null);
            Contract.Requires(subscriberScheduler != null);
            Contract.Requires(observerScheduler != null);

            _trigger  = trigger;
            _provider = new TriggeredRestQueryableProvider <TSource>(trigger, restClient, serializerFactory, subscriberScheduler, observerScheduler);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Joins together the results from several patterns.
        /// </summary>
        /// <typeparam name="TResult">The type of the elements in the result sequence, obtained from the specified patterns.</typeparam>
        /// <param name="provider">Query provider used to construct the IQbservable&lt;T&gt; data source.</param>
        /// <param name="plans">A series of plans created by use of the Then operator on patterns.</param>
        /// <returns>An observable sequence with the results form matching several patterns.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="provider"/> or <paramref name="plans"/> is null.</exception>
        public static IQbservable <TResult> When <TResult>(this IQbservableProvider provider, IEnumerable <QueryablePlan <TResult> > plans)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }
            if (plans == null)
            {
                throw new ArgumentNullException("plans");
            }

            return(provider.CreateQuery <TResult>(
                       Expression.Call(
                           null,
#if CRIPPLED_REFLECTION
                           InfoOf(() => Qbservable.When <TResult>(default(IQbservableProvider), default(IEnumerable <QueryablePlan <TResult> >))),
#else
                           ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
#endif
                           Expression.Constant(provider, typeof(IQbservableProvider)),
                           Expression.Constant(plans, typeof(IEnumerable <QueryablePlan <TResult> >))
                           )
                       ));
        }
Ejemplo n.º 11
0
        public static IQbservable <TResult> Create <TResult>(this IQbservableProvider provider, Expression <Func <IObserver <TResult>, IEnumerable <IObservable <object> > > > iteratorMethod)
        {
            if (provider == null)
            {
                throw new ArgumentNullException(nameof(provider));
            }
            if (iteratorMethod == null)
            {
                throw new ArgumentNullException(nameof(iteratorMethod));
            }

            return(provider.CreateQuery <TResult>(
                       Expression.Call(
                           null,
#if CRIPPLED_REFLECTION
                           InfoOf(() => QbservableEx.Create <TResult>(default(IQbservableProvider), default(Expression <Func <IObserver <TResult>, IEnumerable <IObservable <object> > > >))),
#else
                           ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
#endif
                           Expression.Constant(provider, typeof(IQbservableProvider)),
                           iteratorMethod
                           )
                       ));
        }
Ejemplo n.º 12
0
        public static IQbservable <TSource[]> ForkJoin <TSource>(this IQbservableProvider provider, IEnumerable <IObservable <TSource> > sources)
        {
            if (provider == null)
            {
                throw new ArgumentNullException(nameof(provider));
            }
            if (sources == null)
            {
                throw new ArgumentNullException(nameof(sources));
            }

            return(provider.CreateQuery <TSource[]>(
                       Expression.Call(
                           null,
#if CRIPPLED_REFLECTION
                           InfoOf(() => QbservableEx.ForkJoin <TSource>(default(IQbservableProvider), default(IEnumerable <IObservable <TSource> >))),
#else
                           ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
#endif
                           Expression.Constant(provider, typeof(IQbservableProvider)),
                           GetSourceExpression(sources)
                           )
                       ));
        }
 public static Func <T1, T2, T3, T4, T5, IQbservable <Unit> > ToAsync <T1, T2, T3, T4, T5> (this IQbservableProvider provider, Expression <Action <T1, T2, T3, T4, T5> > action)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 14
0
 public Task ExecuteServerAsync(IQbservableProvider provider)
 {
     return(null);
 }
        public async Task ExecuteServerAsync(IQbservableProvider provider)
        {
            Contract.Requires(!IsClient);

            Task receivingAsync = null;
            ExceptionDispatchInfo fatalException = null;

            try
            {
                await InitializeSinksAsync().ConfigureAwait(false);

                var input = await ServerReceiveQueryAsync().ConfigureAwait(false);

                receivingAsync = ServerReceiveAsync();

                try
                {
                    await ExecuteServerQueryAsync(input, provider).ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    if (ShutDownReason == QbservableProtocolShutDownReason.None)
                    {
                        ShutDownReason = QbservableProtocolShutDownReason.BadClientRequest;
                    }

                    CancelAllCommunication(ex);

                    fatalException = ExceptionDispatchInfo.Capture(ex);
                }
            }
            catch (OperationCanceledException ex)
            {
                if (errors.Count == 1)
                {
                    if (ShutDownReason == QbservableProtocolShutDownReason.None)
                    {
                        ShutDownReason = QbservableProtocolShutDownReason.ServerError;
                    }

                    fatalException = errors[0];
                }
                else if (errors.Count > 1)
                {
                    if (ShutDownReason == QbservableProtocolShutDownReason.None)
                    {
                        ShutDownReason = QbservableProtocolShutDownReason.ServerError;
                    }

                    fatalException = ExceptionDispatchInfo.Capture(new AggregateException(errors.Select(e => e.SourceException)));
                }

                if (ShutDownReason == QbservableProtocolShutDownReason.None)
                {
                    ShutDownReason = QbservableProtocolShutDownReason.ClientTerminated;
                }

                fatalException = ExceptionDispatchInfo.Capture(ex);
            }
            catch (Exception ex)
            {
                CancelAllCommunication(ex);

                fatalException = ExceptionDispatchInfo.Capture(ex);
            }

            if (receivingAsync != null)
            {
                try
                {
                    await receivingAsync.ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                }
                catch (Exception ex)
                {
                    errors.Add(ExceptionDispatchInfo.Capture(ex));
                }
            }

            if (fatalException != null)
            {
                fatalException.Throw();
            }
        }
Ejemplo n.º 16
0
        public async Task ExecuteServerAsync(IQbservableProvider provider)
        {
            Task receivingAsync = null;
            ExceptionDispatchInfo fatalException = null;

            try
            {
                await InitializeSinksAsync().ConfigureAwait(false);

                var input = await ServerReceiveQueryAsync().ConfigureAwait(false);

                receivingAsync = ServerReceiveAsync();

                try
                {
                    await ExecuteServerQueryAsync(input, provider).ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    ShutdownReason |= QbservableProtocolShutdownReason.BadClientRequest;

                    fatalException = ExceptionDispatchInfo.Capture(ex);

                    CancelAllCommunication(fatalException);
                }
            }
            catch (OperationCanceledException ex)
            {
                var exception = TryRollupExceptions();

                if (exception != null)
                {
                    ShutdownReason |= QbservableProtocolShutdownReason.ServerError;

                    fatalException = exception;
                }

                ShutdownReason |= QbservableProtocolShutdownReason.ClientTerminated;

                fatalException = ExceptionDispatchInfo.Capture(ex);
            }
            catch (Exception ex)
            {
                fatalException = ExceptionDispatchInfo.Capture(ex);

                CancelAllCommunication(fatalException);
            }

            if (receivingAsync != null)
            {
                try
                {
                    await receivingAsync.ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                }
                catch (Exception ex)
                {
                    AddError(ExceptionDispatchInfo.Capture(ex));
                }
            }

            if (fatalException != null)
            {
                fatalException.Throw();
            }
        }
 public static Func <T1, T2, T3, T4, T5, IQbservable <Unit> > FromAsyncPattern <T1, T2, T3, T4, T5> (this IQbservableProvider provider, Expression <Func <T1, T2, T3, T4, T5, AsyncCallback, object, IAsyncResult> > begin, Expression <Action <IAsyncResult> > end)
 {
     throw new NotImplementedException();
 }
 public static Func <T1, T2, T3, T4, T5, T6, T7, T8, T9, IQbservable <TResult> > FromAsyncPattern <T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult> (this IQbservableProvider provider, Expression <Func <T1, T2, T3, T4, T5, T6, T7, T8, T9, AsyncCallback, Object, IAsyncResult> > begin, Expression <Func <IAsyncResult, TResult> > end)
 {
     throw new NotImplementedException();
 }
 public static Func <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, IQbservable <TResult> > ToAsync <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, TResult> (this IQbservableProvider provider, Expression <Func <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, TResult> > function, IScheduler scheduler)
 {
     throw new NotImplementedException();
 }
 public static Func <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, IQbservable <Unit> > ToAsync <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> (this IQbservableProvider provider, Expression <Action <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> > action)
 {
     throw new NotImplementedException();
 }
 public static Func <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, IQbservable <Unit> > ToAsync <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> (this IQbservableProvider provider, Expression <Action <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> > action, IScheduler scheduler)
 {
     throw new NotImplementedException();
 }
 public static Func <T1, T2, T3, T4, T5, T6, T7, T8, T9, IQbservable <TResult> > ToAsync <T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult> (this IQbservableProvider provider, Expression <Func <T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult> > function)
 {
     throw new NotImplementedException();
 }
 public static Func <T1, T2, T3, IQbservable <TResult> > ToAsync <T1, T2, T3, TResult> (this IQbservableProvider provider, Expression <Func <T1, T2, T3, TResult> > function, IScheduler scheduler)
 {
     throw new NotImplementedException();
 }