From a28d2a38fd2f8167c5d1e50b4af26e59c4190674 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 15 Aug 2016 16:43:55 +1000 Subject: [PATCH] Increase 'Connection timeout' threshold to 30 seconds, avoid StringReader and replace with our own ReadLine() method. --- .../Network/NetworkProcessor.Original.cs | 5 +- .../Network/NetworkProcessor.WoM.cs | 73 +++++++++++-------- ClassicalSharp/Network/NetworkProcessor.cs | 2 +- 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/ClassicalSharp/Network/NetworkProcessor.Original.cs b/ClassicalSharp/Network/NetworkProcessor.Original.cs index aeb8fa13d..2388aed8e 100644 --- a/ClassicalSharp/Network/NetworkProcessor.Original.cs +++ b/ClassicalSharp/Network/NetworkProcessor.Original.cs @@ -96,9 +96,8 @@ namespace ClassicalSharp.Network { prevCursorVisible = game.CursorVisible; game.Gui.SetNewScreen( new LoadingMapScreen( game, ServerName, ServerMotd ), false ); - if( ServerMotd.Contains( "cfg=" ) ) { - ReadWomConfigurationAsync(); - } + if( ServerMotd.Contains( "cfg=" ) && !game.PureClassic ) + ReadWomConfigAsync(); receivedFirstPosition = false; gzipHeader = new GZipHeaderReader(); diff --git a/ClassicalSharp/Network/NetworkProcessor.WoM.cs b/ClassicalSharp/Network/NetworkProcessor.WoM.cs index 0ea24782d..1ec37f46c 100644 --- a/ClassicalSharp/Network/NetworkProcessor.WoM.cs +++ b/ClassicalSharp/Network/NetworkProcessor.WoM.cs @@ -2,10 +2,7 @@ // This class was partially based on information from http://files.worldofminecraft.com/texturing/ // NOTE: http://files.worldofminecraft.com/ has been down for quite a while, so support was removed on Oct 10, 2015 using System; -using System.Drawing; -using System.IO; using ClassicalSharp.Map; -using ClassicalSharp.Network; namespace ClassicalSharp.Network { @@ -24,39 +21,39 @@ namespace ClassicalSharp.Network { } void ParseWomConfig( string page ) { - using( StringReader reader = new StringReader( page ) ) { - string line; - while( ( line = reader.ReadLine() ) != null ) { - Utils.LogDebug( line ); - string[] parts = line.Split( new [] { '=' }, 2 ); - if( parts.Length < 2 ) continue; - string key = parts[0].TrimEnd(); - string value = parts[1].TrimStart(); - - if( key == "environment.cloud" ) { - FastColour col = ParseWomColour( value, WorldEnv.DefaultCloudsColour ); - game.World.Env.SetCloudsColour( col ); - } else if( key == "environment.sky" ) { - FastColour col = ParseWomColour( value, WorldEnv.DefaultSkyColour ); - game.World.Env.SetSkyColour( col ); - } else if( key == "environment.fog" ) { - FastColour col = ParseWomColour( value, WorldEnv.DefaultFogColour ); - game.World.Env.SetFogColour( col ); - } else if( key == "environment.level" ) { - int waterLevel = 0; - if( Int32.TryParse( value, out waterLevel ) ) - game.World.Env.SetEdgeLevel( waterLevel ); - } else if( key == "user.detail" && !cpe.useMessageTypes ) { - game.Chat.Add( value, MessageType.Status2 ); - } + string line; + int start = 0; + while( (line = ReadLine( ref start, page )) != null ) { + Utils.LogDebug( line ); + int sepIndex = line.IndexOf('='); + if( sepIndex == -1 ) continue; + string key = line.Substring(0, sepIndex).TrimEnd(); + string value = line.Substring(sepIndex + 1).TrimStart(); + + if( key == "environment.cloud" ) { + FastColour col = ParseWomColour( value, WorldEnv.DefaultCloudsColour ); + game.World.Env.SetCloudsColour( col ); + } else if( key == "environment.sky" ) { + FastColour col = ParseWomColour( value, WorldEnv.DefaultSkyColour ); + game.World.Env.SetSkyColour( col ); + } else if( key == "environment.fog" ) { + FastColour col = ParseWomColour( value, WorldEnv.DefaultFogColour ); + game.World.Env.SetFogColour( col ); + } else if( key == "environment.level" ) { + int waterLevel = 0; + if( Int32.TryParse( value, out waterLevel ) ) + game.World.Env.SetEdgeLevel( waterLevel ); + } else if( key == "user.detail" && !cpe.useMessageTypes ) { + game.Chat.Add( value, MessageType.Status2 ); } } } - void ReadWomConfigurationAsync() { + void ReadWomConfigAsync() { string host = ServerMotd.Substring( ServerMotd.IndexOf( "cfg=" ) + 4 ); string url = "http://" + host; url = url.Replace( "$U", game.Username ); + // NOTE: this (should, I did test this) ensure that if the user quickly changes to a // different world, the environment settings from the last world are not loaded in the // new world if the async 'get request' didn't complete before the new world was loaded. @@ -70,5 +67,23 @@ namespace ClassicalSharp.Network { int argb; return Int32.TryParse( value, out argb ) ? new FastColour( argb ) : defaultCol; } + + static string ReadLine(ref int start, string value) { + if (start == -1) return null; + for (int i = start; i < value.Length; i++) { + char c = value[i]; + if (c != '\r' && c != '\n')) continue; + + string line = value.Substring(start, i - start); + start = i + 1; + if (c == '\r' && start < value.Length && value[start] == '\n') + start++; + return line; + } + + string last = value.Substring(start); + start = -1; + return last; + } } } \ No newline at end of file diff --git a/ClassicalSharp/Network/NetworkProcessor.cs b/ClassicalSharp/Network/NetworkProcessor.cs index 93752c2fb..8dcc3a0d0 100644 --- a/ClassicalSharp/Network/NetworkProcessor.cs +++ b/ClassicalSharp/Network/NetworkProcessor.cs @@ -67,7 +67,7 @@ namespace ClassicalSharp.Network { public override void Tick( ScheduledTask task ) { if( Disconnected ) return; - if( (DateTime.UtcNow - lastPacket).TotalSeconds >= 20 ) + if( (DateTime.UtcNow - lastPacket).TotalSeconds >= 30 ) CheckDisconnection( task.Interval ); if( Disconnected ) return;