Пример #1
0
        public static bool CallAsFuncAll(BinaryReader br, BinaryWriter bw, out object result, bool writeResult)
        {
            result = null;
            if (!GetAW(br, bw, out var autoWrap))
            {
                return(false);
            }

            string methodName = br.ReadString();
            var    args       = GetArrayParams(br);

            List <int> changeParameters = new List <int>();

            var res = autoWrap.TryInvokeMember(methodName, args, out result, changeParameters, out var error);

            if (!res)
            {
                SetError(error, bw);
                return(false);
            }

            if (writeResult)
            {
                bw.Write(true);
                WorkWithVariant.WriteObject(AutoWrap.WrapObject(result), bw);
                WriteChangeParams(bw, args, changeParameters);
            }
            return(true);
        }
Пример #2
0
        public static void IteratorNext(BinaryReader br, BinaryWriter bw)
        {
            if (!GetAW(br, bw, out var autoWrap))
            {
                return;
            }

            try
            {
                var enumerator = (System.Collections.IEnumerator)autoWrap.Object;
                var res        = enumerator.MoveNext();
                bw.Write(true);
                if (!res)
                {
                    AutoWrap.ObjectsList.RemoveKey(autoWrap.IndexInStorage);
                    bw.Write(false);
                    return;
                }

                bw.Write(true);
                WorkWithVariant.WriteObject(AutoWrap.WrapObject(enumerator.Current), bw);
            }
            catch (Exception e)
            {
                SetError(AutoWrap.GetExceptionString("Ошибка итератора", "", e), bw);
            }
        }
Пример #3
0
        public static void CallAsDelegate(BinaryReader br, BinaryWriter bw)
        {
            object result;

            if (!GetAW(br, bw, out var autoWrap))
            {
                return;
            }

            var args = GetArrayParams(br);

            try
            {
                var del = (Delegate)autoWrap.Object;
                result = del.DynamicInvoke(args);
            }
            catch (Exception e)
            {
                SetError(AutoWrap.GetExceptionString($"Ошибка вызова делегата Target = ", "", e), bw);
                return;
            }

            bw.Write(true);
            WorkWithVariant.WriteObject(AutoWrap.WrapObject(result), bw);
        }
Пример #4
0
        public static void GetIndex(BinaryReader br, BinaryWriter bw)
        {
            if (!GetAW(br, bw, out var autoWrap))
            {
                return;
            }

            var    parameters = GetArrayParams(br);
            string methodName = "get_Item";

            if (typeof(Array).IsAssignableFrom(autoWrap.Type))
            {
                methodName = "GetValue";
            }

            var changeParams = new List <int>();
            var res          = autoWrap.TryInvokeMember(methodName, parameters, out var result, changeParams, out var error);

            if (!res)
            {
                SetError(error, bw);
            }
            else
            {
                bw.Write(true);
                WorkWithVariant.WriteObject(AutoWrap.WrapObject(result), bw);
            }
        }
Пример #5
0
        private static void WriteChangeParams(BinaryWriter bw, object[] args, List <int> changeParameters)
        {
            bw.Write(changeParameters.Count);

            foreach (var i in changeParameters)
            {
                bw.Write(i);
                WorkWithVariant.WriteObject(AutoWrap.WrapObject(args[i]), bw);
            }
        }
Пример #6
0
        public static object[] GetArrayParams(BinaryReader br)
        {
            int size = br.ReadInt32();
            var res  = new object[size];

            for (int i = 0; i < res.Length; i++)
            {
                res[i] = WorkWithVariant.GetObject(br);
            }

            return(res);
        }
Пример #7
0
        internal void SendEvent(Guid eventKey, object result)
        {
            MemoryStream stream = new MemoryStream();
            var          bw     = new BinaryWriter(stream);

            bw.Write((byte)1);
            bw.Write(eventKey.ToByteArray());
            WorkWithVariant.WriteObject(AutoWrap.WrapObject(result), bw);
            bw.Flush();

            SendStream(stream);
        }
Пример #8
0
        internal void SendAsyncMessage(bool successfully, object result)
        {
            MemoryStream stream = new MemoryStream();
            var          bw     = new BinaryWriter(stream);

            bw.Write((byte)0);
            bw.Write(_key.ToByteArray());
            bw.Write(successfully);
            WorkWithVariant.WriteObject(AutoWrap.WrapObject(result), bw);
            bw.Flush();

            SendStream(stream);
        }
