Exemplo n.º 1
0
        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]);
            }
        }
    public override IMessage Invoke(IMessage request)
    {
        IMethodCallMessage call = (IMethodCallMessage)request;

        Console.WriteLine("Invoke " + call.MethodName);

        Console.Write("ARGS(");
        for (int i = 0; i < call.ArgCount; i++)
        {
            if (i != 0)
            {
                Console.Write(", ");
            }
            Console.Write(call.GetArgName(i) + " " +
                          call.GetArg(i));
        }
        Console.WriteLine(")");
        Console.Write("INARGS(");
        for (int i = 0; i < call.InArgCount; i++)
        {
            if (i != 0)
            {
                Console.Write(", ");
            }
            Console.Write(call.GetInArgName(i) + " " +
                          call.GetInArg(i));
        }
        Console.WriteLine(")");

        ((R1)target).test_field = 1;

        IMethodReturnMessage res = RemotingServices.ExecuteMessage(target, call);

        Console.Write("RESARGS(");
        for (int i = 0; i < res.ArgCount; i++)
        {
            if (i != 0)
            {
                Console.Write(", ");
            }
            Console.Write(res.GetArgName(i) + " " +
                          res.GetArg(i));
        }
        Console.WriteLine(")");

        Console.Write("RESOUTARGS(");
        for (int i = 0; i < res.OutArgCount; i++)
        {
            if (i != 0)
            {
                Console.Write(", ");
            }
            Console.Write(res.GetOutArgName(i) + " " +
                          res.GetOutArg(i));
        }
        Console.WriteLine(")");

        return(res);
    }
Exemplo n.º 3
0
		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];
		}
