Add 15 seconds timeout to session and login

Add AutoTimeout class for use on login and session requests.
Bug report by GamerCorey7.
This commit is contained in:
ORelio 2015-01-27 20:38:59 +01:00
parent ee406b233e
commit 391eca102c
3 changed files with 72 additions and 23 deletions

View file

@ -0,0 +1,41 @@
using System;
using System.Threading;
namespace MinecraftClient
{
/// <summary>
/// Allow easy timeout on pieces of code
/// </summary>
/// <remarks>
/// By ORelio - (c) 2014 - CDDL 1.0
/// </remarks>
public class AutoTimeout
{
/// <summary>
/// Perform the specified action with specified timeout
/// </summary>
/// <param name="action">Action to run</param>
/// <param name="attempts">Maximum timeout in milliseconds</param>
/// <returns>True if the action finished whithout timing out</returns>
public static bool Perform(Action action, int timeout)
{
return Perform(action, TimeSpan.FromMilliseconds(timeout));
}
/// <summary>
/// Perform the specified action with specified timeout
/// </summary>
/// <param name="action">Action to run</param>
/// <param name="attempts">Maximum timeout</param>
/// <returns>True if the action finished whithout timing out</returns>
public static bool Perform(Action action, TimeSpan timeout)
{
Thread thread = new Thread(new ThreadStart(action));
thread.Start();
bool success = thread.Join(timeout);
if (!success)
thread.Abort();
return success;
}
}
}

View file

@ -71,6 +71,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AutoTimeout.cs" />
<Compile Include="ChatBots\Alerts.cs" />
<Compile Include="ChatBots\AntiAFK.cs" />
<Compile Include="ChatBots\AutoRelog.cs" />

View file

@ -229,6 +229,10 @@ namespace MinecraftClient.Protocol
/// <returns>HTTP Status code</returns>
private static int doHTTPSPost(string host, string endpoint, string request, ref string result)
{
string postResult = null;
int statusCode = 520;
AutoTimeout.Perform(() =>
{
TcpClient client = ProxyHandler.newTcpClient(host, 443);
SslStream stream = new SslStream(client.GetStream());
@ -250,10 +254,13 @@ namespace MinecraftClient.Protocol
if (raw_result.StartsWith("HTTP/1.1"))
{
result = raw_result.Substring(raw_result.IndexOf("\r\n\r\n") + 4);
return Settings.str2int(raw_result.Split(' ')[1]);
postResult = raw_result.Substring(raw_result.IndexOf("\r\n\r\n") + 4);
statusCode = Settings.str2int(raw_result.Split(' ')[1]);
}
else return 520; //Web server is returning an unknown error
else statusCode = 520; //Web server is returning an unknown error
}, 15000);
result = postResult;
return statusCode;
}
/// <summary>