浏览代码

Removed Wire use and Listeners.

Changed Heartbeatloop to be self-contained to the LobbyAsync implementation
Changed UpdateLoop in LobbyContentUpdater to a repeating task.
/main/staging/2021_Upgrade/Async_Refactor
当前提交
df835641
共有 7 个文件被更改,包括 62 次插入127 次删除
  1. 1
      Assets/Scripts/GameLobby/Game/GameManager.cs
  2. 13
      Assets/Scripts/GameLobby/Lobby/LobbyAPIInterface.cs
  3. 64
      Assets/Scripts/GameLobby/Lobby/LobbyAsyncRequests.cs
  4. 65
      Assets/Scripts/GameLobby/Lobby/LobbyContentUpdater.cs
  5. 15
      Packages/manifest.json
  6. 16
      Packages/packages-lock.json
  7. 15
      ProjectSettings/PackageManagerSettings.asset

1
Assets/Scripts/GameLobby/Game/GameManager.cs


using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using LobbyRelaySample.lobby;
using UnityEngine;
using UnityEngine.Serialization;

13
Assets/Scripts/GameLobby/Lobby/LobbyAPIInterface.cs


var task = LobbyService.Instance.UpdatePlayerAsync(lobbyId, playerId, updateOptions);
AsyncRequestLobby.Instance.DoRequest(task, onComplete);
}*/
public static void SubscribeToLobbyUpdates(string lobbyId, LobbyEventCallbacks lobbyEvent, Action<ILobbyEvents> onLobbySubscribed)
{
var task = LobbyService.Instance.SubscribeToLobbyEventsAsync(lobbyId, lobbyEvent);
AsyncRequestLobby.Instance.DoRequest(task, onLobbySubscribed);
}
public static void HeartbeatPlayerAsync(string lobbyId)
{
var task = LobbyService.Instance.SendHeartbeatPingAsync(lobbyId);
AsyncRequestLobby.Instance.DoRequest(task, null);
}
}
}

64
Assets/Scripts/GameLobby/Lobby/LobbyAsyncRequests.cs


using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Unity.Services.Authentication;

}
}
#region Lobby
public Action<Lobby> onLobbyUpdated;

/// <summary>
/// Store the LobbySubscription so we can unsubscribe later.
/// </summary>
ILobbyEvents m_lobbySubscription;
LobbyEventCallbacks m_lobbyEvents = new LobbyEventCallbacks();
#endregion
#region Lobby API calls are rate limited, and some other operations might want an alert when the rate limits have passed.

}
//TODO Back to Polling i Guess
void BeginListening(string lobbyID)
{
m_lobbyEvents = new LobbyEventCallbacks();
m_lobbyEvents.LobbyChanged += OnRemoteLobbyChanged;
LobbyAPIInterface.SubscribeToLobbyUpdates(lobbyID, m_lobbyEvents, sub =>
{
m_lobbySubscription = sub;
m_lobbySubscription.SubscribeAsync();
});
}
void EndListening()
{
if (m_lobbySubscription == null)
{
Debug.LogError("Can't End listening without first listening to the lobby Callbacks.");
return;
}
m_lobbySubscription.UnsubscribeAsync();
m_RemoteLobby = null;
m_lobbySubscription = null;
m_lobbyEvents = null;
}
void OnRemoteLobbyChanged(ILobbyChanges changes)
{
if (changes.LobbyDeleted)
{
EndListening();
return;
}
//Synching the cloud lobby
changes.ApplyToLobby(m_RemoteLobby);
onLobbyUpdated?.Invoke(m_RemoteLobby);
}
/// <summary>
/// Attempt to create a new lobby and then join it.

Player = new Player(id: uasId, data: CreateInitialPlayerData(localUser))
};
var lobby = await LobbyService.Instance.CreateLobbyAsync(lobbyName, maxPlayers, createOptions);
#pragma warning disable 4014
LobbyHeartBeatLoop();
#pragma warning restore 4014
JoinLobby(lobby);
return lobby;

void JoinLobby(Lobby response)
{
m_RemoteLobby = response;
BeginListening(m_RemoteLobby.Id);
}
/// <summary>

