Exemplo n.º 1
0
        public Action <DomainObject, DomainPropertyChangedEventArgs> GetAction()
        {
            MethodInfo methodInfo = GetMethod();

            return((domainObject, value) =>
            {
                if (methodInfo.IsStatic)
                {
                    //静态的版本
                    using (var temp = ArgsPool.Borrow2())
                    {
                        var args = temp.Item;
                        args[0] = domainObject;
                        args[1] = value;
                        methodInfo.Invoke(null, args);
                    }
                }
                else
                {
                    //对象的版本,对象肯定就是owner
                    using (var temp = ArgsPool.Borrow1())
                    {
                        var args = temp.Item;
                        args[0] = value;
                        methodInfo.Invoke(domainObject, args);
                    }
                }
            });
        }
Exemplo n.º 2
0
        private void CompleteReceiveFrom(SocketAsyncEventArgs args)
        {
            PacketReadToken token = (PacketReadToken)args.UserToken;

            byte[] receiveBuffer = args.Buffer;

            switch (args.SocketError)
            {
            case SocketError.Success:
                receiveBuffer.CopyTo(token.UserBuffer);
                token.CompletionSource.SetResult(args.BytesTransferred);
                break;

            case SocketError.OperationAborted:
                token.CompletionSource.SetCanceled();
                break;

            default:
                token.CompletionSource.SetException(new SocketException((int)args.SocketError));
                break;
            }

            BufferPool.Return(receiveBuffer, true);
            ArgsPool.Return(args);
        }
        public Func <DomainObject, object> GetAction()
        {
            if (_getAction != null)
            {
                return(_getAction);                    //自定义了方法
            }
            MethodInfo methodInfo = GetMethod();

            return((domainObject) =>
            {
                object value = null;
                if (methodInfo.IsStatic)
                {
                    using (var temp = ArgsPool.Borrow1())
                    {
                        var args = temp.Item;
                        args[0] = domainObject;
                        value = methodInfo.Invoke(null, args);
                    }
                }
                else
                {
                    value = methodInfo.Invoke(domainObject, Array.Empty <object>());
                }
                return value;
            });
        }
Exemplo n.º 4
0
        private DomainObject ConstructDynamicObject(Type objectType, TypeDefine define)
        {
            var constructor = ConstructorRepositoryAttribute.GetDynamicConstructor(objectType);

            using (var temp = ArgsPool.Borrow2())
            {
                var args = temp.Item;
                args[0] = define;
                args[1] = false; //不为空 empty参数
                return((DomainObject)constructor.CreateInstance(args));
            }
        }
Exemplo n.º 5
0
 private IList CreateList(Type listType)
 {
     if (_isDomainCollection(listType))
     {
         var constructor = _getDomainCollectionConstructor(listType);
         using (var temp = ArgsPool.Borrow2())
         {
             var args = temp.Item;
             args[0] = this.MemberDomainProperty;
             return(constructor.CreateInstance(args) as IList);
         }
     }
     return(listType.CreateInstance() as IList);
 }
Exemplo n.º 6
0
        void Accept()
        {
            var args = ArgsPool.Get();

            args.Completed += OnAccept;

            Task.Run(() =>
            {
                if (!_serverSocket.AcceptAsync(args))
                {
                    OnAccept(this, args);
                }
            });
        }
Exemplo n.º 7
0
        void Accept()
        {
            var args = ArgsPool.Get();

            args.Completed += OnAccept;

            Task.Factory.StartNew(() =>
            {
                if (!_serverSocket.AcceptAsync(args))
                {
                    OnAccept(this, args);
                }
            }, TaskCreationOptions.LongRunning);
        }
 private void Dispose(bool disposing)
 {
     if (!_disposed)
     {
         if (disposing)
         {
             foreach (SocketAsyncEventArgs args in ArgsPool)
             {
                 args.Dispose();
             }
             ArgsPool.Clear();
         }
         _disposed = true;
     }
 }
