Add timeout detect on server login

This commit is contained in:
ReinforceZwei 2021-04-30 12:28:27 +08:00 committed by ORelio
parent 7fd0f9157f
commit ad14edad2e

View file

@ -225,6 +225,13 @@ namespace MinecraftClient
handler = Protocol.ProtocolHandler.GetProtocolHandler(client, protocolversion, forgeInfo, this); handler = Protocol.ProtocolHandler.GetProtocolHandler(client, protocolversion, forgeInfo, this);
Log.Info(Translations.Get("mcc.version_supported")); Log.Info(Translations.Get("mcc.version_supported"));
if (!singlecommand)
{
timeoutdetector = new Thread(new ThreadStart(TimeoutDetector));
timeoutdetector.Name = "MCC Connection timeout detector";
timeoutdetector.Start();
}
try try
{ {
if (handler.Login()) if (handler.Login())
@ -248,10 +255,6 @@ namespace MinecraftClient
cmdprompt = new Thread(new ThreadStart(CommandPrompt)); cmdprompt = new Thread(new ThreadStart(CommandPrompt));
cmdprompt.Name = "MCC Command prompt"; cmdprompt.Name = "MCC Command prompt";
cmdprompt.Start(); cmdprompt.Start();
timeoutdetector = new Thread(new ThreadStart(TimeoutDetector));
timeoutdetector.Name = "MCC Connection timeout detector";
timeoutdetector.Start();
} }
} }
else else
@ -276,6 +279,11 @@ namespace MinecraftClient
if (retry) if (retry)
{ {
if (timeoutdetector != null)
{
timeoutdetector.Abort();
timeoutdetector = null;
}
if (ReconnectionAttemptsLeft > 0) if (ReconnectionAttemptsLeft > 0)
{ {
Log.Info(Translations.Get("mcc.reconnect", ReconnectionAttemptsLeft)); Log.Info(Translations.Get("mcc.reconnect", ReconnectionAttemptsLeft));
@ -358,10 +366,7 @@ namespace MinecraftClient
/// </summary> /// </summary>
private void TimeoutDetector() private void TimeoutDetector()
{ {
lock (lastKeepAliveLock) UpdateKeepAlive();
{
lastKeepAlive = DateTime.Now;
}
do do
{ {
Thread.Sleep(TimeSpan.FromSeconds(15)); Thread.Sleep(TimeSpan.FromSeconds(15));
@ -377,6 +382,17 @@ namespace MinecraftClient
while (true); while (true);
} }
/// <summary>
/// Update last keep alive to current time
/// </summary>
private void UpdateKeepAlive()
{
lock (lastKeepAliveLock)
{
lastKeepAlive = DateTime.Now;
}
}
/// <summary> /// <summary>
/// Perform an internal MCC command (not a server command, use SendText() instead for that!) /// Perform an internal MCC command (not a server command, use SendText() instead for that!)
/// </summary> /// </summary>
@ -1747,10 +1763,7 @@ namespace MinecraftClient
/// <param name="isJson">TRUE if the text is JSON-Encoded</param> /// <param name="isJson">TRUE if the text is JSON-Encoded</param>
public void OnTextReceived(string text, bool isJson) public void OnTextReceived(string text, bool isJson)
{ {
lock (lastKeepAliveLock) UpdateKeepAlive();
{
lastKeepAlive = DateTime.Now;
}
List<string> links = new List<string>(); List<string> links = new List<string>();
string json = null; string json = null;
@ -1776,10 +1789,7 @@ namespace MinecraftClient
/// </summary> /// </summary>
public void OnServerKeepAlive() public void OnServerKeepAlive()
{ {
lock (lastKeepAliveLock) UpdateKeepAlive();
{
lastKeepAlive = DateTime.Now;
}
} }
/// <summary> /// <summary>
@ -2116,6 +2126,8 @@ namespace MinecraftClient
/// <param name="TimeOfDay"></param> /// <param name="TimeOfDay"></param>
public void OnTimeUpdate(long WorldAge, long TimeOfDay) public void OnTimeUpdate(long WorldAge, long TimeOfDay)
{ {
// TimeUpdate sent every server tick hence used as timeout detect
UpdateKeepAlive();
// calculate server tps // calculate server tps
if (lastAge != 0) if (lastAge != 0)
{ {