Пример #1
0
        static void Main(string[] args)
        {
            IConfiguration config = new ConfigurationBuilder()
                                    .AddJsonFile("appsettings.json", true, true)
                                    .Build();

            var server_name = config["name"];
            var ip          = config["ip"];
            var port        = Convert.ToUInt16(config["port"]);
            var world_count = Convert.ToByte(config["world_count"]);
            var server_addr = ip + ":" + port;

            var redis_ip   = config["redis:ip"];
            var redis_port = config["redis:port"];
            var redis_addr = redis_ip + ":" + redis_port;


            Log.Logger = new LoggerConfiguration()
                         .Enrich.WithThreadId()
                         .MinimumLevel.Debug()
                         .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                         .Enrich.FromLogContext()
                         .WriteTo.Console()
                         .WriteTo.File("logs\\battle.txt", rollingInterval: RollingInterval.Day
                                       , outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} ({ThreadId}) [{Level:u3}] {Message:lj}{NewLine}{Exception}")
                         .CreateLogger();

            try
            {
                Log.Information("Starting Battle Server host");
                if (Server.StaticInit(port, world_count))
                {
                    ServerMonitor.sInstance.Init(world_count, server_addr, redis_addr, server_name);
                    Subscribe.Do();
                    CacheThread.Run();
                    Server.sInstance.Run();
                }
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Host terminated unexpectedly");
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
Пример #2
0
        static bool ReadyServer()
        {
            ServerConfiguration.Instance.WriteLog();

            if (Server.StaticInit(Convert.ToUInt16(ServerConfiguration.Instance.config["port"]),
                                  Convert.ToByte(ServerConfiguration.Instance.config["world_count"]),
                                  Convert.ToInt32(ServerConfiguration.Instance.config["map_id"]),
                                  Convert.ToBoolean(ServerConfiguration.Instance.config["battle_auth"])) == false)
            {
                Log.Error("Server.StaticInit error");
                return(false);
            }

            string dns = "";

            if (ServerConfiguration.Instance.config["ip"] != null)
            {
                dns = ServerConfiguration.Instance.config["ip"];
            }
            else if (ServerConfiguration.Instance.config["meta_hostname"] != null)
            {
                try
                {
                    HttpClient client = new HttpClient();

                    HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Get, ServerConfiguration.Instance.config["meta_hostname"]);


                    HttpResponseMessage response = client.SendAsync(msg).Result;

                    // Get the response
                    var responseString = response.Content.ReadAsStringAsync().Result;
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        Log.Error($"getInfo error :{responseString}");
                        return(false);
                    }
                    dns = responseString;
                }
                catch (HttpRequestException e)
                {
                    Log.Error($"HttpRequestException :{e.Message}");
                    return(false);
                }
                catch (Exception e)
                {
                    Log.Error($"Exception :{e.Message}");
                    return(false);
                }
            }
            else
            {
                Log.Error("server address error");
                return(false);
            }


            var server_addr = dns + ":" + ServerConfiguration.Instance.config["port"];
            var redis_addr  = ServerConfiguration.Instance.config["redis:ip"] + ":" + ServerConfiguration.Instance.config["redis:port"];

            var DefaultFirstReplicationTimeout = ServerConfiguration.Instance.config.GetSection("DefaultFirstReplicationTimeout");

            if (DefaultFirstReplicationTimeout.Value != null)
            {
                ReplicationManagerServer.DefaultFirstReplicationTimeout = int.Parse(DefaultFirstReplicationTimeout.Value);
            }

            if (Convert.ToBoolean(ServerConfiguration.Instance.config["server_process_update"]))
            {
                ServerProcessManager.sInstance.Init(server_addr, redis_addr);
            }

            if (ServerConfiguration.Instance.channel_update == true)
            {
                Cache.sInstance.Init(Convert.ToByte(ServerConfiguration.Instance.config["world_count"]), server_addr,
                                     redis_addr, ServerConfiguration.Instance.config["name"], Convert.ToInt32(ServerConfiguration.Instance.config["map_id"]));
                Subscribe.Do((Lidgren.Network.NetServer)NetworkManagerServer.sInstance.GetServer());
                CacheThread.Run();
            }

            return(true);
        }