Fixing eventflow

main
Thomas Woischnig 2023-12-04 01:32:13 +01:00
parent fc25760c2f
commit 3c17ee228b
7 changed files with 80 additions and 29 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "com.incobyte.lobbyclient", "name": "com.incobyte.lobbyclient",
"version": "1.0.0", "version": "1.0.1",
"displayName": "Game Lobby Client", "displayName": "Game Lobby Client",
"description": "Provides a client for the game lobvy server to list and join lobbies", "description": "Provides a client for the game lobvy server to list and join lobbies",
"unity": "2022.3", "unity": "2022.3",

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; }
}
}

View File

@ -216,16 +216,23 @@ namespace Lobbies
_ = Task.Run(async () => { await tcpClient.Send(messageData, 0, len); bufferRental.Return(messageData); }); _ = 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; IPAddress? ret = null;
using(var waitForIpEvent = new AutoResetEvent(false))
using (var udpEchoClient = new UdpEchoServer()) using (var udpEchoClient = new UdpEchoServer())
{ {
udpEchoClient.Reached += (ep) => udpEchoClient.Reached += (ep) =>
{ {
ret = ep.Address; ret = ep.Address;
try
{
waitForIpEvent.Set();
udpEchoClient.Stop();
}
catch { }
}; };
udpEchoClient.Start(0); udpEchoClient.Start(0);
@ -235,10 +242,10 @@ namespace Lobbies
udpEchoClient.CheckConnectionPossible(new IPEndPoint(ip, tryPort)); 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 } });
}); });
} }

View File

@ -59,6 +59,10 @@
/// <summary> /// <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. /// 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> /// </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
} }
} }

View File

@ -10,7 +10,7 @@ var lobbyClient = new LobbyClient();
var cancellationTokenSource = new CancellationTokenSource(); var cancellationTokenSource = new CancellationTokenSource();
List<LobbyInfo> openLobbies = new List<LobbyInfo>(); List<LobbyInfo> openLobbies = new List<LobbyInfo>();
LobbyHostInfo? currentLobbyHostInfo = null;
lobbyClient.Connect("lobby.incobyte.de" /*"localhost"*/, 8088, cancellationTokenSource.Token); lobbyClient.Connect("lobby.incobyte.de" /*"localhost"*/, 8088, cancellationTokenSource.Token);
FakeGameHost fakeGameHost = new FakeGameHost(); FakeGameHost fakeGameHost = new FakeGameHost();
@ -73,9 +73,32 @@ _ = Task.Run(async () =>
running = false; running = false;
} }
break; break;
case LobbyClientEventTypes.DirectConnectionTestComplete:
var result = lobbyEvent.EventData as DirectConnectionTestResult;
if (result != null && result.DirectConnectionPossible)
{
Console.WriteLine($"Direct connection to {result.IPAddress!.ToString()} possible, using direct connection!");
Console.WriteLine($"Connecting game client!");
fakeGameHost.Send(new IPEndPoint(result.IPAddress!, currentLobbyHostInfo!.HostPort), "Hello from Game Client!");
Console.Write(">");
}
else
{
Console.WriteLine($"Direct connection no route found!");
Console.WriteLine($"Requesting nat punch to me!");
lobbyClient.RequestLobbyNatPunch(currentLobbyHostInfo!.LobbyId, null, (remoteEndpoint, messageBuffer, messageLength) =>
{
fakeGameHost.Send(remoteEndpoint, messageBuffer, messageLength);
});
Console.Write(">");
}
break;
case LobbyClientEventTypes.LobbyHostInfo: case LobbyClientEventTypes.LobbyHostInfo:
{ {
var lobbyHostInfo = lobbyEvent.EventData as LobbyHostInfo; var lobbyHostInfo = lobbyEvent.EventData as LobbyHostInfo;
currentLobbyHostInfo = lobbyHostInfo;
var p = Console.GetCursorPosition(); var p = Console.GetCursorPosition();
Console.SetCursorPosition(0, p.Top); Console.SetCursorPosition(0, p.Top);
Console.WriteLine($"Host info for lobby {lobbyHostInfo!.LobbyId} is {(lobbyHostInfo.HostIps != null && lobbyHostInfo.HostIps.Length > 0 ? lobbyHostInfo.HostIps[0].ToString() : "")}:{lobbyHostInfo.HostPort}!"); Console.WriteLine($"Host info for lobby {lobbyHostInfo!.LobbyId} is {(lobbyHostInfo.HostIps != null && lobbyHostInfo.HostIps.Length > 0 ? lobbyHostInfo.HostIps[0].ToString() : "")}:{lobbyHostInfo.HostPort}!");
@ -83,23 +106,18 @@ _ = Task.Run(async () =>
//Try direct connection //Try direct connection
if (lobbyHostInfo.HostIps != null && lobbyHostInfo.HostIps.Length > 0) if (lobbyHostInfo.HostIps != null && lobbyHostInfo.HostIps.Length > 0)
{ {
Console.WriteLine($"Trying direct connection to {string.Join<IPAddress>(",", lobbyHostInfo.HostIps)} on port {lobbyHostInfo.HostTryPort}!"); Console.WriteLine($"Trying direct connection to {string.Join<IPAddress>(",", lobbyHostInfo.HostIps)} on port {lobbyHostInfo.HostTryPort}!");
var reachableIp = await lobbyClient.TryDirectConnection(lobbyHostInfo.HostIps, lobbyHostInfo.HostTryPort); _ = lobbyClient.TryDirectConnection(lobbyHostInfo.HostIps, lobbyHostInfo.HostTryPort);
if(reachableIp != null) }
{ else
Console.WriteLine($"Direct connection to {reachableIp.ToString()} possible, using direct connection!"); {
Console.WriteLine($"Connecting game client!"); Console.WriteLine($"Requesting nat punch to me!");
fakeGameHost.Send(new IPEndPoint(reachableIp, lobbyHostInfo.HostPort), "Hello from Game Client!"); lobbyClient.RequestLobbyNatPunch(lobbyHostInfo.LobbyId, null, (remoteEndpoint, messageBuffer, messageLength) =>
Console.Write(">"); {
continue; fakeGameHost.Send(remoteEndpoint, messageBuffer, messageLength);
} });
Console.Write(">");
} }
Console.WriteLine($"Requesting nat punch to me!");
lobbyClient.RequestLobbyNatPunch(lobbyHostInfo.LobbyId, null, (remoteEndpoint, messageBuffer, messageLength) => {
fakeGameHost.Send(remoteEndpoint, messageBuffer, messageLength);
});
Console.Write(">");
} }
break; break;
case LobbyClientEventTypes.ExternalIpAndPort: case LobbyClientEventTypes.ExternalIpAndPort:

View File

@ -13,6 +13,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LobbyServerDto", "LobbyServ
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LobbyServerSourceGenerator", "LobbyServerSourceGenerator\LobbyServerSourceGenerator.csproj", "{04F95131-C7EF-410B-94E5-2D9162763155}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LobbyServerSourceGenerator", "LobbyServerSourceGenerator\LobbyServerSourceGenerator.csproj", "{04F95131-C7EF-410B-94E5-2D9162763155}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Projektmappenelemente", "Projektmappenelemente", "{F27779F1-9CE7-4642-9111-E6B96331C2DB}"
ProjectSection(SolutionItems) = preProject
Assets\NetworkLobbyClient\package.json = Assets\NetworkLobbyClient\package.json
EndProjectSection
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU