Exemple #1
0
        private static void Main(string[] args)
        {
            if (!Parser.Default.ParseArguments(args, sOptions))
            {
                Console.WriteLine(sOptions.GetUsage());
                Logger.Error(sOptions.GetUsage());
                return;
            }

            Utility.FunctionCallLogger = new ScorpionLogger(LogManager.GetLogger("FunctionCallLogger"));
            Utility.Logger             = new ScorpionLogger(LogManager.GetLogger("Network"));

            AppDomain.CurrentDomain.UnhandledException +=
                (sender, eventArgs) =>
            {
                Logger.Fatal(eventArgs.ExceptionObject as Exception, "CurrentDomain UnhandledException. ");
            };

            TaskScheduler.UnobservedTaskException +=
                (sender, eventArgs) =>
            {
                Logger.Fatal(eventArgs.Exception, "TaskScheduler UnobservedTaskException. ");
            };

            var machine = sOptions.MachineName;
            var id      = -1;

            Int32.TryParse(sOptions.ServerId, out id);

            Logger.Info("server id [{0}] is starting, machine [{1}]", id, machine);

            Config.SetDefaultConfig(Config.ApplyJsonFromPath("../Config/default.conf"));
            var ServerConfig = Config.ApplyJsonFromPath(Config.Default.ConfigFile.ToString());
            var dbConfig     = ServerConfig.DBConfig;

            var serverInfos = new List <ServerInfo>();

            var started = false;

            foreach (var config in Config.Default.Services)
            {
                var info = new ServiceInfo(config, ServerConfig);

                dynamic[] ServersConfig = null;

                if (info.Name == "Broker")
                {
                    var list = new List <dynamic>();
                    foreach (var name in GlobalVariable.ServerNames)
                    {
                        if (ServerConfig.BrokerServer.ContainsKey(name))
                        {
                            list.Add(ServerConfig.BrokerServer[name]);
                        }
                    }
                    ServersConfig = list.ToArray();
                }
                else if (info.Name == "Login")
                {
                    ServersConfig = ServerConfig.LoginServer;
                }
                else if (info.Name == "Activity")
                {
                    ServersConfig = ServerConfig.ActivityServer;
                }
                else if (info.Name == "Logic")
                {
                    ServersConfig = ServerConfig.LogicServer;
                }
                else if (info.Name == "Rank")
                {
                    ServersConfig = ServerConfig.RankServer;
                }
                else if (info.Name == "Scene")
                {
                    ServersConfig = ServerConfig.SceneServer;
                }
                else if (info.Name == "Chat")
                {
                    ServersConfig = ServerConfig.ChatServer;
                }
                else if (info.Name == "Team")
                {
                    ServersConfig = ServerConfig.TeamServer;
                }
                else if (info.Name == "Gate")
                {
                    ServersConfig = ServerConfig.GateServer;
                }
                else if (info.Name == "Memcached")
                {
                    ServersConfig = ServerConfig.Memcache;
                }
                else if (info.Name == "WatchDog")
                {
                    ServersConfig = ServerConfig.WatchDogServer;
                }
                else if (info.Name == "GameMaster")
                {
                    ServersConfig = ServerConfig.GameMasterServer;
                }

                foreach (var server in ServersConfig)
                {
                    if (id > 0 && server.Id != id)
                    {
                        continue;
                    }

                    if (machine != server.MachineName)
                    {
                        continue;
                    }

                    Logger.Info("server id [{0}] is starting as {1} server at {2}:{3}", server.Id, info.Name, server.Ip,
                                server.Port);
                    var serverInfo = new ServerInfo(info, server, dbConfig, server.Id);
                    serverInfos.Add(serverInfo);
                    serverInfo.Start();
                    string log = string.Format("{0,-20}started.  {1,-10} [{2}:{3}] {4,-20}",
                                               "[" + serverInfo.Name + "]",
                                               "[" + serverInfo.Id + "]",
                                               server.Ip, server.Port,
                                               "[" + machine + "]");
                    Console.WriteLine(log);

                    started = true;
                }
            }

            if (!started)
            {
                Logger.Error("Can not find a server " + sOptions.ServerId + " at " + sOptions.MachineName +
                             " in configuration.");
                return;
            }

            try
            {
                if (!string.IsNullOrEmpty(ServerConfig.InfluxDb.Uri.ToString()))
                {
                    client = new InfluxDBClient(ServerConfig.InfluxDb.Uri, ServerConfig.InfluxDb.UserName,
                                                ServerConfig.InfluxDb.Password);
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
            }

            while (!File.Exists("exit"))
            {
                Thread.Sleep(1000);

                if (!Directory.Exists("../status"))
                {
                    Directory.CreateDirectory("../status");
                }

                if (File.Exists("stack"))
                {
                    var sb = new StringBuilder();

                    foreach (var info in serverInfos)
                    {
                        sb.AppendLine(info.GetStackTrace());
                    }

                    File.AppendAllText("../status/stack.txt", sb.ToString());
                    File.Delete("stack");
                }

                foreach (var info in serverInfos)
                {
                    try
                    {
                        var status = info.Status;
                        File.WriteAllText("../status/" + info.Name + ".txt", status.Dump());

                        if (client != null)
                        {
                            var p = new InfluxDatapoint <string>();
                            p.MeasurementName = info.Name.Trim();
                            p.UtcTimestamp    = DateTime.Now;
                            p.Precision       = TimePrecision.Seconds;
                            foreach (var s in status)
                            {
                                p.Fields.Add(s.Key, s.Value);
                            }

                            Task.WaitAll(client.PostPointAsync("server", p));
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex, "Send state error.");
                    }
                }
#if !DEBUG
                foreach (var info in serverInfos)
                {
                    try
                    {
                        if (!info.Alive)
                        {
                            var sb = new StringBuilder();
                            sb.AppendLine(info.GetStackTrace());
                            File.AppendAllText("../status/stack.txt", sb.ToString());

                            info.Rescue();
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex, "Send state error.");
                    }
                }
#endif
            }

            Console.WriteLine("------------------------------------");
            Console.WriteLine("Please hold on,program is quiting...");
            Console.WriteLine("------------------------------------");
            foreach (var info in serverInfos)
            {
                info.Stop();
            }
        }