From 04cb9bc4bf295d58dfb12f6daf012ac6433ea6cd Mon Sep 17 00:00:00 2001 From: "Justin Clark-Casey (justincc)" Date: Tue, 5 Aug 2014 01:15:07 +0100 Subject: [PATCH] Put pCampbot "disconnect" command on separate thread like "connect" so that we can continue to run status commands whilst bots are disconnecting. --- OpenSim/Tools/pCampBot/BotManager.cs | 80 +++++++++++++++------------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/OpenSim/Tools/pCampBot/BotManager.cs b/OpenSim/Tools/pCampBot/BotManager.cs index b1f0df887b..dbf8f5f9f9 100644 --- a/OpenSim/Tools/pCampBot/BotManager.cs +++ b/OpenSim/Tools/pCampBot/BotManager.cs @@ -611,45 +611,53 @@ namespace pCampBot private void HandleDisconnect(string module, string[] cmd) { + List connectedBots; + int botsToDisconnectCount; + lock (m_bots) + connectedBots = m_bots.FindAll(b => b.ConnectionState == ConnectionState.Connected); + + if (cmd.Length == 1) { - int botsToDisconnect; - 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; + botsToDisconnectCount = connectedBots.Count; } + 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 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)