public void Listen(TimeSpan?maxWaitTime = null) { maxWaitTime = maxWaitTime ?? TimeSpan.FromSeconds(25); var clock = new Stopwatch(); clock.Start(); while (State != NetworkState.CLOSING_ASSOCIATION && clock.Elapsed < maxWaitTime) { if (_abortRequested) { Logger.Log("Abort requested...aborting."); HandleAbort(); break; } if (_cancelRequested) { Logger.Log("Cancellation requested...cancelling."); HandleCancel(); } if (State != NetworkState.CLOSING_ASSOCIATION && State != NetworkState.TRANSPORT_CONNECTION_OPEN) { var message = Read(); if (message != null) { clock.Restart(); Process(message); Stream.Flush(); clock.Restart(); } } if (State == NetworkState.TRANSPORT_CONNECTION_OPEN && !OutboundMessages.IsEmpty) { while (OutboundMessages.Any()) { if (State == NetworkState.TRANSPORT_CONNECTION_OPEN) { AbstractDIMSEBase dimse; if (OutboundMessages.TryDequeue(out dimse)) { PDataMessenger.Send(dimse, this); } } } } if (!IsClientConnected) { Logger.Log("Connection closed - ending association."); break; } } if (State != NetworkState.CLOSING_ASSOCIATION) { Logger.Log("Network timeout - closing association."); } }
private void HandleCancel() { AbstractDIMSEBase cancel; OutboundMessages.TryPeek(out cancel); if (cancel is CCancel) { OutboundMessages.TryDequeue(out cancel); Stream.Flush(); PDataMessenger.Send(cancel, this); } }
protected override void DoWrite(ChannelOutboundBuffer input) { for (;;) { var msg = input.Current; if (msg == null) { break; } ReferenceCountUtil.Retain(msg); OutboundMessages.Enqueue(msg); input.Remove(); } }
public void Cancel(CCancel cancel) { _cancelRequested = true; OutboundMessages.Enqueue(cancel); }
public void Listen(TimeSpan?maxWaitTime = null) { maxWaitTime = maxWaitTime ?? TimeSpan.FromSeconds(25); IdleClock = IdleClock ?? new Stopwatch(); IdleClock.Reset(); IdleClock.Start(); while (State != NetworkState.CLOSING_ASSOCIATION && IdleClock.Elapsed < maxWaitTime) { if (_abortRequested) { Logger.Log("Abort requested...aborting."); HandleAbort(); break; } if (_cancelRequested) { Logger.Log("Cancellation requested...cancelling."); HandleCancel(); } if (State != NetworkState.CLOSING_ASSOCIATION && State != NetworkState.TRANSPORT_CONNECTION_OPEN) { var message = Read(maxWaitTime.Value.TotalMilliseconds - IdleClock.ElapsedMilliseconds); if (message != null) { try { IdleClock.Restart(); Process(message); Stream.Flush(); IdleClock.Restart(); } catch (IOException e) { Logger.Log($"Network connection was lost. {e.Message}", LogPriority.ERROR); break;//Connection was lost } } } if (State == NetworkState.TRANSPORT_CONNECTION_OPEN && !OutboundMessages.IsEmpty) { while (!OutboundMessages.IsEmpty) { if (State == NetworkState.TRANSPORT_CONNECTION_OPEN) { AbstractDIMSEBase dimse; if (OutboundMessages.TryDequeue(out dimse)) { PDataMessenger.Send(dimse, this); } } } } if (!IsClientConnected) { Logger.Log("Connection closed - ending association."); break; } } if (State != NetworkState.CLOSING_ASSOCIATION) { Logger.Log("Network timeout - closing association."); } }
private void ProcessLobbyCommand(ulong channelId, ulong accountId, string name, string text) { try { var commandParams = text.Split(' '); var channelName = Channels[channelId]; var game = _config.Games.Where(m => m.Chat.ToLower() == channelName.ToLower()).SingleOrDefault(); if (channelName == _config.PrivateChat) { DotaClient.QueueChatMessage(channelId, String.Format("{0}, requesting a lobby in this channel is not allowed, please request a lobby in custom game's public channel.", name, GetValidServerRegions()), false); } else if (commandParams.Length < 2) { DotaClient.QueueChatMessage(channelId, String.Format("{0}, please specify server region. Regions: {1}.", name, GetValidServerRegions()), false); if (game.Maps.Count > 1) { DotaClient.QueueChatMessage(channelId, String.Format(_config.SpecifyMapMessage, name, GetValidMaps(game), _config.ServerRegions.FirstOrDefault().ShortName, game.Maps.FirstOrDefault().ChatId), false); } } else { var region = _config.ServerRegions.Where(sr => sr.ShortName == commandParams[1].Trim().ToLower()).SingleOrDefault(); if (region == null) { DotaClient.QueueChatMessage(channelId, String.Format("{0}, invalid server region. Valid regions: {1}.", name, GetValidServerRegions()), false); } else { string mapName = null; if (commandParams.Length > 2 && game.Maps.Count > 1) { mapName = commandParams[2]; } Map map = null; bool mapOk = true; if (mapName == null) { map = game.Maps.OrderBy(x => Guid.NewGuid()).FirstOrDefault(); } else { map = game.Maps.Where(m => m.ChatId == mapName.Trim().ToLower()).FirstOrDefault(); if (map == null && game.Maps.Count > 1) { mapOk = false; DotaClient.QueueChatMessage(channelId, String.Format(_config.MapNotFoundMessage, name, GetValidMaps(game)), false); } } if (mapOk) { using (IRepository repository = new Repository(_dbOptions)) { var banned = repository.Bans.Any(b => b.Account.AccountId == accountId.ToString() && b.Expires > DateTime.UtcNow); if (!banned || _config.Admins.Contains(accountId)) { try { var timedoutLobbies = LobbyStates.Where(ls => ls.Created.AddMinutes(_config.Games.Where(g => g.Maps.Any(m => m.MapId == ls.CustomMapName)).SingleOrDefault().LobbyTimeout + 1) < DateTime.UtcNow); foreach (var timedoutLobby in timedoutLobbies.ToList()) { _logger.Info("Removed timed out lobby - Game: " + timedoutLobby.CustomMapName + " Server ID: " + timedoutLobby.CustomMapName); LobbyStates.Remove(timedoutLobby); } } catch (Exception e) { _logger.Error("Error while checking timed out lobbies", e); } var myExistingLobby = LobbyStates.Where(ls => ls.RequesterAccountId == accountId).FirstOrDefault(); if (myExistingLobby != null) { DotaClient.QueueChatMessage(channelId, String.Format(_config.LobbyRequesterHasHost, name, _config.Games.Where(g => g.Maps.Any(m => m.MapId == myExistingLobby.CustomMapName)).SingleOrDefault().Name), false); } else { var existingLobby = LobbyStates.Where(ls => game.Maps.Any(m => m.MapId == ls.CustomMapName) && ls.ServerId == region.ServerId).FirstOrDefault(); if (existingLobby != null) { if (existingLobby.LobbyId == 0) { DotaClient.QueueChatMessage(channelId, String.Format(_config.LobbyAlreadyQueued, name, region.LongName), false); } else { DotaClient.QueueChatMessage(channelId, String.Format(_config.LobbyAlreadyHosted, name, region.LongName), true); DotaClient.QueueLobbyShare(channelId, existingLobby.LobbyId, game.CustomGameMode, true); } } else { if (LobbyStates.Count < _config.LobbyBotsPool.Count) { DotaClient.QueueChatMessage(channelId, String.Format("{0}, hosting your lobby, just a sec...", name), false); game.ServerId = region.ServerId; game.RequestedBy = accountId; _logger.Info("Queue lobby - Game: " + game.Name + " - Map: " + map.Name + " - Server ID: " + game.ServerId); OutboundMessages.Enqueue(new Message() { Id = MessageIds.Out.CREATE_LOBBY, Payload = JsonConvert.SerializeObject(new LobbyHost() { Game = game, Map = map }) }); lock (LobbyStates) { //Just in case should something here be multithreaded LobbyStates.Add(new LobbyState() { CustomMapName = map.MapId, ServerId = game.ServerId, RequesterAccountId = accountId, Created = DateTime.UtcNow }); } } else { DotaClient.QueueChatMessage(channelId, String.Format(_config.BusyMessage, name), false); } } } } else if (banned) { DotaClient.QueueChatMessage(channelId, String.Format(_config.LobbyRequesterBanned, name), false); } } } } } } catch (Exception e) { _logger.Error(String.Format("Error while processing !host command: {0}", text), e); } }