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;
byte[] mapSize = new byte[4], map;
FixedBufferStream gzippedMap;
DateTime lastPing;
void HandleHandshake() {
byte protocolVer = reader.ReadUInt8();
@ -79,6 +80,7 @@ namespace ClassicalSharp {
}
void HandlePing() {
lastPing = DateTime.UtcNow;
}
void HandleLevelInit() {

View File

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