LobbyServer/LobbyClientTest/Program.cs

268 lines
12 KiB
C#

// See https://aka.ms/new-console-template for more information
using Lobbies;
using LobbyClientTest;
using LobbyServerDto;
using System.Net;
using System.Net.WebSockets;
Console.WriteLine("Starting lobby client v0.7!");
var lobbyClient = new LobbyClient();
var cancellationTokenSource = new CancellationTokenSource();
List<LobbyInfo> openLobbies = new List<LobbyInfo>();
LobbyHostInfo? currentLobbyHostInfo = null;
lobbyClient.Connect("lobby.incobyte.de" /*"localhost"*/, 8088, cancellationTokenSource.Token);
FakeGameHost fakeGameHost = new FakeGameHost();
int myPort = fakeGameHost.Server(0);
string? myExternalIp = null;
int myExternalPort = -1;
bool running = true;
bool connected = false;
_ = Task.Run(async () =>
{
while (running)
{
foreach (var lobbyEvent in lobbyClient.ReadEvents(20))
{
switch (lobbyEvent.EventType)
{
case LobbyClientEventTypes.Connected:
{
connected = true;
var p = Console.GetCursorPosition();
Console.SetCursorPosition(0, p.Top);
Console.WriteLine("Lobby client connected!");
Console.Write(">");
lobbyClient.ObserveLobbies(GameGuids.NFS);
}
break;
case LobbyClientEventTypes.LobbyAdd:
case LobbyClientEventTypes.LobbyUpdate:
{
var lobbyInfo = lobbyEvent.EventData as LobbyInfo;
openLobbies.Add(lobbyInfo!);
var p = Console.GetCursorPosition();
Console.SetCursorPosition(0, p.Top);
Console.WriteLine($"LobbyInfo: {lobbyInfo!.Id}, name: {lobbyInfo.Name}, mode: {lobbyInfo.GameMode}, maxplayercount: {lobbyInfo.MaxPlayerCount}, playercount: {lobbyInfo.PlayerCount}, password: {lobbyInfo.PasswordProtected}");
Console.Write(">");
}
break;
case LobbyClientEventTypes.LobbyDelete:
{
var lobbyDelete = lobbyEvent.EventData as LobbyDelete;
var existingLobby = openLobbies.FirstOrDefault(l => l.Id == lobbyDelete!.Id);
if (existingLobby != null)
openLobbies.Remove(existingLobby);
var p = Console.GetCursorPosition();
Console.SetCursorPosition(0, p.Top);
Console.WriteLine($"LobbyDelete: {lobbyDelete!.Id}");
Console.Write(">");
}
break;
case LobbyClientEventTypes.Failed:
{
var reason = lobbyEvent.EventData as LobbyClientDisconnectReason;
var p = Console.GetCursorPosition();
Console.SetCursorPosition(0, p.Top);
Console.WriteLine($"Lobby connection failed! WasError: {reason!.WasError}, error: {reason.ErrorMessage}");
running = false;
}
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:
{
var lobbyHostInfo = lobbyEvent.EventData as LobbyHostInfo;
currentLobbyHostInfo = lobbyHostInfo;
var p = Console.GetCursorPosition();
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}!");
//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}!");
_ = lobbyClient.TryDirectConnection(lobbyHostInfo.HostIps, lobbyHostInfo.HostTryPort);
}
else
{
Console.WriteLine($"Requesting nat punch to me!");
lobbyClient.RequestLobbyNatPunch(lobbyHostInfo.LobbyId, null, (remoteEndpoint, messageBuffer, messageLength) =>
{
fakeGameHost.Send(remoteEndpoint, messageBuffer, messageLength);
});
Console.Write(">");
}
}
break;
case LobbyClientEventTypes.ExternalIpAndPort:
{
var seenExternalIpAndPort = lobbyEvent.EventData as SeenExternalIpAndPort;
if (seenExternalIpAndPort != null)
{
myExternalIp = seenExternalIpAndPort.Ip;
myExternalPort = seenExternalIpAndPort.Port;
var p = Console.GetCursorPosition();
Console.SetCursorPosition(0, p.Top);
Console.WriteLine($"Received my external ip {seenExternalIpAndPort!.Ip}:{seenExternalIpAndPort.Port}");
Console.Write(">");
connected = true;
}
}
break;
case LobbyClientEventTypes.LobbyRequestNatPunch:
{
var lobbyRequestNatPunch = lobbyEvent.EventData as LobbyRequestNatPunch;
var p = Console.GetCursorPosition();
Console.SetCursorPosition(0, p.Top);
Console.WriteLine($"Nat punch requested to {lobbyRequestNatPunch!.ClientIp}:{lobbyRequestNatPunch.ClientPort}!");
_ = Task.Run(() =>
{
lobbyClient.QueryExternalIpAndPort((remoteEndpoint, messageData, messageLength) => {
fakeGameHost.Send(remoteEndpoint, messageData, messageLength);
});
var ep = new IPEndPoint(IPAddress.Parse(lobbyRequestNatPunch.ClientIp!), lobbyRequestNatPunch.ClientPort);
for (int z = 0; z < 32; z++)
{
fakeGameHost.Send(ep, "Nat Falcon Punch!");
}
lobbyClient.NotifyLobbyNatPunchDone(lobbyRequestNatPunch.NatPunchId, lobbyClient.externalIp!, lobbyClient.externalPort);
var p = Console.GetCursorPosition();
Console.SetCursorPosition(0, p.Top);
Console.WriteLine($"Nat punch done!");
Console.Write(">");
});
Console.Write(">");
}
break;
case LobbyClientEventTypes.LobbyNatPunchDone:
{
var lobbyNatPunchDone = lobbyEvent.EventData as LobbyNatPunchDone;
var p = Console.GetCursorPosition();
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(">");
}
break;
case LobbyClientEventTypes.Disconnected:
{
var p = Console.GetCursorPosition();
Console.SetCursorPosition(0, p.Top);
Console.WriteLine($"Lobby disonnected!");
running = false;
}
break;
}
}
Thread.Sleep(10);
}
});
while (running)
{
Console.Write(">");
var line = Console.ReadLine();
if (line != null)
{
switch (line)
{
case "host":
{
if (!connected)
{
Console.WriteLine("Not connected yet!");
}
else
{
Console.WriteLine("Hosting game ...");
lobbyClient.HostLobby(GameGuids.NFS, "Hallo, Welt!", 1, 8, null, myPort);
fakeGameHost.isHost = true;
}
}
break;
case "host stop":
{
Console.WriteLine("Stop hosting game ...");
lobbyClient.CloseLobby();
fakeGameHost.isHost = false;
}
break;
case "join":
{
if (!connected)
{
Console.WriteLine("Not connected yet!");
}
else
{
Console.WriteLine("Trying to join first lobby ...");
var firstLobby = openLobbies.FirstOrDefault();
if (firstLobby != null)
{
lobbyClient.RequestLobbyHostInfo(firstLobby.Id, null);
}
else
{
Console.WriteLine("Seeing no open lobby!");
}
}
}
break;
case "observe":
{
Console.WriteLine("Observing lobby list ...");
lobbyClient.ObserveLobbies(GameGuids.NFS);
}
break;
case "observe stop":
{
Console.WriteLine("Stop observing lobby list ...");
lobbyClient.StopObservingLobbies();
}
break;
case "exit":
running = false;
break;
}
}
}
lobbyClient.Stop();
lobbyClient.Dispose();