string uasId = AuthenticationService.Instance.PlayerId;
await LobbyService.Instance.RemovePlayerAsync(lobbyId, uasId);
m_RemoteLobby = null;
EndListening();
// Lobbies will automatically delete the lobby if unoccupied, so we don't need to take further action.
}

private float m_heartbeatTime = 0;
private const float k_heartbeatPeriod = 8; // The heartbeat must be rate-limited to 5 calls per 30 seconds. We'll aim for longer in case periods don't align.
private const int k_heartbeatPeriodMS = 8000; // The heartbeat must be rate-limited to 5 calls per 30 seconds. We'll aim for longer in case periods don't align.
public void DoLobbyHeartbeat(float dt)
async Task LobbyHeartBeatLoop()
m_heartbeatTime += dt;
if (m_heartbeatTime > k_heartbeatPeriod)
while (m_RemoteLobby!=null)
m_heartbeatTime -= k_heartbeatPeriod;
LobbyAPIInterface.HeartbeatPlayerAsync(m_RemoteLobby.Id);
#pragma warning disable 4014
LobbyService.Instance.SendHeartbeatPingAsync(m_RemoteLobby.Id);
#pragma warning restore 4014
await Task.Delay(k_heartbeatPeriodMS);
}
}

65
Assets/Scripts/GameLobby/Lobby/LobbyContentUpdater.cs


using System;
using System.Threading.Tasks;
using LobbyRelaySample.lobby;
using Unity.Services.Lobbies.Models;

private const float k_approvalMaxTime = 10; // Used for determining if a user should timeout if they are unable to connect.
private float m_lifetime = 0;
const int k_UpdateIntervalMS = 1500;
public void BeginTracking(LocalLobby localLobby, LobbyUser localUser)
{

m_ShouldPushData = true;
Locator.Get.Messenger.Subscribe(this);
Locator.Get.UpdateSlow.Subscribe(OnUpdate, 1.5f);
#pragma warning disable 4014
UpdateLoopAsync();
#pragma warning restore 4014
m_lifetime = 0;
LobbyAsyncRequests.Instance.onLobbyUpdated += OnRemoteLobbyUpdated;
}

m_ShouldPushData = false;
Locator.Get.Messenger.Unsubscribe(this);
Locator.Get.UpdateSlow.Unsubscribe(OnUpdate);
if (m_LocalLobby != null)
m_LocalLobby.onChanged -= OnLocalLobbyChanged;
LobbyAsyncRequests.Instance.onLobbyUpdated -= OnRemoteLobbyUpdated;

