Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ManagementProtocol"/> class.
        /// </summary>
        /// <param name="logger">A reference to the logger to use.</param>
        /// <param name="handlerSelector">A reference to the handler selector to use in this protocol.</param>
        public ManagementProtocol(ILogger logger, IHandlerSelector handlerSelector)
            : base(handlerSelector)
        {
            logger.ThrowIfNull(nameof(logger));

            this.Logger = logger.ForContext <GameProtocol>();
        }
Exemplo n.º 2
0
 public void AddHandlerSelector(IHandlerSelector selector)
 {
     lock (_selectorLock)
     {
         _handlerSelectors.Add(selector);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TcpServer"/> class.
        /// </summary>
        /// <param name="options">The options to intialize the instance with.</param>
        /// <param name="loggerFactory">A reference to the factory of loggers to use.</param>
        /// <param name="handlerSelector">A refrence to the selector of handlers for TCP messages.</param>
        /// <param name="clientsManager">A reference to a manager for clients.</param>
        /// <param name="listeners">A reference to the listeners to bind to the server.</param>
        /// <param name="gameworldAdapter">A reference to an adapter that will act as a client to the game world.</param>
        public TcpServer(
            IOptions <TcpServerOptions> options,
            ILoggerFactory loggerFactory,
            IHandlerSelector handlerSelector,
            IClientsManager clientsManager,
            IEnumerable <IListener> listeners,
            ITcpServerToGameworldAdapter gameworldAdapter)
        {
            options.ThrowIfNull(nameof(options));
            loggerFactory.ThrowIfNull(nameof(loggerFactory));
            handlerSelector.ThrowIfNull(nameof(handlerSelector));
            listeners.ThrowIfNull(nameof(listeners));
            gameworldAdapter.ThrowIfNull(nameof(gameworldAdapter));

            if (!listeners.Any())
            {
                throw new ArgumentException("No listeners found after composition.", nameof(listeners));
            }

            DataAnnotationsValidator.ValidateObjectRecursive(options.Value);

            this.id     = Guid.NewGuid().ToString();
            this.logger = loggerFactory.CreateLogger <TcpServer>();
            this.gameworldClientAdapter = gameworldAdapter;
            this.connectionToClientMap  = new Dictionary <IConnection, IClient>();
            this.clientsManager         = clientsManager;

            this.Options = options.Value;

            this.BindListeners(loggerFactory, handlerSelector, listeners);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BaseProtocol"/> class.
        /// </summary>
        /// <param name="handlerSelector">A reference to the handler selector to use.</param>
        /// <param name="keepConnectionOpen">Optional. A value indicating whether to maintain the connection open after processing a message in the connection.</param>
        protected BaseProtocol(IHandlerSelector handlerSelector, bool keepConnectionOpen = true)
        {
            handlerSelector.ThrowIfNull(nameof(handlerSelector));

            this.HandlerSelector    = handlerSelector;
            this.keepConnectionOpen = keepConnectionOpen;
        }
Exemplo n.º 5
0
 public void AddHandlerSelector(IHandlerSelector selector)
 {
     if (selectors == null)
     {
         selectors = new List <IHandlerSelector>();
     }
     selectors.Add(selector);
 }
		public void AddHandlerSelector(IHandlerSelector selector)
		{
			if (selectors == null)
			{
				selectors = new List<IHandlerSelector>();
			}
			selectors.Add(selector);
		}
Exemplo n.º 7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GameProtocol"/> class.
        /// </summary>
        /// <param name="logger">A reference to the logger to use.</param>
        /// <param name="handlerSelector">A reference to the handler selector to use in this protocol.</param>
        /// <param name="protocolConfigOptions">A reference to the protocol configuration options.</param>
        public GameProtocol(
            ILogger logger,
            IHandlerSelector handlerSelector,
            ProtocolConfigurationOptions protocolConfigOptions)
            : base(handlerSelector)
        {
            logger.ThrowIfNull(nameof(logger));
            protocolConfigOptions.ThrowIfNull(nameof(protocolConfigOptions));

            this.Logger = logger.ForContext <GameProtocol>();
            this.ProtocolConfiguration = protocolConfigOptions;
        }
Exemplo n.º 8
0
        /// <summary>
        /// Binds the TCP listener events with handler picking.
        /// </summary>
        /// <param name="loggerFactory">The loggers factory to create loggers with.</param>
        /// <param name="handlerSelector">The selector of handlers to bind listeners' events to.</param>
        /// <param name="listeners">The listeners to bind.</param>
        private void BindListeners(ILoggerFactory loggerFactory, IHandlerSelector handlerSelector, IEnumerable <IListener> listeners)
        {
            IEnumerable <IOutboundPacket> OnPacketReady(IConnection connection, IInboundPacket packet)
            {
                var handler = handlerSelector.SelectForPacket(packet);

                if (handler != null && this.connectionToClientMap.TryGetValue(connection, out IClient client))
                {
                    return(handler.HandleRequestPacket(this, packet, client));
                }

                return(null);
            }

            void OnConnectionClosed(IConnection connection)
            {
                // Clean up the event listeners we set up here.
                connection.PacketReady -= OnPacketReady;
                connection.Closed      -= OnConnectionClosed;

                if (!this.connectionToClientMap.ContainsKey(connection))
                {
                    return;
                }

                var playerId = this.connectionToClientMap[connection].PlayerId;

                this.clientsManager.Unregister(playerId);
                this.connectionToClientMap.Remove(connection);
            }

            void OnNewConnection(IConnection connection)
            {
                if (this.connectionToClientMap.ContainsKey(connection))
                {
                    return;
                }

                this.connectionToClientMap.Add(connection, new ConnectedClient(loggerFactory.CreateLogger <ConnectedClient>(), connection));

                connection.PacketReady += OnPacketReady;

                connection.Closed += OnConnectionClosed;
            }

            foreach (var tcpListener in listeners)
            {
                tcpListener.NewConnection += OnNewConnection;
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LoginProtocol"/> class.
        /// </summary>
        /// <param name="logger">A reference to the logger to use.</param>
        /// <param name="handlerSelector">A reference to the handler selector to use in this protocol.</param>
        /// <param name="protocolConfigOptions">A reference to the protocol configuration options.</param>
        /// <param name="gameConfigOptions">A reference to the game configuration options.</param>
        public LoginProtocol(
            ILogger logger,
            IHandlerSelector handlerSelector,
            ProtocolConfigurationOptions protocolConfigOptions,
            GameConfigurationOptions gameConfigOptions)
            : base(handlerSelector, keepConnectionOpen: false)
        {
            logger.ThrowIfNull(nameof(logger));
            protocolConfigOptions.ThrowIfNull(nameof(protocolConfigOptions));
            gameConfigOptions.ThrowIfNull(nameof(gameConfigOptions));

            this.Logger = logger.ForContext <LoginProtocol>();
            this.ProtocolConfiguration = protocolConfigOptions;
            this.GameConfiguration     = gameConfigOptions;
        }
Exemplo n.º 10
0
        /// <summary>
        /// The main entry point for the program.
        /// </summary>
        /// <param name="args">The arguments for this program.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public static async Task Main(string[] args)
        {
            var configuration = new ConfigurationBuilder()
                                .AddJsonFile("logsettings.json")
                                .Build();

            var host = new HostBuilder()
                       .ConfigureHostConfiguration(configHost =>
            {
                configHost.SetBasePath(Directory.GetCurrentDirectory());
                configHost.AddJsonFile("hostsettings.json", optional: true, reloadOnChange: true);
                configHost.AddEnvironmentVariables(prefix: "OTS_");
                configHost.AddCommandLine(args);
            })
                       .ConfigureAppConfiguration((hostContext, configApp) =>
            {
                configApp.SetBasePath(Directory.GetCurrentDirectory());
                configApp.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
                configApp.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);
                configApp.AddEnvironmentVariables(prefix: "OTS_");
                configApp.AddCommandLine(args);
            })
                       .ConfigureServices(ConfigureServices)
                       .UseSerilog((context, services, loggerConfig) =>
            {
                var telemetryConfigOptions = services.GetRequiredService <IOptions <TelemetryConfiguration> >();

                loggerConfig.ReadFrom.Configuration(configuration)
                .WriteTo.ApplicationInsights(telemetryConfigOptions.Value, TelemetryConverter.Traces, Serilog.Events.LogEventLevel.Information)
                .Enrich.FromLogContext();
            })
                       .Build();

            // Bind the TCP listener events with handler picking.
            // This is needed for now because we don't have a separate listening pipeline for generic (non TCP client requests).
            handlerSelector = host.Services.GetRequiredService <IHandlerSelector>();
            clientMap       = new Dictionary <IConnection, IClient>();
Exemplo n.º 11
0
 public void AddHandlerSelector(IHandlerSelector selector)
 {
     NamingSubSystem.AddHandlerSelector(selector);
 }
Exemplo n.º 12
0
 public void AddHandlerSelector(IHandlerSelector selector)
 {
     throw new NotImplementedException();
 }
		public void AddHandlerSelector(IHandlerSelector selector)
		{
			selectors.Add(selector);
		}