Add connection timeout using server keepalives

Vanilla client will consider that connection has been lost
when no server keepalive was received during the last 30 seconds.
This commit implements a similar mechanism in MCC. See #802
This commit is contained in:
ORelio 2019-09-15 17:01:53 +02:00
parent 1406c00abd
commit 877e50579d
4 changed files with 61 additions and 6 deletions

View file

@ -52,6 +52,8 @@ namespace MinecraftClient
private string uuid;
private string sessionid;
private Inventory playerInventory;
private DateTime lastKeepAlive;
private object lastKeepAliveLock = new object();
public int GetServerPort() { return port; }
public string GetServerHost() { return host; }
@ -64,6 +66,7 @@ namespace MinecraftClient
TcpClient client;
IMinecraftCom handler;
Thread cmdprompt;
Thread timeoutdetector;
/// <summary>
/// Starts the main chat client
@ -167,6 +170,10 @@ namespace MinecraftClient
cmdprompt = new Thread(new ThreadStart(CommandPrompt));
cmdprompt.Name = "MCC Command prompt";
cmdprompt.Start();
timeoutdetector = new Thread(new ThreadStart(TimeoutDetector));
timeoutdetector.Name = "MCC Connection timeout detector";
timeoutdetector.Start();
}
}
}
@ -253,6 +260,29 @@ namespace MinecraftClient
catch (NullReferenceException) { }
}
/// <summary>
/// Periodically checks for server keepalives and consider that connection has been lost if the last received keepalive is too old.
/// </summary>
private void TimeoutDetector()
{
lock (lastKeepAliveLock)
{
lastKeepAlive = DateTime.Now;
}
do
{
Thread.Sleep(TimeSpan.FromSeconds(15));
lock (lastKeepAliveLock)
{
if (lastKeepAlive.AddSeconds(30) < DateTime.Now)
{
OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost, "Connection Timeout");
}
}
}
while (true);
}
/// <summary>
/// Perform an internal MCC command (not a server command, use SendText() instead for that!)
/// </summary>
@ -336,6 +366,9 @@ namespace MinecraftClient
if (cmdprompt != null)
cmdprompt.Abort();
if (timeoutdetector != null)
timeoutdetector.Abort();
Thread.Sleep(1000);
if (client != null)
@ -607,6 +640,10 @@ namespace MinecraftClient
/// <param name="links">Links embedded in text</param>
public void OnTextReceived(string text, bool isJson)
{
lock (lastKeepAliveLock)
{
lastKeepAlive = DateTime.Now;
}
List<string> links = new List<string>();
string json = null;
if (isJson)
@ -637,11 +674,22 @@ namespace MinecraftClient
}
}
/// <summary>
/// Received a connection keep-alive from the server
/// </summary>
public void OnServerKeepAlive()
{
lock (lastKeepAliveLock)
{
lastKeepAlive = DateTime.Now;
}
}
/// <summary>
/// When an inventory is opened
/// </summary>
/// <param name="inventory">Location to reach</param>
public void onInventoryOpen(Inventory inventory)
public void OnInventoryOpen(Inventory inventory)
{
//TODO: Handle Inventory
if (!inventories.Contains(inventory))
@ -654,7 +702,7 @@ namespace MinecraftClient
/// When an inventory is close
/// </summary>
/// <param name="inventoryID">Location to reach</param>
public void onInventoryClose(byte inventoryID)
public void OnInventoryClose(byte inventoryID)
{
for (int i = 0; i < inventories.Count; i++)
{