/// If there have been any data changes since the last update, push them to Lobby. Regardless, pull for the most recent data.
/// (Unless we're already awaiting a query, in which case continue waiting.)
/// </summary>
private void OnUpdate(float dt)
private async Task UpdateLoopAsync()
m_lifetime += dt;
if (m_LocalLobby == null)
return;
if (m_LocalUser.IsHost)
LobbyAsyncRequests.Instance.DoLobbyHeartbeat(dt);
while (m_LocalLobby != null)
{
if (!m_LocalUser.IsApproved && m_lifetime > k_approvalMaxTime)
{
Locator.Get.Messenger.OnReceiveMessage(MessageType.DisplayErrorPopup, "Connection attempt timed out!");
Locator.Get.Messenger.OnReceiveMessage(MessageType.ChangeMenuState, GameState.JoinMenu);
}
if (!m_LocalUser.IsApproved && m_lifetime > k_approvalMaxTime)
{
Locator.Get.Messenger.OnReceiveMessage(MessageType.DisplayErrorPopup, "Connection attempt timed out!");
Locator.Get.Messenger.OnReceiveMessage(MessageType.ChangeMenuState, GameState.JoinMenu);
}
if (m_ShouldPushData)
PushDataToLobby();
if (m_ShouldPushData)
PushDataToLobby();
void PushDataToLobby()
{
m_ShouldPushData = false;
void PushDataToLobby()
{
m_ShouldPushData = false;
if (m_LocalUser.IsHost)
{
DoLobbyDataPush();
}
if (m_LocalUser.IsHost)
{
DoLobbyDataPush();
DoPlayerDataPush();
DoPlayerDataPush();
}
void DoLobbyDataPush()
{
void DoLobbyDataPush()
{
LobbyAsyncRequests.Instance.UpdateLobbyDataAsync(LobbyConverters.LocalToRemoteData(m_LocalLobby));
LobbyAsyncRequests.Instance.UpdateLobbyDataAsync(LobbyConverters.LocalToRemoteData(m_LocalLobby));
}
}
void DoPlayerDataPush()
{
void DoPlayerDataPush()
{
LobbyAsyncRequests.Instance.UpdatePlayerDataAsync(LobbyConverters.LocalToRemoteUserData(m_LocalUser));
LobbyAsyncRequests.Instance.UpdatePlayerDataAsync(LobbyConverters.LocalToRemoteUserData(m_LocalUser));
}
await Task.Delay(k_UpdateIntervalMS);
}
}

15
Packages/manifest.json


"com.unity.render-pipelines.universal": "12.1.6",
"com.unity.services.authentication": "2.0.0",
"com.unity.services.core": "1.2.0",
"com.unity.services.lobby": "1.0.0-pre.7",
"com.unity.services.lobby": "1.0.1",
"com.unity.services.relay": "1.0.1-pre.3",
"com.unity.services.vivox": "15.1.170000-pre.1",
"com.unity.services.wire": "1.0.0",

"com.unity.modules.vr": "1.0.0",
"com.unity.modules.wind": "1.0.0",
"com.unity.modules.xr": "1.0.0"
},
"scopedRegistries": [
{
"name": "Internal Candidates Registry",
"url": "https://artifactory.prd.it.unity3d.com/artifactory/api/npm/upm-candidates",
"scopes": [
"com.unity.services.wire",
"com.unity.services.lobby",
"com.unity.services.authentication"
]
}
]
}
}

16
Packages/packages-lock.json


"com.unity.services.core": "1.3.1",
"com.unity.modules.unitywebrequest": "1.0.0"
},
"url": "https://artifactory.prd.it.unity3d.com/artifactory/api/npm/upm-candidates"
"url": "https://packages.unity.com"
"version": "1.3.1",
"version": "1.4.0",
"depth": 1,
"source": "registry",
"dependencies": {

"url": "https://packages.unity.com"
},
"com.unity.services.lobby": {
"version": "1.0.0-pre.7",
"version": "1.0.1",
"com.unity.services.core": "1.1.0-pre.77",
"com.unity.services.core": "1.4.0",
"com.unity.nuget.newtonsoft-json": "3.0.1",
"com.unity.services.authentication": "1.0.0-pre.73"
"com.unity.nuget.newtonsoft-json": "3.0.2",
"com.unity.services.authentication": "2.0.0"
"url": "https://artifactory.prd.it.unity3d.com/artifactory/api/npm/upm-candidates"
"url": "https://packages.unity.com"
},
"com.unity.services.relay": {
"version": "1.0.1-pre.3",

"com.unity.nuget.newtonsoft-json": "3.0.1",
"com.unity.services.authentication": "1.0.0-pre.37"
},
"url": "https://artifactory.prd.it.unity3d.com/artifactory/api/npm/upm-candidates"
"url": "https://packages.unity.com"
},
"com.unity.settings-manager": {
"version": "2.0.0",

15
ProjectSettings/PackageManagerSettings.asset


m_Scopes: []
m_IsDefault: 1
m_Capabilities: 7
- m_Id: scoped:Internal Candidates Registry
m_Name: Internal Candidates Registry
m_Url: https://artifactory.prd.it.unity3d.com/artifactory/api/npm/upm-candidates
m_Scopes:
- com.unity.services.wire
- com.unity.services.lobby
- com.unity.services.authentication
m_IsDefault: 0
m_Capabilities: 0
m_UserSelectedRegistryName: Internal Candidates Registry
m_UserSelectedRegistryName:
m_UserModificationsInstanceId: -824
m_OriginalInstanceId: -828
m_UserModificationsInstanceId: -822
m_OriginalInstanceId: -826
m_LoadAssets: 0
正在加载...
取消
保存