From 8f5681ffdb393e38e5e35b7ad0727b7e94728404 Mon Sep 17 00:00:00 2001 From: puvvadar Date: Mon, 21 Jun 2021 11:46:16 -0700 Subject: [PATCH] Add GetActiveConnectionCount to IConnectionSet --- .../ConnectionLayer/IConnectionSet.h | 4 ++++ .../TcpTransport/TcpConnectionSet.cpp | 18 ++++++++++++++++++ .../TcpTransport/TcpConnectionSet.h | 1 + .../UdpTransport/UdpConnectionSet.cpp | 18 ++++++++++++++++++ .../UdpTransport/UdpConnectionSet.h | 1 + .../Code/Source/MultiplayerSystemComponent.cpp | 2 +- 6 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Code/Framework/AzNetworking/AzNetworking/ConnectionLayer/IConnectionSet.h b/Code/Framework/AzNetworking/AzNetworking/ConnectionLayer/IConnectionSet.h index 5b53efd951..1bcddb162b 100644 --- a/Code/Framework/AzNetworking/AzNetworking/ConnectionLayer/IConnectionSet.h +++ b/Code/Framework/AzNetworking/AzNetworking/ConnectionLayer/IConnectionSet.h @@ -47,5 +47,9 @@ namespace AzNetworking //! Returns the current total connection count for this connection set //! @return the current total connection count for this connection set virtual uint32_t GetConnectionCount() const = 0; + + //! Returns the current total count of connections not pending disconnect for this connection set + //! @return the current total count of connections not pending disconnect for this connection set + virtual uint32_t GetActiveConnectionCount() const = 0; }; } diff --git a/Code/Framework/AzNetworking/AzNetworking/TcpTransport/TcpConnectionSet.cpp b/Code/Framework/AzNetworking/AzNetworking/TcpTransport/TcpConnectionSet.cpp index be18fafff9..f3a3605ffa 100644 --- a/Code/Framework/AzNetworking/AzNetworking/TcpTransport/TcpConnectionSet.cpp +++ b/Code/Framework/AzNetworking/AzNetworking/TcpTransport/TcpConnectionSet.cpp @@ -114,6 +114,24 @@ namespace AzNetworking return aznumeric_cast(m_connectionIdMap.size()); } + uint32_t TcpConnectionSet::GetActiveConnectionCount() const + { + uint32_t activeConnections = 0; + for (auto iter = m_connectionIdMap.begin(); iter != m_connectionIdMap.end(); ++iter) + { + if (iter->second.get()) + { + ConnectionState state = iter->second.get()->GetConnectionState(); + if (state == ConnectionState::Connected || state == ConnectionState::Connecting) + { + ++activeConnections; + } + } + } + + return activeConnections; + } + TcpConnection* TcpConnectionSet::GetConnection(SocketFd socketFd) const { SocketFdMap::const_iterator lookup = m_socketFdMap.find(socketFd); diff --git a/Code/Framework/AzNetworking/AzNetworking/TcpTransport/TcpConnectionSet.h b/Code/Framework/AzNetworking/AzNetworking/TcpTransport/TcpConnectionSet.h index 25d42c2481..e3f950b457 100644 --- a/Code/Framework/AzNetworking/AzNetworking/TcpTransport/TcpConnectionSet.h +++ b/Code/Framework/AzNetworking/AzNetworking/TcpTransport/TcpConnectionSet.h @@ -49,6 +49,7 @@ namespace AzNetworking IConnection* GetConnection(ConnectionId connectionId) const override; ConnectionId GetNextConnectionId() override; uint32_t GetConnectionCount() const override; + uint32_t GetActiveConnectionCount() const override; //! @} //! Retrieves a connection from this connection list instance by socket fd. diff --git a/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpConnectionSet.cpp b/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpConnectionSet.cpp index 4e3347c080..8555bed86c 100644 --- a/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpConnectionSet.cpp +++ b/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpConnectionSet.cpp @@ -111,6 +111,24 @@ namespace AzNetworking return aznumeric_cast(m_connectionIdMap.size()); } + uint32_t UdpConnectionSet::GetActiveConnectionCount() const + { + uint32_t activeConnections = 0; + for (auto iter = m_connectionIdMap.begin(); iter != m_connectionIdMap.end(); ++iter) + { + if (iter->second.get()) + { + ConnectionState state = iter->second.get()->GetConnectionState(); + if (state == ConnectionState::Connected || state == ConnectionState::Connecting) + { + ++activeConnections; + } + } + } + + return activeConnections; + } + UdpConnection* UdpConnectionSet::GetConnection(const IpAddress& address) const { RemoteAddressMap::const_iterator lookup = m_remoteAddressMap.find(address); diff --git a/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpConnectionSet.h b/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpConnectionSet.h index 82752904fd..071a9776ec 100644 --- a/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpConnectionSet.h +++ b/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpConnectionSet.h @@ -50,6 +50,7 @@ namespace AzNetworking IConnection* GetConnection(ConnectionId connectionId) const override; ConnectionId GetNextConnectionId() override; uint32_t GetConnectionCount() const override; + uint32_t GetActiveConnectionCount() const override; //! @} //! Retrieves a connection from this connection list instance by endpoint remote address diff --git a/Gems/Multiplayer/Code/Source/MultiplayerSystemComponent.cpp b/Gems/Multiplayer/Code/Source/MultiplayerSystemComponent.cpp index 99f632fa94..2e2e820e60 100644 --- a/Gems/Multiplayer/Code/Source/MultiplayerSystemComponent.cpp +++ b/Gems/Multiplayer/Code/Source/MultiplayerSystemComponent.cpp @@ -700,7 +700,7 @@ namespace Multiplayer // We avoid this for client server as the host itself is a user and non-transient dedicated servers if (sv_isTransient && m_agentType == MultiplayerAgentType::DedicatedServer && connection->GetConnectionRole() == ConnectionRole::Acceptor) { - if (m_networkInterface->GetConnectionSet().GetConnectionCount() == 0) + if (m_networkInterface->GetConnectionSet().GetActiveConnectionCount() == 0) { Terminate(); }