Exemplo n.º 9
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="parent"></param>
 /// <param name="listType"></param>
 /// <param name="property">集合在对象中的属性定义</param>
 /// <returns></returns>
 private IList CreateList(DomainObject parent, Type listType, DomainProperty property)
 {
     if (_isDomainCollection(listType))
     {
         var constructor = _getDomainCollectionConstructor(listType);
         using (var temp = ArgsPool.Borrow1())
         {
             var args = temp.Item;
             args[0] = property;
             var collection = constructor.CreateInstance(args) as IDomainCollection;
             collection.Parent = parent;
             return(collection as IList);
         }
     }
     return(listType.CreateInstance() as IList);
 }
        /// <summary>
        /// 使用自定义方法加载参数数据
        /// </summary>
        /// <param name="objectType">运行时的实际类型,有可能是派生类的类型</param>
        /// <param name="data"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool TryLoadData(Type objectType, DynamicData data, out object value)
        {
            value = null;
            var method = _getLoadData(objectType);

            if (method == null)
            {
                return(false);
            }
            using (var temp = ArgsPool.Borrow1())
            {
                var args = temp.Item;
                args[0] = data;
                value   = method.Invoke(null, args);
            }
            return(true);
        }
Exemplo n.º 11
0
        /// <summary>
        /// 以<paramref name="data"/>为数据格式创建前定义的类型的实例
        /// </summary>
        /// <param name="data"></param>
        internal DynamicObject CreateInstance(DTObject data)
        {
            if (data.IsEmpty())
            {
                return((DynamicObject)this.GetEmptyInstance());
            }
            DynamicObject obj = null;

            using (var temp = ArgsPool.Borrow2())
            {
                var args = temp.Item;
                args[0] = this;
                args[1] = false;
                obj     = (DynamicObject)this.Constructor.CreateInstance(args);
            }
            //加载数据
            obj.Load(data);
            return(obj);
        }
Exemplo n.º 12
0
        private object ReadPrimitive(PropertyRepositoryAttribute tip, DynamicData data)
        {
            var value = tip.Lazy ? ReadValueByLazy(tip, data) : ReadValueFromData(tip, data);

            if (!tip.IsEmptyable)
            {
                return(value);
            }
            if (value == null)
            {
                //Emptyable类型的数据有可能存的是null值
                return(tip.CreateDefaultEmptyable.Invoke(null, null));
            }
            using (var temp = ArgsPool.Borrow1())
            {
                var args = temp.Item;
                args[0] = value;
                return(tip.EmptyableConstructor.CreateInstance(args));
            }
        }
Exemplo n.º 13
0
        /// <inheritdoc />
        public override ValueTask <int> ReadAsync(EndPoint remoteEndPoint, Memory <byte> readBuffer, SocketFlags flags = SocketFlags.None)
        {
            int totalBytes = readBuffer.Length;

            if (totalBytes > datagramSize)
            {
                throw new ArgumentException(
                          string.Format(Resources.Culture, Resources.RawDatagramNetworkReaderRentedBufferSizeError, totalBytes, datagramSize),
                          nameof(readBuffer)
                          );
            }

            TaskCompletionSource <int> tcs  = new TaskCompletionSource <int>();
            SocketAsyncEventArgs       args = ArgsPool.Rent();

            byte[] transmissionBuffer = BufferPool.Rent(datagramSize);

            args.SetBuffer(transmissionBuffer, 0, datagramSize);

            args.RemoteEndPoint = remoteEndPoint;
            args.SocketFlags    = flags;

            PacketReadToken token = new PacketReadToken(tcs, in readBuffer);

            args.UserToken = token;

            if (Connection.ReceiveFromAsync(args))
            {
                return(new ValueTask <int>(tcs.Task));
            }

            // inlining CompleteReceiveFrom(SocketAsyncEventArgs) for performance
            int result = args.BytesTransferred;

            transmissionBuffer.CopyTo(readBuffer);

            CleanupTransmissionBufferAndState(args);  // transmissionBuffer was assigned to args.Buffer earlier, so this call is safe

            return(new ValueTask <int>(result));
        }
Exemplo n.º 14
0
        void OnAccept(object sender, SocketAsyncEventArgs args)
        {
            var error  = args.SocketError;
            var socket = args.AcceptSocket;
            var reject = _clients.Count >= MaxClients;

            args.Completed -= OnAccept;
            ArgsPool.TryPut(args);

            if (error != SocketError.Success || reject)
            {
                var isAbort = error == SocketError.OperationAborted;

                if (!isAbort)
                {
                    _log.Info("Rejected connection to {0} from {1}: {2}",
                              Info.Name, socket.RemoteEndPoint,
                              reject ? "Maximum amount of clients reached" : error.ToErrorString());
                }

                socket.SafeClose();

                // Are we shutting down?
                if (isAbort)
                {
                    _event.Set();
                    return;
                }
            }
            else
            {
                var client = new GameClient(this, socket);

                _log.Info("Accepted connection to {0} from {1}",
                          Info.Name, socket.RemoteEndPoint);
            }

            Accept();
        }
