Fixed issue of the client being killed before receiving the disconnect message.

This commit is contained in:
high 2011-08-17 21:45:41 -04:00
parent 4dae27001c
commit 7b58100546
3 changed files with 55 additions and 29 deletions

View file

@ -91,22 +91,35 @@ namespace TShockAPI
} }
void GameHooks_Update(GameTime obj) void GameHooks_Update(GameTime obj)
{
FlushAll();
}
public void FlushAll()
{ {
for (int i = 0; i < Netplay.serverSock.Length; i++) for (int i = 0; i < Netplay.serverSock.Length; i++)
{ {
if (Netplay.serverSock[i] == null || !Netplay.serverSock[i].active) Flush(Netplay.serverSock[i]);
continue; }
}
if (!Netplay.serverSock[i].tcpClient.Client.Poll(0, SelectMode.SelectWrite))
continue;
byte[] buff = buffers[i].GetBytes(BytesPerUpdate);
if (buff == null)
continue;
public bool Flush(ServerSock socket)
{
try try
{ {
Netplay.serverSock[i].tcpClient.Client.Send(buff); if (socket == null || !socket.active)
return false;
if (!socket.tcpClient.Client.Poll(0, SelectMode.SelectWrite))
return false;
byte[] buff = buffers[socket.whoAmI].GetBytes(BytesPerUpdate);
if (buff == null)
return false;
socket.tcpClient.Client.Send(buff);
return true;
} }
catch (ObjectDisposedException) catch (ObjectDisposedException)
{ {
@ -114,7 +127,11 @@ namespace TShockAPI
catch (SocketException) catch (SocketException)
{ {
} }
catch (Exception e)
{
Log.ConsoleError(e.ToString());
} }
return false;
} }

View file

@ -156,6 +156,16 @@ namespace TShockAPI
public virtual void Disconnect(string reason) public virtual void Disconnect(string reason)
{ {
SendData(PacketTypes.Disconnect, reason); SendData(PacketTypes.Disconnect, reason);
Flush();
}
public virtual void Flush()
{
var sock = Netplay.serverSock[Index];
if (sock == null)
return;
TShock.PacketBuffer.Flush(sock);
} }

View file

@ -63,7 +63,7 @@ namespace TShockAPI
public static ConfigFile Config { get; set; } public static ConfigFile Config { get; set; }
public static IDbConnection DB; public static IDbConnection DB;
public static bool OverridePort; public static bool OverridePort;
static PacketBufferer bufferer; public static PacketBufferer PacketBuffer;
/// <summary> /// <summary>
/// Called after TShock is initialized. Useful for plugins that needs hooks before tshock but also depend on tshock being loaded. /// Called after TShock is initialized. Useful for plugins that needs hooks before tshock but also depend on tshock being loaded.
@ -178,7 +178,7 @@ namespace TShockAPI
ServerHooks.Leave += OnLeave; ServerHooks.Leave += OnLeave;
ServerHooks.Chat += OnChat; ServerHooks.Chat += OnChat;
ServerHooks.Command += ServerHooks_OnCommand; ServerHooks.Command += ServerHooks_OnCommand;
NetHooks.GetData += GetData; NetHooks.GetData += OnGetData;
NetHooks.GreetPlayer += OnGreetPlayer; NetHooks.GreetPlayer += OnGreetPlayer;
NpcHooks.StrikeNpc += NpcHooks_OnStrikeNpc; NpcHooks.StrikeNpc += NpcHooks_OnStrikeNpc;
@ -187,7 +187,7 @@ namespace TShockAPI
//RconHandler.StartThread(); //RconHandler.StartThread();
if (Config.BufferPackets) if (Config.BufferPackets)
bufferer = new PacketBufferer(); PacketBuffer = new PacketBufferer();
Log.ConsoleInfo("AutoSave " + (Config.AutoSave ? "Enabled" : "Disabled")); Log.ConsoleInfo("AutoSave " + (Config.AutoSave ? "Enabled" : "Disabled"));
Log.ConsoleInfo("Backups " + (Backups.Interval > 0 ? "Enabled" : "Disabled")); Log.ConsoleInfo("Backups " + (Backups.Interval > 0 ? "Enabled" : "Disabled"));
@ -211,7 +211,7 @@ namespace TShockAPI
ServerHooks.Leave -= OnLeave; ServerHooks.Leave -= OnLeave;
ServerHooks.Chat -= OnChat; ServerHooks.Chat -= OnChat;
ServerHooks.Command -= ServerHooks_OnCommand; ServerHooks.Command -= ServerHooks_OnCommand;
NetHooks.GetData -= GetData; NetHooks.GetData -= OnGetData;
NetHooks.GreetPlayer -= OnGreetPlayer; NetHooks.GreetPlayer -= OnGreetPlayer;
NpcHooks.StrikeNpc -= NpcHooks_OnStrikeNpc; NpcHooks.StrikeNpc -= NpcHooks_OnStrikeNpc;
if (File.Exists(Path.Combine(SavePath, "tshock.pid"))) if (File.Exists(Path.Combine(SavePath, "tshock.pid")))
@ -550,7 +550,7 @@ namespace TShockAPI
} }
} }
private void GetData(GetDataEventArgs e) private void OnGetData(GetDataEventArgs e)
{ {
if (e.Handled) if (e.Handled)
return; return;
@ -569,8 +569,7 @@ namespace TShockAPI
return; return;
} }
//if (type == PacketTypes.SyncPlayers) //Debug.WriteLine("Recv: {0:X} ({2}): {3} ({1:XX})", player.Index, (byte)type, player.TPlayer.dead ? "dead " : "alive", type);
//Debug.WriteLine("Recv: {0:X} ({2}): {3} ({1:XX})", player.Index, (byte)type, player.TPlayer.dead ? "dead " : "alive", type.ToString());
// Stop accepting updates from player as this player is going to be kicked/banned during OnUpdate (different thread so can produce race conditions) // Stop accepting updates from player as this player is going to be kicked/banned during OnUpdate (different thread so can produce race conditions)
if ((Config.BanKillTileAbusers || Config.KickKillTileAbusers) && if ((Config.BanKillTileAbusers || Config.KickKillTileAbusers) &&
@ -655,9 +654,9 @@ namespace TShockAPI
/// <returns>False on exception</returns> /// <returns>False on exception</returns>
public static bool SendBytes(ServerSock client, byte[] bytes) public static bool SendBytes(ServerSock client, byte[] bytes)
{ {
if (bufferer != null) if (PacketBuffer != null)
{ {
bufferer.SendBytes(client, bytes); PacketBuffer.SendBytes(client, bytes);
return true; return true;
} }