public void SetClientProtocol(IQbservableProtocol protocol) { Contract.Requires(Protocol == null); Contract.Requires(protocol != null); this.protocol = protocol; }
public LocalEvaluationVisitor(LocalEvaluator evaluator, IQbservableProtocol protocol) { Contract.Requires(evaluator != null); Contract.Requires(protocol != null); this.evaluator = evaluator; this.protocol = protocol; }
public static Expression Create(string name, IQbservableProtocol protocol, object instance, PropertyInfo property) { Contract.Requires(protocol != null); Contract.Requires(property != null); Contract.Ensures(Contract.Result <Expression>() != null); return(CreateInvoke( new DuplexCallback(name, protocol, (_, __) => ConvertIfSequence(name, protocol, property.GetValue(instance))), property.PropertyType)); }
public DuplexCallbackEnumerator(string name, int enumeratorId, IQbservableProtocol protocol, IServerDuplexQbservableProtocolSink sink) { Contract.Requires(!string.IsNullOrEmpty(name)); Contract.Requires(protocol != null); Contract.Requires(sink != null); this.name = name; this.enumeratorId = enumeratorId; this.protocol = protocol; this.sink = sink; }
private static object CreateRemoteObservable(string name, IQbservableProtocol protocol, object instance, Type dataType) { Contract.Requires(!string.IsNullOrEmpty(name)); Contract.Requires(protocol != null); Contract.Requires(instance != null); Contract.Requires(dataType != null); Contract.Ensures(Contract.Result <object>() != null); var sink = protocol.GetOrAddSink(protocol.CreateClientDuplexSink); return(sink.RegisterObservableCallback(clientId => (IObservableDuplexCallback)Activator.CreateInstance(typeof(DuplexCallbackObservable <>).MakeGenericType(dataType), name, clientId, protocol.ClientId, instance))); }
private DuplexCallback(string name, IQbservableProtocol protocol, Func <int, object[], object> invoke) { Contract.Requires(!string.IsNullOrEmpty(name)); Contract.Requires(protocol != null); Contract.Requires(invoke != null); Name = name; this.invoke = invoke; ClientId = protocol.ClientId; Id = protocol.GetOrAddSink(protocol.CreateClientDuplexSink) .RegisterInvokeCallback(this); }
public static Expression Create(string name, IQbservableProtocol protocol, object instance, MethodInfo method, IEnumerable <Expression> argExpressions) { Contract.Requires(protocol != null); Contract.Requires(method != null); Contract.Requires(argExpressions != null); Contract.Ensures(Contract.Result <Expression>() != null); return(CreateInvoke( new DuplexCallback(name, protocol, (_, arguments) => ConvertIfSequence(name, protocol, method.Invoke(instance, arguments))), method.ReturnType, argExpressions)); }
public static Expression CreateObservable(string name, IQbservableProtocol protocol, object instance, Type dataType, Type type) { Contract.Requires(!string.IsNullOrEmpty(name)); Contract.Requires(protocol != null); Contract.Requires(dataType != null); Contract.Requires(type != null); Contract.Ensures(Contract.Result <Expression>() != null); return(Expression.Constant( CreateRemoteObservable(name, protocol, instance, dataType), type)); }
public void SetServerProtocol(IQbservableProtocol protocol) { Contract.Requires(Protocol == null); Contract.Requires(protocol != null); this.protocol = protocol; this.sink = protocol.FindSink <IServerDuplexQbservableProtocolSink>(); if (sink == null) { throw new InvalidOperationException(Errors.ProtocolDuplexSinkUnavailableForClientCallback); } }
public ServerQbservableProvider( IQbservableProtocol protocol, QbservableServiceOptions options, Func <object, IQbservable <TSource> > sourceSelector) { Contract.Requires(protocol != null); Contract.Requires(options != null); Contract.Requires(sourceSelector != null); Protocol = protocol; Options = options; this.sourceSelector = sourceSelector; }
public Expression PrepareExpression(IQbservableProtocol protocol) { Contract.Requires(protocol != null); Contract.Ensures(Contract.Result <Expression>() != null); Log.ClientSendingExpression(clientId, Expression); if (!Expression.Type.GetIsGenericType() || (Expression.Type.GetGenericTypeDefinition() != typeof(IQbservable <>) && Expression.Type.GetGenericTypeDefinition() != typeof(ClientQuery <>))) { throw new InvalidOperationException("The query must end as an IQbservable<T>."); } var visitor = ReplaceConstantsVisitor.CreateForGenericTypeByDefinition( typeof(ClientQuery <>), #if REFLECTION (_, actualType) => Activator.CreateInstance(typeof(QbservableSourcePlaceholder <>).MakeGenericType(actualType.GetGenericArguments()[0]), true), #else (_, actualType) => Activator.CreateInstance(typeof(QbservableSourcePlaceholder <>).MakeGenericType(actualType.GetGenericArguments()[0])), #endif type => typeof(IQbservable <>).MakeGenericType(type.GetGenericArguments()[0])); var result = visitor.Visit(Expression); if (visitor.ReplacedConstants == 0) { throw new InvalidOperationException("A queryable observable service was not found in the query."); } var evaluator = Provider.ClientEvaluator; if (!evaluator.IsKnownType(Provider.SourceType)) { evaluator.AddKnownType(Provider.SourceType); } var evaluationVisitor = new LocalEvaluationVisitor(evaluator, protocol); var preparedExpression = evaluationVisitor.Visit(result); Contract.Assume(!evaluationVisitor.HasAnyExpectedTypes); Log.ClientRewrittenExpression(clientId, preparedExpression); return(preparedExpression); }
private static object ConvertIfSequence(string name, IQbservableProtocol protocol, object instance) { Contract.Requires(!string.IsNullOrEmpty(name)); Contract.Requires(protocol != null); if (instance != null) { var type = instance.GetType(); #if SERIALIZATION if (!type.IsSerializable) #endif { var observableType = type.GetGenericInterfaceFromDefinition(typeof(IObservable <>)); if (observableType != null) { return(CreateRemoteObservable(name, protocol, instance, observableType.GetGenericArguments()[0])); } var enumerableType = type.GetGenericInterfaceFromDefinition(typeof(IEnumerable <>)); var enumerable = instance as IEnumerable; if (enumerableType != null) { return(CreateRemoteEnumerable(name, protocol, enumerable, enumerableType.GetGenericArguments()[0])); } else if (enumerable != null) { return(CreateRemoteEnumerable(name, protocol, enumerable.Cast <object>(), typeof(object))); } } } return(instance); }
public Expression EvaluateCompilerGenerated(MemberExpression member, Type expectedType, IQbservableProtocol protocol) { Contract.Requires(member != null); Contract.Requires(protocol != null); var closure = member.Expression as ConstantExpression; if (closure == null) { return(null); } var instance = closure.Value; string name; object value; Type type; var field = member.Member as FieldInfo; if (field != null) { name = field.DeclaringType.Name + "." + field.Name; type = field.FieldType; value = field.GetValue(instance); } else { var property = (PropertyInfo)member.Member; name = property.DeclaringType.Name + "." + property.Name; type = property.PropertyType; value = property.GetValue(instance); } return(TryEvaluateSequences(name, value, type, expectedType, protocol)); }
public override Expression Invoke(MethodCallExpression call, ExpressionVisitor visitor, IQbservableProtocol protocol) { Contract.Requires(call != null); Contract.Requires(visitor != null); Contract.Requires(protocol != null); Contract.Ensures(Contract.Result <Expression>() != null); return(null); }
public override Expression GetValue(FieldInfo field, MemberExpression member, ExpressionVisitor visitor, IQbservableProtocol protocol) { Contract.Requires(field != null); Contract.Requires(member != null); Contract.Requires(visitor != null); Contract.Requires(protocol != null); Contract.Ensures(Contract.Result <Expression>() != null); return(null); }
protected abstract Expression TryEvaluateObservable(string name, object value, Type type, IQbservableProtocol protocol);
protected abstract Either <object, Expression> TryEvaluateEnumerable(string name, object value, Type type, IQbservableProtocol protocol);
protected override Expression TryEvaluateObservable(string name, object value, Type type, IQbservableProtocol protocol) { var observableType = value.GetType().GetGenericInterfaceFromDefinition(typeof(IObservable <>)); if (observableType != null) { return(DuplexCallback.CreateObservable(name, protocol, value, observableType.GetGenericArguments()[0], type)); } return(null); }
public override Expression GetValue(FieldInfo field, MemberExpression member, ExpressionVisitor visitor, IQbservableProtocol protocol) { object instance = Evaluate(member.Expression, visitor, _ => Errors.ExpressionMemberMissingLocalInstanceFormat, member.Member); var value = field.GetValue(instance); return(TryEvaluateSequences(field.DeclaringType.Name + "." + field.Name, value, member.Type, field.FieldType, protocol)); }
public abstract Expression Invoke(MethodCallExpression call, ExpressionVisitor visitor, IQbservableProtocol protocol);
public abstract Expression GetValue(PropertyInfo property, MemberExpression member, ExpressionVisitor visitor, IQbservableProtocol protocol);
protected override Expression TryEvaluateObservable(string name, object value, Type type, IQbservableProtocol protocol) { Contract.Requires(!string.IsNullOrEmpty(name)); Contract.Requires(value != null); Contract.Requires(type != null); Contract.Requires(protocol != null); return(null); }
protected override Either <object, Expression> TryEvaluateEnumerable(string name, object value, Type type, IQbservableProtocol protocol) { var iterator = value as IEnumerable; if (iterator != null) { var iteratorType = iterator.GetType(); if (iteratorType.GetCustomAttribute <CompilerGeneratedAttribute>(true) != null || (type.GetIsGenericType() && type.GetGenericTypeDefinition() == typeof(IEnumerable <>)) || type == typeof(IEnumerable)) { value = EvaluateIterator(iterator); return(Either.Left <object, Expression>(value)); } } return(null); }
public override Expression Invoke(MethodCallExpression call, ExpressionVisitor visitor, IQbservableProtocol protocol) { if (call.Method.ReturnType == typeof(void)) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Errors.ExpressionCallLocalVoidFormat, call.Method, call.Object)); } object instance = Evaluate(call.Object, visitor, _ => Errors.ExpressionCallMissingLocalInstanceFormat, call.Method); var result = call.Method.Invoke(instance, EvaluateArguments(call, visitor).ToArray()); return(TryEvaluateSequences(call.Method.DeclaringType.Name + "." + call.Method.Name, result, call.Type, call.Method.ReturnType, protocol)); }
internal Expression GetValue(MemberExpression member, ExpressionVisitor visitor, IQbservableProtocol protocol) { Contract.Requires(member != null); Contract.Requires(visitor != null); Contract.Requires(protocol != null); Contract.Ensures(Contract.Result <Expression>() != null); var property = member.Member as PropertyInfo; if (property != null) { return(GetValue(property, member, visitor, protocol)); } else { return(GetValue((FieldInfo)member.Member, member, visitor, protocol)); } }
public override Expression GetValue(FieldInfo field, MemberExpression member, ExpressionVisitor visitor, IQbservableProtocol protocol) => DuplexCallback.Create( field.DeclaringType.Name + "." + field.Name, protocol, Evaluate(member.Expression, visitor, _ => Errors.ExpressionMemberMissingLocalInstanceFormat, member.Member), field);
public abstract Expression GetValue(FieldInfo field, MemberExpression member, ExpressionVisitor visitor, IQbservableProtocol protocol);
public override Expression Invoke(MethodCallExpression call, ExpressionVisitor visitor, IQbservableProtocol protocol) { object instance; if (call.Method.ReturnType == typeof(void)) { instance = null; } else { instance = Evaluate(call.Object, visitor, _ => Errors.ExpressionCallMissingLocalInstanceFormat, call.Method); } return(DuplexCallback.Create(call.Method.DeclaringType.Name + "." + call.Method.Name, protocol, instance, call.Method, visitor.Visit(call.Arguments))); }
public Expression TryEvaluateSequences(string name, object value, Type type, Type expectedType, IQbservableProtocol protocol) { Contract.Requires(!string.IsNullOrEmpty(name)); Contract.Requires(type != null); Contract.Requires(protocol != null); if (value != null) { var evaluatedType = expectedType ?? type; var isSequence = evaluatedType == typeof(IEnumerable) || (evaluatedType.GetIsGenericType() && (evaluatedType.GetGenericTypeDefinition() == typeof(IEnumerable <>) || evaluatedType.GetGenericTypeDefinition() == typeof(IObservable <>))); if (isSequence || !IsTypeKnown(value)) { var result = TryEvaluateEnumerable(name, value, evaluatedType, protocol); if (result != null) { return(result.IsLeft ? Expression.Constant(result.Left, type) : result.Right); } else { var expression = TryEvaluateObservable(name, value, evaluatedType, protocol); if (expression != null) { return(expression); } } } } return(Expression.Constant(value, type)); }
protected override Either <object, Expression> TryEvaluateEnumerable(string name, object value, Type type, IQbservableProtocol protocol) { Expression expression = null; if (type.GetIsGenericType() && type.GetGenericTypeDefinition() == typeof(IEnumerable <>)) { expression = DuplexCallback.CreateEnumerable(name, protocol, value, type.GetGenericArguments()[0], type); } else if (type == typeof(IEnumerable)) { var enumerable = (IEnumerable)value; expression = DuplexCallback.CreateEnumerable(name, protocol, enumerable.Cast <object>(), typeof(object), type); } return(expression == null ? null : Either.Right <object, Expression>(expression)); }