Skip to content

fix: NetworkTransport early and post late updates #3113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#endif
using UnityEngine.SceneManagement;
using Debug = UnityEngine.Debug;
using Unity.Netcode.Transports.UTP;

namespace Unity.Netcode
{
Expand Down Expand Up @@ -291,6 +292,10 @@ public void NetworkUpdate(NetworkUpdateStage updateStage)
case NetworkUpdateStage.EarlyUpdate:
{
UpdateTopology();

// Handle processing any new connections or transport events
NetworkConfig.NetworkTransport.EarlyUpdate();

ConnectionManager.ProcessPendingApprovals();
ConnectionManager.PollAndHandleNetworkEvents();

Expand Down Expand Up @@ -379,6 +384,9 @@ public void NetworkUpdate(NetworkUpdateStage updateStage)
// Metrics update needs to be driven by NetworkConnectionManager's update to assure metrics are dispatched after the send queue is processed.
MetricsManager.UpdateMetrics();

// Handle sending any pending transport messages
NetworkConfig.NetworkTransport.PostLateUpdate();

// TODO: Determine a better way to handle this
NetworkObject.VerifyParentingStatus();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ protected void InvokeOnTransportEvent(NetworkEvent eventType, ulong clientId, Ar
/// /// <param name="networkManager">optionally pass in NetworkManager</param>
public abstract void Initialize(NetworkManager networkManager = null);

/// <summary>
/// Invoked by NetworkManager at the beginning of its EarlyUpdate
/// </summary>
internal virtual void EarlyUpdate()
{

}

/// <summary>
/// Invoked by NetworkManager during PostLateUpdate
/// </summary>
internal virtual void PostLateUpdate()
{

}

protected virtual NetworkTopologyTypes OnCurrentTopology()
{
return NetworkTopologyTypes.ClientServer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -944,17 +944,13 @@ private bool ProcessEvent()
return false;
}

private void Update()
private JobHandle m_FlushSendJobHandle;

internal override void EarlyUpdate()
{
m_FlushSendJobHandle.Complete();
if (m_Driver.IsCreated)
{
foreach (var kvp in m_SendQueue)
{
SendBatchedMessages(kvp.Key, kvp.Value);
}

m_Driver.ScheduleUpdate().Complete();

if (m_ProtocolType == ProtocolType.RelayUnityTransport && m_Driver.GetRelayConnectionStatus() == RelayConnectionStatus.AllocationInvalid)
{
Debug.LogError("Transport failure! Relay allocation needs to be recreated, and NetworkManager restarted. " +
Expand All @@ -964,6 +960,8 @@ private void Update()
return;
}

m_Driver.ScheduleUpdate().Complete();

while (AcceptConnection() && m_Driver.IsCreated)
{
;
Expand All @@ -973,14 +971,35 @@ private void Update()
{
;
}
}
}

#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
if (m_NetworkManager)
internal override void PostLateUpdate()
{
if (m_Driver.IsCreated)
{
foreach (var kvp in m_SendQueue)
{
ExtractNetworkMetrics();
SendBatchedMessages(kvp.Key, kvp.Value);
}
#endif

// Schedule a flush send as the last transport action for the
// current frame.
m_FlushSendJobHandle = m_Driver.ScheduleFlushSend(default);
}

#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
if (m_NetworkManager)
{
ExtractNetworkMetrics();
}
#endif
base.PostLateUpdate();
}

private void Update()
{

}

private void OnDestroy()
Expand Down
Loading