コード例 #1
0
ファイル: SimpleWireFormat.cs プロジェクト: pmq20/mono_forked
        public IMethodCallMessage DeserializeRequest(Stream serializationStream, string uri)
        {
            if (serializationStream == null)
            {
                throw new ArgumentNullException("serializationStream is null");
            }

            Type svr_type = RemotingServices.GetServerTypeForUri(uri);

            if (svr_type == null)
            {
                throw new RemotingException("no registered server for uri " + uri);
            }

            BinaryReader reader = new BinaryReader(serializationStream);

            string method_name = reader.ReadString();
            int    arg_count   = reader.ReadInt32();

            object [] args = new object [arg_count];
            for (int i = 0; i < arg_count; i++)
            {
                args [i] = DeserializeObject(reader);
            }

            MonoMethodMessage msg = new MonoMethodMessage(svr_type, method_name, args);

            msg.Uri = uri;

            return(msg);
        }
コード例 #2
0
        internal object LoadRemoteFieldNew(IntPtr classPtr, IntPtr fieldPtr)
        {
            Mono.RuntimeClassHandle classHandle = new Mono.RuntimeClassHandle(classPtr);
            RuntimeFieldHandle      fieldHandle = new RuntimeFieldHandle(fieldPtr);
            RuntimeTypeHandle       typeHandle  = classHandle.GetTypeHandle();

            FieldInfo field = FieldInfo.GetFieldFromHandle(fieldHandle);

            if (InCurrentContext())
            {
                object o = _rp._server;
                return(field.GetValue(o));
            }

            object[] inArgs = new object[] { Type.GetTypeFromHandle(typeHandle).FullName,
                                             field.Name };
            object[]   outArgsMsg = new object[1];
            MethodInfo minfo      = typeof(object).GetMethod("FieldGetter", BindingFlags.NonPublic | BindingFlags.Instance);

            if (minfo == null)
            {
                throw new MissingMethodException("System.Object", "FieldGetter");
            }
            MonoMethodMessage msg = new MonoMethodMessage(minfo, inArgs, outArgsMsg);

            object[]  outArgs;
            Exception exc;

            RealProxy.PrivateInvoke(_rp, msg, out exc, out outArgs);
            if (exc != null)
            {
                throw exc;
            }
            return(outArgs[0]);
        }
コード例 #3
0
        public override IMessage Invoke(IMessage request)
        {
            IMethodCallMessage methodCallMessage = request as IMethodCallMessage;

            if (methodCallMessage != null)
            {
                if (methodCallMessage.MethodBase == RemotingProxy._cache_GetHashCodeMethod)
                {
                    return(new MethodResponse(base.ObjectIdentity.GetHashCode(), null, null, methodCallMessage));
                }
                if (methodCallMessage.MethodBase == RemotingProxy._cache_GetTypeMethod)
                {
                    return(new MethodResponse(base.GetProxiedType(), null, null, methodCallMessage));
                }
            }
            IInternalMessage internalMessage = request as IInternalMessage;

            if (internalMessage != null)
            {
                if (internalMessage.Uri == null)
                {
                    internalMessage.Uri = this._targetUri;
                }
                internalMessage.TargetIdentity = this._objectIdentity;
            }
            this._objectIdentity.NotifyClientDynamicSinks(true, request, true, false);
            IMessageSink messageSink;

            if (Thread.CurrentContext.HasExitSinks && !this._hasEnvoySink)
            {
                messageSink = Thread.CurrentContext.GetClientContextSinkChain();
            }
            else
            {
                messageSink = this._sink;
            }
            MonoMethodMessage monoMethodMessage = request as MonoMethodMessage;
            IMessage          result;

            if (monoMethodMessage == null || monoMethodMessage.CallType == CallType.Sync)
            {
                result = messageSink.SyncProcessMessage(request);
            }
            else
            {
                AsyncResult  asyncResult = monoMethodMessage.AsyncResult;
                IMessageCtrl messageCtrl = messageSink.AsyncProcessMessage(request, asyncResult);
                if (asyncResult != null)
                {
                    asyncResult.SetMessageCtrl(messageCtrl);
                }
                result = new ReturnMessage(null, new object[0], 0, null, monoMethodMessage);
            }
            this._objectIdentity.NotifyClientDynamicSinks(false, request, true, false);
            return(result);
        }
