Esempio n. 1
0
        public byte[] OnReceive(ActorContext context)
        {
            if (context.Action == 0)
            {
                Logger.Debug("ping");

                var onlinecount = 0;
                if (context.ServerItmes.ContainsKey("OnlineCount"))
                {
                    onlinecount = Convert.ToInt32(context.ServerItmes["OnlineCount"]);
                }

                Ping(context.ServerName, onlinecount);
                return(null);
            }
            else if (context.Action == 2)//断线
            {
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug($"Session[{context.SessionId}] Logout ~");
                }

                var session = DistrictContainer.OnlineManager.GetSession(context.SessionId);
                if (session != null)
                {
                    DistrictContainer.OnlineManager.Logout(session.Id);
                    OnSessionClosed(session);
                }

                return(null);
            }

            var token = GetToken(context.Request.Token);

            context.Token = token;

            if (Logger.IsDebugEnabled)
            {
                Logger.Debug($"C2S Headers #{context.Token.Rid}# #{context.RemoteIp}# {context.Request.Headers}");
                Logger.Debug($"C2S Query #{context.Token.Rid}# #{context.RemoteIp}# {context.Request.QueryString}");
            }

            //授权检查
            if (!VerifyToken(DistrictContainer.GetDistrictConfig(token.Did), token))
            {
                throw new PirateXException("AuthError", "授权失败")
                      {
                          Code = StatusCode.Unauthorized
                      }
            }
            ;

            var format = context.Request.Headers["format"];

            if (!string.IsNullOrEmpty(format))
            {
                context.ResponseCovnert = format;
            }
            else
            {
                context.ResponseCovnert = DefaultResponseCovnert;
            }

            var lang = context.Request.Headers["lang"];

            if (!string.IsNullOrEmpty(lang))
            {
                Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);
            }

            //request timeout
            //if ((DateTime.UtcNow.GetTimestamp() - context.Request.Timestamp) > 1000 * 60 * 2)
            //{
            //    Logger.Warn($"C2S Timeout t #{context.SessionId}# #{context.RemoteIp}# {context.Request.Headers} ");
            //    return;
            //}

            if (context.Request.O <= context.LastNo)
            {
                Logger.Warn($"C2S Timeout o #{context.SessionId}# #{context.RemoteIp}# {context.Request.Headers} ");
                return(null);
            }

            //执行动作
            var actionname = context.Request.C;

            using (var action = GetActionInstanceByName(actionname))
            {
                if (action != null)
                {
                    try
                    {
                        if (Equals(actionname, "NewSeed"))
                        {
                            OnSessionConnected(ToSession(context, context.Token));
                        }
                        else
                        {
                            //session = OnlineManager.GetSession(token.Rid);
                            var container = DistrictContainer.GetDistrictContainer(token.Did);
                            action.Resolver = container ?? throw new PirateXException("ContainerNull", "容器未定义")
                                                    {
                                                        Code = StatusCode.ContainerNull
                                                    };                                                                                               //.BeginLifetimeScope();
                        }

                        action.ServerReslover = DistrictContainer.ServerIoc;
                        action.Context        = context;
                        action.Logger         = Logger;
                        action.MessageSender  = this;

                        action.Execute();

                        if (Equals(actionname, "NewSeed"))
                        {
                            //session 保存
                            DistrictContainer.OnlineManager.Login(ToSession(context, context.Token));
                        }

                        var result = action.ResponseData;
                        if (result == null)
                        {
                            return(this.SendMessage(context, string.Empty));
                        }
                        return(result);
                    }
                    catch (Exception exception)
                    {
                        return(HandleException(context, exception));
                    }
                }
            }

            var headers = new NameValueCollection()
            {
                { "c", context.Request.C },
                { "i", MessageType.Boradcast },
                { "o", Convert.ToString(context.Request.O) },
                { "code", Convert.ToString((int)StatusCode.NotFound) },
                { "errorCode", "NotFound" },
                { "errorMsg", $"Action {actionname} not found!" }
            };

            //返回类型

            return(SendMessage <string>(context, headers, null));
        }