Пример #9
0
 public static void DeleteObjects(BinaryReader br, BinaryWriter bw)
 {
     try
     {
         int count = br.ReadInt32();
         for (int i = 0; i < count; i++)
         {
             int target = br.ReadInt32();
             AutoWrap.ObjectsList.RemoveKey(target);
         }
         bw.Write(true);
         WorkWithVariant.WriteObject(null, bw);
     }
     catch (Exception e)
     {
         SetError(AutoWrap.GetExceptionString("Ошибка удаления объектов", "", e), bw);
     }
 }
Пример #10
0
        //public static void GetWrapperForObjectWithEvents(BinaryReader br, BinaryWriter bw, IPAddress address)
        //{
        //    try
        //    {
        //        if (!GetAW(br, bw, out var autoWrap)) return;

        //        Type type = autoWrap.Type;
        //        Type genType = typeof(WrapperForEvents<>);
        //        Type constructed = genType.MakeGenericType(type);
        //        var propertyName = "WrapperCreator";

        //        var fi = constructed.GetField(propertyName);
        //        Delegate func = (Delegate)fi.GetValue(null);

        //        //var mi = constructed.GetMethod("СоздатьОбертку");
        //        // Delegate функция = (Delegate)mi.Invoke(null,null);

        //        var taskId = new Guid();
        //        var port = br.ReadInt32();

        //        var ac = new TcpAsyncCallBack(taskId, address, port);
        //        var callBack = new Action<Guid, object>(ac.SendEvent);

        //        bw.Write(true);
        //        WorkWithVariant.WriteObject(AutoWrap.WrapObject(func.DynamicInvoke(callBack, autoWrap.Object)), bw);
        //    }
        //    catch (Exception e)
        //    {
        //        SetError(AutoWrap.GetExceptionString("Ошибка создания оберки событий", "", e), bw);
        //    }
        //}

        public static void GetPropVal(BinaryReader br, BinaryWriter bw)
        {
            if (!GetAW(br, bw, out var autoWrap))
            {
                return;
            }

            string propertyName = br.ReadString();
            var    res          = autoWrap.TryGetMember(propertyName, out var result, out var error);

            if (!res)
            {
                SetError(error, bw);
                return;
            }

            bw.Write(true);
            WorkWithVariant.WriteObject(AutoWrap.WrapObject(result), bw);
        }
Пример #11
0
        public static void CallBinaryOperation(BinaryReader br, BinaryWriter bw)
        {
            if (!GetAW(br, bw, out var autoWrap))
            {
                return;
            }
            var expressionType = (ExpressionType)br.ReadByte();

            if (!OperatorInfo.OperatorMatches.TryGetValue(expressionType, out var methodName))
            {
                SetError($"Нет соответствия {expressionType} имени метода", bw);
                return;
            }

            object param2 = WorkWithVariant.GetObject(br);
            var    args   = new[] { autoWrap.Object, param2 };

            CallStaticMethod(bw, autoWrap.Type, methodName, args);
        }
Пример #12
0
        public static void SetPropVal(BinaryReader br, BinaryWriter bw)
        {
            if (!GetAW(br, bw, out var autoWrap))
            {
                return;
            }

            string propertyName = br.ReadString();
            object result       = WorkWithVariant.GetObject(br);
            var    res          = autoWrap.TrySetMember(propertyName, result, out var error);

            if (!res)
            {
                SetError(error, bw);
            }
            else
            {
                bw.Write(true);
                WorkWithVariant.WriteObject(null, bw);
            }
        }
Пример #13
0
        public static void CallAsyncFunc(BinaryReader br, BinaryWriter bw, IPAddress address)
        {
            if (!CallAsFuncAll(br, bw, out var result, false))
            {
                return;
            }
            try
            {
                var taskId   = new Guid(br.ReadBytes(16));
                var port     = br.ReadInt32();
                var typeInfo = result.GetType().GetTypeInfo();

                var asyncCallBack = new TcpAsyncCallBack(taskId, address, port);
                var callBack      = new Action <bool, object>(asyncCallBack.SendAsyncMessage);
                if (!typeInfo.IsGenericType)
                {
                    AsyncRunner.TaskExecute((Task)result, callBack);
                    return;
                }

                var args   = new[] { result, callBack };
                var method = InformationOnTheTypes.FindMethod(typeof(AsyncRunner), true, "Execute", args);

                if (method == null)
                {
                    SetError("Неверный результат", bw);
                    return;
                }

                method.ExecuteMethod(null, args);
            }
            catch (Exception e)
            {
                SetError(AutoWrap.GetExceptionString("Ошибка вызова делегата", "", e), bw);
                return;
            }

            bw.Write(true);
            WorkWithVariant.WriteObject(null, bw);
        }
