Пример #1
0
        private static void Start(object s)
        {
            var key = (RSAAsymmetricKey)s;

            lock (Sync) {
                if (IsRunning)
                {
                    return;
                }

                channels      = new LobbyChannelProvider();
                authorization = new GuestUserProvider {
                    FirstUserIsAdmin = true
                };
                permissions = new GuestPermissionProvider();
                settings    = new ServerSettings();

                server = new GablarskiServer(settings, authorization, permissions, channels);
                server.AddConnectionProvider(new UdpConnectionProvider(GablarskiProtocol.Port, GablarskiProtocol.Instance, key), ExecutionMode.GlobalOrder);

                server.Start();
            }
        }
Пример #2
0
        public static void Main()
        {
            Trace.Listeners.Add(new ConsoleTraceListener());

            log4net.Config.XmlConfigurator.Configure();

            var serverConfig = (BarrelConfiguration)ConfigurationManager.GetSection("barrel");

            if (serverConfig == null)
            {
                LogManager.GetLogger("Barrel").Fatal("Section 'barrel' not found in configuration.");
                return;
            }

            var log = LogManager.GetLogger(serverConfig.Name.Remove(" "));

            log.Info("Checking configuration");
            if (!serverConfig.CheckConfiguration(log))
            {
                log.Fatal("Errors found in configuration, shutting down.");
                return;
            }

            ServerProviders providers = serverConfig.GetProviders(log);

            if (providers == null)
            {
                log.Fatal("Errors loading server configuration, shutting down.");
                return;
            }

            log.Info("Setting up");

            GablarskiServer server = new GablarskiServer(new ServerSettings
            {
                Name           = serverConfig.Name,
                Description    = serverConfig.Description,
                ServerPassword = serverConfig.Password,
                ServerLogo     = serverConfig.LogoURL
            }, providers.Users, providers.Permissions, providers.Channels);

            if (serverConfig.Network)
            {
                var keyFile          = new FileInfo("server.key");
                RSAAsymmetricKey key = null;
                if (keyFile.Exists)
                {
                    try {
                        using (var stream = File.OpenRead(keyFile.FullName)) {
                            var           reader     = new StreamValueReader(stream);
                            RSAParameters parameters = RSAParametersSerializer.Deserialize(reader);
                            key = new RSAAsymmetricKey(parameters);
                        }
                    } catch (Exception ex) {
                        Trace.TraceWarning("Failed to read key: {0}", ex);
                        keyFile.Delete();
                    }
                }

                if (!keyFile.Exists)
                {
                    var           rsa        = new RSACrypto();
                    RSAParameters parameters = rsa.ExportKey(true);
                    key = new RSAAsymmetricKey(parameters);

                    using (var stream = File.OpenWrite(keyFile.FullName)) {
                        var writer = new StreamValueWriter(stream);
                        RSAParametersSerializer.Serialize(writer, parameters);
                    }
                }

                server.AddConnectionProvider(new UdpConnectionProvider(serverConfig.Port, GablarskiProtocol.Instance, key), ExecutionMode.GlobalOrder);
            }

            foreach (IConnectionProvider provider in providers.ConnectionProviders)
            {
                server.AddConnectionProvider(provider, ExecutionMode.GlobalOrder);
            }

            server.Start();

            log.Info("Server started");

            while (true)
            {
                Console.ReadKey();
            }
        }