From 444af6cf57e61b8f111d00e2c29cb2ed3bf54c1e Mon Sep 17 00:00:00 2001 From: Zack Piispanen Date: Tue, 9 Sep 2014 03:49:57 -0400 Subject: [PATCH] Update to latest TSAPI. Changed update manager to use same code as stat tracker, which hopefully doesnt break on mono. Also changed it to not check every update to see if time has passed. Made packetbufferer async for windows and if specified for mono. Fixed maxspawns error message to explain range. Ticked version. --- TShockAPI/Commands.cs | 12 ++++- TShockAPI/PacketBufferer.cs | 4 +- TShockAPI/Properties/AssemblyInfo.cs | 4 +- TShockAPI/StatTracker.cs | 17 +----- TShockAPI/TShock.cs | 3 +- TShockAPI/UpdateManager.cs | 78 ++++++++++++++++++---------- TShockAPI/Utils.cs | 15 ++++++ TerrariaServerAPI | 2 +- 8 files changed, 83 insertions(+), 52 deletions(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index ff74e903..6c9aeb38 100755 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -1541,7 +1541,15 @@ namespace TShockAPI private static void CheckUpdates(CommandArgs args) { args.Player.SendInfoMessage("An update check has been queued."); - ThreadPool.QueueUserWorkItem(UpdateManager.CheckUpdate); + try + { + TShock.UpdateManager.UpdateCheck(null); + } + catch (Exception) + { + //swallow the exception + return; + } } private static void UpdatePlugins(CommandArgs args) @@ -3363,7 +3371,7 @@ namespace TShockAPI int maxSpawns = -1; if (!int.TryParse(args.Parameters[0], out maxSpawns) || maxSpawns < 0 || maxSpawns > Main.maxNPCs) { - args.Player.SendWarningMessage("Invalid maximum spawns!"); + args.Player.SendWarningMessage("Invalid maximum spawns! Acceptable range is {0} to {1}", 0, Main.maxNPCs); return; } diff --git a/TShockAPI/PacketBufferer.cs b/TShockAPI/PacketBufferer.cs index ad3b6d03..1722bfe4 100644 --- a/TShockAPI/PacketBufferer.cs +++ b/TShockAPI/PacketBufferer.cs @@ -197,10 +197,10 @@ namespace TShockAPI { if (socket.tcpClient.Client != null && socket.tcpClient.Client.Poll(0, SelectMode.SelectWrite)) { - if (ServerApi.RunningMono) + if (ServerApi.RunningMono && !ServerApi.UseAsyncSocketsInMono) socket.networkStream.Write(buffer, offset, count); else - socket.tcpClient.Client.Send(buffer, offset, count, SocketFlags.None); + socket.networkStream.BeginWrite(buffer, offset, count, socket.ServerWriteCallBack, socket.networkStream); return true; } } diff --git a/TShockAPI/Properties/AssemblyInfo.cs b/TShockAPI/Properties/AssemblyInfo.cs index b6820aaf..ba209b94 100644 --- a/TShockAPI/Properties/AssemblyInfo.cs +++ b/TShockAPI/Properties/AssemblyInfo.cs @@ -49,5 +49,5 @@ using System.Runtime.InteropServices; // Build Number // MMdd of the build -[assembly: AssemblyVersion("4.2.3.0720")] -[assembly: AssemblyFileVersion("4.2.3.0720")] +[assembly: AssemblyVersion("4.2.3.0909")] +[assembly: AssemblyFileVersion("4.2.3.0909")] diff --git a/TShockAPI/StatTracker.cs b/TShockAPI/StatTracker.cs index ccda72f9..d8f422c5 100644 --- a/TShockAPI/StatTracker.cs +++ b/TShockAPI/StatTracker.cs @@ -28,21 +28,6 @@ namespace TShockAPI } } - private HttpWebResponse GetResponseNoException(HttpWebRequest req) - { - try - { - return (HttpWebResponse)req.GetResponse(); - } - catch (WebException we) - { - var resp = we.Response as HttpWebResponse; - if (resp == null) - throw; - return resp; - } - } - private void SendUpdate(object info) { Thread.Sleep(1000*60*15); @@ -65,7 +50,7 @@ namespace TShockAPI client.Timeout = 5000; try { - using (var resp = GetResponseNoException(client)) + using (var resp = TShock.Utils.GetResponseNoException(client)) { if (resp.StatusCode != HttpStatusCode.OK) { diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 3e7e66e5..206ead01 100755 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -77,6 +77,7 @@ namespace TShockAPI public static RestManager RestManager; public static Utils Utils = Utils.Instance; public static StatTracker StatTracker = new StatTracker(); + public static UpdateManager UpdateManager; /// /// Used for implementing REST Tokens prior to the REST system starting up. /// @@ -613,6 +614,7 @@ namespace TShockAPI FixChestStacks(); StatTracker.Initialize(); + UpdateManager = new UpdateManager(); } private void ComputeMaxStyles() @@ -656,7 +658,6 @@ namespace TShockAPI private void OnUpdate(EventArgs args) { - UpdateManager.UpdateProcedureCheck(); if (Backups.IsBackupTime) Backups.Backup(); //call these every second, not every update diff --git a/TShockAPI/UpdateManager.cs b/TShockAPI/UpdateManager.cs index a3eff893..4f8305f6 100644 --- a/TShockAPI/UpdateManager.cs +++ b/TShockAPI/UpdateManager.cs @@ -18,32 +18,44 @@ along with this program. If not, see . using System; using System.Collections.Generic; +using System.IO; using System.Net; using System.Threading; using Newtonsoft.Json; namespace TShockAPI { - internal class UpdateManager + public class UpdateManager { - private static string updateUrl = "https://github.com/NyxStudios/TShock/blob/general-devel/tshock_update.json?raw=true"; - public static DateTime lastcheck = DateTime.MinValue; + private string updateUrl = "https://github.com/NyxStudios/TShock/blob/general-devel/tshock_update.json?raw=true"; /// /// Check once every X minutes. /// - private static readonly int CheckXMinutes = 30; + private readonly int CheckXMinutes = 30; - public static void UpdateProcedureCheck() + public UpdateManager() { - if ((DateTime.Now - lastcheck).TotalMinutes >= CheckXMinutes) - { - ThreadPool.QueueUserWorkItem(CheckUpdate); - lastcheck = DateTime.Now; - } + ThreadPool.QueueUserWorkItem(CheckForUpdates); } - public static void CheckUpdate(object o) + private void CheckForUpdates(object state) + { + try + { + UpdateCheck(state); + } + catch (Exception) + { + //swallow the exception + return; + } + + Thread.Sleep(CheckXMinutes * 60 * 1000); + ThreadPool.QueueUserWorkItem(CheckForUpdates); + } + + public void UpdateCheck(object o) { var updates = ServerIsOutOfDate(); if (updates != null) @@ -56,29 +68,39 @@ namespace TShockAPI /// Checks to see if the server is out of date. /// /// - private static Dictionary ServerIsOutOfDate() + private Dictionary ServerIsOutOfDate() { - using (var client = new WebClient()) + var client = (HttpWebRequest)WebRequest.Create(updateUrl); + client.Timeout = 5000; + try { - client.Headers.Add("user-agent", - "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)"); - try + using (var resp = TShock.Utils.GetResponseNoException(client)) { - string updatejson = client.DownloadString(updateUrl); - var update = JsonConvert.DeserializeObject>(updatejson); - var version = new Version(update["version"]); - if (TShock.VersionNum.CompareTo(version) < 0) - return update; + if (resp.StatusCode != HttpStatusCode.OK) + { + throw new IOException("Server did not respond with an OK."); + } + + using(var reader = new StreamReader(resp.GetResponseStream())) + { + string updatejson = reader.ReadToEnd(); + var update = JsonConvert.DeserializeObject>(updatejson); + var version = new Version(update["version"]); + if (TShock.VersionNum.CompareTo(version) < 0) + return update; + } } - catch (Exception e) - { - Log.Error(e.ToString()); - } - return null; } + catch (Exception e) + { + Log.ConsoleError("UpdateManager Exception: {0}", e); + throw e; + } + + return null; } - private static void NotifyAdministrators(Dictionary update) + private void NotifyAdministrators(Dictionary update) { var changes = update["changes"].Split(new[] {'\n'}, StringSplitOptions.RemoveEmptyEntries); NotifyAdministrator(TSPlayer.Server, changes); @@ -91,7 +113,7 @@ namespace TShockAPI } } - private static void NotifyAdministrator(TSPlayer player, string[] changes) + private void NotifyAdministrator(TSPlayer player, string[] changes) { player.SendMessage("The server is out of date.", Color.Red); for (int j = 0; j < changes.Length; j++) diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index bfe6c0c6..4b6c6208 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -1014,5 +1014,20 @@ namespace TShockAPI return result; } + + public HttpWebResponse GetResponseNoException(HttpWebRequest req) + { + try + { + return (HttpWebResponse)req.GetResponse(); + } + catch (WebException we) + { + var resp = we.Response as HttpWebResponse; + if (resp == null) + throw; + return resp; + } + } } } diff --git a/TerrariaServerAPI b/TerrariaServerAPI index d446a828..2fc5b098 160000 --- a/TerrariaServerAPI +++ b/TerrariaServerAPI @@ -1 +1 @@ -Subproject commit d446a8288a5d7984f4298a507a64249865259472 +Subproject commit 2fc5b098323ce7062df5ed91b1c532066fb8cb65