Skip to content

Commit 9439a3f

Browse files
authored
Merge branch 'Corona-Studio:main' into main
2 parents 68d9dfb + e8822f1 commit 9439a3f

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

ConnectX.Client/Proxy/FakeServerMultiCaster.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
199199
}
200200
catch (Exception ex)
201201
{
202-
_logger.LogError(ex, "Error receiving multicast message");
203-
}
204-
finally
205-
{
206-
multicastSocket.Close();
202+
_logger.LogFailedToReceiveMulticastMessage(ex);
207203
}
208204

209205
await Task.Delay(3000, stoppingToken);
@@ -236,4 +232,7 @@ public static partial void LogSendLanServerToPartner(this ILogger logger, string
236232

237233
[LoggerMessage(LogLevel.Information, "Start listening LAN multicast")]
238234
public static partial void LogStartListeningLanMulticast(this ILogger logger);
235+
236+
[LoggerMessage(LogLevel.Error, "Failed to receive multicast message.")]
237+
public static partial void LogFailedToReceiveMulticastMessage(this ILogger logger, Exception exception);
239238
}

ConnectX.Server/Managers/GroupManager.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,9 @@ private void OnCreateGroupReceived(MessageContext<CreateGroup> ctx)
303303
CreateRoomAsync(userId, ctx).Forget();
304304
}
305305

306-
private IPEndPoint? TryAssignRelayServerAddress<T>(Guid userId, MessageContext<T> ctx)
306+
private IPEndPoint? TryAssignRelayServerAddress<T>(Guid userId, int roomSeed, MessageContext<T> ctx)
307307
{
308-
var relayServerAddress = _relayServerManager.GetRandomRelayServerAddress();
308+
var relayServerAddress = _relayServerManager.GetRandomRelayServerAddress(roomSeed);
309309

310310
if (relayServerAddress == null)
311311
{
@@ -388,15 +388,19 @@ private async Task CreateRoomAsync(Guid userId, MessageContext<CreateGroup> ctx)
388388

389389
var message = ctx.Message;
390390
var owner = _userMapping[userId];
391-
var assignedRelayServerAddress = ctx.Message.UseRelayServer ? TryAssignRelayServerAddress(userId, ctx) : null;
391+
var roomSeed = Random.Shared.Next(0, 114514);
392+
var assignedRelayServerAddress = ctx.Message.UseRelayServer
393+
? TryAssignRelayServerAddress(userId, roomSeed, ctx)
394+
: null;
392395
var ownerSession = new UserSessionInfo(owner, assignedRelayServerAddress);
393396

394397
var group = new Group(message.RoomName, message.RoomPassword, ownerSession, [ownerSession])
395398
{
396399
IsPrivate = message.IsPrivate,
397400
MaxUserCount = message.MaxUserCount <= 0 ? 10 : message.MaxUserCount,
398401
RoomDescription = message.RoomDescription,
399-
NetworkId = Convert.ToUInt64(networkDetail.Id, 16)
402+
NetworkId = Convert.ToUInt64(networkDetail.Id, 16),
403+
RelayServerSeed = roomSeed
400404
};
401405

402406
if (assignedRelayServerAddress != null &&
@@ -482,7 +486,7 @@ private void OnJoinGroupReceived(MessageContext<JoinGroup> ctx)
482486

483487
var user = _userMapping[userId];
484488
var assignedRelayServerAddress =
485-
ctx.Message.UseRelayServer ? TryAssignRelayServerAddress(userId, ctx) : null;
489+
ctx.Message.UseRelayServer ? TryAssignRelayServerAddress(userId, group.RelayServerSeed, ctx) : null;
486490
var info = new UserSessionInfo(user, assignedRelayServerAddress);
487491

488492
group.Users.Add(info);

ConnectX.Server/Managers/RelayServerManager.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ private bool IsSessionAttached(ISession session)
4848
return true;
4949
}
5050

51-
public IPEndPoint? GetRandomRelayServerAddress()
51+
public IPEndPoint? GetRandomRelayServerAddress(int roomSeed)
5252
{
5353
if (_serverAddressMapping.IsEmpty) return null;
5454

55-
return Random.Shared.GetItems(_serverAddressMapping.Values.ToArray(), 1)[0];
55+
var servers = _serverAddressMapping.Values.ToArray();
56+
57+
return servers[roomSeed % servers.Length];
5658
}
5759

5860
public bool TryGetRelayServerSession(IPEndPoint endPoint, [NotNullWhen(true)] out ISession? session)

ConnectX.Server/Models/Group.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class Group(
1010
List<UserSessionInfo> users)
1111
{
1212
public Guid RoomId { get; } = Guid.CreateVersion7();
13+
public required int RelayServerSeed { get; init; }
1314
public UserSessionInfo RoomOwner { get; } = roomOwner;
1415
public string RoomShortId { get; } = RandomHelper.GetRandomString();
1516
public required ulong NetworkId { get; set; }

ConnectX.Server/Services/PeerInfoService.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
4343
var status = await zeroTierApiService.GetNetworkPeersAsync(stoppingToken);
4444
peers = status?.ToImmutableList();
4545
}
46+
catch (TaskCanceledException e)
47+
{
48+
if (stoppingToken.IsCancellationRequested)
49+
throw;
50+
51+
_logger.LogFailedToGetNetworkPeers(e);
52+
53+
await Task.Delay(1000, stoppingToken);
54+
continue;
55+
}
4656
catch (HttpRequestException e)
4757
{
4858
_logger.LogFailedToGetNetworkPeers(e);

0 commit comments

Comments
 (0)