Projektdateien hinzufügen.
This commit is contained in:
1 parent
cbbf47885d
commit
2674eb2a10
32 files changed
+2091
No files matched your search
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LobbyClientTest
|
||||
{
|
||||
internal class FakeGameHost
|
||||
{
|
||||
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 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;
|
||||
}
|
||||
|
||||
public void Send(EndPoint ep, string text)
|
||||
{
|
||||
byte[] data = Encoding.ASCII.GetBytes(text);
|
||||
_socket.SendTo(data, 0, data.Length, SocketFlags.None, ep);
|
||||
}
|
||||
|
||||
private void Receive()
|
||||
{
|
||||
_socket.BeginReceiveFrom(state.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv = (ar) =>
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\LobbyClient\LobbyClient.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,184 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
using Lobbies;
|
||||
using LobbyClientTest;
|
||||
using LobbyServerDto;
|
||||
using System.Net;
|
||||
|
||||
Console.WriteLine("Starting lobby client!");
|
||||
var lobbyClient = new LobbyClient();
|
||||
var cancellationTokenSource = new CancellationTokenSource();
|
||||
|
||||
List<LobbyInfo> openLobbies = new List<LobbyInfo>();
|
||||
|
||||
lobbyClient.Connect("localhost", 8088, cancellationTokenSource.Token);
|
||||
|
||||
FakeGameHost fakeGameHost = new FakeGameHost();
|
||||
int myPort = fakeGameHost.Server(0);
|
||||
IPEndPoint? hostInfo = null;
|
||||
|
||||
bool running = true;
|
||||
|
||||
_ = Task.Run(() =>
|
||||
{
|
||||
while (running)
|
||||
{
|
||||
foreach (var lobbyEvent in lobbyClient.ReadEvents(20))
|
||||
{
|
||||
switch (lobbyEvent.EventType)
|
||||
{
|
||||
case LobbyClientEventTypes.Connected:
|
||||
{
|
||||
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.LobbyHostInfo:
|
||||
{
|
||||
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($"Requesting nat punch!");
|
||||
lobbyClient.RequestLobbyNatPunch(lobbyHostInfo.LobbyId, null, null, myPort);
|
||||
Console.Write(">");
|
||||
}
|
||||
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++)
|
||||
{
|
||||
fakeGameHost.Send(ep, "Nat Falcon Punch!");
|
||||
}
|
||||
lobbyClient.NotifyLobbyNatPunchDone(lobbyRequestNatPunch.NatPunchId);
|
||||
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(hostInfo!, "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":
|
||||
{
|
||||
Console.WriteLine("Hosting game ...");
|
||||
lobbyClient.HostLobby(GameGuids.NFS, "Hallo, Welt!", 1, 8, null, null, myPort);
|
||||
fakeGameHost.isHost = true;
|
||||
}
|
||||
break;
|
||||
case "host stop":
|
||||
{
|
||||
Console.WriteLine("Stop hosting game ...");
|
||||
lobbyClient.CloseLobby();
|
||||
fakeGameHost.isHost = false;
|
||||
}
|
||||
break;
|
||||
case "join":
|
||||
{
|
||||
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();
|
||||
|
||||
|
||||
Reference in new issue
Block a user