コード例 #4
0
ファイル: RealProxy.cs プロジェクト: zgramana/mono
        static object[] ProcessResponse(IMethodReturnMessage mrm, MonoMethodMessage call)
        {
            // Check return type

            MethodInfo mi = (MethodInfo)call.MethodBase;

            if (mrm.ReturnValue != null && !mi.ReturnType.IsInstanceOfType(mrm.ReturnValue))
            {
                throw new InvalidCastException("Return value has an invalid type");
            }

            // Check out parameters


            int no;

            if (call.NeedsOutProcessing(out no))
            {
                ParameterInfo[] parameters = mi.GetParameters();
                object[]        outArgs    = new object [no];
                int             narg       = 0;

                foreach (ParameterInfo par in parameters)
                {
                    if (par.IsOut && !par.ParameterType.IsByRef)
                    {
                        // Special marshalling required
                        object outArg = par.Position < mrm.ArgCount ? mrm.GetArg(par.Position) : null;
                        if (outArg != null)
                        {
                            object local = call.GetArg(par.Position);
                            if (local == null)
                            {
                                throw new RemotingException("Unexpected null value in local out parameter '" + par.Name + "'");
                            }
                            RemotingServices.UpdateOutArgObject(par, local, outArg);
                        }
                    }
                    else if (par.ParameterType.IsByRef)
                    {
                        object outArg = par.Position < mrm.ArgCount ? mrm.GetArg(par.Position) : null;
                        if (outArg != null && !par.ParameterType.GetElementType().IsInstanceOfType(outArg))
                        {
                            throw new InvalidCastException("Return argument '" + par.Name + "' has an invalid type");
                        }
                        outArgs [narg++] = outArg;
                    }
                }
                return(outArgs);
            }
            else
            {
                return(new object [0]);
            }
        }
コード例 #5
0
        private static object[] ProcessResponse(IMethodReturnMessage mrm, MonoMethodMessage call)
        {
            MethodInfo methodInfo = (MethodInfo)call.MethodBase;

            if (mrm.ReturnValue != null && !methodInfo.ReturnType.IsInstanceOfType(mrm.ReturnValue))
            {
                throw new InvalidCastException("Return value has an invalid type");
            }
            int num;

            if (call.NeedsOutProcessing(out num))
            {
                ParameterInfo[] parameters = methodInfo.GetParameters();
                object[]        array      = new object[num];
                int             num2       = 0;
                foreach (ParameterInfo parameterInfo in parameters)
                {
                    if (parameterInfo.IsOut && !parameterInfo.ParameterType.IsByRef)
                    {
                        object obj = (parameterInfo.Position >= mrm.ArgCount) ? null : mrm.GetArg(parameterInfo.Position);
                        if (obj != null)
                        {
                            object arg = call.GetArg(parameterInfo.Position);
                            if (arg == null)
                            {
                                throw new RemotingException("Unexpected null value in local out parameter '" + parameterInfo.Name + "'");
                            }
                            RemotingServices.UpdateOutArgObject(parameterInfo, arg, obj);
                        }
                    }
                    else if (parameterInfo.ParameterType.IsByRef)
                    {
                        object obj2 = (parameterInfo.Position >= mrm.ArgCount) ? null : mrm.GetArg(parameterInfo.Position);
                        if (obj2 != null && !parameterInfo.ParameterType.GetElementType().IsInstanceOfType(obj2))
                        {
                            throw new InvalidCastException("Return argument '" + parameterInfo.Name + "' has an invalid type");
                        }
                        array[num2++] = obj2;
                    }
                }
                return(array);
            }
            return(new object[0]);
        }
コード例 #6
0
ファイル: RealProxy.cs プロジェクト: samcf111/unityMono5.5.0
        internal void StoreRemoteField(IntPtr classPtr, IntPtr fieldPtr, object arg)
        {
            Mono.RuntimeClassHandle classHandle = new Mono.RuntimeClassHandle(classPtr);
            RuntimeFieldHandle      fieldHandle = new RuntimeFieldHandle(fieldPtr);
            RuntimeTypeHandle       typeHandle  = classHandle.GetTypeHandle();
            FieldInfo field = FieldInfo.GetFieldFromHandle(fieldHandle);

            if (InCurrentContext())
            {
                object o = _rp._server;
                field.SetValue(o, arg);
                return;
            }

            string typeName  = Type.GetTypeFromHandle(typeHandle).FullName;
            string fieldName = field.Name;

            object [] inArgs = new object[] { typeName,
                                              fieldName,
                                              arg };
            MethodInfo minfo = typeof(object).GetMethod("FieldSetter", BindingFlags.NonPublic | BindingFlags.Instance);

            if (minfo == null)
            {
                throw new MissingMethodException("System.Object", "FieldSetter");
            }

            MonoMethodMessage msg = new MonoMethodMessage(minfo, inArgs, null);

            object [] outArgs;
            Exception exc;

            RealProxy.PrivateInvoke(_rp, msg, out exc, out outArgs);
            if (exc != null)
            {
                throw exc;
            }
        }
