Exemple #1
0
        private static void Main(string[] args)
        {
            string configPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "config.json");

            if (!File.Exists(configPath))
            {
                Console.WriteLine("================================");
                Console.WriteLine("There is no config. Please select the transport you would like:");
                Console.WriteLine("[R]uffles");
                Console.WriteLine("[U]net");

                ConsoleKey key = ConsoleKey.Escape;

                do
                {
                    key = Console.ReadKey(true).Key;
                }while (key != ConsoleKey.R && key != ConsoleKey.U);

                if (key == ConsoleKey.U)
                {
                    Transport = new UnetTransport();

                    Console.WriteLine("================================");
                    Console.WriteLine("Please select a template for the UNET config file:");
                    Console.WriteLine("[M]LAPI - Copies the same UNET settings as the MLAPI uses by default");
                    Console.WriteLine("[H]LAPI - Copies the same UNET settings as the HLAPI uses by default");
                    Console.WriteLine("[E]mpty - Default UNET settings");

                    key = ConsoleKey.Escape;

                    do
                    {
                        key = Console.ReadKey(true).Key;
                    }while (key != ConsoleKey.M && key != ConsoleKey.H && key != ConsoleKey.E);

                    if (key == ConsoleKey.M)
                    {
                        UnetTransport.UnetConfig config = (UnetTransport.UnetConfig)Transport.GetConfig();

                        config.ConnectionConfig.AddChannel(QosType.ReliableFragmentedSequenced);
                        config.ConnectionConfig.AddChannel(QosType.Reliable);
                        config.ConnectionConfig.AddChannel(QosType.UnreliableSequenced);
                        config.ConnectionConfig.AddChannel(QosType.ReliableSequenced);
                        config.ConnectionConfig.AddChannel(QosType.ReliableSequenced);
                        config.ConnectionConfig.AddChannel(QosType.UnreliableSequenced);
                        config.ConnectionConfig.AddChannel(QosType.Unreliable);

                        Config = new RelayConfig()
                        {
                            Transport                   = TransportType.UNET,
                            AllowTemporaryAlloc         = true,
                            MaxTemporaryAlloc           = 1024 * 16,
                            BandwidthGracePrediodLength = 60 * 2,
                            BandwidthLimit              = -1,
                            EnableRuntimeMetaLogging    = true,
                            GracePeriodBandwidthLimit   = -1,
                            TransportConfig             = config,
                            BufferSize                  = 1024 * 8,
                            ListenPort                  = 8888
                        };
                    }
                    else if (key == ConsoleKey.H)
                    {
                        UnetTransport.UnetConfig config = (UnetTransport.UnetConfig)Transport.GetConfig();

                        config.ConnectionConfig.AddChannel(QosType.ReliableSequenced);
                        config.ConnectionConfig.AddChannel(QosType.Unreliable);

                        Config = new RelayConfig()
                        {
                            Transport                   = TransportType.UNET,
                            BufferSize                  = 1024 * 8,
                            AllowTemporaryAlloc         = true,
                            BandwidthGracePrediodLength = 60 * 2,
                            BandwidthLimit              = -1,
                            EnableRuntimeMetaLogging    = true,
                            GracePeriodBandwidthLimit   = -1,
                            ListenPort                  = 8888,
                            MaxTemporaryAlloc           = 1024 * 16,
                            TransportConfig             = config
                        };
                    }
                    else if (key == ConsoleKey.E)
                    {
                        UnetTransport.UnetConfig config = (UnetTransport.UnetConfig)Transport.GetConfig();

                        Config = new RelayConfig()
                        {
                            Transport                   = TransportType.UNET,
                            BufferSize                  = 1024 * 8,
                            AllowTemporaryAlloc         = true,
                            BandwidthGracePrediodLength = 60 * 2,
                            BandwidthLimit              = -1,
                            EnableRuntimeMetaLogging    = true,
                            GracePeriodBandwidthLimit   = -1,
                            ListenPort                  = 8888,
                            MaxTemporaryAlloc           = 1024 * 16,
                            TransportConfig             = config
                        };
                    }
                }
                else if (key == ConsoleKey.R)
                {
                    Transport = new RufflesTransport();

                    RufflesTransport.RufflesConfig config = (RufflesTransport.RufflesConfig)Transport.GetConfig();

                    Config = new RelayConfig()
                    {
                        Transport                   = TransportType.Ruffles,
                        AllowTemporaryAlloc         = true,
                        BandwidthGracePrediodLength = 60 * 2,
                        BandwidthLimit              = -1,
                        BufferSize                  = 1024 * 8,
                        EnableRuntimeMetaLogging    = true,
                        GracePeriodBandwidthLimit   = -1,
                        ListenPort                  = 8888,
                        MaxTemporaryAlloc           = 1024 * 16,
                        TransportConfig             = config
                    };
                }

                if (Config != null)
                {
                    object config = Transport.BeforeSerializeConfig(Config);

                    string serializedJson = JsonConvert.SerializeObject(config, Formatting.Indented);

                    File.WriteAllText(configPath, Transport.ProcessSerializedJson(serializedJson));
                }
                else
                {
                    // TODO: Something went wrong. No config??
                }
            }
            else
            {
                try
                {
                    Config = JsonConvert.DeserializeObject <RelayConfig>(File.ReadAllText(configPath));

                    switch (Config.Transport)
                    {
                    case TransportType.Ruffles:
                        Transport = new RufflesTransport();
                        break;

                    case TransportType.UNET:
                        Transport = new UnetTransport();
                        break;
                    }

                    // Post deserialization job
                    Config = (RelayConfig)Transport.AfterDeserializedConfig(Config);
                }
                catch (Exception e)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("[ERROR] Error parsing config file: " + e);
                    Console.Read();
                    Environment.Exit(1);
                    return;
                }
            }

            Program.MESSAGE_BUFFER = new byte[Config.BufferSize];

            try
            {
                Console.WriteLine("[INFO] Starting server...");
                Transport.Start(Config.TransportConfig);
                Console.WriteLine("[INFO] Server started!");
            }
            catch (DllNotFoundException e)
            {
                Console.WriteLine("[FATAL] Could not locate one or more shared libraries! Message: \n" + e);
            }
            catch (Exception e)
            {
                Console.WriteLine("[FATAL] An unexpected error occurred! Message: \n" + e);
            }

            Stopwatch watch = new Stopwatch();

            while (true)
            {
                try
                {
                    watch.Restart();

                    RunLoop();

                    int timeBetweenTicks = (int)((1f / Config.TicksPerSecond) * 1000f);
                    int timeToSleep      = timeBetweenTicks - (int)watch.ElapsedMilliseconds;

                    if (timeToSleep > 0)
                    {
                        Thread.Sleep(timeToSleep);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("[ERROR] Exception during loop: " + e);
                }
            }
        }