Beispiel #1
0
        //should generalize this method
        //it is duplicated in DProxy
        public static Message ConstructReplyFor(MethodCall method_call, object[] vals)
        {
            MethodReturn method_return = new MethodReturn(method_call.message.Header.Serial);
            Message      replyMsg      = method_return.message;

            Signature inSig = Signature.GetSig(vals);

            if (vals != null && vals.Length != 0)
            {
                MessageWriter writer = new MessageWriter(Connection.NativeEndianness);

                foreach (object arg in vals)
                {
                    writer.Write(arg.GetType(), arg);
                }

                replyMsg.Body = writer.ToArray();
            }

            //TODO: we should be more strict here, but this fallback was added as a quick fix for p2p
            if (method_call.Sender != null)
            {
                replyMsg.Header.Fields[FieldCode.Destination] = method_call.Sender;
            }

            replyMsg.Signature = inSig;

            //replyMsg.WriteHeader ();

            return(replyMsg);
        }
Beispiel #2
0
        //TODO: merge this with the above method
        public static Message ConstructReplyFor(MethodCall method_call, Type retType, object retVal)
        {
            MethodReturn method_return = new MethodReturn(method_call.message.Header.Serial);
            Message      replyMsg      = method_return.message;

            Signature inSig = Signature.GetSig(retType);

            if (inSig != Signature.Empty)
            {
                MessageWriter writer = new MessageWriter(Connection.NativeEndianness);
                writer.Write(retType, retVal);
                replyMsg.Body = writer.ToArray();
            }

            //TODO: we should be more strict here, but this fallback was added as a quick fix for p2p
            if (method_call.Sender != null)
            {
                replyMsg.Header.Fields[FieldCode.Destination] = method_call.Sender;
            }

            replyMsg.Signature = inSig;

            //replyMsg.WriteHeader ();

            return(replyMsg);
        }
Beispiel #3
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);
        }
Beispiel #4
0
        public static Message ConstructDynamicReply(MethodCall method_call, MethodInfo mi, object retVal, object[] vals)
        {
            Type retType = mi.ReturnType;

            MethodReturn method_return = new MethodReturn(method_call.message.Header.Serial);
            Message      replyMsg      = method_return.message;

            Signature outSig = Signature.GetSig(retType);

            outSig += Signature.GetSig(Mapper.GetTypes(ArgDirection.Out, mi.GetParameters()));

            if (outSig != Signature.Empty)
            {
                MessageWriter writer = new MessageWriter(Connection.NativeEndianness);

                //first write the return value, if any
                if (retType != null && retType != typeof(void))
                {
                    writer.Write(retType, retVal);
                }

                //then write the out args
                WriteDynamicValues(writer, mi.GetParameters(), vals);

                replyMsg.Body = writer.ToArray();
            }

            //TODO: we should be more strict here, but this fallback was added as a quick fix for p2p
            if (method_call.Sender != null)
            {
                replyMsg.Header.Fields[FieldCode.Destination] = method_call.Sender;
            }

            replyMsg.Signature = outSig;

            return(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);
        }
Beispiel #6
0
		public static Message ConstructDynamicReply (MethodCall method_call, MethodInfo mi, object retVal, object[] vals)
		{
			Type retType = mi.ReturnType;

			MethodReturn method_return = new MethodReturn (method_call.message.Header.Serial);
			Message replyMsg = method_return.message;

			Signature outSig = Signature.GetSig (retType);
			outSig += Signature.GetSig (Mapper.GetTypes (ArgDirection.Out, mi.GetParameters ()));

			if (outSig != Signature.Empty) {
				MessageWriter writer = new MessageWriter (Connection.NativeEndianness);

				//first write the return value, if any
				if (retType != null && retType != typeof (void))
					writer.Write (retType, retVal);

				//then write the out args
				WriteDynamicValues (writer, mi.GetParameters (), vals);

				replyMsg.Body = writer.ToArray ();
			}

			//TODO: we should be more strict here, but this fallback was added as a quick fix for p2p
			if (method_call.Sender != null)
				replyMsg.Header.Fields[FieldCode.Destination] = method_call.Sender;

			replyMsg.Signature = outSig;

			return replyMsg;
		}
Beispiel #7
0
		public static Message ConstructReply (MethodCall method_call, params object[] vals)
		{
			MethodReturn method_return = new MethodReturn (method_call.message.Header.Serial);
			Message replyMsg = method_return.message;

			Signature inSig = Signature.GetSig (vals);

			if (vals != null && vals.Length != 0) {
				MessageWriter writer = new MessageWriter (Connection.NativeEndianness);

				foreach (object arg in vals)
					writer.Write (arg.GetType (), arg);

				replyMsg.Body = writer.ToArray ();
			}

			//TODO: we should be more strict here, but this fallback was added as a quick fix for p2p
			if (method_call.Sender != null)
				replyMsg.Header.Fields[FieldCode.Destination] = method_call.Sender;

			replyMsg.Signature = inSig;

			//replyMsg.WriteHeader ();

			return replyMsg;
		}
		//TODO: merge this with the above method
		public static Message ConstructReplyFor (MethodCall method_call, Type retType, object retVal)
		{
			MethodReturn method_return = new MethodReturn (method_call.message.Header.Serial);
			Message replyMsg = method_return.message;

			Signature inSig = Signature.GetSig (retType);

			if (inSig != Signature.Empty) {
				MessageWriter writer = new MessageWriter (Connection.NativeEndianness);
				writer.Write (retType, retVal);
				replyMsg.Body = writer.ToArray ();
			}

			//TODO: we should be more strict here, but this fallback was added as a quick fix for p2p
			if (method_call.Sender != null)
				replyMsg.Header.Fields[FieldCode.Destination] = method_call.Sender;

			replyMsg.Signature = inSig;

			//replyMsg.WriteHeader ();

			return replyMsg;
		}
Beispiel #9
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);
        }