Пример #1
0
 /// <summary>
 /// 接收消息前,可用于重写Filter
 /// </summary>
 /// <returns>是否过滤掉</returns>
 protected virtual bool Filter(GameSession session, GameProtoHandlerInfo handler)
 {
     if (!handler.AllowAnonymous && !session.IsAuthorized)
     {
         return(true);
     }
     return(false);
 }
Пример #2
0
        /// <summary>
        /// 消息驱动,通过消息实体名找处理函数
        /// </summary>
        private void ReflectProtocols()
        {
            foreach (var type in Assembly.GetEntryAssembly().ExportedTypes)
            {
                if (type.GetTypeInfo().BaseType == typeof(GameController))
                {
                    _services.AddTransient(typeof(GameController), type);
                }
            }

            var provider    = _services.BuildServiceProvider();
            var controllers = provider.GetServices <GameController>();

            foreach (var controller in controllers)
            {
                Type type = controller.GetType();
                controller.Server = this;
                _controllers.Add(type, controller);

                MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);
                foreach (var methodInfo in methods)
                {
                    if (methodInfo.IsSpecialName || !methodInfo.Name.StartsWith("Call_"))
                    {
                        continue;
                    }
                    if (methodInfo.ReturnType != typeof(int))
                    {
                        _logger.LogWarning($"Method handler [{controller.GetType().Name}.{methodInfo.Name}] has wrong return type!");
                        continue;
                    }
                    ParameterInfo[] parameterInfos = methodInfo.GetParameters();
                    bool            allowanonymous = methodInfo.GetCustomAttribute <AllowAnonymousAttribute>() != null;
                    if (parameterInfos.Length > 2 || parameterInfos.Length < 1)
                    {
                        _logger.LogWarning($"Protocol Handler {controller.GetType().Name }.{methodInfo.Name} parameters count wrong!");
                    }
                    else
                    {
                        List <Type> parameterTypes = new List <Type>();
                        foreach (var p in parameterInfos)
                        {
                            parameterTypes.Add(p.ParameterType);
                        }
                        var requestType = parameterTypes.FirstOrDefault(x => x != typeof(GameSession));
                        if (requestType == null)
                        {
                            _logger.LogWarning($"Protocol Handler {controller.GetType().Name }.{methodInfo.Name} parameters not contains request entity!");
                            continue;
                        }
                        String cmd = requestType.Name;
                        Type   methodGenericType;
                        GameProtoHandlerParameters paramSeq;
                        if (parameterTypes.Count == 1)
                        {
                            methodGenericType = typeof(Func <,>);
                            paramSeq          = GameProtoHandlerParameters.Request;
                        }
                        else
                        {
                            methodGenericType = typeof(Func <, ,>);
                            if (parameterTypes[0] == typeof(GameSession))
                            {
                                paramSeq = GameProtoHandlerParameters.GameSessionAndRequest;
                            }
                            else
                            {
                                paramSeq = GameProtoHandlerParameters.RequestAndGameSession;
                            }
                        }
                        parameterTypes.Add(typeof(int));
                        Type     methodType   = methodGenericType.MakeGenericType(parameterTypes.ToArray());
                        Delegate actionMethod = Delegate.CreateDelegate(methodType, controller, methodInfo);
                        var      handler      = new GameProtoHandlerInfo()
                        {
                            CMD            = cmd,
                            MethodInfo     = methodInfo,
                            ProcessAction  = actionMethod,
                            RequestType    = requestType,
                            Controller     = controller,
                            AllowAnonymous = allowanonymous,
                            ParameterTypes = paramSeq
                        };
                        GameProtoHandlerInfo oldhandler = null;
                        if (!_handlers.TryGetValue(handler.CMD, out oldhandler))
                        {
                            _handlers.Add(handler.CMD, handler);
                            _serializer.Register(requestType);
                        }
                        else
                        {
                            _logger.LogWarning($"Request CMD [{handler.CMD}] in [{handler.Controller.GetType().Name }.{handler.MethodInfo.Name}] already declared in method: [{oldhandler.Controller.GetType().Name }.{oldhandler.MethodInfo.Name}]");
                        }
                    }
                }
            }

            foreach (var controller in controllers)
            {
                controller.RegisterEvents();
            }
        }
Пример #3
0
 public GameRequestTask(GameProtoHandlerInfo handler, GameSession session, Object entity)
     : base(session)
 {
     Handler = handler;
     Entity  = entity;
 }