Skip to content

Commit 7c209f8

Browse files
MTT-3026 feat: Lobby and Authentication services to use async await (#596)
* adding wip for solution, focusing on query lobbies. removing async wrapping needs, let's use async await properly # Conflicts: # Assets/BossRoom/Scripts/Client/UI/Lobby/LobbyUIMediator.cs # Assets/BossRoom/Scripts/Shared/Infrastructure/UpdateRunner.cs # Assets/BossRoom/Scripts/Shared/Net/UnityServices/Auth/AuthenticationServiceFacade.cs # Assets/BossRoom/Scripts/Shared/Net/UnityServices/Infrastructure/RateLimitCooldown.cs # Assets/BossRoom/Scripts/Shared/Net/UnityServices/Lobbies/LobbyServiceFacade.cs * Taking Sam's changes further All of the methods that are exposed are now async-await based and are wrapping native Tasks with basic error handling. # Conflicts: # Assets/BossRoom/Scripts/Shared/Net/UnityServices/Lobbies/LobbyAPIInterface.cs * wip - using async api's everywhere # Conflicts: # Assets/BossRoom/Scripts/Client/Game/State/ClientMainMenuState.cs # Assets/BossRoom/Scripts/Shared/Net/UnityServices/Auth/AuthenticationServiceFacade.cs * # * # * Logic updated to rely on async-await pattern Rate limiting code is now simplified and is not handling retires (should be handled by the sdk) * # * # * # * # * # * # * # * Addressing pr comments * Formatting and redundant usings Co-authored-by: Samuel Bellomo <[email protected]>
1 parent 0e98b21 commit 7c209f8

15 files changed

+301
-356
lines changed

Assets/BossRoom/Scripts/Client/Game/State/ClientMainMenuState.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using BossRoom.Scripts.Shared.Net.UnityServices.Auth;
23
using Unity.Multiplayer.Samples.BossRoom.Shared;
34
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
@@ -43,16 +44,24 @@ protected override void InitializeScope()
4344
}
4445

4546
[Inject]
46-
void InjectDependenciesAndInitialize(AuthenticationServiceFacade authServiceFacade, LocalLobbyUser localUser, LocalLobby localLobby)
47+
async void InjectDependenciesAndInitialize(AuthenticationServiceFacade authServiceFacade, LocalLobbyUser localUser, LocalLobby localLobby)
4748
{
48-
var unityAuthenticationInitOptions = new InitializationOptions();
49-
var profile = ProfileManager.Profile;
50-
if (profile.Length > 0)
49+
try
5150
{
52-
unityAuthenticationInitOptions.SetProfile(profile);
53-
}
51+
var unityAuthenticationInitOptions = new InitializationOptions();
52+
var profile = ProfileManager.Profile;
53+
if (profile.Length > 0)
54+
{
55+
unityAuthenticationInitOptions.SetProfile(profile);
56+
}
5457

55-
authServiceFacade.DoSignInAsync(OnAuthSignIn, OnSignInFailed, unityAuthenticationInitOptions);
58+
await authServiceFacade.SignInAsync(unityAuthenticationInitOptions);
59+
OnAuthSignIn();
60+
}
61+
catch (Exception)
62+
{
63+
OnSignInFailed();
64+
}
5665

