Added packet buffering
This commit is contained in:
parent
b9cb3e69e1
commit
1d042fccaa
5 changed files with 120 additions and 6 deletions
|
|
@ -110,6 +110,8 @@ namespace TShockAPI
|
|||
/// </summary>
|
||||
public string HashAlgorithm = "sha512";
|
||||
|
||||
public bool BufferPackets = false;
|
||||
|
||||
public static ConfigFile Read(string path)
|
||||
{
|
||||
if (!File.Exists(path))
|
||||
|
|
|
|||
19
TShockAPI/LinqExt.cs
Normal file
19
TShockAPI/LinqExt.cs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace TShockAPI
|
||||
{
|
||||
public static class LinqExt
|
||||
{
|
||||
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
|
||||
{
|
||||
if (source == null) throw new ArgumentNullException("source");
|
||||
if (action == null) throw new ArgumentNullException("action");
|
||||
|
||||
foreach (T item in source)
|
||||
action(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
92
TShockAPI/PacketBufferer.cs
Normal file
92
TShockAPI/PacketBufferer.cs
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Terraria;
|
||||
using TerrariaAPI.Hooks;
|
||||
|
||||
namespace TShockAPI
|
||||
{
|
||||
public class PacketBufferer : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Maximum number of bytes to send per update per socket
|
||||
/// </summary>
|
||||
public int BytesPerUpdate { get; set; }
|
||||
|
||||
PacketBuffer[] buffers = new PacketBuffer[Netplay.serverSock.Length];
|
||||
|
||||
public PacketBufferer()
|
||||
{
|
||||
BytesPerUpdate = 0xFFFF;
|
||||
buffers.ForEach(p => p = new PacketBuffer());
|
||||
|
||||
ServerHooks.SendBytes += ServerHooks_SendBytes;
|
||||
ServerHooks.SocketReset += ServerHooks_SocketReset;
|
||||
GameHooks.PostUpdate += GameHooks_Update;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
GameHooks.PostUpdate -= GameHooks_Update;
|
||||
ServerHooks.SendBytes -= ServerHooks_SendBytes;
|
||||
ServerHooks.SocketReset -= ServerHooks_SocketReset;
|
||||
}
|
||||
|
||||
|
||||
void GameHooks_Update(GameTime obj)
|
||||
{
|
||||
for (int i = 0; i < Netplay.serverSock.Length; i++)
|
||||
{
|
||||
if (Netplay.serverSock[i] == null || !Netplay.serverSock[i].active)
|
||||
continue;
|
||||
|
||||
if (!Netplay.serverSock[i].tcpClient.Client.Poll(0, SelectMode.SelectWrite))
|
||||
continue;
|
||||
|
||||
byte[] buff = buffers[i].GetBytes(BytesPerUpdate);
|
||||
Netplay.serverSock[i].networkStream.Write(buff, 0, buff.Length);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ServerHooks_SocketReset(ServerSock socket)
|
||||
{
|
||||
buffers[socket.whoAmI] = new PacketBuffer();
|
||||
}
|
||||
|
||||
void ServerHooks_SendBytes(ServerSock socket, byte[] buffer, int offset, int count, HandledEventArgs e)
|
||||
{
|
||||
e.Handled = true;
|
||||
lock (buffers[socket.whoAmI])
|
||||
{
|
||||
buffers[socket.whoAmI].AddRange(new MemoryStream(buffer, offset, count).ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public class PacketBuffer : List<byte>
|
||||
{
|
||||
public byte[] GetBytes(int max)
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
if (this.Count < 1)
|
||||
return null;
|
||||
|
||||
var ret = new byte[Math.Min(max, this.Count)];
|
||||
this.CopyTo(0, ret, 0, ret.Length);
|
||||
this.RemoveRange(0, ret.Length);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -61,15 +61,11 @@ namespace TShockAPI
|
|||
public static UserManager Users;
|
||||
public static ItemManager Itembans;
|
||||
public static RemeberedPosManager RememberedPos;
|
||||
|
||||
public static ConfigFile Config { get; set; }
|
||||
|
||||
public static IDbConnection DB;
|
||||
|
||||
public static Process TShockProcess;
|
||||
public static bool OverridePort;
|
||||
PacketBufferer bufferer;
|
||||
|
||||
public static double ElapsedTime;
|
||||
|
||||
public override Version Version
|
||||
{
|
||||
|
|
@ -186,6 +182,9 @@ namespace TShockAPI
|
|||
Commands.InitCommands();
|
||||
//RconHandler.StartThread();
|
||||
|
||||
if (Config.BufferPackets)
|
||||
bufferer = new PacketBufferer();
|
||||
|
||||
Log.ConsoleInfo("AutoSave " + (Config.AutoSave ? "Enabled" : "Disabled"));
|
||||
Log.ConsoleInfo("Backups " + (Backups.Interval > 0 ? "Enabled" : "Disabled"));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -104,10 +104,12 @@
|
|||
<Compile Include="FileTools.cs" />
|
||||
<Compile Include="GetDataHandlers.cs" />
|
||||
<Compile Include="Group.cs" />
|
||||
<Compile Include="LinqExt.cs" />
|
||||
<Compile Include="Log.cs" />
|
||||
<Compile Include="Net\NetTile.cs" />
|
||||
<Compile Include="Net\WorldInfoMsg.cs" />
|
||||
<Compile Include="DB\RegionManager.cs" />
|
||||
<Compile Include="PacketBufferer.cs" />
|
||||
<Compile Include="RconHandler.cs" />
|
||||
<Compile Include="DB\RememberPosManager.cs" />
|
||||
<Compile Include="Resources.Designer.cs">
|
||||
|
|
@ -171,7 +173,7 @@
|
|||
</PropertyGroup>
|
||||
<ProjectExtensions>
|
||||
<VisualStudio>
|
||||
<UserProperties BuildVersion_UpdateAssemblyVersion="True" BuildVersion_UpdateFileVersion="True" BuildVersion_BuildAction="Both" BuildVersion_BuildVersioningStyle="None.None.None.MonthAndDayStamp" BuildVersion_StartDate="2011/6/17" BuildVersion_IncrementBeforeBuild="False" />
|
||||
<UserProperties BuildVersion_IncrementBeforeBuild="False" BuildVersion_StartDate="2011/6/17" BuildVersion_BuildVersioningStyle="None.None.None.MonthAndDayStamp" BuildVersion_BuildAction="Both" BuildVersion_UpdateFileVersion="True" BuildVersion_UpdateAssemblyVersion="True" />
|
||||
</VisualStudio>
|
||||
</ProjectExtensions>
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue