浏览代码

Lobby package now requires a heartbeat, so this adds that in. Note that I've identified a bug with the UpdateSlow where the wrong delta time was being passed, and I'm not *technically* fixing that, but it's close enough and just a one-line fix.

/main/staging
nathaniel.buck@unity3d.com 3 年前
当前提交
9b8e52ec
共有 5 个文件被更改,包括 32 次插入3 次删除
  1. 4
      Assets/Scripts/Infrastructure/UpdateSlow.cs
  2. 7
      Assets/Scripts/Lobby/LobbyAPIInterface.cs
  3. 15
      Assets/Scripts/Lobby/LobbyAsyncRequests.cs
  4. 3
      Assets/Scripts/Lobby/LobbyContentHeartbeat.cs
  5. 6
      Assets/Scripts/Tests/PlayMode/UpdateSlowTests.cs

4
Assets/Scripts/Infrastructure/UpdateSlow.cs


}
/// <summary>
/// Some objects might need to be on a slower update loop than the usual MonoBehaviour Update, e.g. to refresh data from services.
/// Some objects might need to be on a slower update loop than the usual MonoBehaviour Update and without precise timing, e.g. to refresh data from services.
/// Some might also not want to be coupled to a Unity object at all but still need an update loop.
/// </summary>
public class UpdateSlow : MonoBehaviour, IUpdateSlow

while (m_updateTimer > effectivePeriod)
{
m_updateTimer -= effectivePeriod;
OnUpdate(effectivePeriod);
OnUpdate(m_updatePeriod); // Using m_updatePeriod will be incorrect on the first update for a new subscriber, due to the staggering. However, we don't expect UpdateSlow subscribers to require precision, and this is less verbose than tracking per-subscriber.
}
}

7
Assets/Scripts/Lobby/LobbyAPIInterface.cs


var task = LobbyService.LobbyApiClient.UpdatePlayerAsync(updateRequest);
new InProgressRequest<Response<Lobby>>(task, onComplete);
}
public static void HeartbeatPlayerAsync(string lobbyId)
{
HeartbeatRequest request = new HeartbeatRequest(lobbyId);
var task = LobbyService.LobbyApiClient.HeartbeatAsync(request);
new InProgressRequest<Response>(task, null);
}
}
}

15
Assets/Scripts/Lobby/LobbyAsyncRequests.cs


}
return true;
}
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.
/// <summary>
/// Lobby requires a periodic ping to detect rooms that are still active, in order to mitigate "zombie" lobbies.
/// </summary>
public void DoLobbyHeartbeat(float dt)
{
m_heartbeatTime += dt;
if (m_heartbeatTime > k_heartbeatPeriod)
{
m_heartbeatTime -= k_heartbeatPeriod;
LobbyAPIInterface.HeartbeatPlayerAsync(m_lastKnownLobby.Id);
}
}
}
}

3
Assets/Scripts/Lobby/LobbyContentHeartbeat.cs


{
if (m_isAwaitingQuery || m_localLobby == null)
return;
LobbyAsyncRequests.Instance.DoLobbyHeartbeat(dt);
void PushDataToLobby()
{

6
Assets/Scripts/Tests/PlayMode/UpdateSlowTests.cs


private class Subscriber : IDisposable
{
private Action m_thingToDo;
public float prevDt;
public Subscriber(Action thingToDo)
{

private void OnUpdate(float dt)
{
m_thingToDo?.Invoke();
prevDt = dt;
}
}

yield return new WaitForSeconds(0.1f);
Assert.AreEqual(1, updateCount, "Slow update period should have passed.");
Assert.AreEqual(1.5f, sub.prevDt, "Slow update should have received the full time delta.");
Assert.AreEqual(1.5f, sub.prevDt, "Slow update should have received the full time delta again.");
Assert.AreEqual(1.5f, sub.prevDt, "Slow update should have received the full time delta with two subscribers.");
Assert.AreEqual(1.5f, sub2.prevDt, "Slow update should have received the full time delta on the second subscriber as well.");
sub2.Dispose();
yield return new WaitForSeconds(k_period);

正在加载...
取消
保存