Пример #1
0
        public override void HandleMethodCall(MethodCall method_call)
        {
            //object retVal = obj.GetType ().InvokeMember (method_call.Member, BindingFlags.InvokeMethod, null, obj, new object[0]);
            //IDynamicMetaObjectProvider idyn = obj as IDynamicMetaObjectProvider;

            object retVal = null;

            Exception raisedException = null;

            try {
                object[] args = MessageHelper.GetDynamicValues(method_call.message);
                retVal = ops.InvokeMember(obj, method_call.Member, args);
                //retVal = ops.Call (ops.GetMember (obj, method_call.Member), args);
            } catch (Exception e) {
                raisedException = e;
            }

            if (!method_call.message.ReplyExpected)
            {
                return;
            }

            Message msg      = method_call.message;
            Message replyMsg = null;

            if (raisedException == null)
            {
                MethodReturn method_return = new MethodReturn(msg.Header.Serial);
                replyMsg = method_return.message;
                if (retVal != null)
                {
                    if (retVal.GetType().FullName == "IronRuby.Builtins.MutableString")
                    {
                        retVal = retVal.ToString();
                    }
                    // TODO: Invalid sig handling
                    Signature     outSig    = Signature.GetSig(retVal.GetType());
                    MessageWriter retWriter = new MessageWriter();
                    retWriter.Write(retVal.GetType(), retVal);
                    //retWriter.WriteValueType (retVal, retVal.GetType ());
                    replyMsg.Body      = retWriter.ToArray();
                    replyMsg.Signature = outSig;
                }
            }
            else
            {
                Error error = method_call.CreateError(Mapper.GetInterfaceName(raisedException.GetType()), raisedException.Message);
                replyMsg = error.message;
            }

            if (method_call.Sender != null)
            {
                replyMsg.Header[FieldCode.Destination] = method_call.Sender;
            }

            conn.Send(replyMsg);
        }
        public virtual void HandleMethodCall(MethodCall method_call)
        {
            Type type = obj.GetType ();

            //object retObj = type.InvokeMember (msg.Member, BindingFlags.InvokeMethod, null, obj, MessageHelper.GetDynamicValues (msg));

            //TODO: there is no member name mapping for properties etc. yet

            // FIXME: Inefficient to do this on every call
            MethodInfo mi = Mapper.GetMethod (type, method_call);

            if (mi == null) {
                conn.MaybeSendUnknownMethodError (method_call);
                return;
            }

            MethodCaller2 mCaller;
            if (!mCallers.TryGetValue (mi, out mCaller)) {
                //mCaller = TypeImplementer.GenCaller (mi, obj);
                mCaller = TypeImplementer.GenCaller2 (mi);
                mCallers[mi] = mCaller;
            }

            Signature inSig, outSig;
            TypeImplementer.SigsForMethod (mi, out inSig, out outSig);

            Message msg = method_call.message;
            MessageReader msgReader = new MessageReader (method_call.message);
            MessageWriter retWriter = new MessageWriter ();

            /*
            MessageWriter retWriter = null;
            if (msg.ReplyExpected)
                retWriter = new MessageWriter ();
            */

            Exception raisedException = null;
            try {
                //mCaller (msgReader, method_call.message, retWriter);
                mCaller (obj, msgReader, method_call.message, retWriter);
            } catch (Exception e) {
                raisedException = e;
            }

            if (!msg.ReplyExpected)
                return;

            Message replyMsg;

            if (raisedException == null) {
                MethodReturn method_return = new MethodReturn (msg.Header.Serial);
                replyMsg = method_return.message;
                replyMsg.Body = retWriter.ToArray ();
                replyMsg.Signature = outSig;
            } else {
                Error error;
                // BusException allows precisely formatted Error messages.
                BusException busException = raisedException as BusException;
                if (busException != null)
                    error = method_call.CreateError (busException.ErrorName, busException.ErrorMessage);
                else if (raisedException is ArgumentException && raisedException.TargetSite.Name == mi.Name) {
                    // Name match trick above is a hack since we don't have the resolved MethodInfo.
                    ArgumentException argException = (ArgumentException)raisedException;
                    using (System.IO.StringReader sr = new System.IO.StringReader (argException.Message)) {
                        error = method_call.CreateError ("org.freedesktop.DBus.Error.InvalidArgs", sr.ReadLine ());
                    }
                } else
                    error = method_call.CreateError (Mapper.GetInterfaceName (raisedException.GetType ()), raisedException.Message);

                replyMsg = error.message;
            }

            if (method_call.Sender != null)
                replyMsg.Header[FieldCode.Destination] = method_call.Sender;

            conn.Send (replyMsg);
        }
Пример #3
0
        public virtual void HandleMethodCall(MethodCall method_call)
        {
            Type type = obj.GetType();

            //object retObj = type.InvokeMember (msg.Member, BindingFlags.InvokeMethod, null, obj, MessageHelper.GetDynamicValues (msg));

            //TODO: there is no member name mapping for properties etc. yet

            // FIXME: Inefficient to do this on every call
            MethodInfo mi = Mapper.GetMethod(type, method_call);

            if (mi == null)
            {
                conn.MaybeSendUnknownMethodError(method_call);
                return;
            }

            MethodCaller2 mCaller;

            if (!mCallers.TryGetValue(mi, out mCaller))
            {
                //mCaller = TypeImplementer.GenCaller (mi, obj);
                mCaller      = TypeImplementer.GenCaller2(mi);
                mCallers[mi] = mCaller;
            }

            Signature inSig, outSig;

            TypeImplementer.SigsForMethod(mi, out inSig, out outSig);

            Message       msg       = method_call.message;
            MessageReader msgReader = new MessageReader(method_call.message);
            MessageWriter retWriter = new MessageWriter();

            /*
             * MessageWriter retWriter = null;
             * if (msg.ReplyExpected)
             *      retWriter = new MessageWriter ();
             */

            Exception raisedException = null;

            try {
                //mCaller (msgReader, method_call.message, retWriter);
                mCaller(obj, msgReader, method_call.message, retWriter);
            } catch (Exception e) {
                raisedException = e;
            }

            if (!msg.ReplyExpected)
            {
                return;
            }

            Message replyMsg;

            if (raisedException == null)
            {
                MethodReturn method_return = new MethodReturn(msg.Header.Serial);
                replyMsg           = method_return.message;
                replyMsg.Body      = retWriter.ToArray();
                replyMsg.Signature = outSig;
            }
            else
            {
                Error error;
                // BusException allows precisely formatted Error messages.
                BusException busException = raisedException as BusException;
                if (busException != null)
                {
                    error = method_call.CreateError(busException.ErrorName, busException.ErrorMessage);
                }
                else if (raisedException is ArgumentException && raisedException.TargetSite.Name == mi.Name)
                {
                    // Name match trick above is a hack since we don't have the resolved MethodInfo.
                    ArgumentException argException = (ArgumentException)raisedException;
                    using (System.IO.StringReader sr = new System.IO.StringReader(argException.Message)) {
                        error = method_call.CreateError("org.freedesktop.DBus.Error.InvalidArgs", sr.ReadLine());
                    }
                }
                else
                {
                    error = method_call.CreateError(Mapper.GetInterfaceName(raisedException.GetType()), raisedException.Message);
                }

                replyMsg = error.message;
            }

            if (method_call.Sender != null)
            {
                replyMsg.Header[FieldCode.Destination] = method_call.Sender;
            }

            conn.Send(replyMsg);
        }