Skip to content

Commit

Permalink
Split packet sending for client update manager to different thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Extremelyd1 committed Jan 10, 2025
1 parent 6271a26 commit f3cf349
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
20 changes: 16 additions & 4 deletions HKMP/Networking/Client/NetClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ internal class NetClient : INetClient {
public NetClient(PacketManager packetManager) {
_packetManager = packetManager;

_updateTaskTokenSource = new CancellationTokenSource();
_dtlsClient = new DtlsClient();

_dtlsClient.DataReceivedEvent += OnReceiveData;
Expand Down Expand Up @@ -135,9 +136,6 @@ private void OnConnectFailed(ConnectFailedResult result) {
/// </summary>
/// <param name="packets">A list of raw received packets.</param>
private void OnReceiveData(byte[] buffer, int length) {
// TODO: check if this is the correct place to make this call
UpdateManager.ProcessUpdate();

var packets = PacketManager.HandleReceivedData(buffer, length, ref _leftoverData);

foreach (var packet in packets) {
Expand Down Expand Up @@ -238,6 +236,20 @@ List<AddonData> addonData
UpdateManager = new ClientUpdateManager(_dtlsClient.DtlsTransport);
// During the connection process we register the connection failed callback if we time out
UpdateManager.OnTimeout += OnConnectTimedOut;

new Thread(() => {
var cancellationToken = _updateTaskTokenSource.Token;

while (!cancellationToken.IsCancellationRequested) {
UpdateManager.ProcessUpdate();

// TODO: figure out a good way to get rid of the sleep here
// some way to signal when clients should be updated again would suffice
// also see NetServer#StartClientUpdates
Thread.Sleep(5);
}
}).Start();

UpdateManager.StartUpdates();

UpdateManager.SetLoginRequestData(username, authKey, addonData);
Expand All @@ -255,7 +267,7 @@ public void Disconnect() {
IsConnected = false;

// Request cancellation for the update task
_updateTaskTokenSource.Cancel();
_updateTaskTokenSource?.Cancel();

// Clear all client addon packet handlers, because their IDs become invalid
_packetManager.ClearClientAddonPacketHandlers();
Expand Down
14 changes: 12 additions & 2 deletions HKMP/Networking/Server/DtlsServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,13 @@ ref endPoint

ServerDatagramTransport serverDatagramTransport;
if (!_dtlsClients.TryGetValue(ipEndPoint, out var dtlsServerClient)) {
Logger.Debug("Received data on server socket from unknown IP");
Logger.Debug($"Received data on server socket from unknown IP ({ipEndPoint}), length: {numReceived}");

serverDatagramTransport = _currentDatagramTransport;
// Set the IP endpoint of the datagram transport instance so it can send data to the correct IP
serverDatagramTransport.IPEndPoint = ipEndPoint;
} else {
Logger.Debug($"Received data on server socket from existing client ({ipEndPoint}), length: {numReceived}");
serverDatagramTransport = dtlsServerClient.DatagramTransport;
}

Expand Down Expand Up @@ -164,8 +165,17 @@ private void ClientReceiveLoop(CancellationToken cancellationToken, DtlsServerCl
var buffer = new byte[dtlsTransport.GetReceiveLimit()];

var numReceived = dtlsTransport.Receive(buffer, 0, dtlsTransport.GetReceiveLimit(), 5);
if (numReceived <= 0) {
continue;
}

DataReceivedEvent?.Invoke(dtlsServerClient, buffer, numReceived);
Logger.Debug($"DtlsServerClient ({dtlsServerClient.EndPoint}) received {numReceived} bytes of data, invoking event");

try {
DataReceivedEvent?.Invoke(dtlsServerClient, buffer, numReceived);
} catch (Exception e) {
Logger.Error($"Error occurred while invoking DataReceivedEvent:\n{e}");
}
}
}
}

0 comments on commit f3cf349

Please sign in to comment.