Ejemplo n.º 1
0
 private bool IsTargetAccepted(object target)
 {
     return(_semantics.HasOption(CallbackOptions.Strict)
          ? Protocol.IsTopLevelInterface(target.GetType())
          : _semantics.HasOption(CallbackOptions.Duck) ||
            Protocol.IsInstanceOfType(target));
 }
Ejemplo n.º 2
0
        object IProtocolAdapter.Dispatch(Type protocol, IMethodCallMessage message)
        {
            IHandler handler = this;

            protocol = protocol ?? message.MethodBase.ReflectedType;

            var options   = CallbackOptions.None;
            var semantics = new CallbackSemantics();

            handler.Handle(semantics, true);

            if (!semantics.IsSpecified(CallbackOptions.Duck) &&
                typeof(IDuck).IsAssignableFrom(protocol))
            {
                options |= CallbackOptions.Duck;
            }

            if (!semantics.IsSpecified(CallbackOptions.Strict) &&
                typeof(IStrict).IsAssignableFrom(protocol))
            {
                options |= CallbackOptions.Strict;
            }

            if (!semantics.IsSpecified(CallbackOptions.Resolve) &&
                typeof(IResolving).IsAssignableFrom(protocol))
            {
                options |= CallbackOptions.Resolve;
            }

            if (options != CallbackOptions.None)
            {
                semantics.SetOption(options, true);
                handler = this.Semantics(options);
            }

            var broadcast    = semantics.HasOption(CallbackOptions.Broadcast);
            var handleMethod = new HandleMethod(protocol, message, semantics);
            var callback     = semantics.HasOption(CallbackOptions.Resolve)
                             ? new ResolveMethod(protocol, broadcast, handleMethod)
                             : (object)handleMethod;

            var handled = handler.Handle(callback, broadcast);

            if (!(handled || semantics.HasOption(CallbackOptions.BestEffot)))
            {
                throw new MissingMethodException(
                          $"Method '{message.MethodName}' on {message.TypeName} not handled");
            }

            var result = handleMethod.Result;

            return(handled || result != null ? result
                 : RuntimeHelper.GetDefault(handleMethod.ResultType));
        }
Ejemplo n.º 3
0
        protected override bool HandleCallback(
            object callback, bool greedy, IHandler composer)
        {
            var handled = false;

            if (Composition.IsComposed <CallbackSemantics>(callback))
            {
                return(false);
            }

            var semantics = callback as CallbackSemantics;

            if (semantics != null)
            {
                _semantics.MergeInto(semantics);
                handled = true;
            }
            else if (!greedy)
            {
                if (_semantics.IsSpecified(CallbackOptions.Broadcast))
                {
                    greedy = _semantics.HasOption(CallbackOptions.Broadcast);
                }
                else
                {
                    var cs = new CallbackSemantics();
                    if (Handle(cs, true) && cs.IsSpecified(CallbackOptions.Broadcast))
                    {
                        greedy = cs.HasOption(CallbackOptions.Broadcast);
                    }
                }
            }

            if (greedy || !handled)
            {
                handled = base.HandleCallback(callback, greedy, composer) || handled;
            }

            return(handled);
        }