Adapt NetworkManager retry interval to current reliable packet RTT

This will make sure to not drop packets when connection is slower.
This commit is contained in:
mrkubax10 2024-02-09 12:17:17 +01:00
parent 318dd45b19
commit 5d49b048fd
3 changed files with 18 additions and 6 deletions

View File

@ -30,7 +30,7 @@ SOFTWARE.
using namespace polygun::network; using namespace polygun::network;
static const double NETWORK_MANAGER_PACKET_RETRY_INTERVAL = 4; static const time_t NETWORK_MANAGER_PACKET_RETRY_INTERVAL = 4;
static const uint8_t NETWORK_MANAGER_MAX_RETRY_COUNT = 10; static const uint8_t NETWORK_MANAGER_MAX_RETRY_COUNT = 10;
NetworkManager::NetworkManager(std::shared_ptr<UDPSocket> socket, const NetworkEndpoint& endpoint) : NetworkManager::NetworkManager(std::shared_ptr<UDPSocket> socket, const NetworkEndpoint& endpoint) :
@ -40,7 +40,8 @@ NetworkManager::NetworkManager(std::shared_ptr<UDPSocket> socket, const NetworkE
m_outgoing_packets(), m_outgoing_packets(),
m_incoming_packets(), m_incoming_packets(),
m_socket(socket), m_socket(socket),
m_endpoint(endpoint) m_endpoint(endpoint),
m_reliable_packet_rtt(0)
{} {}
NetworkManager::NetworkManager(const std::string& ip, unsigned short port) : NetworkManager::NetworkManager(const std::string& ip, unsigned short port) :
@ -50,7 +51,8 @@ NetworkManager::NetworkManager(const std::string& ip, unsigned short port) :
m_outgoing_packets(), m_outgoing_packets(),
m_incoming_packets(), m_incoming_packets(),
m_socket(new UDPSocket), m_socket(new UDPSocket),
m_endpoint() m_endpoint(),
m_reliable_packet_rtt(0)
{ {
m_socket->connect(ip, port); m_socket->connect(ip, port);
m_endpoint = m_socket->get_endpoint(); m_endpoint = m_socket->get_endpoint();
@ -66,7 +68,7 @@ NetworkPacket NetworkManager::create_packet(uint8_t flags, uint8_t id) {
void NetworkManager::send_packet(const NetworkPacket& packet) { void NetworkManager::send_packet(const NetworkPacket& packet) {
acquire(); acquire();
if(packet.get_flags() & NetworkPacketFlag::FLAG_RELIABLE) { if(packet.get_flags() & NetworkPacketFlag::FLAG_RELIABLE) {
m_outgoing_packets.push_back(QueuedPacket{packet, -10, 0}); m_outgoing_packets.push_back(QueuedPacket{packet, -10, time(nullptr), 0});
sort_packets(m_outgoing_packets); sort_packets(m_outgoing_packets);
} }
else else
@ -99,8 +101,13 @@ void NetworkManager::push_packet(NetworkPacket& packet) {
std::vector<QueuedPacket>::iterator it = std::find_if(m_outgoing_packets.begin(), m_outgoing_packets.end(), [num](const QueuedPacket& queued_packet) { std::vector<QueuedPacket>::iterator it = std::find_if(m_outgoing_packets.begin(), m_outgoing_packets.end(), [num](const QueuedPacket& queued_packet) {
return queued_packet.m_packet.get_num()==num; return queued_packet.m_packet.get_num()==num;
}); });
if(it!=m_outgoing_packets.end()) if(it!=m_outgoing_packets.end()) {
m_reliable_packet_rtt = time(nullptr)-it->m_send_time;
#if defined(NETWORK_MANAGER_VERBOSE)
LOG_VERBOSE("Reliable packet RTT: %jd", m_reliable_packet_rtt);
#endif
m_outgoing_packets.erase(it); m_outgoing_packets.erase(it);
}
release(); release();
return; return;
} }
@ -134,7 +141,8 @@ void NetworkManager::update() {
i--; i--;
continue; continue;
} }
if(time(0)-packet.m_last_retry>=NETWORK_MANAGER_PACKET_RETRY_INTERVAL) { const time_t retry_interval = std::max(NETWORK_MANAGER_PACKET_RETRY_INTERVAL, m_reliable_packet_rtt);
if(time(0)-packet.m_last_retry>=retry_interval) {
if(packet.m_last_retry>0) if(packet.m_last_retry>0)
LOG_VERBOSE("Resending reliable packet with num %d",packet.m_packet.get_num()); LOG_VERBOSE("Resending reliable packet with num %d",packet.m_packet.get_num());
m_socket->send(packet.m_packet, m_endpoint); m_socket->send(packet.m_packet, m_endpoint);

View File

@ -38,8 +38,10 @@ namespace polygun::network {
struct QueuedPacket { struct QueuedPacket {
NetworkPacket m_packet; NetworkPacket m_packet;
time_t m_last_retry; time_t m_last_retry;
time_t m_send_time;
uint8_t m_retries; uint8_t m_retries;
}; };
public: public:
NetworkManager(std::shared_ptr<UDPSocket> socket, const NetworkEndpoint& endpoint); NetworkManager(std::shared_ptr<UDPSocket> socket, const NetworkEndpoint& endpoint);
NetworkManager(const std::string& ip, unsigned short port); NetworkManager(const std::string& ip, unsigned short port);
@ -66,6 +68,7 @@ namespace polygun::network {
std::vector<QueuedPacket> m_incoming_packets; std::vector<QueuedPacket> m_incoming_packets;
std::shared_ptr<UDPSocket> m_socket; std::shared_ptr<UDPSocket> m_socket;
NetworkEndpoint m_endpoint; NetworkEndpoint m_endpoint;
time_t m_reliable_packet_rtt;
private: private:
static void sort_packets(std::vector<QueuedPacket>& packets); static void sort_packets(std::vector<QueuedPacket>& packets);

View File

@ -236,6 +236,7 @@ void GameSessionScreen::connection_thread_func() {
return; return;
case network::NetworkPacketID::PACKET_ACTIVITY: case network::NetworkPacketID::PACKET_ACTIVITY:
last_server_activity = time(nullptr); last_server_activity = time(nullptr);
continue;
default: default:
break; break;
} }