From c55f65b78ff1dbc36de9654af0b8e429a206ca42 Mon Sep 17 00:00:00 2001 From: puvvadar Date: Thu, 3 Jun 2021 15:35:16 -0700 Subject: [PATCH] Integrate parts of Session Server API --- .../Source/MultiplayerSystemComponent.cpp | 28 +++++++++++++++++++ .../Code/Source/MultiplayerSystemComponent.h | 14 ++++++++++ 2 files changed, 42 insertions(+) diff --git a/Gems/Multiplayer/Code/Source/MultiplayerSystemComponent.cpp b/Gems/Multiplayer/Code/Source/MultiplayerSystemComponent.cpp index 0818f605df..50da2136fb 100644 --- a/Gems/Multiplayer/Code/Source/MultiplayerSystemComponent.cpp +++ b/Gems/Multiplayer/Code/Source/MultiplayerSystemComponent.cpp @@ -29,6 +29,8 @@ #include #include #include +#include +#include #include #include @@ -142,6 +144,7 @@ namespace Multiplayer void MultiplayerSystemComponent::Activate() { AZ::TickBus::Handler::BusConnect(); + AzFramework::SessionNotificationBus::Handler::BusConnect(); m_networkInterface = AZ::Interface::Get()->CreateNetworkInterface(AZ::Name(MPNetworkInterfaceName), sv_protocol, TrustZone::ExternalClientToServer, *this); m_consoleCommandHandler.Connect(AZ::Interface::Get()->GetConsoleCommandInvokedEvent()); AZ::Interface::Register(this); @@ -153,9 +156,34 @@ namespace Multiplayer void MultiplayerSystemComponent::Deactivate() { AZ::Interface::Unregister(this); + AzFramework::SessionNotificationBus::Handler::BusDisconnect(); AZ::TickBus::Handler::BusDisconnect(); } + bool MultiplayerSystemComponent::OnSessionHealthCheck() + { + return true; + } + + bool MultiplayerSystemComponent::OnCreateSessionBegin(const AzFramework::SessionConfig& sessionConfig) + { + Multiplayer::MultiplayerAgentType serverType = sv_isDedicated ? MultiplayerAgentType::DedicatedServer : MultiplayerAgentType::ClientServer; + AZ::Interface::Get()->InitializeMultiplayer(serverType); + return m_networkInterface->Listen(sessionConfig.m_port); + } + + bool MultiplayerSystemComponent::OnDestroySessionBegin() + { + bool disconnectSuccessful = true; + IConnectionSet& connectionSet = m_networkInterface->GetConnectionSet(); + connectionSet.VisitConnections([&disconnectSuccessful](IConnection& connection) + { + bool didDisconnect = connection.Disconnect(DisconnectReason::TerminatedByServer, TerminationEndpoint::Remote); + disconnectSuccessful = disconnectSuccessful && didDisconnect; + }); + return disconnectSuccessful; + } + void MultiplayerSystemComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time) { AZ::TimeMs deltaTimeMs = aznumeric_cast(static_cast(deltaTime * 1000.0f)); diff --git a/Gems/Multiplayer/Code/Source/MultiplayerSystemComponent.h b/Gems/Multiplayer/Code/Source/MultiplayerSystemComponent.h index e8e05a9d4c..bd989cf841 100644 --- a/Gems/Multiplayer/Code/Source/MultiplayerSystemComponent.h +++ b/Gems/Multiplayer/Code/Source/MultiplayerSystemComponent.h @@ -25,8 +25,14 @@ #include #include #include +#include #include +namespace AzFramework +{ + struct SessionConfig; +} + namespace AzNetworking { class INetworkInterface; @@ -38,6 +44,7 @@ namespace Multiplayer class MultiplayerSystemComponent final : public AZ::Component , public AZ::TickBus::Handler + , public AzFramework::SessionNotificationBus::Handler , public AzNetworking::IConnectionListener , public IMultiplayer { @@ -58,6 +65,13 @@ namespace Multiplayer void Deactivate() override; //! @} + //! AzFramework::SessionNotificationBus::Handler overrides. + //! @{ + bool OnSessionHealthCheck() override; + bool OnCreateSessionBegin(const AzFramework::SessionConfig& sessionConfig) override; + bool OnDestroySessionBegin() override; + //! @} + //! AZ::TickBus::Handler overrides. //! @{ void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;