Skip to content

Commit 2cb609d

Browse files
authored
Merge branch 'Corona-Studio:main' into main
2 parents 79f50d3 + 34d0639 commit 2cb609d

24 files changed

+452
-161
lines changed

ConnectX.Client/Client.cs

Lines changed: 65 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using ConnectX.Client.Interfaces;
1+
using System.Collections.ObjectModel;
2+
using ConnectX.Client.Interfaces;
23
using ConnectX.Client.Managers;
34
using ConnectX.Client.Route;
45
using ConnectX.Shared.Helpers;
@@ -14,6 +15,12 @@ namespace ConnectX.Client;
1415

1516
public record PartnerConnectionState(bool IsConnected, bool IsDirectConnection, int Latency);
1617

18+
public record OpResult(
19+
GroupInfo? Info,
20+
GroupCreationStatus Status,
21+
IReadOnlyDictionary<string, string> Metadata,
22+
string? Error);
23+
1724
public class Client
1825
{
1926
private readonly IDispatcher _dispatcher;
@@ -112,12 +119,17 @@ private void OnGroupUserStateChanged(MessageContext<GroupUserStateChanged> ctx)
112119

113120
switch (state)
114121
{
122+
case GroupUserStates.Joined:
123+
if (_isInGroup && _roomInfoManager.CurrentGroupInfo?.RoomId != null)
124+
_roomInfoManager.AcquireGroupInfoAsync(_roomInfoManager.CurrentGroupInfo!.RoomId).Forget();
125+
break;
115126
case GroupUserStates.Left:
116127
case GroupUserStates.Disconnected:
117128
case GroupUserStates.Kicked:
118129
if (userInfo?.UserId == _serverLinkHolder.UserId)
119130
ResetRoomState().Forget();
120-
else _roomInfoManager.AcquireGroupInfoAsync(_roomInfoManager.CurrentGroupInfo!.RoomId).Forget();
131+
else if (_isInGroup && _roomInfoManager.CurrentGroupInfo?.RoomId != null)
132+
_roomInfoManager.AcquireGroupInfoAsync(_roomInfoManager.CurrentGroupInfo!.RoomId).Forget();
121133
break;
122134
case GroupUserStates.Dismissed:
123135
ResetRoomState().Forget();
@@ -127,74 +139,84 @@ private void OnGroupUserStateChanged(MessageContext<GroupUserStateChanged> ctx)
127139
OnGroupStateChanged?.Invoke(state, userInfo);
128140
}
129141

130-
private async Task<(GroupInfo?, GroupCreationStatus, string?)> PerformOpAndGetRoomInfoAsync<T>(T message, CancellationToken ct)
142+
private async Task<OpResult> PerformOpAndGetRoomInfoAsync<T>(T message, CancellationToken ct)
131143
{
132-
var createResult = await PerformGroupOpAsync(message);
144+
var opResult = await PerformGroupOpAsync(message);
133145

134-
if (createResult == null)
135-
return (null, GroupCreationStatus.Other, null);
136-
if (createResult.Status != GroupCreationStatus.Succeeded)
137-
return (null, createResult.Status, createResult.ErrorMessage);
146+
if (opResult == null)
147+
return new OpResult(null, GroupCreationStatus.Other, ReadOnlyDictionary<string, string>.Empty, null);
148+
if (opResult.Status != GroupCreationStatus.Succeeded)
149+
return new OpResult(null, opResult.Status, ReadOnlyDictionary<string, string>.Empty, opResult.ErrorMessage);
138150

139-
var groupInfo = await _roomInfoManager.AcquireGroupInfoAsync(createResult.RoomId);
151+
var groupInfo = await _roomInfoManager.AcquireGroupInfoAsync(opResult.RoomId);
140152

141153
if (groupInfo == null)
142-
return (null, GroupCreationStatus.Other, "Failed to acquire group info");
154+
return new OpResult(null, GroupCreationStatus.Other, ReadOnlyDictionary<string, string>.Empty, "Failed to acquire group info");
143155

144156
if (OperatingSystem.IsWindows() &&
145157
message is JoinGroup or CreateGroup)
146158
{
147-
if (createResult.Status != GroupCreationStatus.Succeeded)
148-
return (null, createResult.Status, createResult.ErrorMessage);
149-
150-
_logger.LogJoiningNetwork(groupInfo.RoomNetworkId);
151-
152-
var networkResult = await _zeroTierNodeLinkHolder.JoinNetworkAsync(groupInfo.RoomNetworkId, ct);
153-
154-
if (!networkResult)
155-
return (null, GroupCreationStatus.Other, "Failed to join the network");
159+
if (opResult.Status != GroupCreationStatus.Succeeded)
160+
return new OpResult(null, opResult.Status, ReadOnlyDictionary<string, string>.Empty, opResult.ErrorMessage);
156161

157-
await TaskHelper.WaitUntilAsync(_zeroTierNodeLinkHolder.IsNodeOnline, ct);
158-
159-
var nodeId = _zeroTierNodeLinkHolder.Node!.IdString;
160-
var updateInfo = new UpdateRoomMemberNetworkInfo
162+
if (opResult.Metadata.TryGetValue(GroupOpResult.UseRelayServerKey, out var useRelayServerStr) &&
163+
bool.TryParse(useRelayServerStr, out var useRelayServer) &&
164+
!useRelayServer)
161165
{
162-
NetworkNodeId = nodeId,
163-
NetworkIpAddresses = _zeroTierNodeLinkHolder.GetIpAddresses()
164-
};
165-
166-
var result = await _dispatcher.SendAndListenOnce<UpdateRoomMemberNetworkInfo, GroupOpResult>(
167-
_serverLinkHolder.ServerSession!,
168-
updateInfo, ct);
169-
170-
if (result is not { Status: GroupCreationStatus.Succeeded })
171-
return (null, result?.Status ?? GroupCreationStatus.Other, "Failed to update room member network info");
166+
// If the group is not using relay server, we need to join the network
167+
_logger.LogJoiningNetwork(groupInfo.RoomNetworkId);
168+
169+
var networkResult = await _zeroTierNodeLinkHolder.JoinNetworkAsync(groupInfo.RoomNetworkId, ct);
170+
171+
if (!networkResult)
172+
return new OpResult(null, GroupCreationStatus.Other, ReadOnlyDictionary<string, string>.Empty, "Failed to join the network");
173+
174+
await TaskHelper.WaitUntilAsync(_zeroTierNodeLinkHolder.IsNodeOnline, ct);
175+
176+
var nodeId = _zeroTierNodeLinkHolder.Node!.IdString;
177+
var updateInfo = new UpdateRoomMemberNetworkInfo
178+
{
179+
NetworkNodeId = nodeId,
180+
NetworkIpAddresses = _zeroTierNodeLinkHolder.GetIpAddresses()
181+
};
182+
183+
var result = await _dispatcher.SendAndListenOnce<UpdateRoomMemberNetworkInfo, GroupOpResult>(
184+
_serverLinkHolder.ServerSession!,
185+
updateInfo, ct);
186+
187+
if (result is not { Status: GroupCreationStatus.Succeeded })
188+
return new OpResult(
189+
null,
190+
result?.Status ?? GroupCreationStatus.Other,
191+
ReadOnlyDictionary<string, string>.Empty,
192+
"Failed to update room member network info");
193+
}
172194
}
173195

174196
_isInGroup = true;
175-
return (groupInfo, GroupCreationStatus.Succeeded, null);
197+
return new OpResult(groupInfo, GroupCreationStatus.Succeeded, opResult.Metadata, null);
176198
}
177199

