Skip to content

feat: returning to offline state on transport failure [MTT-4302] #707

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
merged 8 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion Assets/Scripts/ConnectionManagement/ConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ void Start()
NetworkManager.OnClientDisconnectCallback += OnClientDisconnectCallback;
NetworkManager.OnServerStarted += OnServerStarted;
NetworkManager.ConnectionApprovalCallback += ApprovalCheck;
NetworkManager.OnTransportFailure += OnTransportFailure;
}

void OnDestroy()
Expand All @@ -100,7 +101,7 @@ void OnDestroy()
NetworkManager.OnClientDisconnectCallback -= OnClientDisconnectCallback;
NetworkManager.OnServerStarted -= OnServerStarted;
NetworkManager.ConnectionApprovalCallback -= ApprovalCheck;

NetworkManager.OnTransportFailure -= OnTransportFailure;
}

internal void ChangeState(ConnectionState nextState)
Expand Down Expand Up @@ -135,6 +136,11 @@ void ApprovalCheck(NetworkManager.ConnectionApprovalRequest request, NetworkMana
m_CurrentState.ApprovalCheck(request, response);
}

void OnTransportFailure()
{
m_CurrentState.OnTransportFailure();
}

public void StartClientLobby(string playerName)
{
m_CurrentState.StartClientLobby(playerName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Unity.Multiplayer.Samples.BossRoom
/// entering. If successful, transitions to the ClientConnected state. If not, transitions to the Offline state. If
/// given a disconnect reason first, transitions to the DisconnectingWithReason state.
/// </summary>
class ClientConnectingState : ConnectionState
class ClientConnectingState : OnlineState
{
[Inject]
protected LobbyServiceFacade m_LobbyServiceFacade;
Expand All @@ -39,12 +39,6 @@ public override void OnClientDisconnect(ulong _)
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
}

public override void OnUserRequestedShutdown()
{
m_ConnectStatusPublisher.Publish(ConnectStatus.UserRequestedDisconnect);
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
}

public override void OnDisconnectReasonReceived(ConnectStatus disconnectReason)
{
m_ConnectStatusPublisher.Publish(disconnectReason);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ public override void OnClientDisconnect(ulong _)
}
}

public override void OnUserRequestedShutdown()
{
m_ConnectStatusPublisher.Publish(ConnectStatus.UserRequestedDisconnect);
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
}

public override void OnDisconnectReasonReceived(ConnectStatus disconnectReason)
{
m_ConnectStatusPublisher.Publish(disconnectReason);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Unity.Multiplayer.Samples.BossRoom
{
/// <summary>
/// Base class representing a connection state. Contains a static member for each possible state.
/// Base class representing a connection state.
/// </summary>
abstract class ConnectionState
{
Expand Down Expand Up @@ -39,5 +39,7 @@ public virtual void OnUserRequestedShutdown() { }
public virtual void OnDisconnectReasonReceived(ConnectStatus disconnectReason) { }

public virtual void ApprovalCheck(NetworkManager.ConnectionApprovalRequest request, NetworkManager.ConnectionApprovalResponse response) { }

public virtual void OnTransportFailure() { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Unity.Multiplayer.Samples.BossRoom
/// Since our disconnect process runs in multiple steps host side, this state is the first step client side. This
/// state simply waits for the actual disconnect, then transitions to the offline state.
/// </summary>
class DisconnectingWithReasonState : ConnectionState
class DisconnectingWithReasonState : OnlineState
{
public override void Enter() { }

Expand All @@ -15,10 +15,5 @@ public override void OnClientDisconnect(ulong _)
{
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
}

public override void OnUserRequestedShutdown()
{
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Unity.Multiplayer.Samples.BossRoom
/// Connection state corresponding to a listening host. Handles incoming client connections. When shutting down or
/// being timed out, transitions to the Offline state.
/// </summary>
class HostingState : ConnectionState
class HostingState : OnlineState
{
[Inject]
LobbyServiceFacade m_LobbyServiceFacade;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Unity.Multiplayer.Samples.BossRoom
{
/// <summary>
/// Base class representing an online connection state.
/// </summary>
abstract class OnlineState : ConnectionState
{

public override void OnUserRequestedShutdown()
{
// This behaviour will be the same for every online state
m_ConnectStatusPublisher.Publish(ConnectStatus.UserRequestedDisconnect);
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
}

public override void OnTransportFailure()
{
// This behaviour will be the same for every online state
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Unity.Multiplayer.Samples.BossRoom
/// Connection state corresponding to a host starting up. Starts the host when entering the state. If successful,
/// transitions to the Hosting state, if not, transitions back to the Offline state.
/// </summary>
class StartingHostState : ConnectionState
class StartingHostState : OnlineState
{
[Inject]
LobbyServiceFacade m_LobbyServiceFacade;
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ Additional documentation and release notes are available at [Multiplayer Documen
## [Unreleased] - yyyy-mm-dd

### Added
* Added handling the OnTransportFailure callback (#707). This callback is invoked when a failure happens on the transport's side, for example if the host loses connection to the Relay service. This won't get called when the host is just listening with direct IP, this would need to be handled differently (by pinging an external service like google to test for internet connectivity for example). Boss Room now handles that callback by returning to the Offline state.
* Pickup and Drop action added to the Action system. Actionable once targeting a "Heavy"-tagged NetworkObject. (#372) - This shows NetworkObject parenting with a pattern to follow animation bones (the hands when picking up)

*
### Changed
* Updated tools, authentication and relay packages (#690)
* Replaced our dependency injection solution with VContainer. (#679)
Expand Down