コード例 #7
0
        /* this is called from unmanaged code */
        internal static object PrivateInvoke(RealProxy rp, IMessage msg, out Exception exc,
                                             out object [] out_args)
        {
            MonoMethodMessage mMsg = (MonoMethodMessage)msg;

            mMsg.LogicalCallContext = CallContext.CreateLogicalCallContext(true);
            CallType call_type   = mMsg.CallType;
            bool     is_remproxy = (rp is RemotingProxy);

            out_args = null;
            IMethodReturnMessage res_msg = null;

            if (call_type == CallType.BeginInvoke)
            {
                // todo: set CallMessage in runtime instead
                mMsg.AsyncResult.CallMessage = mMsg;
            }

            if (call_type == CallType.EndInvoke)
            {
                res_msg = (IMethodReturnMessage)mMsg.AsyncResult.EndInvoke();
            }

            // Check for constructor msg
            if (mMsg.MethodBase.IsConstructor)
            {
                if (is_remproxy)
                {
                    res_msg = (IMethodReturnMessage)(rp as RemotingProxy).ActivateRemoteObject((IMethodMessage)msg);
                }
                else
                {
                    msg = new ConstructionCall(rp.GetProxiedType());
                }
            }

            if (null == res_msg)
            {
                bool failed = false;

                try {
                    res_msg = (IMethodReturnMessage)rp.Invoke(msg);
                } catch (Exception ex) {
                    failed = true;
                    if (call_type == CallType.BeginInvoke)
                    {
                        // If async dispatch crashes, don't propagate the exception.
                        // The exception will be raised when calling EndInvoke.
                        mMsg.AsyncResult.SyncProcessMessage(new ReturnMessage(ex, msg as IMethodCallMessage));
                        res_msg = new ReturnMessage(null, null, 0, null, msg as IMethodCallMessage);
                    }
                    else
                    {
                        throw;
                    }
                }

                // Note, from begining this code used AsyncResult.IsCompleted for
                // checking if it was a remoting or custom proxy, but in some
                // cases the remoting proxy finish before the call returns
                // causing this method to be called, therefore causing all kind of bugs.
                if ((!is_remproxy) && call_type == CallType.BeginInvoke && !failed)
                {
                    IMessage asyncMsg = null;

                    // allow calltype EndInvoke to finish
                    asyncMsg = mMsg.AsyncResult.SyncProcessMessage(res_msg as IMessage);
                    out_args = res_msg.OutArgs;
                    res_msg  = new ReturnMessage(asyncMsg, null, 0, null, res_msg as IMethodCallMessage);
                }
            }

            if (res_msg.LogicalCallContext != null && res_msg.LogicalCallContext.HasInfo)
            {
                CallContext.UpdateCurrentCallContext(res_msg.LogicalCallContext);
            }

            exc = res_msg.Exception;

            // todo: remove throw exception from the runtime invoke
            if (null != exc)
            {
                out_args = null;
                throw exc.FixRemotingException();
            }
            else if (res_msg is IConstructionReturnMessage)
            {
                if (out_args == null)
                {
                    out_args = res_msg.OutArgs;
                }
            }
            else if (mMsg.CallType == CallType.BeginInvoke)
            {
                // We don't have OutArgs in this case.
            }
            else if (mMsg.CallType == CallType.Sync)
            {
                out_args = ProcessResponse(res_msg, mMsg);
            }
            else if (mMsg.CallType == CallType.EndInvoke)
            {
                out_args = ProcessResponse(res_msg, mMsg.AsyncResult.CallMessage);
            }
            else
            {
                if (out_args == null)
                {
                    out_args = res_msg.OutArgs;
                }
            }

            return(res_msg.ReturnValue);
        }