178-
public async Task<(GroupInfo? Info, GroupCreationStatus Status, string? Error)> CreateGroupAsync(CreateGroup createGroup, CancellationToken ct)
200+
public async Task<OpResult> CreateGroupAsync(CreateGroup createGroup, CancellationToken ct)
179201
{
180202
if (!_serverLinkHolder.IsConnected)
181-
return (null, GroupCreationStatus.Other, "Not connected to the server");
203+
return new OpResult(null, GroupCreationStatus.Other, ReadOnlyDictionary<string, string>.Empty, "Not connected to the server");
182204
if (!_serverLinkHolder.IsSignedIn)
183-
return (null, GroupCreationStatus.Other, "Not signed in");
205+
return new OpResult(null, GroupCreationStatus.Other, ReadOnlyDictionary<string, string>.Empty, "Not signed in");
184206
if (!OperatingSystem.IsWindows() && !createGroup.UseRelayServer)
185-
return (null, GroupCreationStatus.Other, "Only Windows platform supports direct connection");
207+
return new OpResult(null, GroupCreationStatus.Other, ReadOnlyDictionary<string, string>.Empty, "Only Windows platform supports direct connection");
186208

187209
return await PerformOpAndGetRoomInfoAsync(createGroup, ct);
188210
}
189211

190-
public async Task<(GroupInfo?, GroupCreationStatus, string?)> JoinGroupAsync(JoinGroup joinGroup, CancellationToken ct)
212+
public async Task<OpResult> JoinGroupAsync(JoinGroup joinGroup, CancellationToken ct)
191213
{
192214
if (!_serverLinkHolder.IsConnected)
193-
return (null, GroupCreationStatus.Other, "Not connected to the server");
215+
return new OpResult(null, GroupCreationStatus.Other, ReadOnlyDictionary<string, string>.Empty, "Not connected to the server");
194216
if (!_serverLinkHolder.IsSignedIn)
195-
return (null, GroupCreationStatus.Other, "Not signed in");
196-
if (!OperatingSystem.IsWindows() && !joinGroup.UseRelayServer)
197-
return (null, GroupCreationStatus.Other, "Only Windows platform supports direct connection");
217+
return new OpResult(null, GroupCreationStatus.Other, ReadOnlyDictionary<string, string>.Empty, "Not signed in");
218+
//if (!OperatingSystem.IsWindows() && !joinGroup.UseRelayServer)
219+
// return (null, GroupCreationStatus.Other, "Only Windows platform supports direct connection");
198220

199221
return await PerformOpAndGetRoomInfoAsync(joinGroup, ct);
200222
}

