public ProviderBasedServerPool(string id, IGameServerProvider provider, ILogger logger, IGameSessions gameSessions) { Id = id; this.provider = provider; this.logger = logger; Task.Run(async() => { isRunning = true; while (isRunning) { try { // resolve pending requests while (_pendingRequests.Any() && _readyServers.Any()) { if (_pendingRequests.TryDequeue(out var request)) { var serverId = _readyServers.Keys.FirstOrDefault(); if (serverId != null && _readyServers.TryRemove(serverId, out var server)) { request.RequestCompletedCompletionSource.SetResult(server); _runningServers.TryAdd(serverId, server); var startupParameters = new GameServerStartupParameters { GameSessionConnectionToken = await gameSessions.CreateServerConnectionToken(request.Id, server.GameServer.Id), Config = request.GameSessionConfiguration, GameSessionId = request.Id }; server.RunTcs.TrySetResult(startupParameters); } else { _pendingRequests.Enqueue(request); } } } //meet running servers requirements to satisfy requests + min ready server requirements var serversToStart = _pendingRequests.Count - _readyServers.Count - _startingServers.Count + MinServerReady; for (int i = 0; i < serversToStart; i++) { var guid = Guid.NewGuid().ToString(); _ = StartServer(provider.Type + "/" + guid); } //clean timedout servers foreach (var server in _startingServers.Values.ToArray()) { if (server.CreatedOn < DateTime.UtcNow - TimeSpan.FromMinutes(10)) { _startingServers.TryRemove(server.Id, out _); } } } catch (Exception ex) { logger.Log(LogLevel.Fatal, "serverpools." + provider.Type, "An error occured while running the server pool logic", ex); } await Task.Delay(1000); } foreach (var rq in _pendingRequests) { rq.RequestCompletedCompletionSource.TrySetCanceled(); } while (_startingServers.Any() || _readyServers.Any()) { foreach (var server in _readyServers.Values) { await provider.StopServer(server.Id); } await Task.Delay(1000); } }); }
public SampleGameFindingResolver(IGameSessions gameSessions) { this.gameSessions = gameSessions; }
public ProviderBasedServerPoolProvider(IEnumerable <IGameServerProvider> serverProviders, ILogger logger, IGameSessions gameSessions) { this.serverProviders = serverProviders; this.logger = logger; this.gameSessions = gameSessions; }