Exemplo n.º 15
0
        private object ParseValueList(PropertyRepositoryAttribute tip, object value)
        {
            var         propertyType = tip.PropertyType;
            var         elementType  = propertyType.ResolveElementType();
            IEnumerable temp         = null;

            if (tip.IsEmptyable)
            {
                temp = value.ToString().Split(',').Select((item) =>
                {
                    var itemValue = DataUtil.ToValue(item.FromBase64(), tip.EmptyableValueType);

                    object emptyableValue = null;
                    using (var argsTemp = ArgsPool.Borrow1())
                    {
                        var args       = argsTemp.Item;
                        args[0]        = itemValue;
                        emptyableValue = tip.EmptyableConstructor.CreateInstance(args);
                    }
                    return(emptyableValue);
                });
            }
            else
            {
                temp = value.ToString().Split(',').Select((item) =>
                {
                    return(DataUtil.ToValue(item.FromBase64(), elementType));
                });
            }

            var list = CreateList(propertyType);

            foreach (var t in temp)
            {
                list.Add(t);
            }
            return(list);
        }
        public Action <DomainObject, DomainPropertyChangedEventArgs> GetAction()
        {
            MethodInfo methodInfo = GetMethod();

            return((domainObject, value) =>
            {
                if (this.IgnoreWithRepository)
                {
                    var current = DataContext.Current;
                    if (current != null && current.InBuildObject)
                    {
                        return; //指示了在仓储构建时不触发行为,所以返回
                    }
                }

                if (methodInfo.IsStatic)
                {
                    //静态的版本
                    using (var temp = ArgsPool.Borrow2())
                    {
                        var args = temp.Item;
                        args[0] = domainObject;
                        args[1] = value;
                        methodInfo.Invoke(null, args);
                    }
                }
                else
                {
                    //对象的版本,对象肯定就是owner
                    using (var temp = ArgsPool.Borrow1())
                    {
                        var args = temp.Item;
                        args[0] = value;
                        methodInfo.Invoke(domainObject, args);
                    }
                }
            });
        }
Exemplo n.º 17
0
        public Func <DomainObject, object> GetAction()
        {
            MethodInfo methodInfo = GetMethod();

            return((domainObject) =>
            {
                if (this.IgnoreWithRepository)
                {
                    var current = DataContext.Current;
                    if (current != null && current.InBuildObject)
                    {
                        return domainObject; //指示了在仓储构建时不触发行为,所以返回
                    }
                }

                if (_getAction != null)
                {
                    return _getAction(domainObject);
                }

                object value = null;
                if (methodInfo.IsStatic)
                {
                    using (var temp = ArgsPool.Borrow1())
                    {
                        var args = temp.Item;
                        args[0] = domainObject;
                        value = methodInfo.Invoke(null, args);
                    }
                }
                else
                {
                    value = methodInfo.Invoke(domainObject, Array.Empty <object>());
                }
                return value;
            });
        }
Exemplo n.º 18
0
        private static object ExecuteAction(UIElement e, string actionName, ScriptView arg)
        {
            IScriptView result = null;

            if (e.CallRegisteredScriptAction(actionName, arg, ref result))
            {
                return(result);                                                           //优先执行注册的行为
            }
            var action = e.ObjectType.ResolveMethod(actionName, ActionParameters);

            if (action != null)
            {
                using (var temp = ArgsPool.Borrow1())
                {
                    var args = temp.Item;
                    args[0] = arg;
                    return(action.Invoke(e, args));
                }
            }

            return(null);

            //IScriptView result = null;
            //if (e.CallRegisteredScriptAction(actionName, arg, ref result)) return result; //优先执行注册的行为
            //var action = DependencyAction.GetAction(e.ObjectType, actionName);
            //if (action != null)
            //{
            //    if (!action.AllowClientAccess) throw new XamlException("组件方法" + actionName + "未公开");
            //    return e.CallAction(action, arg);
            //}

            ////如果未能成功在e上执行行文,那么找父亲对象的方法
            //var parent = e.Parent as UIElement;
            //if (parent != null) return parent.CallScriptAction(null, actionName, arg);

            //throw new XamlException("未能执行" + actionName);
        }
 internal void Push(SocketAsyncEventArgs item)
 {
     lock (_syncRoot) ArgsPool.Push(item);
 }
 internal SocketAsyncEventArgs Pop()
 {
     lock (_syncRoot)
         return(ArgsPool.Count > 0 ? ArgsPool.Pop() : (new SocketAsyncEventArgs()));
 }