Add timeout for server ping

Thanks doranchak & FantomHD (post no.1193)
+ Add missing doc for MinecraftVersionPrompt
This commit is contained in:
ORelio 2015-04-14 15:36:51 +02:00
parent 2cf46c0487
commit 791ecba454
2 changed files with 21 additions and 15 deletions

View file

@ -327,9 +327,9 @@ namespace MinecraftClient
}
/// <summary>
///
/// Ask for server version when failed to ping server and/or determinate serveur version
/// </summary>
/// <returns></returns>
/// <returns>TRUE if a Minecraft version has been read from prompt</returns>
public static bool MinecraftVersionPrompt()
{

View file

@ -24,26 +24,32 @@ namespace MinecraftClient.Protocol
/// <returns>TRUE if ping was successful</returns>
public static bool GetServerInfo(string serverIP, ushort serverPort, ref int protocolversion)
{
bool success = false;
int protocolversionTmp = 0;
if (AutoTimeout.Perform(() =>
{
try
{
if (Protocol16Handler.doPing(serverIP, serverPort, ref protocolversion))
if (Protocol16Handler.doPing(serverIP, serverPort, ref protocolversionTmp)
|| Protocol17Handler.doPing(serverIP, serverPort, ref protocolversionTmp))
{
return true;
}
else if (Protocol17Handler.doPing(serverIP, serverPort, ref protocolversion))
{
return true;
}
else
{
ConsoleIO.WriteLineFormatted("§8Unexpected answer from the server (is that a Minecraft server ?)");
return false;
success = true;
}
else ConsoleIO.WriteLineFormatted("§8Unexpected answer from the server (is that a Minecraft server ?)");
}
catch
{
ConsoleIO.WriteLineFormatted("§8An error occured while attempting to connect to this IP.");
}
}, TimeSpan.FromSeconds(30)))
{
protocolversion = protocolversionTmp;
return success;
}
else
{
ConsoleIO.WriteLineFormatted("§8A timeout occured while attempting to connect to this IP.");
return false;
}
}
@ -260,7 +266,7 @@ namespace MinecraftClient.Protocol
statusCode = Settings.str2int(raw_result.Split(' ')[1]);
}
else statusCode = 520; //Web server is returning an unknown error
}, 30000);
}, TimeSpan.FromSeconds(30));
result = postResult;
return statusCode;
}