Beispiel #1
0
        private Object ReadObject(Stream stream, ArrayList objectContainer)
        {
            String typeName    = GetString(ReadBinaryString(stream));
            Int32  memberCount = Int32.Parse(ReadNumber(stream));

            stream.Position++;
            if (typeName.Equals("PHPRPC_Date"))
            {
                return(ReadDateTime(stream, objectContainer));
            }
            Type   type = GetTypeByAlias(typeName);
            Object result;

            if (type == null)
            {
                result = new Hashtable(memberCount);
                objectContainer.Add(result);
                for (Int32 i = 0; i < memberCount; i++)
                {
                    String key = ReadKey(stream);
                    if (key[0] == '\0')
                    {
                        key = key.Substring(key.IndexOf('\0', 1) + 1);
                    }
                    ((Hashtable)result)[key] = Deserialize(stream, objectContainer);
                }
            }
            else
            {
                result = CreateInstance(type);
                objectContainer.Add(result);
                for (Int32 i = 0; i < memberCount; i++)
                {
                    FieldInfo field = GetField(type, ReadKey(stream));
                    Object    value = Deserialize(stream, objectContainer);
                    if (field != null)
                    {
                        field.SetValue(result, PHPConvert.ChangeType(value, field.FieldType, encoding));
                    }
                }
                MethodInfo __wakeup;
                if (__wakeupcache.ContainsKey(type))
                {
                    __wakeup = (MethodInfo)__wakeupcache[type];
                }
                else
                {
                    BindingFlags bindingflags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase;
                    __wakeup            = type.GetMethod("__wakeup", bindingflags, null, new Type[0], new ParameterModifier[0]);
                    __wakeupcache[type] = __wakeup;
                }
                if (__wakeup != null)
                {
                    __wakeup.Invoke(result, null);
                }
            }
            stream.Position++;
            return(result);
        }
Beispiel #2
0
        private void InvokeCallback(Object state)
        {
            RequestState requestState = state as RequestState;
            Hashtable    result       = GetResponseBody(requestState.response, requestState.encryptMode);
            PHPRPC_Error error;
            Int32        errno = (Int32)result["phprpc_errno"];

            if (errno > 0)
            {
                String errstr = (String)result["phprpc_errstr"];
                error = new PHPRPC_Error(errno, errstr);
            }
            else
            {
                error = null;
            }
            if (result.ContainsKey("phprpc_output"))
            {
                output = (String)result["phprpc_output"];
            }
            else
            {
                output = String.Empty;
            }
            if (requestState.syncCallback != null)
            {
                Delegate callback     = (Delegate)requestState.syncCallback;
                Type     callbackType = callback.GetType();
                Object[] args         = requestState.args;
                Object   retval       = error;
                if (result.ContainsKey("phprpc_result"))
                {
                    if (result.ContainsKey("phprpc_args"))
                    {
                        args = (Object[])PHPConvert.ToArray((AssocArray)Deserialize(Decrypt((Byte[])result["phprpc_args"], 1, requestState.encryptMode)), typeof(Object[]), encoding);
                    }
                    retval = Deserialize(Decrypt((Byte[])result["phprpc_result"], 2, requestState.encryptMode));
                }
                if (callbackType.IsGenericType)
                {
                    Type resultType = callbackType.GetGenericArguments()[0];
                    if (retval != error)
                    {
                        callback.DynamicInvoke(PHPConvert.ChangeType(retval, resultType, encoding), args, output, error, false);
                    }
                    else
                    {
                        callback.DynamicInvoke(null, args, output, error, true);
                    }
                }
                else
                {
                    ((PHPRPC_Callback)callback)(retval, args, output, error);
                }
            }
        }
Beispiel #3
0
        public Object Invoke(Object proxy, MethodInfo method, Object[] args)
        {
            Type[]  paramTypes = ToTypes(method.GetParameters());
            Int32   n          = paramTypes.Length;
            Boolean byRef      = false;

            for (Int32 i = 0; i < n; i++)
            {
                if (paramTypes[i].IsByRef)
                {
                    byRef = true;
                    break;
                }
            }
#if !NET1
            if ((n > 0) && ((paramTypes[n - 1] == typeof(PHPRPC_Callback)) || ((paramTypes[n - 1].IsGenericType) && (paramTypes[n - 1].GetGenericTypeDefinition() == typeof(PHPRPC_Callback <>)))))
            {
                Object[] tmpargs = new Object[n - 1];
                Array.Copy(args, tmpargs, n - 1);
                client.Invoke(method.Name, tmpargs, (Delegate)args[n - 1], byRef, client.EncryptMode);
                return(null);
            }
            else if ((n > 1) && ((paramTypes[n - 2] == typeof(PHPRPC_Callback)) || ((paramTypes[n - 2].IsGenericType) && (paramTypes[n - 2].GetGenericTypeDefinition() == typeof(PHPRPC_Callback <>)))) && (paramTypes[n - 1] == typeof(Byte)))
            {
                Object[] tmpargs = new Object[n - 2];
                Array.Copy(args, tmpargs, n - 2);
                client.Invoke(method.Name, tmpargs, (Delegate)args[n - 2], byRef, (Byte)args[n - 1]);
                return(null);
            }
#endif
#if SILVERLIGHT
            throw new PHPRPC_Error(1, "SilverLight do not support synchronous invoke.");
#else
            Object result = client.Invoke(method.Name, args, byRef);
            if (result is PHPRPC_Error)
            {
                throw (PHPRPC_Error)result;
            }
            if (byRef)
            {
                for (Int32 i = 0; i < n; i++)
                {
                    if (paramTypes[i].IsByRef)
                    {
                        args[i] = PHPConvert.ChangeType(args[i], paramTypes[i].GetElementType(), client.Charset);
                    }
                }
            }
            return(PHPConvert.ChangeType(result, method.ReturnType, client.Charset));
#endif
        }
Beispiel #4
0
        private Byte[] Call(MethodInfo function, Object obj, ArrayList arguments)
        {
            ParameterInfo[] p        = function.GetParameters();
            String          funcname = function.Name;
            MemoryStream    ms       = new MemoryStream();
            StreamWriter    sw       = new StreamWriter(ms, encoding);
            Int32           size     = arguments.Count;

            if (p.Length != size)
            {
                if (p.Length == size + 1)
                {
                    Type type = p[p.Length - 1].ParameterType;
                    if (type == typeof(TextWriter) || type == typeof(StreamWriter))
                    {
                        arguments.Add(sw);
                    }
                    else
                    {
                        throw new ArgumentException("number of arguments mismatch for " + funcname + "().");
                    }
                }
                else
                {
                    throw new ArgumentException("number of arguments mismatch for " + funcname + "().");
                }
            }
            Object[] args = arguments.ToArray();
            if (size < arguments.Count)
            {
                arguments.RemoveAt(size);
            }
            for (Int32 i = 0, n = args.Length; i < n; i++)
            {
                if (args[i] != null)
                {
                    args[i] = PHPConvert.ChangeType(args[i], p[i].ParameterType, encoding);
                }
            }
            Byte[] result = Serialize(function.Invoke(obj, args));
            sw.Close();
            output = encoding.GetString(ms.ToArray());
            ms.Close();
            for (Int32 i = 0; i < size; i++)
            {
                arguments[i] = args[i];
            }
            return(result);
        }