diff --git a/MinecraftClient/AutoTimeout.cs b/MinecraftClient/AutoTimeout.cs new file mode 100644 index 00000000..abc263de --- /dev/null +++ b/MinecraftClient/AutoTimeout.cs @@ -0,0 +1,41 @@ +using System; +using System.Threading; + +namespace MinecraftClient +{ + /// + /// Allow easy timeout on pieces of code + /// + /// + /// By ORelio - (c) 2014 - CDDL 1.0 + /// + public class AutoTimeout + { + /// + /// Perform the specified action with specified timeout + /// + /// Action to run + /// Maximum timeout in milliseconds + /// True if the action finished whithout timing out + public static bool Perform(Action action, int timeout) + { + return Perform(action, TimeSpan.FromMilliseconds(timeout)); + } + + /// + /// Perform the specified action with specified timeout + /// + /// Action to run + /// Maximum timeout + /// True if the action finished whithout timing out + 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; + } + } +} \ No newline at end of file diff --git a/MinecraftClient/MinecraftClient.csproj b/MinecraftClient/MinecraftClient.csproj index 52dba333..c174cb1e 100644 --- a/MinecraftClient/MinecraftClient.csproj +++ b/MinecraftClient/MinecraftClient.csproj @@ -71,6 +71,7 @@ + diff --git a/MinecraftClient/Protocol/ProtocolHandler.cs b/MinecraftClient/Protocol/ProtocolHandler.cs index 076420d2..08b309f3 100644 --- a/MinecraftClient/Protocol/ProtocolHandler.cs +++ b/MinecraftClient/Protocol/ProtocolHandler.cs @@ -230,30 +230,37 @@ namespace MinecraftClient.Protocol private static int doHTTPSPost(string host, string endpoint, string request, ref string result) { - TcpClient client = ProxyHandler.newTcpClient(host, 443); - SslStream stream = new SslStream(client.GetStream()); - stream.AuthenticateAsClient(host); - - List http_request = new List(); - http_request.Add("POST " + endpoint + " HTTP/1.1"); - http_request.Add("Host: " + host); - http_request.Add("User-Agent: MCC/" + Program.Version); - http_request.Add("Content-Type: application/json"); - http_request.Add("Content-Length: " + Encoding.ASCII.GetBytes(request).Length); - http_request.Add("Connection: close"); - http_request.Add(""); - http_request.Add(request); - - stream.Write(Encoding.ASCII.GetBytes(String.Join("\r\n", http_request.ToArray()))); - System.IO.StreamReader sr = new System.IO.StreamReader(stream); - string raw_result = sr.ReadToEnd(); - - if (raw_result.StartsWith("HTTP/1.1")) + string postResult = null; + int statusCode = 520; + AutoTimeout.Perform(() => { - result = raw_result.Substring(raw_result.IndexOf("\r\n\r\n") + 4); - return Settings.str2int(raw_result.Split(' ')[1]); - } - else return 520; //Web server is returning an unknown error + TcpClient client = ProxyHandler.newTcpClient(host, 443); + SslStream stream = new SslStream(client.GetStream()); + stream.AuthenticateAsClient(host); + + List http_request = new List(); + http_request.Add("POST " + endpoint + " HTTP/1.1"); + http_request.Add("Host: " + host); + http_request.Add("User-Agent: MCC/" + Program.Version); + http_request.Add("Content-Type: application/json"); + http_request.Add("Content-Length: " + Encoding.ASCII.GetBytes(request).Length); + http_request.Add("Connection: close"); + http_request.Add(""); + http_request.Add(request); + + stream.Write(Encoding.ASCII.GetBytes(String.Join("\r\n", http_request.ToArray()))); + System.IO.StreamReader sr = new System.IO.StreamReader(stream); + string raw_result = sr.ReadToEnd(); + + if (raw_result.StartsWith("HTTP/1.1")) + { + postResult = raw_result.Substring(raw_result.IndexOf("\r\n\r\n") + 4); + statusCode = Settings.str2int(raw_result.Split(' ')[1]); + } + else statusCode = 520; //Web server is returning an unknown error + }, 15000); + result = postResult; + return statusCode; } ///