Direct connection support

This commit is contained in:
twoischnig committed 2023-12-04 01:05:29 +01:00
1 parent a99b097a83
commit fc25760c2f
12 files changed
+397 -40

No files matched your search

+20 -6
View File
@@ -3,6 +3,7 @@ using Lobbies;
using LobbyClientTest;
using LobbyServerDto;
using System.Net;
using System.Net.WebSockets;
Console.WriteLine("Starting lobby client v0.7!");
var lobbyClient = new LobbyClient();
@@ -16,12 +17,11 @@ FakeGameHost fakeGameHost = new FakeGameHost();
int myPort = fakeGameHost.Server(0);
string? myExternalIp = null;
int myExternalPort = -1;
IPEndPoint? hostInfo = null;
bool running = true;
bool connected = false;
_ = Task.Run(() =>
_ = Task.Run(async () =>
{
while (running)
{
@@ -78,8 +78,23 @@ _ = Task.Run(() =>
var lobbyHostInfo = lobbyEvent.EventData as LobbyHostInfo;
var p = Console.GetCursorPosition();
Console.SetCursorPosition(0, p.Top);
Console.WriteLine($"Host info for lobby {lobbyHostInfo!.LobbyId} is {lobbyHostInfo.HostIp}:{lobbyHostInfo.HostPort}!");
hostInfo = new IPEndPoint(IPAddress.Parse(lobbyHostInfo.HostIp!), lobbyHostInfo.HostPort);
Console.WriteLine($"Host info for lobby {lobbyHostInfo!.LobbyId} is {(lobbyHostInfo.HostIps != null && lobbyHostInfo.HostIps.Length > 0 ? lobbyHostInfo.HostIps[0].ToString() : "")}:{lobbyHostInfo.HostPort}!");
//Try direct connection
if (lobbyHostInfo.HostIps != null && lobbyHostInfo.HostIps.Length > 0)
{
Console.WriteLine($"Trying direct connection to {string.Join<IPAddress>(",", lobbyHostInfo.HostIps)} on port {lobbyHostInfo.HostTryPort}!");
var reachableIp = await lobbyClient.TryDirectConnection(lobbyHostInfo.HostIps, lobbyHostInfo.HostTryPort);
if(reachableIp != null)
{
Console.WriteLine($"Direct connection to {reachableIp.ToString()} possible, using direct connection!");
Console.WriteLine($"Connecting game client!");
fakeGameHost.Send(new IPEndPoint(reachableIp, lobbyHostInfo.HostPort), "Hello from Game Client!");
Console.Write(">");
continue;
}
}
Console.WriteLine($"Requesting nat punch to me!");
lobbyClient.RequestLobbyNatPunch(lobbyHostInfo.LobbyId, null, (remoteEndpoint, messageBuffer, messageLength) => {
fakeGameHost.Send(remoteEndpoint, messageBuffer, messageLength);
@@ -111,7 +126,7 @@ _ = Task.Run(() =>
Console.SetCursorPosition(0, p.Top);
Console.WriteLine($"Nat punch requested to {lobbyRequestNatPunch!.ClientIp}:{lobbyRequestNatPunch.ClientPort}!");
Task.Run(() =>
_ = Task.Run(() =>
{
lobbyClient.QueryExternalIpAndPort((remoteEndpoint, messageData, messageLength) => {
fakeGameHost.Send(remoteEndpoint, messageData, messageLength);
@@ -140,7 +155,6 @@ _ = Task.Run(() =>
Console.SetCursorPosition(0, p.Top);
Console.WriteLine($"Nat punch request done!");
Console.WriteLine($"Connecting game client!");
fakeGameHost.Send(new IPEndPoint(IPAddress.Parse(lobbyNatPunchDone!.ExternalIp!), lobbyNatPunchDone.ExternalPort), "Hello from Game Client!");
Console.Write(">");
}