Compare commits
10
Commits
b7f44b5215
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d1542df493 | ||
|
|
da2a9431df | ||
|
|
c198d8a727 | ||
|
|
8862c6c8a6 | ||
|
|
57855e80e3 | ||
|
|
5cd58eeec5 | ||
|
|
9e5a79fb68 | ||
|
|
287e268df7 | ||
|
|
2d82d60f9e | ||
|
|
c8472e0670 |
No files matched your search
@@ -66,7 +66,10 @@
|
||||
"System.Memory": "4.5.5"
|
||||
},
|
||||
"runtime": {
|
||||
"LobbyServerDto.dll": {}
|
||||
"LobbyServerDto.dll": {
|
||||
"assemblyVersion": "1.0.0.0",
|
||||
"fileVersion": "1.0.0.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"name": "com.incobyte.lobbyclient",
|
||||
"version": "1.0.2",
|
||||
"version": "1.0.8",
|
||||
"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 lobby server to list and join lobbies",
|
||||
"unity": "2022.3",
|
||||
"keywords": [
|
||||
"nat punch",
|
||||
|
||||
+210
-77
@@ -21,7 +21,7 @@ namespace Lobbies
|
||||
|
||||
UdpEchoServer udpEchoServer = new UdpEchoServer();
|
||||
|
||||
private Dictionary<Guid, LobbyInfo> lobbyInformation = new Dictionary<Guid, LobbyInfo>();
|
||||
private ConcurrentDictionary<Guid, LobbyInfo> lobbyInformation = new ConcurrentDictionary<Guid, LobbyInfo>();
|
||||
private string? host;
|
||||
private int port;
|
||||
private int connectionId;
|
||||
@@ -29,6 +29,8 @@ namespace Lobbies
|
||||
public string? externalIp;
|
||||
public int externalPort;
|
||||
|
||||
private CancellationTokenRegistration connectionCancellationRegistration;
|
||||
|
||||
public void Connect(string host, int port, CancellationToken cancellationToken)
|
||||
{
|
||||
this.host = host;
|
||||
@@ -37,9 +39,10 @@ namespace Lobbies
|
||||
try
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
using var cts = cancellationToken.Register(() =>
|
||||
connectionCancellationRegistration.Dispose();
|
||||
connectionCancellationRegistration = cancellationToken.Register(() =>
|
||||
{
|
||||
tcpClient.Stop();
|
||||
tcpClient.Stop();
|
||||
});
|
||||
|
||||
tcpClient.DataReceived -= TcpClient_DataReceived;
|
||||
@@ -56,26 +59,11 @@ namespace Lobbies
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<LobbyClientEvent> ReadEvents(int maxEvents)
|
||||
public bool TryReadEvent(out LobbyClientEvent? result)
|
||||
{
|
||||
if (events.Count > 0)
|
||||
{
|
||||
maxEvents = Math.Min(maxEvents, events.Count);
|
||||
|
||||
while (maxEvents > 0)
|
||||
{
|
||||
if(events.TryDequeue(out var _event))
|
||||
{
|
||||
yield return _event;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return events.TryDequeue(out result);
|
||||
}
|
||||
|
||||
|
||||
public void HostLobby(Guid gameId, string name, int gameMode, int maxPlayerCount, string? password, int port)
|
||||
{
|
||||
udpEchoServer.Start(0);
|
||||
@@ -102,8 +90,16 @@ namespace Lobbies
|
||||
};
|
||||
|
||||
byte[] messageData = bufferRental.Rent();
|
||||
var len = lobbyCreate.Serialize(messageData);
|
||||
await tcpClient.Send(messageData, 0, len); bufferRental.Return(messageData);
|
||||
try
|
||||
{
|
||||
|
||||
var len = lobbyCreate.Serialize(messageData);
|
||||
await tcpClient.Send(messageData, 0, len);
|
||||
}
|
||||
finally
|
||||
{
|
||||
bufferRental.Return(messageData);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -111,8 +107,11 @@ namespace Lobbies
|
||||
{
|
||||
_ = Task.Run(async () => {
|
||||
byte[]? passwordHash = null;
|
||||
if (!string.IsNullOrEmpty(password) && lobbyInformation.ContainsKey(lobbyId) && lobbyInformation[lobbyId].PasswordSalt != null)
|
||||
passwordHash = PasswordHash.Hash(password, lobbyInformation[lobbyId].PasswordSalt!);
|
||||
|
||||
if (!string.IsNullOrEmpty(password) &&
|
||||
lobbyInformation.TryGetValue(lobbyId, out var lobby) &&
|
||||
lobby.PasswordSalt != null)
|
||||
passwordHash = PasswordHash.Hash(password, lobby.PasswordSalt!);
|
||||
|
||||
var lobbyCreate = new LobbyRequestHostInfo()
|
||||
{
|
||||
@@ -120,9 +119,16 @@ namespace Lobbies
|
||||
PasswordHash = passwordHash,
|
||||
};
|
||||
|
||||
byte[] messageData = bufferRental.Rent();
|
||||
var len = lobbyCreate.Serialize(messageData);
|
||||
await tcpClient.Send(messageData, 0, len); bufferRental.Return(messageData);
|
||||
var messageData = bufferRental.Rent();
|
||||
try
|
||||
{
|
||||
var len = lobbyCreate.Serialize(messageData);
|
||||
await tcpClient.Send(messageData, 0, len);
|
||||
}
|
||||
finally
|
||||
{
|
||||
bufferRental.Return(messageData);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -149,21 +155,48 @@ namespace Lobbies
|
||||
Task.Run(() =>
|
||||
{
|
||||
byte[]? passwordHash = null;
|
||||
if (!string.IsNullOrEmpty(password) && lobbyInformation.ContainsKey(lobbyId) && lobbyInformation[lobbyId].PasswordSalt != null)
|
||||
passwordHash = PasswordHash.Hash(password, lobbyInformation[lobbyId].PasswordSalt!);
|
||||
if (!string.IsNullOrEmpty(password) &&
|
||||
lobbyInformation.TryGetValue(lobbyId, out var lobby) &&
|
||||
lobby.PasswordSalt != null)
|
||||
{
|
||||
passwordHash = PasswordHash.Hash(password, lobby.PasswordSalt);
|
||||
}
|
||||
|
||||
if (!QueryExternalIpAndPort(sendUdpToGetExternalPortMappingCallback, out var ip, out var mappedPort))
|
||||
{
|
||||
events.Enqueue(new LobbyClientEvent
|
||||
{
|
||||
EventType = LobbyClientEventTypes.Failed,
|
||||
EventData = new LobbyClientDisconnectReason
|
||||
{
|
||||
WasError = true,
|
||||
ErrorMessage = "Could not determine external IP/port."
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
QueryExternalIpAndPort(sendUdpToGetExternalPortMappingCallback);
|
||||
var lobbyRequestNatPunch = new LobbyRequestNatPunch()
|
||||
{
|
||||
LobbyId = lobbyId,
|
||||
PasswordHash = passwordHash,
|
||||
ClientIp = externalIp,
|
||||
ClientPort = externalPort
|
||||
ClientIp = ip,
|
||||
ClientPort = mappedPort
|
||||
};
|
||||
|
||||
byte[] messageData = bufferRental.Rent();
|
||||
var len = lobbyRequestNatPunch.Serialize(messageData);
|
||||
_ = Task.Run(async () => { await tcpClient.Send(messageData, 0, len); bufferRental.Return(messageData); });
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await tcpClient.Send(messageData, 0, len);
|
||||
}
|
||||
finally
|
||||
{
|
||||
bufferRental.Return(messageData);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -179,31 +212,65 @@ namespace Lobbies
|
||||
/// after that the udp client will be disposed.</param>
|
||||
public int RequestLobbyNatPunch(Guid lobbyId, string? password, int port = 0)
|
||||
{
|
||||
if(port < 0 && port > 65535)
|
||||
if(port < 0 || port > 65535)
|
||||
throw new ArgumentOutOfRangeException(nameof(port));
|
||||
|
||||
var udpEchoServer = new UdpEchoServer();
|
||||
udpEchoServer.Start(port);
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
byte[]? passwordHash = null;
|
||||
if (!string.IsNullOrEmpty(password) && lobbyInformation.ContainsKey(lobbyId) && lobbyInformation[lobbyId].PasswordSalt != null)
|
||||
passwordHash = PasswordHash.Hash(password, lobbyInformation[lobbyId].PasswordSalt!);
|
||||
var udpEchoServer = new UdpEchoServer();
|
||||
|
||||
QueryExternalIpAndPort(udpEchoServer.Send);
|
||||
udpEchoServer.Dispose();
|
||||
var lobbyRequestNatPunch = new LobbyRequestNatPunch()
|
||||
try
|
||||
{
|
||||
udpEchoServer.Start(port);
|
||||
|
||||
byte[]? passwordHash = null;
|
||||
if (!string.IsNullOrEmpty(password) &&
|
||||
lobbyInformation.TryGetValue(lobbyId, out var lobby) &&
|
||||
lobby.PasswordSalt != null)
|
||||
{
|
||||
passwordHash = PasswordHash.Hash(password, lobby.PasswordSalt);
|
||||
}
|
||||
|
||||
if (!QueryExternalIpAndPort(udpEchoServer.Send, out var ip, out var mappedPort))
|
||||
{
|
||||
events.Enqueue(new LobbyClientEvent
|
||||
{
|
||||
EventType = LobbyClientEventTypes.Failed,
|
||||
EventData = new LobbyClientDisconnectReason
|
||||
{
|
||||
WasError = true,
|
||||
ErrorMessage = "Could not determine external IP/port."
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
var lobbyRequestNatPunch = new LobbyRequestNatPunch()
|
||||
{
|
||||
LobbyId = lobbyId,
|
||||
PasswordHash = passwordHash,
|
||||
ClientIp = ip,
|
||||
ClientPort = mappedPort
|
||||
};
|
||||
|
||||
byte[] messageData = bufferRental.Rent();
|
||||
var len = lobbyRequestNatPunch.Serialize(messageData);
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await tcpClient.Send(messageData, 0, len);
|
||||
}
|
||||
finally
|
||||
{
|
||||
bufferRental.Return(messageData);
|
||||
}
|
||||
});
|
||||
}
|
||||
finally
|
||||
{
|
||||
LobbyId = lobbyId,
|
||||
PasswordHash = passwordHash,
|
||||
ClientIp = externalIp,
|
||||
ClientPort = externalPort
|
||||
};
|
||||
|
||||
byte[] messageData = bufferRental.Rent();
|
||||
var len = lobbyRequestNatPunch.Serialize(messageData);
|
||||
_ = Task.Run(async () => { await tcpClient.Send(messageData, 0, len); bufferRental.Return(messageData); });
|
||||
udpEchoServer.Dispose();
|
||||
}
|
||||
});
|
||||
|
||||
return udpEchoServer.Port;
|
||||
@@ -231,9 +298,13 @@ namespace Lobbies
|
||||
HostTryPort = udpEchoServer.Port
|
||||
};
|
||||
|
||||
byte[] messageData = bufferRental.Rent();
|
||||
byte[] messageData = bufferRental.Rent();
|
||||
var len = lobbyUpdate.Serialize(messageData);
|
||||
_ = Task.Run(async () => { await tcpClient.Send(messageData, 0, len); bufferRental.Return(messageData); });
|
||||
_ = Task.Run(async () => {
|
||||
try { await tcpClient.Send(messageData, 0, len); }
|
||||
finally { bufferRental.Return(messageData); }
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public void CloseLobby()
|
||||
@@ -247,7 +318,9 @@ namespace Lobbies
|
||||
|
||||
byte[] messageData = bufferRental.Rent();
|
||||
var len = lobbyDelete.Serialize(messageData);
|
||||
_ = Task.Run(async () => { await tcpClient.Send(messageData, 0, len); bufferRental.Return(messageData); });
|
||||
_ = Task.Run(async () => {
|
||||
try { await tcpClient.Send(messageData, 0, len); } finally { bufferRental.Return(messageData); }
|
||||
});
|
||||
}
|
||||
|
||||
public void ObserveLobbies(Guid gameId)
|
||||
@@ -256,7 +329,7 @@ namespace Lobbies
|
||||
|
||||
byte[] messageData = bufferRental.Rent();
|
||||
var len = lobbiesObserve.Serialize(messageData);
|
||||
_ = Task.Run(async () => { await tcpClient.Send(messageData, 0, len); bufferRental.Return(messageData); });
|
||||
_ = Task.Run(async () => { try { await tcpClient.Send(messageData, 0, len); } finally { bufferRental.Return(messageData); } });
|
||||
}
|
||||
|
||||
public void StopObservingLobbies()
|
||||
@@ -265,7 +338,7 @@ namespace Lobbies
|
||||
|
||||
byte[] messageData = bufferRental.Rent();
|
||||
var len = lobbiesStopObserve.Serialize(messageData);
|
||||
_ = Task.Run(async () => { await tcpClient.Send(messageData, 0, len); bufferRental.Return(messageData); });
|
||||
_ = Task.Run(async () => { try { await tcpClient.Send(messageData, 0, len); } finally { bufferRental.Return(messageData); } });
|
||||
}
|
||||
|
||||
public void NotifyLobbyNatPunchDone(int natPunchId, string externalIp, int externalPort)
|
||||
@@ -274,7 +347,7 @@ namespace Lobbies
|
||||
|
||||
byte[] messageData = bufferRental.Rent();
|
||||
var len = lobbyNatPunchDone.Serialize(messageData);
|
||||
_ = Task.Run(async () => { await tcpClient.Send(messageData, 0, len); bufferRental.Return(messageData); });
|
||||
_ = Task.Run(async () => { try { await tcpClient.Send(messageData, 0, len); } finally { bufferRental.Return(messageData); } });
|
||||
}
|
||||
|
||||
public Task TryDirectConnection(IPAddress[] ipAddressesToTry, int tryPort)
|
||||
@@ -348,28 +421,60 @@ namespace Lobbies
|
||||
return new IPAddress[0];
|
||||
}
|
||||
|
||||
public void QueryExternalIpAndPort(SendUdpMessageCallback sendUdpCallback)
|
||||
public bool QueryExternalIpAndPort(SendUdpMessageCallback sendUdpCallback, out string? ip, out int port)
|
||||
{
|
||||
ip = null;
|
||||
port = 0;
|
||||
|
||||
byte[] messageData = bufferRental.Rent();
|
||||
|
||||
try
|
||||
{
|
||||
if (sendUdpCallback == null)
|
||||
throw new ArgumentNullException(nameof(sendUdpCallback));
|
||||
|
||||
if (string.IsNullOrWhiteSpace(host))
|
||||
return false;
|
||||
|
||||
if (connectionId <= 0)
|
||||
return false;
|
||||
|
||||
var ips = GetIPsByName(host, true, false);
|
||||
if (ips.Length == 0)
|
||||
return false;
|
||||
|
||||
externalIp = null;
|
||||
externalPort = 0;
|
||||
waitForExternalIp.Reset();
|
||||
var queryExternalPortAndIp = new QueryExternalPortAndIp() { LobbyClientId = connectionId };
|
||||
var len = queryExternalPortAndIp.Serialize(messageData);
|
||||
var ip = GetIPsByName(host!, true, false).First();
|
||||
|
||||
do
|
||||
var queryExternalPortAndIp = new QueryExternalPortAndIp
|
||||
{
|
||||
sendUdpCallback(new IPEndPoint(ip, port), messageData, len);
|
||||
}
|
||||
while (!waitForExternalIp.WaitOne(100));
|
||||
}
|
||||
catch
|
||||
{
|
||||
LobbyClientId = connectionId
|
||||
};
|
||||
|
||||
int len = queryExternalPortAndIp.Serialize(messageData);
|
||||
var remoteEndpoint = new IPEndPoint(ips[0], this.port);
|
||||
|
||||
for (int tries = 0; tries < 100; tries++)
|
||||
{
|
||||
sendUdpCallback(remoteEndpoint, messageData, len);
|
||||
|
||||
if (waitForExternalIp.WaitOne(100))
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(externalIp) && externalPort > 0)
|
||||
{
|
||||
ip = externalIp;
|
||||
port = externalPort;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
finally
|
||||
{
|
||||
bufferRental.Return(messageData);
|
||||
}
|
||||
@@ -432,7 +537,7 @@ namespace Lobbies
|
||||
var lobbyDelete = LobbyDelete.Deserialize(data.Span);
|
||||
if (lobbyDelete != null)
|
||||
{
|
||||
lobbyInformation.Remove(lobbyDelete.Id);
|
||||
lobbyInformation.TryRemove(lobbyDelete.Id, out var _);
|
||||
events.Enqueue(new LobbyClientEvent { EventType = LobbyClientEventTypes.LobbyDelete, EventData = lobbyDelete });
|
||||
}
|
||||
}
|
||||
@@ -486,19 +591,47 @@ namespace Lobbies
|
||||
|
||||
public IEnumerable<IPAddress> GatherLocalIpAddresses()
|
||||
{
|
||||
foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
|
||||
foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces())
|
||||
{
|
||||
IPInterfaceProperties ipProps = netInterface.GetIPProperties();
|
||||
if (netInterface.OperationalStatus != OperationalStatus.Up)
|
||||
continue;
|
||||
|
||||
foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses)
|
||||
if (netInterface.NetworkInterfaceType == NetworkInterfaceType.Loopback)
|
||||
continue;
|
||||
|
||||
// Optional: virtuelle Interfaces rausfiltern (je nach Bedarf)
|
||||
if (netInterface.Description.Contains("Virtual", StringComparison.OrdinalIgnoreCase) ||
|
||||
netInterface.Description.Contains("VMware", StringComparison.OrdinalIgnoreCase) ||
|
||||
netInterface.Description.Contains("Docker", StringComparison.OrdinalIgnoreCase))
|
||||
continue;
|
||||
|
||||
var ipProps = netInterface.GetIPProperties();
|
||||
|
||||
foreach (var addr in ipProps.UnicastAddresses)
|
||||
{
|
||||
yield return addr.Address;
|
||||
}
|
||||
var ip = addr.Address;
|
||||
|
||||
// Nur IPv4
|
||||
if (ip.AddressFamily != AddressFamily.InterNetwork)
|
||||
continue;
|
||||
|
||||
// Loopback nochmal sicherheitshalber
|
||||
if (IPAddress.IsLoopback(ip))
|
||||
continue;
|
||||
|
||||
// Link-local (APIPA) raus (169.254.x.x)
|
||||
if (ip.GetAddressBytes()[0] == 169 && ip.GetAddressBytes()[1] == 254)
|
||||
continue;
|
||||
|
||||
yield return ip;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Stop();
|
||||
connectionCancellationRegistration.Dispose();
|
||||
waitForExternalIp.Dispose();
|
||||
tcpClient.Dispose();
|
||||
udpEchoServer.Dispose();
|
||||
|
||||
+137
-98
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using LobbyServerDto;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -13,142 +15,171 @@ namespace Lobbies
|
||||
internal delegate void DisconnectedEventArgs(bool clean, string error);
|
||||
internal event DisconnectedEventArgs? Disconnected;
|
||||
|
||||
|
||||
internal delegate void ConnectedEventArgs();
|
||||
internal event ConnectedEventArgs? Connected;
|
||||
|
||||
TcpClient? tcpClient;
|
||||
NetworkStream? networkStream;
|
||||
CancellationTokenSource? cancellationTokenSource = new CancellationTokenSource();
|
||||
bool running = false;
|
||||
private const int HeaderSize = 4;
|
||||
private const int MaxMessageSize = 4096;
|
||||
private static readonly TimeSpan PingInterval = TimeSpan.FromSeconds(10);
|
||||
|
||||
private TcpClient? tcpClient;
|
||||
private NetworkStream? networkStream;
|
||||
private CancellationTokenSource? cancellationTokenSource = new CancellationTokenSource();
|
||||
private bool running = false;
|
||||
private readonly BufferRental bufferRental = new BufferRental(MaxMessageSize);
|
||||
private async Task ReadExact(NetworkStream stream, Memory<byte> buffer, int length, CancellationToken token)
|
||||
{
|
||||
int offset = 0;
|
||||
|
||||
while (offset < length)
|
||||
{
|
||||
int read = await stream.ReadAsync(buffer.Slice(offset), token);
|
||||
if (read == 0)
|
||||
throw new EndOfStreamException();
|
||||
|
||||
offset += read;
|
||||
}
|
||||
}
|
||||
|
||||
internal async Task Connect(string host, int port)
|
||||
{
|
||||
bool wasError = false;
|
||||
bool cleanDisconnect = true;
|
||||
string error = string.Empty;
|
||||
Task? pingTask = null;
|
||||
|
||||
try
|
||||
{
|
||||
cancellationTokenSource!.Token.ThrowIfCancellationRequested();
|
||||
if (cancellationTokenSource == null || cancellationTokenSource.IsCancellationRequested)
|
||||
cancellationTokenSource = new CancellationTokenSource();
|
||||
|
||||
running = true;
|
||||
var token = cancellationTokenSource.Token;
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
running = true;
|
||||
tcpClient = new TcpClient();
|
||||
using (cancellationTokenSource!.Token.Register(() => { tcpClient.Close(); }))
|
||||
|
||||
using (token.Register(() =>
|
||||
{
|
||||
try { tcpClient.Close(); } catch { }
|
||||
}))
|
||||
{
|
||||
await tcpClient.ConnectAsync(host, port);
|
||||
}
|
||||
|
||||
networkStream = tcpClient.GetStream();
|
||||
|
||||
Memory<byte> buffer = new byte[4096];
|
||||
Memory<byte> target = new byte[4096];
|
||||
|
||||
int currentOffset = 0;
|
||||
int currentMessageRemainingLength = 0;
|
||||
int currentMessageLength = 0;
|
||||
bool validMessage = true;
|
||||
int currentReadOffset = 0;
|
||||
bool offsetSizeInt = false;
|
||||
Connected?.Invoke();
|
||||
|
||||
while (running)
|
||||
pingTask = Task.Run(() => PingLoop(token), token);
|
||||
|
||||
Memory<byte> buffer = new byte[MaxMessageSize];
|
||||
|
||||
while (running && !token.IsCancellationRequested)
|
||||
{
|
||||
int copyOffset = 0;
|
||||
int receivedBytes = currentReadOffset;
|
||||
await ReadExact(networkStream, buffer, HeaderSize, token);
|
||||
|
||||
if (currentReadOffset < 4)
|
||||
{
|
||||
receivedBytes += await networkStream.ReadAsync(buffer.Slice(currentReadOffset), cancellationTokenSource.Token) + currentReadOffset;
|
||||
}
|
||||
int length = BitConverter.ToInt32(buffer.Span.Slice(0, HeaderSize));
|
||||
|
||||
if (receivedBytes == 0 && running && !cancellationTokenSource.Token.IsCancellationRequested)
|
||||
{
|
||||
throw new Exception("Connection lost!");
|
||||
}
|
||||
if (length < 0)
|
||||
throw new InvalidDataException("Negative message length received.");
|
||||
|
||||
if (receivedBytes > 3 || (currentMessageRemainingLength > 0 && receivedBytes > currentMessageRemainingLength))
|
||||
{
|
||||
currentReadOffset = 0;
|
||||
if (currentMessageLength == 0)
|
||||
{
|
||||
currentMessageRemainingLength = BitConverter.ToInt32(buffer.Span);
|
||||
currentMessageLength = currentMessageRemainingLength;
|
||||
receivedBytes -= 4;
|
||||
copyOffset += 4;
|
||||
offsetSizeInt = true;
|
||||
}
|
||||
else
|
||||
offsetSizeInt = false;
|
||||
if (length > MaxMessageSize)
|
||||
throw new InvalidDataException($"Message too large: {length} > {MaxMessageSize}.");
|
||||
|
||||
var receivedCount = Math.Min(receivedBytes, currentMessageRemainingLength);
|
||||
receivedBytes -= receivedCount;
|
||||
copyOffset += receivedCount;
|
||||
if (length == 0)
|
||||
continue;
|
||||
|
||||
if (validMessage && currentOffset + receivedCount > 0)
|
||||
{
|
||||
if (currentOffset + receivedCount < target.Length)
|
||||
buffer.Slice(offsetSizeInt ? 4 : 0, receivedCount).CopyTo(target.Slice(currentOffset));
|
||||
else
|
||||
validMessage = false;
|
||||
}
|
||||
await ReadExact(networkStream, buffer, length, token);
|
||||
|
||||
currentOffset += receivedCount;
|
||||
currentMessageRemainingLength -= receivedCount;
|
||||
|
||||
if (currentMessageRemainingLength <= 0)
|
||||
{
|
||||
if (validMessage)
|
||||
DataReceived?.Invoke(currentMessageLength, target);
|
||||
|
||||
if (receivedBytes > 0)
|
||||
{
|
||||
buffer.Slice(copyOffset, receivedBytes).CopyTo(buffer);
|
||||
currentReadOffset += receivedBytes;
|
||||
}
|
||||
|
||||
currentOffset = 0;
|
||||
currentMessageLength = 0;
|
||||
currentMessageRemainingLength = 0;
|
||||
validMessage = true;
|
||||
}
|
||||
}
|
||||
else if (receivedBytes > 0)
|
||||
{
|
||||
currentReadOffset += receivedBytes;
|
||||
}
|
||||
}
|
||||
DataReceived?.Invoke(length, buffer.Slice(0, length));
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
cleanDisconnect = true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
cleanDisconnect = false;
|
||||
error = e.Message;
|
||||
}
|
||||
finally
|
||||
finally
|
||||
{
|
||||
wasError = running;
|
||||
|
||||
running = false;
|
||||
|
||||
networkStream?.Dispose();
|
||||
tcpClient?.Dispose();
|
||||
|
||||
tcpClient = null;
|
||||
try { cancellationTokenSource?.Cancel(); } catch { }
|
||||
|
||||
if (pingTask != null)
|
||||
{
|
||||
try { await pingTask; } catch { }
|
||||
}
|
||||
|
||||
try { networkStream?.Dispose(); } catch { }
|
||||
try { tcpClient?.Close(); } catch { }
|
||||
try { tcpClient?.Dispose(); } catch { }
|
||||
|
||||
networkStream = null;
|
||||
tcpClient = null;
|
||||
|
||||
Disconnected?.Invoke(!wasError, error);
|
||||
Disconnected?.Invoke(cleanDisconnect, error);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task PingLoop(CancellationToken token)
|
||||
{
|
||||
while (running && !token.IsCancellationRequested)
|
||||
{
|
||||
await Task.Delay(PingInterval, token);
|
||||
|
||||
if (!running || token.IsCancellationRequested)
|
||||
break;
|
||||
|
||||
await SendPing(token);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SendPing(CancellationToken token)
|
||||
{
|
||||
if (!running || networkStream == null)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
await networkStream.WriteAsync(BitConverter.GetBytes(0), 0, 4, token);
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
internal async Task Send(byte[] buffer, int offset, int count)
|
||||
{
|
||||
byte[] frame = bufferRental.Rent();
|
||||
try
|
||||
{
|
||||
if (running && networkStream != null)
|
||||
{
|
||||
await networkStream.WriteAsync(BitConverter.GetBytes(count - offset), 0, 4, cancellationTokenSource!.Token);
|
||||
await networkStream.WriteAsync(buffer, offset, count, cancellationTokenSource!.Token);
|
||||
}
|
||||
if (!running || networkStream == null || cancellationTokenSource == null)
|
||||
return;
|
||||
|
||||
if (count < 0 || count > MaxMessageSize)
|
||||
throw new ArgumentOutOfRangeException(nameof(count));
|
||||
|
||||
if (offset < 0 || offset + count > buffer.Length)
|
||||
throw new ArgumentOutOfRangeException(nameof(offset));
|
||||
|
||||
BitConverter.GetBytes(count).CopyTo(frame, 0);
|
||||
Buffer.BlockCopy(buffer, offset, frame, HeaderSize, count);
|
||||
|
||||
await networkStream.WriteAsync(frame, 0, HeaderSize + count, cancellationTokenSource.Token);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
finally
|
||||
{
|
||||
bufferRental.Return(frame);
|
||||
}
|
||||
}
|
||||
|
||||
internal void Stop()
|
||||
{
|
||||
@@ -158,9 +189,17 @@ namespace Lobbies
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
cancellationTokenSource?.Cancel();
|
||||
tcpClient?.Dispose();
|
||||
cancellationTokenSource?.Dispose();
|
||||
running = false;
|
||||
|
||||
try { cancellationTokenSource?.Cancel(); } catch { }
|
||||
try { networkStream?.Dispose(); } catch { }
|
||||
try { tcpClient?.Close(); } catch { }
|
||||
try { tcpClient?.Dispose(); } catch { }
|
||||
try { cancellationTokenSource?.Dispose(); } catch { }
|
||||
|
||||
networkStream = null;
|
||||
tcpClient = null;
|
||||
cancellationTokenSource = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ using Lobbies;
|
||||
using LobbyClientTest;
|
||||
using LobbyServerDto;
|
||||
using System.Net;
|
||||
using System.Net.WebSockets;
|
||||
|
||||
Console.WriteLine("Starting lobby client v0.7!");
|
||||
var lobbyClient = new LobbyClient();
|
||||
@@ -25,8 +24,10 @@ _ = Task.Run(() =>
|
||||
{
|
||||
while (running)
|
||||
{
|
||||
foreach (var lobbyEvent in lobbyClient.ReadEvents(20))
|
||||
while (lobbyClient.TryReadEvent(out var lobbyEvent))
|
||||
{
|
||||
if (lobbyEvent == null) continue;
|
||||
|
||||
switch (lobbyEvent.EventType)
|
||||
{
|
||||
case LobbyClientEventTypes.LobbyJoinFailed:
|
||||
@@ -169,7 +170,7 @@ _ = Task.Run(() =>
|
||||
lobbyClient.QueryExternalIpAndPort((remoteEndpoint, messageData, messageLength) =>
|
||||
{
|
||||
fakeGameHost.Send(remoteEndpoint, messageData, messageLength);
|
||||
});
|
||||
}, out var ip, out var port);
|
||||
|
||||
var ep = new IPEndPoint(IPAddress.Parse(lobbyRequestNatPunch.ClientIp!), lobbyRequestNatPunch.ClientPort);
|
||||
for (int z = 0; z < 32; z++)
|
||||
|
||||
+29
-8
@@ -16,15 +16,36 @@ ConcurrentDictionary<Guid, List<int>> clientsWatchingGameId = new ConcurrentDict
|
||||
BufferRental bufferRental = new BufferRental(4096);
|
||||
|
||||
udpServer.QueryIpAndPort += (clientId, ip, port) =>
|
||||
{
|
||||
var messageData = bufferRental.Rent();
|
||||
var seenExternalIpAndPort = new SeenExternalIpAndPort() { Ip = ip, Port = port };
|
||||
var messageDataLength = seenExternalIpAndPort.Serialize(messageData);
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
Console.WriteLine($"{DateTime.Now}: [TcpServer] sending QueryExternalPortAndIp result for lobby client id {clientId} with {ip}:{port}");
|
||||
byte[]? messageData = null;
|
||||
|
||||
try
|
||||
{
|
||||
await tcpServer.Send(clientId, messageData, 0, messageDataLength);
|
||||
bufferRental.Return(messageData);
|
||||
});
|
||||
messageData = bufferRental.Rent();
|
||||
var seenExternalIpAndPort = new SeenExternalIpAndPort() { Ip = ip, Port = port };
|
||||
var messageDataLength = seenExternalIpAndPort.Serialize(messageData);
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await tcpServer.Send(clientId, messageData, 0, messageDataLength);
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Console.WriteLine($"{DateTime.Now}: [TcpServer] error in QueryExternalPortAndIp result for lobby client id {clientId} with {ip}:{port}: {ex.ToString()}");
|
||||
}
|
||||
|
||||
bufferRental.Return(messageData);
|
||||
messageData = null;
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"{DateTime.Now}: [TcpServer] error in QueryExternalPortAndIp result for lobby client id {clientId} with {ip}:{port}: {ex.ToString()}");
|
||||
if (messageData != null)
|
||||
bufferRental.Return(messageData);
|
||||
}
|
||||
};
|
||||
|
||||
tcpServer.DataReceived += (clientId, dataLength, data) =>
|
||||
|
||||
+100
-85
@@ -13,16 +13,27 @@ namespace LobbyServer
|
||||
public delegate void ClientDisconnectedEventArgs(int clientId);
|
||||
public event ClientDisconnectedEventArgs? ClientDisconnected;
|
||||
|
||||
private const int HeaderSize = 4;
|
||||
private const int MaxMessageSize = 4096;
|
||||
private readonly BufferRental bufferRental = new BufferRental(MaxMessageSize);
|
||||
|
||||
internal class Client : IDisposable
|
||||
{
|
||||
internal CancellationTokenSource? cancellationToken = null;
|
||||
internal NetworkStream? stream;
|
||||
internal TcpClient? client;
|
||||
internal DateTime lastSeenUtc = DateTime.UtcNow;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
cancellationToken?.Dispose();
|
||||
try { stream?.Dispose(); } catch { }
|
||||
try { client?.Close(); } catch { }
|
||||
try { client?.Dispose(); } catch { }
|
||||
try { cancellationToken?.Dispose(); } catch { }
|
||||
|
||||
cancellationToken = null;
|
||||
stream = null;
|
||||
client = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +49,8 @@ namespace LobbyServer
|
||||
cancellationToken.TryReset();
|
||||
running = true;
|
||||
clientIdCounter = 0;
|
||||
_ = Task.Run(() => Listener(port));
|
||||
_ = Task.Run(() => MonitorClients());
|
||||
_ = Task.Run(() => Listener(port));
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
@@ -55,7 +67,6 @@ namespace LobbyServer
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
activeClients.Clear ();
|
||||
|
||||
serverClosed.WaitOne();
|
||||
}
|
||||
@@ -93,127 +104,131 @@ namespace LobbyServer
|
||||
|
||||
public async Task Send(int clientId, byte[] buffer, int offset, int count)
|
||||
{
|
||||
byte[] frame = bufferRental.Rent();
|
||||
try
|
||||
{
|
||||
if (activeClients.TryGetValue(clientId, out var lobbyClient) && lobbyClient.stream != null && lobbyClient.cancellationToken != null)
|
||||
{
|
||||
await lobbyClient.stream.WriteAsync(BitConverter.GetBytes(count - offset), 0, 4, lobbyClient.cancellationToken.Token);
|
||||
await lobbyClient.stream.WriteAsync(buffer, offset, count, lobbyClient.cancellationToken.Token);
|
||||
{
|
||||
BitConverter.GetBytes(count).CopyTo(frame, 0);
|
||||
Buffer.BlockCopy(buffer, offset, frame, HeaderSize, count);
|
||||
|
||||
await lobbyClient.stream.WriteAsync(frame, 0, HeaderSize + count, lobbyClient.cancellationToken.Token);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
finally
|
||||
{
|
||||
bufferRental.Return(frame);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ReadExact(NetworkStream stream, Memory<byte> buffer, int length, CancellationToken token)
|
||||
{
|
||||
int offset = 0;
|
||||
|
||||
while (offset < length)
|
||||
{
|
||||
int read = await stream.ReadAsync(buffer.Slice(offset), token);
|
||||
if (read == 0)
|
||||
throw new EndOfStreamException();
|
||||
|
||||
offset += read;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ClientThread(TcpClient client)
|
||||
{
|
||||
int myId = Interlocked.Increment(ref clientIdCounter);
|
||||
Client? lobbyClient = null;
|
||||
|
||||
try
|
||||
{
|
||||
await using NetworkStream stream = client.GetStream();
|
||||
{
|
||||
var stream = client.GetStream();
|
||||
|
||||
Memory<byte> buffer = new byte[4096];
|
||||
Memory<byte> target = new byte[4096];
|
||||
Memory<byte> buffer = new byte[MaxMessageSize];
|
||||
|
||||
|
||||
using var lobbyClient = new Client
|
||||
lobbyClient = new Client
|
||||
{
|
||||
cancellationToken = new CancellationTokenSource(),
|
||||
stream = stream,
|
||||
client = client
|
||||
client = client,
|
||||
lastSeenUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
activeClients.TryAdd(myId, lobbyClient);
|
||||
|
||||
int currentOffset = 0;
|
||||
int currentMessageRemainingLength = 0;
|
||||
int currentMessageLength = 0;
|
||||
bool validMessage = true;
|
||||
bool offsetSizeInt = false;
|
||||
int currentReadOffset = 0;
|
||||
|
||||
var lobbyClientConnectionInfo = new LobbyClientConnectionInfo { Id = myId };
|
||||
byte[] sendBuffer = new byte[128];
|
||||
int sendLen = lobbyClientConnectionInfo.Serialize(sendBuffer);
|
||||
await Send(myId, sendBuffer, 0, sendLen);
|
||||
|
||||
while (running)
|
||||
|
||||
while (running && !lobbyClient.cancellationToken.Token.IsCancellationRequested)
|
||||
{
|
||||
int copyOffset = 0;
|
||||
int receivedBytes = currentReadOffset;
|
||||
await ReadExact(stream, buffer, HeaderSize, lobbyClient.cancellationToken.Token);
|
||||
lobbyClient.lastSeenUtc = DateTime.UtcNow;
|
||||
|
||||
if (currentReadOffset < 4)
|
||||
{
|
||||
receivedBytes += await stream.ReadAsync(buffer.Slice(currentReadOffset), lobbyClient.cancellationToken.Token) + currentReadOffset;
|
||||
if (receivedBytes == 0)
|
||||
throw new Exception("Connection lost!");
|
||||
}
|
||||
int length = BitConverter.ToInt32(buffer.Span.Slice(0, HeaderSize));
|
||||
|
||||
if (length < 0)
|
||||
throw new InvalidDataException("Negative message length received.");
|
||||
|
||||
if (length > MaxMessageSize)
|
||||
throw new InvalidDataException($"Message too large: {length} > {MaxMessageSize}.");
|
||||
|
||||
if (length == 0)
|
||||
continue;
|
||||
|
||||
if (receivedBytes > 3 || (currentMessageRemainingLength > 0 && receivedBytes >= currentMessageRemainingLength))
|
||||
{
|
||||
currentReadOffset = 0;
|
||||
if (currentMessageLength == 0)
|
||||
{
|
||||
currentMessageRemainingLength = BitConverter.ToInt32(buffer.Span);
|
||||
currentMessageLength = currentMessageRemainingLength;
|
||||
receivedBytes -= 4;
|
||||
copyOffset += 4;
|
||||
offsetSizeInt = true;
|
||||
}
|
||||
else
|
||||
offsetSizeInt = false;
|
||||
|
||||
var receivedCount = Math.Min(receivedBytes, currentMessageRemainingLength);
|
||||
|
||||
receivedBytes -= receivedCount;
|
||||
copyOffset += receivedCount;
|
||||
|
||||
if (validMessage && currentOffset + receivedCount > 0)
|
||||
{
|
||||
if (currentOffset + receivedCount < target.Length)
|
||||
buffer.Slice(offsetSizeInt ? 4 : 0, receivedCount).CopyTo(target.Slice(currentOffset));
|
||||
else
|
||||
validMessage = false;
|
||||
}
|
||||
|
||||
currentOffset += receivedCount;
|
||||
currentMessageRemainingLength -= receivedCount;
|
||||
|
||||
if (currentMessageRemainingLength <= 0)
|
||||
{
|
||||
if(validMessage)
|
||||
DataReceived?.Invoke(myId, currentMessageLength, target);
|
||||
|
||||
if (receivedBytes > 0)
|
||||
{
|
||||
buffer.Slice(copyOffset, receivedBytes).CopyTo(buffer);
|
||||
currentReadOffset += receivedBytes;
|
||||
}
|
||||
|
||||
currentOffset = 0;
|
||||
currentMessageLength = 0;
|
||||
currentMessageRemainingLength = 0;
|
||||
validMessage = true;
|
||||
}
|
||||
}
|
||||
else if(receivedBytes > 0)
|
||||
{
|
||||
currentReadOffset += receivedBytes;
|
||||
}
|
||||
}
|
||||
await ReadExact(stream, buffer, length, lobbyClient.cancellationToken.Token);
|
||||
|
||||
DataReceived?.Invoke(myId, length, buffer.Slice(0, length));
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
activeClients.TryRemove(myId, out _);
|
||||
lobbyClient?.Dispose();
|
||||
ClientDisconnected?.Invoke(myId);
|
||||
activeClients.TryRemove(myId, out var _);
|
||||
client?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
internal string? GetClientIp(int cliendId)
|
||||
private async Task MonitorClients()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (activeClients.TryGetValue(cliendId, out var client))
|
||||
while (running)
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
foreach (var kv in activeClients)
|
||||
{
|
||||
var id = kv.Key;
|
||||
var client = kv.Value;
|
||||
|
||||
if ((now - client.lastSeenUtc) > TimeSpan.FromSeconds(30))
|
||||
{
|
||||
try
|
||||
{
|
||||
client.cancellationToken?.Cancel();
|
||||
client.client?.Close();
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
|
||||
await Task.Delay(1000, cancellationToken.Token);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
internal string? GetClientIp(int cliendIp)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (activeClients.TryGetValue(cliendIp, out var client))
|
||||
{
|
||||
return (client.client!.Client.RemoteEndPoint as IPEndPoint)!.Address.ToString();
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
using System.Net.Sockets;
|
||||
using System.Net;
|
||||
using LobbyServerDto;
|
||||
|
||||
namespace LobbyServer
|
||||
{
|
||||
/// <summary>
|
||||
/// Small udp server to receive udp packets and report their remote ip and port to a event delegate
|
||||
/// Small udp server to receive udp packets and report their remote ip and port to an event delegate
|
||||
/// </summary>
|
||||
internal class UdpCandidateServer : IDisposable
|
||||
{
|
||||
@@ -35,14 +34,18 @@ namespace LobbyServer
|
||||
{
|
||||
var receiveResult = await serverSocket.ReceiveAsync(cancellationTokenSource.Token);
|
||||
if(receiveResult.Buffer.Length > 0)
|
||||
{
|
||||
{
|
||||
Memory<byte> receivedData = new Memory<byte>(receiveResult.Buffer);
|
||||
switch (LobbyMessageIdentifier.ReadLobbyMessageIdentifier(receivedData.Span))
|
||||
var identifier = LobbyMessageIdentifier.ReadLobbyMessageIdentifier(receivedData.Span);
|
||||
switch (identifier)
|
||||
{
|
||||
case QueryExternalPortAndIp.TypeId:
|
||||
case QueryExternalPortAndIp.TypeId:
|
||||
var queryExternalPortAndIp = QueryExternalPortAndIp.Deserialize(receivedData.Span);
|
||||
QueryIpAndPort?.Invoke(queryExternalPortAndIp.LobbyClientId, receiveResult.RemoteEndPoint.Address.ToString(), receiveResult.RemoteEndPoint.Port);
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine($"{DateTime.Now}: [UdpCandidateServer] received unkown identifier {identifier}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Drawing;
|
||||
|
||||
namespace LobbyServerDto
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user