コード例 #8
0
        public override IMessage Invoke(IMessage request)
        {
            IMethodCallMessage mm = request as IMethodCallMessage;

            if (mm != null)
            {
                if (mm.MethodBase == _cache_GetHashCodeMethod)
                {
                    return(new MethodResponse(ObjectIdentity.GetHashCode(), null, null, mm));
                }

                if (mm.MethodBase == _cache_GetTypeMethod)
                {
                    return(new MethodResponse(GetProxiedType(), null, null, mm));
                }
            }

            IInternalMessage im = request as IInternalMessage;

            if (im != null)
            {
                if (im.Uri == null)
                {
                    im.Uri = _targetUri;
                }
                im.TargetIdentity = _objectIdentity;
            }

            _objectIdentity.NotifyClientDynamicSinks(true, request, true, false);

            IMessage     response;
            IMessageSink sink;

            // Needs to go through the client context sink?
            if (Thread.CurrentContext.HasExitSinks && !_hasEnvoySink)
            {
                sink = Thread.CurrentContext.GetClientContextSinkChain();
            }
            else
            {
                sink = _sink;
            }

            MonoMethodMessage mMsg = request as MonoMethodMessage;

            if (mMsg == null || mMsg.CallType == CallType.Sync)
            {
                response = sink.SyncProcessMessage(request);
            }
            else
            {
                AsyncResult  ares  = mMsg.AsyncResult;
                IMessageCtrl mctrl = sink.AsyncProcessMessage(request, ares);
                if (ares != null)
                {
                    ares.SetMessageCtrl(mctrl);
                }
                response = new ReturnMessage(null, new object[0], 0, null, mMsg);
            }

            _objectIdentity.NotifyClientDynamicSinks(false, request, true, false);

            return(response);
        }
コード例 #9
0
        internal static object PrivateInvoke(RealProxy rp, IMessage msg, out Exception exc, out object[] out_args)
        {
            MonoMethodMessage monoMethodMessage = (MonoMethodMessage)msg;

            monoMethodMessage.LogicalCallContext = CallContext.CreateLogicalCallContext(true);
            CallType callType = monoMethodMessage.CallType;
            bool     flag     = rp is RemotingProxy;

            out_args = null;
            IMethodReturnMessage methodReturnMessage = null;

            if (callType == CallType.BeginInvoke)
            {
                monoMethodMessage.AsyncResult.CallMessage = monoMethodMessage;
            }
            if (callType == CallType.EndInvoke)
            {
                methodReturnMessage = (IMethodReturnMessage)monoMethodMessage.AsyncResult.EndInvoke();
            }
            if (monoMethodMessage.MethodBase.IsConstructor)
            {
                if (flag)
                {
                    methodReturnMessage = (IMethodReturnMessage)(rp as RemotingProxy).ActivateRemoteObject((IMethodMessage)msg);
                }
                else
                {
                    msg = new ConstructionCall(rp.GetProxiedType());
                }
            }
            if (methodReturnMessage == null)
            {
                bool flag2 = false;
                try
                {
                    methodReturnMessage = (IMethodReturnMessage)rp.Invoke(msg);
                }
                catch (Exception e)
                {
                    flag2 = true;
                    if (callType != CallType.BeginInvoke)
                    {
                        throw;
                    }
                    monoMethodMessage.AsyncResult.SyncProcessMessage(new ReturnMessage(e, msg as IMethodCallMessage));
                    methodReturnMessage = new ReturnMessage(null, null, 0, null, msg as IMethodCallMessage);
                }
                if (!flag && callType == CallType.BeginInvoke && !flag2)
                {
                    IMessage ret = monoMethodMessage.AsyncResult.SyncProcessMessage(methodReturnMessage);
                    out_args            = methodReturnMessage.OutArgs;
                    methodReturnMessage = new ReturnMessage(ret, null, 0, null, methodReturnMessage as IMethodCallMessage);
                }
            }
            if (methodReturnMessage.LogicalCallContext != null && methodReturnMessage.LogicalCallContext.HasInfo)
            {
                CallContext.UpdateCurrentCallContext(methodReturnMessage.LogicalCallContext);
            }
            exc = methodReturnMessage.Exception;
            if (exc != null)
            {
                out_args = null;
                throw exc.FixRemotingException();
            }
            if (methodReturnMessage is IConstructionReturnMessage)
            {
                if (out_args == null)
                {
                    out_args = methodReturnMessage.OutArgs;
                }
            }
            else if (monoMethodMessage.CallType != CallType.BeginInvoke)
            {
                if (monoMethodMessage.CallType == CallType.Sync)
                {
                    out_args = RealProxy.ProcessResponse(methodReturnMessage, monoMethodMessage);
                }
                else if (monoMethodMessage.CallType == CallType.EndInvoke)
                {
                    out_args = RealProxy.ProcessResponse(methodReturnMessage, monoMethodMessage.AsyncResult.CallMessage);
                }
                else if (out_args == null)
                {
                    out_args = methodReturnMessage.OutArgs;
                }
            }
            return(methodReturnMessage.ReturnValue);
        }