5766
void OnAuthSignIn()
5867
{

Assets/BossRoom/Scripts/Client/UI/Lobby/LobbyUIMediator.cs

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Unity.Multiplayer.Samples.BossRoom.Client;
44
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
55
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Lobbies;
6-
using Unity.Services.Authentication;
6+
using Unity.Services.Core;
77
using Unity.Services.Lobbies.Models;
88
using UnityEngine;
99

@@ -62,78 +62,96 @@ void OnDestroy()
6262

6363
//Lobby and Relay calls done from UI
6464

65-
public void CreateLobbyRequest(string lobbyName, bool isPrivate, int maxPlayers)
65+
public async void CreateLobbyRequest(string lobbyName, bool isPrivate, int maxPlayers)
6666
{
6767
// before sending request to lobby service, populate an empty lobby name, if necessary
6868
if (string.IsNullOrEmpty(lobbyName))
6969
{
7070
lobbyName = k_DefaultLobbyName;
7171
}
7272

73-
m_LobbyServiceFacade.CreateLobbyAsync(lobbyName, maxPlayers, isPrivate, OnCreatedLobby, OnFailedLobbyCreateOrJoin);
7473
BlockUIWhileLoadingIsInProgress();
74+
75+
var lobbyCreationAttempt = await m_LobbyServiceFacade.TryCreateLobbyAsync(lobbyName, maxPlayers, isPrivate);
76+
77+
if (lobbyCreationAttempt.Success)
78+
{
79+
m_LocalUser.IsHost = true;
80+
m_LobbyServiceFacade.SetRemoteLobby(lobbyCreationAttempt.Lobby);
81+
82+
m_GameNetPortal.PlayerName = m_LocalUser.DisplayName;
83+
84+
Debug.Log($"Created lobby with ID: {m_LocalLobby.LobbyID} and code {m_LocalLobby.LobbyCode}, Internal Relay Join Code{m_LocalLobby.RelayJoinCode}");
85+
m_GameNetPortal.StartUnityRelayHost();
86+
}
87+
else
88+
{
89+
UnblockUIAfterLoadingIsComplete();
90+
}
7591
}
7692

77-
public void QueryLobbiesRequest(bool blockUI)
93+
public async void QueryLobbiesRequest(bool blockUI)
7894
{
79-
if (!AuthenticationService.Instance.IsAuthorized)
95+
if (UnityServices.State != ServicesInitializationState.Initialized)
8096
{
8197
return;
8298
}
8399

84-
m_LobbyServiceFacade.RetrieveLobbyListAsync(
85-
OnSuccess,
86-
OnFailure
87-
);
88-
89100
if (blockUI)
90101
{
91102
BlockUIWhileLoadingIsInProgress();
92103
}
93104

94-
void OnSuccess(QueryResponse qr)
105+
await m_LobbyServiceFacade.RetrieveAndPublishLobbyListAsync();
106+
UnblockUIAfterLoadingIsComplete();
107+
}
108+
109+
public async void JoinLobbyWithCodeRequest(string lobbyCode)
110+
{
111+
BlockUIWhileLoadingIsInProgress();
112+
113+
var result = await m_LobbyServiceFacade.TryJoinLobbyAsync(null, lobbyCode);
114+
115+
if (result.Success)
95116
{
96-
UnblockUIAfterLoadingIsComplete();
117+
OnJoinedLobby(result.Lobby);
97118
}
98-
99-
void OnFailure()
119+
else
100120
{
101121
UnblockUIAfterLoadingIsComplete();
102122
}
103123
}
104124

105-
public void JoinLobbyWithCodeRequest(string lobbyCode)
125+
public async void JoinLobbyRequest(LocalLobby lobby)
106126
{
107-
m_LobbyServiceFacade.JoinLobbyAsync(null, lobbyCode, OnJoinedLobby, OnFailedLobbyCreateOrJoin);
108127
BlockUIWhileLoadingIsInProgress();
109-
}
110128

111-
public void JoinLobbyRequest(LocalLobby lobby)
112-
{
113-
m_LobbyServiceFacade.JoinLobbyAsync(lobby.LobbyID, lobby.LobbyCode, OnJoinedLobby, OnFailedLobbyCreateOrJoin);
114-
BlockUIWhileLoadingIsInProgress();
115-
}
116-
117-
public void QuickJoinRequest()
118-
{
119-
m_LobbyServiceFacade.QuickJoinLobbyAsync(OnJoinedLobby, OnFailedLobbyCreateOrJoin);
120-
BlockUIWhileLoadingIsInProgress();
121-
}
129+
var result = await m_LobbyServiceFacade.TryJoinLobbyAsync(lobby.LobbyID, lobby.LobbyCode);
122130

123-
void OnFailedLobbyCreateOrJoin()
124-
{
125-
UnblockUIAfterLoadingIsComplete();
131+
if (result.Success)
132+
{
133+
OnJoinedLobby(result.Lobby);
134+
}
135+
else
136+
{
137+
UnblockUIAfterLoadingIsComplete();
138+
}
126139
}
127140

128-
void OnCreatedLobby(Lobby lobby)
141+
public async void QuickJoinRequest()
129142
{
130-
m_LocalUser.IsHost = true;
131-
m_LobbyServiceFacade.SetRemoteLobby(lobby);
143+
BlockUIWhileLoadingIsInProgress();
132144

133-
m_GameNetPortal.PlayerName = m_LocalUser.DisplayName;
145+
var result = await m_LobbyServiceFacade.TryQuickJoinLobbyAsync();
134146

135-
Debug.Log($"Created lobby with ID: {m_LocalLobby.LobbyID} and code {m_LocalLobby.LobbyCode}, Internal Relay Join Code{m_LocalLobby.RelayJoinCode}");
136-
m_GameNetPortal.StartUnityRelayHost();
147+
if (result.Success)
148+
{
149+
OnJoinedLobby(result.Lobby);
150+
}
151+
else
152+
{
153+
UnblockUIAfterLoadingIsComplete();
154+
}
137155
}
138156

139157
void OnJoinedLobby(Lobby remoteLobby)

Assets/BossRoom/Scripts/Client/UI/UnityServicesUIHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void ServiceErrorHandler(UnityServiceErrorMessage error)
5050
void HandleLobbyError(UnityServiceErrorMessage error)
5151
{
5252
var errorMessage = error.Message;
53+
5354
switch (((LobbyServiceException)error.OriginalException).Reason)
5455
{
5556
case LobbyExceptionReason.LobbyConflict:
@@ -72,7 +73,7 @@ void HandleLobbyError(UnityServiceErrorMessage error)
7273

7374
void OnDestroy()
7475
{
75-
m_Subscriptions.Dispose();
76+
m_Subscriptions?.Dispose();
7677
}
7778
}
7879
}

Assets/BossRoom/Scripts/Shared/ApplicationController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private void Start()
7979

8080
private void OnDestroy()
8181
{
82-
m_LobbyServiceFacade.EndTracking();
82+
m_LobbyServiceFacade?.EndTracking();
8383
DIScope.RootScope.Dispose();
8484
}
8585

Assets/BossRoom/Scripts/Shared/Infrastructure/UpdateRunner.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class UpdateRunner : MonoBehaviour
1313
class SubscriberData
1414
{
1515
public float Period;
16-
public float PeriodCurrent;
16+
public float NextCallTime;
1717
}
1818

1919
readonly Queue<Action> m_PendingHandlers = new Queue<Action>();
@@ -31,7 +31,7 @@ public void OnDestroy()
3131
/// Subscribe in order to have onUpdate called approximately every period seconds (or every frame, if period <= 0).
3232
/// Don't assume that onUpdate will be called in any particular order compared to other subscribers.
3333
/// </summary>
34-
public void Subscribe(Action<float> onUpdate, float period)
34+
public void Subscribe(Action<float> onUpdate, float updatePeriod)
3535
{
3636
if (onUpdate == null)
3737
{
@@ -56,7 +56,7 @@ public void Subscribe(Action<float> onUpdate, float period)
5656
{
5757
if (m_Subscribers.Add(onUpdate))
5858
{
59-
m_SubscriberData.Add(onUpdate, new SubscriberData() {Period = period, PeriodCurrent = 0});
59+
m_SubscriberData.Add(onUpdate, new SubscriberData() {Period = updatePeriod, NextCallTime = 0});
6060
}
6161
});
6262
}
@@ -84,17 +84,14 @@ void Update()
8484
m_PendingHandlers.Dequeue()?.Invoke();
8585
}
8686

