Merge pull request #1425 from aws-lumberyard-dev/mp_systemcomp_cleanup
Cleanup and consolidate MultiplayerSystemComponent and add StopListening to AzNetworking
This commit is contained in:
@@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -88,6 +88,10 @@ namespace AzNetworking
|
||||
//! @return boolean true if the packet is confirmed acknowledged, false if the packet number is out of range, lost, or still pending acknowledgment
|
||||
virtual bool WasPacketAcked(ConnectionId connectionId, PacketId packetId) = 0;
|
||||
|
||||
//! Closes the network interface to stop accepting new incoming connections.
|
||||
//! @return boolean true if the operation was successful, false if it failed
|
||||
virtual bool StopListening() = 0;
|
||||
|
||||
//! Disconnects the specified connection.
|
||||
//! @param connectionId identifier of the connection to terminate
|
||||
//! @param reason reason for the disconnect
|
||||
|
||||
@@ -114,6 +114,24 @@ namespace AzNetworking
|
||||
return aznumeric_cast<uint32_t>(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);
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -162,6 +162,12 @@ namespace AzNetworking
|
||||
return connection->WasPacketAcked(packetId);
|
||||
}
|
||||
|
||||
bool TcpNetworkInterface::StopListening()
|
||||
{
|
||||
m_port = 0;
|
||||
return m_listenThread.StopListening(*this);
|
||||
}
|
||||
|
||||
bool TcpNetworkInterface::Disconnect(ConnectionId connectionId, DisconnectReason reason)
|
||||
{
|
||||
IConnection* connection = m_connectionSet.GetConnection(connectionId);
|
||||
|
||||
@@ -63,6 +63,7 @@ namespace AzNetworking
|
||||
bool SendReliablePacket(ConnectionId connectionId, const IPacket& packet) override;
|
||||
PacketId SendUnreliablePacket(ConnectionId connectionId, const IPacket& packet) override;
|
||||
bool WasPacketAcked(ConnectionId connectionId, PacketId packetId) override;
|
||||
bool StopListening() override;
|
||||
bool Disconnect(ConnectionId connectionId, DisconnectReason reason) override;
|
||||
//! @}
|
||||
|
||||
|
||||
@@ -111,6 +111,24 @@ namespace AzNetworking
|
||||
return aznumeric_cast<uint32_t>(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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -116,17 +116,29 @@ namespace AzNetworking
|
||||
|
||||
m_port = port;
|
||||
m_allowIncomingConnections = true;
|
||||
m_socket->Open(m_port, UdpSocket::CanAcceptConnections::True, m_trustZone);
|
||||
m_readerThread.RegisterSocket(m_socket.get());
|
||||
return true;
|
||||
if (m_socket->Open(m_port, UdpSocket::CanAcceptConnections::True, m_trustZone))
|
||||
{
|
||||
m_readerThread.RegisterSocket(m_socket.get());
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
ConnectionId UdpNetworkInterface::Connect(const IpAddress& remoteAddress)
|
||||
{
|
||||
if (!m_socket->IsOpen())
|
||||
{
|
||||
m_socket->Open(m_port, UdpSocket::CanAcceptConnections::False, m_trustZone);
|
||||
m_readerThread.RegisterSocket(m_socket.get());
|
||||
if (m_socket->Open(m_port, UdpSocket::CanAcceptConnections::False, m_trustZone))
|
||||
{
|
||||
m_readerThread.RegisterSocket(m_socket.get());
|
||||
}
|
||||
else
|
||||
{
|
||||
return InvalidConnectionId;
|
||||
}
|
||||
}
|
||||
|
||||
const ConnectionId connectionId = m_connectionSet.GetNextConnectionId();
|
||||
@@ -359,6 +371,20 @@ namespace AzNetworking
|
||||
return connection->WasPacketAcked(packetId);
|
||||
}
|
||||
|
||||
bool UdpNetworkInterface::StopListening()
|
||||
{
|
||||
if (!m_socket->IsOpen())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
m_port = 0;
|
||||
m_readerThread.UnregisterSocket(m_socket.get());
|
||||
m_allowIncomingConnections = false;
|
||||
m_socket->Close();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UdpNetworkInterface::Disconnect(ConnectionId connectionId, DisconnectReason reason)
|
||||
{
|
||||
IConnection* connection = m_connectionSet.GetConnection(connectionId);
|
||||
|
||||
@@ -60,6 +60,7 @@ namespace AzNetworking
|
||||
bool SendReliablePacket(ConnectionId connectionId, const IPacket& packet) override;
|
||||
PacketId SendUnreliablePacket(ConnectionId connectionId, const IPacket& packet) override;
|
||||
bool WasPacketAcked(ConnectionId connectionId, PacketId packetId) override;
|
||||
bool StopListening() override;
|
||||
bool Disconnect(ConnectionId connectionId, DisconnectReason reason) override;
|
||||
//! @}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user