This commit is contained in:
twoischnig committed 2023-11-29 20:28:43 +01:00
1 parent 2674eb2a10
commit 840218f119
25 files changed
+650 -169

No files matched your search

+46 -31
View File
@@ -1,52 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace LobbyClientTest
{
internal class FakeGameHost
internal class FakeGameHost : IDisposable
{
private Socket _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
private const int bufSize = 8 * 1024;
private State state = new State();
private EndPoint epFrom = new IPEndPoint(IPAddress.Any, 0);
private AsyncCallback recv = null;
public bool isHost;
public class State
{
public byte[] buffer = new byte[bufSize];
}
public bool isHost = false;
UdpClient? udpClient;
bool running = true;
public int Server(int port)
{
_socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true);
_socket.ExclusiveAddressUse = false;
_socket.Bind(new IPEndPoint(IPAddress.Any, port));
Receive();
return ((IPEndPoint)_socket.LocalEndPoint!).Port;
udpClient = new UdpClient(port);
_ = Task.Run(() =>
{
Receive();
});
return ((IPEndPoint)udpClient.Client.LocalEndPoint!).Port;
}
public void Send(EndPoint ep, string text)
public void Send(IPEndPoint ep, string text)
{
byte[] data = Encoding.ASCII.GetBytes(text);
_socket.SendTo(data, 0, data.Length, SocketFlags.None, ep);
udpClient?.Send(data, data.Length, ep);
}
public void Send(IPEndPoint ep, byte[] message, int length)
{
udpClient?.Send(message, length, ep);
}
private void Receive()
{
_socket.BeginReceiveFrom(state.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv = (ar) =>
try
{
State so = (State)ar.AsyncState;
int bytes = _socket.EndReceiveFrom(ar, ref epFrom);
_socket.BeginReceiveFrom(so.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv, so);
Console.WriteLine($"Game {(isHost ? "host" : "client")} received: {epFrom.ToString()}: {bytes}, {Encoding.ASCII.GetString(so.buffer, 0, bytes)}");
if (isHost)
Send(epFrom, "Hello from Game Server!");
}, state);
IPEndPoint remoteEp = new IPEndPoint(IPAddress.Any, 0);
while (running)
{
var data = udpClient!.Receive(ref remoteEp);
Console.WriteLine($"Game {(isHost ? "host" : "client")} received: {remoteEp.ToString()}: {data.Length}, {Encoding.ASCII.GetString(data, 0, data.Length)}");
if (isHost)
Send(remoteEp, "Hello from Game Server!");
}
}
catch
{
}
finally
{
}
}
public void Dispose()
{
running = false;
udpClient?.Dispose();
}
}
}
+71 -20
View File
@@ -4,30 +4,34 @@ using LobbyClientTest;
using LobbyServerDto;
using System.Net;
Console.WriteLine("Starting lobby client!");
Console.WriteLine("Starting lobby client v0.7!");
var lobbyClient = new LobbyClient();
var cancellationTokenSource = new CancellationTokenSource();
List<LobbyInfo> openLobbies = new List<LobbyInfo>();
lobbyClient.Connect("localhost", 8088, cancellationTokenSource.Token);
lobbyClient.Connect("lobby.incobyte.de" /*"localhost"*/, 8088, cancellationTokenSource.Token);
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(() =>
{
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!");
@@ -76,24 +80,56 @@ _ = Task.Run(() =>
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($"Requesting nat punch!");
lobbyClient.RequestLobbyNatPunch(lobbyHostInfo.LobbyId, null, null, myPort);
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}!");
//NatPuncher.NatPunch(new IPEndPoint(IPAddress.Any, myPort), new IPEndPoint(IPAddress.Parse(lobbyRequestNatPunch.ClientIp!), lobbyRequestNatPunch.ClientPort));
var ep = new IPEndPoint(IPAddress.Parse(lobbyRequestNatPunch.ClientIp!), lobbyRequestNatPunch.ClientPort);
for (int z = 0; z < 16; z++)
Task.Run(() =>
{
fakeGameHost.Send(ep, "Nat Falcon Punch!");
}
lobbyClient.NotifyLobbyNatPunchDone(lobbyRequestNatPunch.NatPunchId);
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;
@@ -104,7 +140,8 @@ _ = Task.Run(() =>
Console.SetCursorPosition(0, p.Top);
Console.WriteLine($"Nat punch request done!");
Console.WriteLine($"Connecting game client!");
fakeGameHost.Send(hostInfo!, "Hello from Game Client!");
fakeGameHost.Send(new IPEndPoint(IPAddress.Parse(lobbyNatPunchDone!.ExternalIp!), lobbyNatPunchDone.ExternalPort), "Hello from Game Client!");
Console.Write(">");
}
break;
@@ -133,9 +170,16 @@ while (running)
{
case "host":
{
Console.WriteLine("Hosting game ...");
lobbyClient.HostLobby(GameGuids.NFS, "Hallo, Welt!", 1, 8, null, null, myPort);
fakeGameHost.isHost = true;
if (!connected)
{
Console.WriteLine("Not connected yet!");
}
else
{
Console.WriteLine("Hosting game ...");
lobbyClient.HostLobby(GameGuids.NFS, "Hallo, Welt!", 1, 8, null, "127.0.0.1", myPort);
fakeGameHost.isHost = true;
}
}
break;
case "host stop":
@@ -147,15 +191,22 @@ while (running)
break;
case "join":
{
Console.WriteLine("Trying to join first lobby ...");
var firstLobby = openLobbies.FirstOrDefault();
if (firstLobby != null)
if (!connected)
{
lobbyClient.RequestLobbyHostInfo(firstLobby.Id, null);
Console.WriteLine("Not connected yet!");
}
else
{
Console.WriteLine("Seeing no open lobby!");
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;