87-
float dt = Time.deltaTime;
88-
8987
foreach (var subscriber in m_Subscribers)
9088
{
9189
var subscriberData = m_SubscriberData[subscriber];
92-
subscriberData.PeriodCurrent += dt;
9390

94-
if (subscriberData.PeriodCurrent > subscriberData.Period)
91+
if (Time.time >= subscriberData.NextCallTime)
9592
{
96-
subscriber.Invoke(subscriberData.PeriodCurrent);
97-
subscriberData.PeriodCurrent = 0;
93+
subscriber.Invoke(Time.deltaTime);
94+
subscriberData.NextCallTime = Time.time + subscriberData.Period;
9895
}
9996
}
10097
}

Assets/BossRoom/Scripts/Shared/Net/ConnectionManagement/ClientGameNetPortal.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,10 @@ public async void StartClientUnityRelayModeAsync(string joinCode, Action<string>
198198

199199
try
200200
{
201-
var clientRelayUtilityTask = UnityRelayUtilities.JoinRelayServerFromJoinCode(joinCode);
202-
await clientRelayUtilityTask;
203-
var (ipv4Address, port, allocationIdBytes, connectionData, hostConnectionData, key) = clientRelayUtilityTask.Result;
201+
var (ipv4Address, port, allocationIdBytes, connectionData, hostConnectionData, key) =
202+
await UnityRelayUtilities.JoinRelayServerFromJoinCode(joinCode);
204203

205-
m_LobbyServiceFacade.UpdatePlayerRelayInfoAsync(allocationIdBytes.ToString(), joinCode, null, null);
204+
await m_LobbyServiceFacade.UpdatePlayerRelayInfoAsync(allocationIdBytes.ToString(), joinCode);
206205
utp.SetClientRelayData(ipv4Address, port, allocationIdBytes, key, connectionData, hostConnectionData, isSecure: true);
207206
}
208207
catch (Exception e)

Assets/BossRoom/Scripts/Shared/Net/ConnectionManagement/GameNetPortal.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,13 @@ public async void StartUnityRelayHost()
200200

201201
try
202202
{
203-
// we now need to get the joinCode?
204-
var serverRelayUtilityTask = UnityRelayUtilities.AllocateRelayServerAndGetJoinCode(k_MaxUnityRelayConnections);
205-
await serverRelayUtilityTask;
206-
// we now have the info from the relay service
207-
var (ipv4Address, port, allocationIdBytes, connectionData, key, joinCode) = serverRelayUtilityTask.Result;
203+
var (ipv4Address, port, allocationIdBytes, connectionData, key, joinCode) =
204+
await UnityRelayUtilities.AllocateRelayServerAndGetJoinCode(k_MaxUnityRelayConnections);
208205

209206
m_LocalLobby.RelayJoinCode = joinCode;
210207
//next line enabled lobby and relay services integration
211-
m_LobbyServiceFacade.UpdatePlayerRelayInfoAsync(allocationIdBytes.ToString(), joinCode, null, null);
208+
await m_LobbyServiceFacade.UpdateLobbyDataAsync(m_LocalLobby.GetDataForUnityServices());
209+
await m_LobbyServiceFacade.UpdatePlayerRelayInfoAsync(allocationIdBytes.ToString(), joinCode);
212210

213211
// we now need to set the RelayCode somewhere :P
214212
utp.SetHostRelayData(ipv4Address, port, allocationIdBytes, key, connectionData, isSecure: true);

Assets/BossRoom/Scripts/Shared/Net/ConnectionManagement/ServerGameNetPortal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void OnClientDisconnect(ulong clientId)
100100
m_Portal.NetManager.OnClientDisconnectCallback -= OnClientDisconnect;
101101
if (m_LobbyServiceFacade.CurrentUnityLobby != null)
102102
{
103-
m_LobbyServiceFacade.DeleteLobbyAsync(m_LobbyServiceFacade.CurrentUnityLobby.Id, null, null);
103+
m_LobbyServiceFacade.DeleteLobbyAsync(m_LobbyServiceFacade.CurrentUnityLobby.Id);
104104
}
105105
}
106106
else

Assets/BossRoom/Scripts/Shared/Net/UnityServices/Auth/AuthenticationServiceFacade.cs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,25 @@ void InjectDependencies(IPublisher<UnityServiceErrorMessage> unityServiceErrorMe
1818
m_UnityServiceErrorMessagePublisher = unityServiceErrorMessagePublisher;
1919
}
2020

21-
void OnServiceException(Exception e)
21+
public async Task SignInAsync(InitializationOptions initializationOptions)
2222
{
23-
Debug.LogWarning(e.Message);
24-
var reason = $"{e.Message} ({e.InnerException?.Message})";
25-
m_UnityServiceErrorMessagePublisher.Publish(new UnityServiceErrorMessage("Authentication Error", reason, UnityServiceErrorMessage.Service.Authentication, e));
26-
}
27-
28-
public void DoSignInAsync(Action onSigninComplete, Action onFailed, InitializationOptions initializationOptions)
29-
{
30-
var task = TrySignIn(initializationOptions);
31-
UnityServiceCallsTaskWrapper.RunTask<Exception>(task, onSigninComplete, onFailed, OnServiceException);
32-
}
33-
34-
async Task TrySignIn(InitializationOptions initializationOptions)
35-
{
36-
await Unity.Services.Core.UnityServices.InitializeAsync(initializationOptions);
23+
try
24+
{
25+
await Unity.Services.Core.UnityServices.InitializeAsync(initializationOptions);
3726

38-
if (!AuthenticationService.Instance.IsSignedIn)
27+
if (!AuthenticationService.Instance.IsSignedIn)
28+
{
29+
await AuthenticationService.Instance.SignInAnonymouslyAsync();
30+
}
31+
}
32+
catch (Exception e)
3933
{
40-
await AuthenticationService.Instance.SignInAnonymouslyAsync();
34+
var reason = $"{e.Message} ({e.InnerException?.Message})";
35+
m_UnityServiceErrorMessagePublisher.Publish(new UnityServiceErrorMessage("Authentication Error", reason, UnityServiceErrorMessage.Service.Authentication, e));
36+
throw;
4137
}
38+
39+
4240
}
4341
}
4442
}

0 commit comments

Comments
 (0)