Don't immediately disconnect on first failed write, thus ensuring all kick messages are shown.

This commit is contained in:
UnknownShadow200 2015-12-28 23:23:42 +11:00
parent 09757d6316
commit 140ed90269
2 changed files with 15 additions and 6 deletions

View File

@ -68,6 +68,7 @@ namespace ClassicalSharp {
int mapSizeIndex, mapIndex; int mapSizeIndex, mapIndex;
byte[] mapSize = new byte[4], map; byte[] mapSize = new byte[4], map;
FixedBufferStream gzippedMap; FixedBufferStream gzippedMap;
DateTime lastPing;
void HandleHandshake() { void HandleHandshake() {
byte protocolVer = reader.ReadUInt8(); byte protocolVer = reader.ReadUInt8();
@ -77,8 +78,9 @@ namespace ClassicalSharp {
receivedFirstPosition = false; receivedFirstPosition = false;
game.LocalPlayer.ParseHackFlags( ServerName, ServerMotd ); game.LocalPlayer.ParseHackFlags( ServerName, ServerMotd );
} }
void HandlePing() { void HandlePing() {
lastPing = DateTime.UtcNow;
} }
void HandleLevelInit() { void HandleLevelInit() {

View File

@ -50,6 +50,7 @@ namespace ClassicalSharp {
MakeLoginPacket( game.Username, game.Mppass ); MakeLoginPacket( game.Username, game.Mppass );
SendPacket(); SendPacket();
lastPing = DateTime.UtcNow;
} }
public override void Dispose() { public override void Dispose() {
@ -101,8 +102,7 @@ namespace ClassicalSharp {
bool Is304Status( WebException ex ) { bool Is304Status( WebException ex ) {
if( ex == null || ex.Status != WebExceptionStatus.ProtocolError ) if( ex == null || ex.Status != WebExceptionStatus.ProtocolError )
return false; return false;
HttpWebResponse response = (HttpWebResponse)ex.Response; HttpWebResponse response = (HttpWebResponse)ex.Response;
return response.StatusCode == HttpStatusCode.NotModified; return response.StatusCode == HttpStatusCode.NotModified;
} }
@ -110,6 +110,12 @@ namespace ClassicalSharp {
public override void Tick( double delta ) { public override void Tick( double delta ) {
if( Disconnected ) return; if( Disconnected ) return;
if( (DateTime.UtcNow - lastPing).TotalSeconds >= 15 ) {
game.Disconnect( "&eDisconnected from the server",
"No ping packet received for over 15 seconds." );
Dispose();
return;
}
try { try {
reader.ReadPendingData(); reader.ReadPendingData();
@ -164,9 +170,10 @@ namespace ClassicalSharp {
try { try {
writer.Send(); writer.Send();
} catch( IOException ex ) { } catch( IOException ex ) {
ErrorHandler.LogError( "writing packets", ex ); // NOTE: Not immediately disconnecting, because it means we miss out on kick messages sometimes.
game.Disconnect( "&eLost connection to the server", "I/O Error while writing packets" ); //ErrorHandler.LogError( "writing packets", ex );
Dispose(); //game.Disconnect( "&eLost connection to the server", "I/O Error while writing packets" );
//Dispose();
writer.index = 0; writer.index = 0;
} }
} }