Put pCampbot "disconnect" command on separate thread like "connect" so that we can continue to run status commands whilst bots are disconnecting.

bullet-2.82
Justin Clark-Casey (justincc) 2014-08-05 01:15:07 +01:00
parent e57e9e95d4
commit fcc665a567
1 changed files with 44 additions and 36 deletions

View File

@ -611,45 +611,53 @@ namespace pCampBot
private void HandleDisconnect(string module, string[] cmd) private void HandleDisconnect(string module, string[] cmd)
{ {
List<Bot> connectedBots;
int botsToDisconnectCount;
lock (m_bots) lock (m_bots)
connectedBots = m_bots.FindAll(b => b.ConnectionState == ConnectionState.Connected);
if (cmd.Length == 1)
{ {
int botsToDisconnect; botsToDisconnectCount = connectedBots.Count;
int connectedBots = m_bots.Count(b => b.ConnectionState == ConnectionState.Connected);
if (cmd.Length == 1)
{
botsToDisconnect = connectedBots;
}
else
{
if (!ConsoleUtil.TryParseConsoleNaturalInt(MainConsole.Instance, cmd[1], out botsToDisconnect))
return;
botsToDisconnect = Math.Min(botsToDisconnect, connectedBots);
}
DisconnectingBots = true;
MainConsole.Instance.OutputFormat("Disconnecting {0} bots", botsToDisconnect);
int disconnectedBots = 0;
for (int i = m_bots.Count - 1; i >= 0; i--)
{
if (disconnectedBots >= botsToDisconnect)
break;
Bot thisBot = m_bots[i];
if (thisBot.ConnectionState == ConnectionState.Connected)
{
Util.FireAndForget(o => thisBot.Disconnect());
disconnectedBots++;
}
}
DisconnectingBots = false;
} }
else
{
if (!ConsoleUtil.TryParseConsoleNaturalInt(MainConsole.Instance, cmd[1], out botsToDisconnectCount))
return;
botsToDisconnectCount = Math.Min(botsToDisconnectCount, connectedBots.Count);
}
Thread disconnectBotThread = new Thread(o => DisconnectBotsInternal(connectedBots, botsToDisconnectCount));
disconnectBotThread.Name = "Bots disconnection thread";
disconnectBotThread.Start();
}
private void DisconnectBotsInternal(List<Bot> connectedBots, int disconnectCount)
{
DisconnectingBots = true;
MainConsole.Instance.OutputFormat("Disconnecting {0} bots", disconnectCount);
int disconnectedBots = 0;
for (int i = connectedBots.Count - 1; i >= 0; i--)
{
if (disconnectedBots >= disconnectCount)
break;
Bot thisBot = connectedBots[i];
if (thisBot.ConnectionState == ConnectionState.Connected)
{
thisBot.Disconnect();
disconnectedBots++;
}
}
DisconnectingBots = false;
} }
private void HandleSit(string module, string[] cmd) private void HandleSit(string module, string[] cmd)