Fixing eventflow

This commit is contained in:
twoischnig committed 2023-12-04 01:32:13 +01:00
1 parent fc25760c2f
commit 3c17ee228b
7 files changed
+80 -29

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
using System.Net;
namespace Lobbies
{
public class DirectConnectionTestResult
{
/// <summary>
/// True if a direct connection was possible
/// </summary>
public bool DirectConnectionPossible { get; internal set; }
/// <summary>
/// The ip address the connection succeeded on
/// </summary>
public IPAddress? IPAddress { get; internal set; }
}
}
+11 -4
View File
@@ -216,16 +216,23 @@ namespace Lobbies
_ = Task.Run(async () => { await tcpClient.Send(messageData, 0, len); bufferRental.Return(messageData); });
}
public async Task<IPAddress?> TryDirectConnection(IPAddress[] ipAddressesToTry, int tryPort)
public Task TryDirectConnection(IPAddress[] ipAddressesToTry, int tryPort)
{
return await Task.Run(() =>
return Task.Run(() =>
{
IPAddress? ret = null;
using(var waitForIpEvent = new AutoResetEvent(false))
using (var udpEchoClient = new UdpEchoServer())
{
udpEchoClient.Reached += (ep) =>
{
ret = ep.Address;
try
{
waitForIpEvent.Set();
udpEchoClient.Stop();
}
catch { }
};
udpEchoClient.Start(0);
@@ -235,10 +242,10 @@ namespace Lobbies
udpEchoClient.CheckConnectionPossible(new IPEndPoint(ip, tryPort));
}
Thread.Sleep(500);
waitForIpEvent.WaitOne(500);
}
return ret;
events.Enqueue(new LobbyClientEvent { EventType = LobbyClientEventTypes.DirectConnectionTestComplete, EventData = new DirectConnectionTestResult { DirectConnectionPossible = ret != null, IPAddress = ret } });
});
}
+5 -1
View File
@@ -59,6 +59,10 @@
/// <summary>
/// Response to a query of our external ip and port seen by the lobby server. EventData is <see cref="SeenExternalIpAndPort"/> with the clients seen external address.
/// </summary>
ExternalIpAndPort
ExternalIpAndPort,
/// <summary>
/// A direct connection test without a nat punch was testet. EventData is <see cref="DirectConnectionResultTest"/> with information if a direct connection was possible and on what address.
/// </summary>
DirectConnectionTestComplete
}
}