Exemplo n.º 4
0
 public virtual Object GetArg(int argNum)
 {
     return(mrm.GetArg(argNum));
 }
        public static void WriteMethodResponse(BinaryWriter writer, object obj, Header[] headers, BinaryFormatter formatter)
        {
            IMethodReturnMessage resp = (IMethodReturnMessage)obj;

            writer.Write((byte)BinaryElement.MethodResponse);

            string[] internalProperties = MethodReturnDictionary.InternalReturnKeys;

            int    infoArrayLength = 0;
            object info            = null;

            object[] extraProperties = null;

            // Type of return value

            ReturnTypeTag returnTypeTag;
            MethodFlags   contextFlag = MethodFlags.ExcludeLogicalCallContext;

            if (resp.Exception != null)
            {
                returnTypeTag      = ReturnTypeTag.Exception | ReturnTypeTag.Null;
                internalProperties = MethodReturnDictionary.InternalExceptionKeys;
                infoArrayLength    = 1;
            }
            else if (resp.ReturnValue == null)
            {
                returnTypeTag = ReturnTypeTag.Null;
            }
            else if (IsMethodPrimitive(resp.ReturnValue.GetType()))
            {
                returnTypeTag = ReturnTypeTag.PrimitiveType;
            }
            else
            {
                returnTypeTag = ReturnTypeTag.ObjectType;
                infoArrayLength++;
            }

            // Message flags

            MethodFlags formatFlag;

            if ((resp.LogicalCallContext != null) && resp.LogicalCallContext.HasInfo)
            {
                contextFlag = MethodFlags.IncludesLogicalCallContext;
                infoArrayLength++;
            }

            if (resp.Properties.Count > internalProperties.Length && ((returnTypeTag & ReturnTypeTag.Exception) == 0))
            {
                extraProperties = GetExtraProperties(resp.Properties, internalProperties);
                infoArrayLength++;
            }

            if (resp.OutArgCount == 0)
            {
                formatFlag = MethodFlags.NoArguments;
            }
            else
            {
                if (AllTypesArePrimitive(resp.Args))
                {
                    formatFlag = MethodFlags.PrimitiveArguments;
                }
                else
                {
                    if (infoArrayLength == 0)
                    {
                        formatFlag = MethodFlags.ArgumentsInSimpleArray;
                    }
                    else
                    {
                        formatFlag = MethodFlags.ArgumentsInMultiArray;
                        infoArrayLength++;
                    }
                }
            }

            writer.Write((byte)(contextFlag | formatFlag));
            writer.Write((byte)returnTypeTag);

            // FIXME: what are the following 2 bytes for?
            writer.Write((byte)0);
            writer.Write((byte)0);

            // Arguments

            if (returnTypeTag == ReturnTypeTag.PrimitiveType)
            {
                writer.Write(BinaryCommon.GetTypeCode(resp.ReturnValue.GetType()));
                ObjectWriter.WritePrimitiveValue(writer, resp.ReturnValue);
            }

            if (formatFlag == MethodFlags.PrimitiveArguments)
            {
                writer.Write((uint)resp.ArgCount);
                for (int n = 0; n < resp.ArgCount; n++)
                {
                    object val = resp.GetArg(n);
                    if (val != null)
                    {
                        writer.Write(BinaryCommon.GetTypeCode(val.GetType()));
                        ObjectWriter.WritePrimitiveValue(writer, val);
                    }
                    else
                    {
                        writer.Write((byte)BinaryTypeCode.Null);
                    }
                }
            }

            if (infoArrayLength > 0)
            {
                object[] infoArray = new object[infoArrayLength];
                int      n         = 0;

                if ((returnTypeTag & ReturnTypeTag.Exception) != 0)
                {
                    infoArray[n++] = resp.Exception;
                }

                if (formatFlag == MethodFlags.ArgumentsInMultiArray)
                {
                    infoArray[n++] = resp.Args;
                }

                if (returnTypeTag == ReturnTypeTag.ObjectType)
                {
                    infoArray[n++] = resp.ReturnValue;
                }

                if (contextFlag == MethodFlags.IncludesLogicalCallContext)
                {
                    infoArray[n++] = resp.LogicalCallContext;
                }

                if (extraProperties != null)
                {
                    infoArray[n++] = extraProperties;
                }

                info = infoArray;
            }
            else if ((formatFlag & MethodFlags.ArgumentsInSimpleArray) > 0)
            {
                info = resp.Args;
            }

            if (info != null)
            {
                ObjectWriter objectWriter = new ObjectWriter(formatter);
                objectWriter.WriteObjectGraph(writer, info, headers);
            }
            else
            {
                writer.Write((byte)BinaryElement.End);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Provide trace output for the method return.  It is at this point that
        /// exceptions and return values will be logged (if available), and the
        /// elapsed time for the method to execute is calculated.
        /// </summary>
        /// <param name="msg">The incoming method call</param>
        /// <param name="returnMsg">The method return</param>
        private void TraceMethodReturn(IMessage msg, IMessage returnMsg)
        {
            IMethodReturnMessage methodReturn = returnMsg as IMethodReturnMessage;

            // Handle method returns only
            if ((methodReturn == null) || !(msg is IMethodMessage))
            {
                return;
            }

            // If no tracing is performed, exit.
            if (_traceLevel == TraceLevel.Off)
            {
                return;
            }

            // If the TraceLevel is set to "Info" or "Verbose", the method call
            // was logged and the indent increased.  Decrease it now.
            if (((int)_traceLevel) >= ((int)TraceLevel.Info))
            {
                Trace.Unindent();
            }

            // Calculate the time spent in the method call.
            TimeSpan elapsedTimeInCall = DateTime.Now - _timeOfMethodCall;

            StringBuilder textOutput = new StringBuilder(TRACE_OUTPUT_BUFFER_SIZE);

            // If an exception occurred, log it to the trace log and return.
            Exception e = methodReturn.Exception;

            if (e != null)
            {
                textOutput.Append(Type.GetType(methodReturn.TypeName).Name);
                textOutput.Append(".");
                textOutput.Append(methodReturn.MethodName);
                textOutput.Append("(");
                for (int index = 0; index < methodReturn.ArgCount; index++)
                {
                    if (index > 0)
                    {
                        textOutput.Append(", ");
                    }
                    textOutput.Append(methodReturn.GetArgName(index));
                    textOutput.Append(" = ");
                    textOutput.Append(methodReturn.GetArg(index).ToString());
                }
                textOutput.Append(")\n");
                textOutput.Append("----- EXCEPTION -----\n");
                textOutput.Append(e.ToString());
                textOutput.Append("\n----- EXCEPTION -----");

                Trace.WriteLine(textOutput.ToString(), string.Format(
                                    "Method Return - {0} ms", elapsedTimeInCall.TotalMilliseconds));
                return;
            }

            // If the trace level is set to "Info" or "Verbose", then log the
            // return values from the call as well as the time spent in the call.
            if (((int)_traceLevel) >= ((int)TraceLevel.Info))
            {
                textOutput.Append(Type.GetType(methodReturn.TypeName).Name);
                textOutput.Append(".");
                textOutput.Append(methodReturn.MethodName);
                textOutput.Append("(");
                if (methodReturn.OutArgCount > 0)
                {
                    textOutput.Append("ref/out parameters[");
                    for (int index = 0; index < methodReturn.OutArgCount; index++)
                    {
                        if (index > 0)
                        {
                            textOutput.Append(", ");
                        }
                        textOutput.Append(methodReturn.GetOutArgName(index));
                        textOutput.Append(" = ");
                        textOutput.Append(methodReturn.GetOutArg(index).ToString());
                    }
                    textOutput.Append("]");
                }

                if (methodReturn.ReturnValue.GetType() != typeof(void))
                {
                    if (methodReturn.OutArgCount > 0)
                    {
                        textOutput.Append(", ");
                    }
                    textOutput.Append("returned [");
                    textOutput.Append(methodReturn.ReturnValue.ToString());
                    textOutput.Append("]");
                }
                textOutput.Append(")");

                Trace.WriteLine(textOutput.ToString(), string.Format(
                                    "Method Return - {0} ms", elapsedTimeInCall.TotalMilliseconds));
            }
        }
Exemplo n.º 7
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]);
        }
        public static void WriteMethodResponse(BinaryWriter writer, object obj, Header[] headers, ISurrogateSelector surrogateSelector, StreamingContext context, FormatterAssemblyStyle assemblyFormat, FormatterTypeStyle typeFormat)
        {
            IMethodReturnMessage methodReturnMessage = (IMethodReturnMessage)obj;

            writer.Write(22);
            string[] array = MethodReturnDictionary.InternalReturnKeys;
            int      num   = 0;
            object   obj2  = null;

            object[]      array2      = null;
            MethodFlags   methodFlags = MethodFlags.ExcludeLogicalCallContext;
            ReturnTypeTag returnTypeTag;

            if (methodReturnMessage.Exception != null)
            {
                returnTypeTag = (ReturnTypeTag)34;
                array         = MethodReturnDictionary.InternalExceptionKeys;
                num           = 1;
            }
            else if (methodReturnMessage.ReturnValue == null)
            {
                returnTypeTag = ReturnTypeTag.Null;
            }
            else if (MessageFormatter.IsMethodPrimitive(methodReturnMessage.ReturnValue.GetType()))
            {
                returnTypeTag = ReturnTypeTag.PrimitiveType;
            }
            else
            {
                returnTypeTag = ReturnTypeTag.ObjectType;
                num++;
            }
            if (methodReturnMessage.LogicalCallContext != null && methodReturnMessage.LogicalCallContext.HasInfo)
            {
                methodFlags = MethodFlags.IncludesLogicalCallContext;
                num++;
            }
            if (methodReturnMessage.Properties.Count > array.Length && (byte)(returnTypeTag & ReturnTypeTag.Exception) == 0)
            {
                array2 = MessageFormatter.GetExtraProperties(methodReturnMessage.Properties, array);
                num++;
            }
            MethodFlags methodFlags2;

            if (methodReturnMessage.OutArgCount == 0)
            {
                methodFlags2 = MethodFlags.NoArguments;
            }
            else if (MessageFormatter.AllTypesArePrimitive(methodReturnMessage.Args))
            {
                methodFlags2 = MethodFlags.PrimitiveArguments;
            }
            else if (num == 0)
            {
                methodFlags2 = MethodFlags.ArgumentsInSimpleArray;
            }
            else
            {
                methodFlags2 = MethodFlags.ArgumentsInMultiArray;
                num++;
            }
            writer.Write((byte)(methodFlags | methodFlags2));
            writer.Write((byte)returnTypeTag);
            writer.Write(0);
            writer.Write(0);
            if (returnTypeTag == ReturnTypeTag.PrimitiveType)
            {
                writer.Write(BinaryCommon.GetTypeCode(methodReturnMessage.ReturnValue.GetType()));
                ObjectWriter.WritePrimitiveValue(writer, methodReturnMessage.ReturnValue);
            }
            if (methodFlags2 == MethodFlags.PrimitiveArguments)
            {
                writer.Write((uint)methodReturnMessage.ArgCount);
                for (int i = 0; i < methodReturnMessage.ArgCount; i++)
                {
                    object arg = methodReturnMessage.GetArg(i);
                    if (arg != null)
                    {
                        writer.Write(BinaryCommon.GetTypeCode(arg.GetType()));
                        ObjectWriter.WritePrimitiveValue(writer, arg);
                    }
                    else
                    {
                        writer.Write(17);
                    }
                }
            }
            if (num > 0)
            {
                object[] array3 = new object[num];
                int      num2   = 0;
                if ((byte)(returnTypeTag & ReturnTypeTag.Exception) != 0)
                {
                    array3[num2++] = methodReturnMessage.Exception;
                }
                if (methodFlags2 == MethodFlags.ArgumentsInMultiArray)
                {
                    array3[num2++] = methodReturnMessage.Args;
                }
                if (returnTypeTag == ReturnTypeTag.ObjectType)
                {
                    array3[num2++] = methodReturnMessage.ReturnValue;
                }
                if (methodFlags == MethodFlags.IncludesLogicalCallContext)
                {
                    array3[num2++] = methodReturnMessage.LogicalCallContext;
                }
                if (array2 != null)
                {
                    array3[num2++] = array2;
                }
                obj2 = array3;
            }
            else if ((methodFlags2 & MethodFlags.ArgumentsInSimpleArray) > (MethodFlags)0)
            {
                obj2 = methodReturnMessage.Args;
            }
            if (obj2 != null)
            {
                ObjectWriter objectWriter = new ObjectWriter(surrogateSelector, context, assemblyFormat, typeFormat);
                objectWriter.WriteObjectGraph(writer, obj2, headers);
            }
            else
            {
                writer.Write(11);
            }
        }