Exemplo n.º 1
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[FieldCode.Destination] = method_call.Sender;
            }

            replyMsg.Signature = inSig;

            //replyMsg.WriteHeader ();

            return(replyMsg);
        }
Exemplo n.º 2
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[FieldCode.Destination] = method_call.Sender;
            }

            replyMsg.Signature = outSig;

            return(replyMsg);
        }
Exemplo n.º 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);
        }