Пример #14
0
        public static void SetIndex(BinaryReader br, BinaryWriter bw)
        {
            if (!GetAW(br, bw, out var autoWrap))
            {
                return;
            }

            var indexes = GetArrayParams(br);

            object[] Params     = new object[indexes.Length + 1];
            var      value      = WorkWithVariant.GetObject(br);
            string   methodName = "set_Item";

            if (typeof(Array).IsAssignableFrom(autoWrap.Type))
            {
                methodName = "SetValue";
                indexes.CopyTo(Params, 1);
                Params[0] = value;
            }
            else
            {
                indexes.CopyTo(Params, 0);
                Params[Params.Length - 1] = value;
            }

            var changeParams = new List <int>();
            var res          = autoWrap.TryInvokeMember(methodName, Params, out var result, changeParams, out var error);

            if (!res)
            {
                SetError(error, bw);
            }
            else
            {
                bw.Write(true);
                WorkWithVariant.WriteObject(null, bw);
            }
        }
Пример #15
0
        private static void CallStaticMethod(BinaryWriter bw, Type T, string methodName, object[] args)
        {
            try
            {
                var method = InformationOnTheTypes.FindMethod(T, true, methodName, args);

                if (method == null)
                {
                    SetError($"Нет найден метод  {method} для типа {T}", bw);
                    return;
                }

                var obj = method.ExecuteMethod(null, args);

                bw.Write(true);
                WorkWithVariant.WriteObject(AutoWrap.WrapObject(obj), bw);
                bw.Write((int)0);
            }
            catch (Exception e)
            {
                SetError(AutoWrap.GetExceptionString("Ошибка бинарной операции ", "", e), bw);
            }
        }
Пример #16
0
 private static void SetError(string errorStr, BinaryWriter bw)
 {
     bw.Write(false);
     WorkWithVariant.WriteObject(errorStr, bw);
 }
Пример #17
0
        public static bool CallAsGenericFuncAll(BinaryReader br, BinaryWriter bw, out object result, bool writeResult)
        {
            result = null;;
            if (!GetAW(br, bw, out var autoWrap))
            {
                return(false);
            }

            string methodName = br.ReadString();
            var    arguments  = GetArrayParams(br);
            var    Params     = GetArrayParams(br);

            // Можно параметры передавать ввиде типов и строк
            var genericArguments = new Type[arguments.Length];

            for (int i = 0; i < genericArguments.Length; i++)
            {
                genericArguments[i] = NetObjectToNative.FindTypeForCreateObject(arguments[i]);
            }

            result = null;
            var typesOfParameters = AllMethodsForName.GetTypesParameters(Params);
            var res = InformationOnTheTypes.FindGenericMethodsWithGenericArguments(
                autoWrap.Type,
                autoWrap.IsType,
                methodName,
                genericArguments,
                typesOfParameters);

            if (res == null)
            {
                SetError("Не найден дженерик метод " + methodName, bw);
                return(false);
            }

            try
            {
                var copyParams = new object[Params.Length];
                Params.CopyTo(copyParams, 0);

                var obj = autoWrap.IsType ? null : autoWrap.Object;
                result = res.ExecuteMethod(obj, copyParams);

                var returnType = res.Method.ReturnType;

                if (result != null && returnType.GetTypeInfo().IsInterface)
                {
                    result = new AutoWrap(result, returnType);
                }

                if (writeResult)
                {
                    bw.Write(true);
                    WorkWithVariant.WriteObject(AutoWrap.WrapObject(result), bw);
                    WriteChangeParams(bw, copyParams, AutoWrap.GetChangeParams(Params, copyParams));
                }
            }
            catch (Exception e)
            {
                SetError(AutoWrap.GetExceptionString($"Ошибка вызова Дженерик метода {methodName}", "", e), bw);
                return(false);
            }

            return(true);
        }