Skip to content

Commit

Permalink
Fix MacOS networking issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Extremelyd1 committed Dec 13, 2022
1 parent bb02b90 commit f1f1935
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
16 changes: 6 additions & 10 deletions HKMP/Networking/Client/ClientUpdateManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using Hkmp.Animation;
using Hkmp.Game;
Expand All @@ -14,23 +13,20 @@ namespace Hkmp.Networking.Client {
/// Specialization of <see cref="UdpUpdateManager{TOutgoing,TPacketId}"/> for client to server packet sending.
/// </summary>
internal class ClientUpdateManager : UdpUpdateManager<ServerUpdatePacket, ServerPacketId> {
/// <summary>
/// The endpoint of the client.
/// </summary>
private readonly IPEndPoint _endPoint;

/// <summary>
/// Construct the update manager with a UDP net client.
/// </summary>
/// <param name="udpSocket">The UDP socket for the local client.</param>
/// <param name="endPoint">The endpoint of the server.</param>
public ClientUpdateManager(Socket udpSocket, IPEndPoint endPoint) : base(udpSocket) {
_endPoint = endPoint;
public ClientUpdateManager(Socket udpSocket) : base(udpSocket) {
}

/// <inheritdoc />
protected override void SendPacket(Packet.Packet packet) {
UdpSocket.SendToAsync(new ArraySegment<byte>(packet.ToArray()), SocketFlags.None, _endPoint);
if (!UdpSocket.Connected) {
return;
}

UdpSocket?.SendAsync(new ArraySegment<byte>(packet.ToArray()), SocketFlags.None);
}

/// <inheritdoc />
Expand Down
18 changes: 7 additions & 11 deletions HKMP/Networking/Client/NetClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Hkmp.Api.Client;
Expand Down Expand Up @@ -115,7 +114,7 @@ private void OnConnectFailed(ConnectFailedResult result) {
IsConnected = false;

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

// Invoke callback if it exists on the main thread of Unity
ThreadUtil.RunActionOnMainThread(() => { ConnectFailedEvent?.Invoke(result); });
Expand Down Expand Up @@ -212,10 +211,7 @@ List<AddonData> addonData
return;
}

UpdateManager = new ClientUpdateManager(
_udpNetClient.UdpSocket,
(IPEndPoint) _udpNetClient.UdpSocket.RemoteEndPoint
);
UpdateManager = new ClientUpdateManager(_udpNetClient.UdpSocket);
// During the connection process we register the connection failed callback if we time out
UpdateManager.OnTimeout += OnConnectTimedOut;
UpdateManager.StartUpdates();
Expand All @@ -227,12 +223,12 @@ List<AddonData> addonData
new Thread(() => {
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);
}
// 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.SetLoginRequestData(username, authKey, addonData);
Expand Down
4 changes: 2 additions & 2 deletions HKMP/Networking/Client/UdpNetClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private void ReceiveData(CancellationToken token) {
ref endPoint
);
} catch (SocketException e) {
Logger.Error($"UDP Socket exception: {e.GetType()}, {e.Message}");
Logger.Error($"UDP Socket exception: {e.GetType()}, {e.Message}, {e.ErrorCode}");
}

var packets = PacketManager.HandleReceivedData(buffer, ref _leftoverData);
Expand All @@ -100,7 +100,7 @@ public void Disconnect() {
// Request cancellation of the receive thread
_receiveTokenSource.Cancel();

UdpSocket.Close();
UdpSocket?.Close();
UdpSocket = null;
}
}
Expand Down

0 comments on commit f1f1935

Please sign in to comment.