ConnectX.Client/Managers/RoomInfoManager.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
5454
continue;
5555
}
5656

57-
if (OperatingSystem.IsWindows())
57+
if (OperatingSystem.IsWindows() &&
58+
!CurrentGroupInfo.UseRelayServer)
5859
{
5960
await TaskHelper.WaitUntilAsync(zeroTierNodeLinkHolder.IsNodeOnline, stoppingToken);
6061
await TaskHelper.WaitUntilAsync(zeroTierNodeLinkHolder.IsNetworkReady, stoppingToken);
@@ -80,7 +81,8 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
8081
if ((self.NetworkIpAddresses == null ||
8182
self.NetworkIpAddresses.Length == 0 ||
8283
string.IsNullOrEmpty(self.NetworkNodeId)) &&
83-
OperatingSystem.IsWindows())
84+
OperatingSystem.IsWindows() &&
85+
!CurrentGroupInfo.UseRelayServer)
8486
{
8587
needToRefreshRoomInfo = true;
8688

ConnectX.ClientConsole/ConnectX.ClientConsole.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<ItemGroup>
2020
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.5" />
21-
<PackageReference Include="Serilog" Version="4.2.0" />
21+
<PackageReference Include="Serilog" Version="4.3.0" />
2222
<PackageReference Include="Serilog.Extensions.Hosting" Version="9.0.0" />
2323
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
2424
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />

ConnectX.ClientConsole/ConsoleService.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ private Command RoomCommand()
102102
{
103103
Commands.Room.Join.RoomIdOption,
104104
Commands.Room.Join.RoomShortIdOption,
105-
Commands.Room.PasswordOption,
106-
Commands.Room.UseRelayServerOption
105+
Commands.Room.PasswordOption
107106
};
108107

109108
joinCommand.SetHandler(HandleRoomJoinAsync);
@@ -159,7 +158,6 @@ private async Task HandleRoomJoinAsync(InvocationContext obj)
159158
var roomId = (Guid?)obj.ParseResult.GetValueForOption(Commands.Room.Join.RoomIdOption);
160159
var roomShortId = (string?)obj.ParseResult.GetValueForOption(Commands.Room.Join.RoomShortIdOption);
161160
var password = (string?)obj.ParseResult.GetValueForOption(Commands.Room.PasswordOption);
162-
var useRelayServer = (bool)obj.ParseResult.GetValueForOption(Commands.Room.UseRelayServerOption)!;
163161

164162
if (!roomId.HasValue && string.IsNullOrEmpty(roomShortId))
165163
{
@@ -172,12 +170,12 @@ private async Task HandleRoomJoinAsync(InvocationContext obj)
172170
GroupId = roomId ?? Guid.Empty,
173171
RoomShortId = roomShortId,
174172
RoomPassword = password,
175-
UseRelayServer = useRelayServer
173+
// UseRelayServer = useRelayServer
176174
};
177175

178-
var (groupInfo, status, error) = await client.JoinGroupAsync(message, CancellationToken.None);
176+
var (groupInfo, status, metadata, error) = await client.JoinGroupAsync(message, CancellationToken.None);
179177

180-
logger.LogInformation("Room join result received, {@info}, {status:G}, {error}", groupInfo, status, error);
178+
logger.LogInformation("Room join result received, Info: {@info}, Status: {status:G}, Metadata: {@metadata}, Error: {error}", groupInfo, status, metadata, error);
181179

182180
_lastGroupInfo = groupInfo;
183181
}
@@ -201,9 +199,9 @@ private async Task HandleRoomCreateAsync(InvocationContext obj)
201199
UseRelayServer = useRelayServer
202200
};
203201

