Improved buffering slightly.
Disconnect messages are no longer buffered. (Fixes disconnect messages not being received)
This commit is contained in:
parent
7b58100546
commit
dcadec30de
6 changed files with 123 additions and 21 deletions
|
|
@ -40,6 +40,7 @@ using Terraria;
|
|||
using TerrariaAPI;
|
||||
using TerrariaAPI.Hooks;
|
||||
using TShockAPI.DB;
|
||||
using TShockAPI.Net;
|
||||
|
||||
namespace TShockAPI
|
||||
{
|
||||
|
|
@ -179,6 +180,7 @@ namespace TShockAPI
|
|||
ServerHooks.Chat += OnChat;
|
||||
ServerHooks.Command += ServerHooks_OnCommand;
|
||||
NetHooks.GetData += OnGetData;
|
||||
NetHooks.SendData += NetHooks_SendData;
|
||||
NetHooks.GreetPlayer += OnGreetPlayer;
|
||||
NpcHooks.StrikeNpc += NpcHooks_OnStrikeNpc;
|
||||
|
||||
|
|
@ -212,6 +214,7 @@ namespace TShockAPI
|
|||
ServerHooks.Chat -= OnChat;
|
||||
ServerHooks.Command -= ServerHooks_OnCommand;
|
||||
NetHooks.GetData -= OnGetData;
|
||||
NetHooks.SendData -= NetHooks_SendData;
|
||||
NetHooks.GreetPlayer -= OnGreetPlayer;
|
||||
NpcHooks.StrikeNpc -= NpcHooks_OnStrikeNpc;
|
||||
if (File.Exists(Path.Combine(SavePath, "tshock.pid")))
|
||||
|
|
@ -556,6 +559,9 @@ namespace TShockAPI
|
|||
return;
|
||||
|
||||
PacketTypes type = e.MsgID;
|
||||
|
||||
Debug.WriteLine("Recv: {0:X}: {2} ({1:XX})", e.Msg.whoAmI, (byte)type, type);
|
||||
|
||||
var player = Players[e.Msg.whoAmI];
|
||||
if (player == null)
|
||||
{
|
||||
|
|
@ -569,7 +575,7 @@ namespace TShockAPI
|
|||
return;
|
||||
}
|
||||
|
||||
//Debug.WriteLine("Recv: {0:X} ({2}): {3} ({1:XX})", player.Index, (byte)type, player.TPlayer.dead ? "dead " : "alive", type);
|
||||
|
||||
|
||||
// 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) &&
|
||||
|
|
@ -656,10 +662,20 @@ namespace TShockAPI
|
|||
{
|
||||
if (PacketBuffer != null)
|
||||
{
|
||||
PacketBuffer.SendBytes(client, bytes);
|
||||
PacketBuffer.BufferBytes(client, bytes);
|
||||
return true;
|
||||
}
|
||||
|
||||
return SendBytesBufferless(client,bytes);
|
||||
}
|
||||
/// <summary>
|
||||
/// Send bytes to a client ignoring the packet buffer
|
||||
/// </summary>
|
||||
/// <param name="client">socket to send to</param>
|
||||
/// <param name="bytes">bytes to send</param>
|
||||
/// <returns>False on exception</returns>
|
||||
public static bool SendBytesBufferless(ServerSock client, byte[] bytes)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (client.tcpClient.Connected)
|
||||
|
|
@ -668,11 +684,44 @@ namespace TShockAPI
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Warn("This is a normal exception");
|
||||
Log.Warn(ex.ToString());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void NetHooks_SendData(SendDataEventArgs e)
|
||||
{
|
||||
if (e.MsgID == PacketTypes.Disconnect)
|
||||
{
|
||||
Action<ServerSock,string> senddisconnect = (sock, str) =>
|
||||
{
|
||||
if (sock == null || !sock.active)
|
||||
return;
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
new DisconnectMsg {Reason = str}.PackFull(ms);
|
||||
SendBytesBufferless(sock, ms.ToArray());
|
||||
}
|
||||
};
|
||||
|
||||
if (e.remoteClient != -1)
|
||||
{
|
||||
senddisconnect(Netplay.serverSock[e.remoteClient], e.text);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < Netplay.serverSock.Length; i++)
|
||||
{
|
||||
if (e.ignoreClient != -1 && e.ignoreClient == i)
|
||||
continue;
|
||||
|
||||
senddisconnect(Netplay.serverSock[i], e.text);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnSaveWorld(bool resettime, HandledEventArgs e)
|
||||
{
|
||||
Tools.Broadcast("Saving world. Momentary lag might result from this.", Color.Red);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue