public string Register(string id, Closure func)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException(nameof(id));
            }

            if (func == null)
            {
                throw new ArgumentNullException(nameof(func));
            }

            object LateBoundMethod(params object[] args)
            {
                var list = new List <object> {
                    Instance
                };
                var objs = LuaParser.Parse(args, Instance.OwnerScript);

                list.AddRange(objs);

                return(func.Call(list.ToArray()).ToObject());
            }

            RpcRegister.RegisterMethod(id, LateBoundMethod);
            return(id);
        }
Esempio n. 2
0
        public static Method Register(
            string methodId,
            Expression<Action> implementExpression){

            if (implementExpression == null) {
                throw new ArgumentNullException(nameof(implementExpression));
            }

            var method = MethodUtil.GetMethodInfo(implementExpression);

            if (typeof(Delegate).IsAssignableFrom(method.DeclaringType)) {
                throw new ArgumentException($"{nameof(implementExpression)}不能为Delegate!");
            }

            Func<object[], object> func = null;

            if (method.IsStatic) {
                if (method.ReturnType == typeof(void)) {
                    func = RpcRegister.RegisterStaticVoidMethod(methodId, method);
                } else {
                    func = RpcRegister.RegisterStaticValueMethod(methodId, method);
                }
            } else {
                if (method.ReturnType == typeof(void)) {
                    func = RpcRegister.RegisterVoidMethod(methodId, GetObject(implementExpression), method);
                } else {
                    func = RpcRegister.RegisterValueMethod(methodId, GetObject(implementExpression), method);
                }
            }

            return new Method { Id = methodId, Function = func }; 
        }
Esempio n. 3
0
        public Server(string id, RawMessageManager rawMessageManager, ModuleManager moduleManager,
                      CacheManager cacheManager, ControllerComponentManager controllerComponentManager)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException(nameof(id));
            }

            ID = id;
            LuaHelper.RegisterType <Server>();

            _moduleManager = moduleManager ?? throw new ArgumentNullException(nameof(moduleManager));
            _cacheManager  = cacheManager ?? throw new ArgumentNullException(nameof(cacheManager));
            _controllerComponentManager = controllerComponentManager ?? throw new ArgumentNullException(nameof(controllerComponentManager));
            _rawMessageManager          = rawMessageManager ?? throw new ArgumentNullException(nameof(rawMessageManager));

            _events = new Dictionary <string, Action <IUser, object> >();

            _serverSocket               = new ServerSocket(id);
            _serverSocket.Accepted     += AcceptConnected;
            _serverSocket.Connected    += PeerConnected;
            _serverSocket.Disconnected += PeerDisconnected;

            RpcRegister.RegisterMethod(this);

            RpcMethodIds   = new List <string>();
            RpcPacketTypes = new List <string>();
        }
Esempio n. 4
0
        public static Method Register<TDelegate>(
            string methodId,
            Expression<TDelegate> implementExpression) where TDelegate : Delegate {

            if (implementExpression == null) {
                throw new ArgumentNullException(nameof(implementExpression));
            }

            var method = MethodUtil.GetMethodInfo(implementExpression);

            if (!typeof(Delegate).IsAssignableFrom(method.DeclaringType)) {
                throw new ArgumentException($"{nameof(implementExpression)}不是delegate类型!");
            }

            var func = RpcRegister.RegisterDelegate(methodId, implementExpression);
            return new Method { Id = methodId, Function = func };
        }
        public string Register(string id, dynamic func, PythonHelper pythonHelper)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException(nameof(id));
            }

            if (func == null)
            {
                throw new ArgumentNullException(nameof(func));
            }

            object LateBoundMethod(params object[] args)
            {
                var objs = PythonParser.Parse(args);

                return(pythonHelper.Call(func, objs));
            }

            RpcRegister.RegisterMethod(id, LateBoundMethod);
            return(id);
        }