204-
var (groupInfo, status, error) = await client.CreateGroupAsync(message, CancellationToken.None);
205-
206-
logger.LogInformation("Room created, {@info}, {status:G}, {error}", groupInfo, status, error);
202+
var (groupInfo, status, metadata, error) = await client.CreateGroupAsync(message, CancellationToken.None);
203+
204+
logger.LogInformation("Room join result received, Info: {@info}, Status: {status:G}, Metadata: {@metadata}, Error: {error}", groupInfo, status, metadata, error);
207205

208206
_lastGroupInfo = groupInfo;
209207
}

ConnectX.MessageRegister.SourceGenerator/ConnectX.MessageRegister.SourceGenerator.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
</PropertyGroup>
1414

1515
<ItemGroup>
16-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.13.0" PrivateAssets="all" />
17-
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0">
16+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" PrivateAssets="all" />
17+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="4.14.0">
1818
<PrivateAssets>all</PrivateAssets>
1919
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2020
</PackageReference>

ConnectX.Relay/ConnectX.Relay.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.4.0" />
2121
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.5" />
2222
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.5" />
23-
<PackageReference Include="Serilog" Version="4.2.0" />
23+
<PackageReference Include="Serilog" Version="4.3.0" />
2424
<PackageReference Include="Serilog.Extensions.Hosting" Version="9.0.0" />
2525
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
2626
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />

ConnectX.Relay/DefaultServerSettingProvider.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ public class DefaultServerSettingProvider : IServerSettingProvider
1919

2020
public required IPEndPoint EndPoint { get; init; }
2121
public required IPEndPoint RelayEndPoint { get; init; }
22+
23+
public int MaxReferenceConnectionCount { get; init; }
24+
25+
public uint ServerPriority { get; init; }
2226
}

ConnectX.Relay/Interfaces/IServerSettingProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ public interface IServerSettingProvider
1818

1919
IPEndPoint EndPoint { get; }
2020
IPEndPoint RelayEndPoint { get; }
21+
22+
int MaxReferenceConnectionCount { get; }
23+
24+
// the value should be between 0-100
25+
uint ServerPriority { get; }
2126
}

0 commit comments

Comments
 (0)