From 21040dc00b7ffd4a9260421ce6cd2a51a770a442 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 11 Jun 2018 06:26:25 +0100 Subject: [PATCH] Implementing sending server GRF info list response in multiple packets Add/adjust GRF info message logging --- src/network/network_udp.cpp | 53 ++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/src/network/network_udp.cpp b/src/network/network_udp.cpp index 01d7b12cfb..98860a8dd8 100644 --- a/src/network/network_udp.cpp +++ b/src/network/network_udp.cpp @@ -316,11 +316,32 @@ void ServerNetworkUDPSocketHandler::Receive_CLIENT_GET_NEWGRFS(Packet *p, Networ size_t packet_len = 0; - DEBUG(net, 6, "[udp] newgrf data request from %s", client_addr->GetAddressAsString()); - num_grfs = p->Recv_uint8 (); + DEBUG(net, 6, "[udp] newgrf data request (%u) from %s", num_grfs, client_addr->GetAddressAsString()); + if (num_grfs > NETWORK_MAX_GRF_COUNT) return; + auto flush_response = [&]() { + if (in_reply.empty()) return; + + Packet packet(PACKET_UDP_SERVER_NEWGRFS); + packet.Send_uint8(in_reply.size()); + for (const GRFInfo &info : in_reply) { + char name[NETWORK_GRF_NAME_LENGTH]; + + /* The name could be an empty string, if so take the filename */ + strecpy(name, info.name, lastof(name)); + this->SendGRFIdentifier(&packet, &info.ident); + packet.Send_string(name); + } + + this->SendPacket(&packet, client_addr); + DEBUG(net, 6, "[udp] sent newgrf data response (%u of %u) to %s", (uint) in_reply.size(), num_grfs, client_addr->GetAddressAsString()); + + packet_len = 0; + in_reply.clear(); + }; + for (i = 0; i < num_grfs; i++) { GRFInfo info; this->ReceiveGRFIdentifier(p, &info.ident); @@ -344,29 +365,17 @@ void ServerNetworkUDPSocketHandler::Receive_CLIENT_GET_NEWGRFS(Packet *p, Networ /* If the reply might exceed the size of the packet, only reply * the current list and do not send the other data. * The name could be an empty string, if so take the filename. */ - packet_len += sizeof(info.ident.grfid) + sizeof(info.ident.md5sum) + + size_t required_length = sizeof(info.ident.grfid) + sizeof(info.ident.md5sum) + min(strlen(info.name) + 1, (size_t)NETWORK_GRF_NAME_LENGTH); - if (packet_len > SEND_MTU - 4) { // 4 is 3 byte header + grf count in reply - break; + if (packet_len + required_length > SEND_MTU - 4) { // 4 is 3 byte header + grf count in reply + flush_response(); } + packet_len += required_length; in_reply.push_back(info); } - if (in_reply.empty()) return; - - Packet packet(PACKET_UDP_SERVER_NEWGRFS); - packet.Send_uint8(in_reply.size()); - for (const GRFInfo &info : in_reply) { - char name[NETWORK_GRF_NAME_LENGTH]; - - /* The name could be an empty string, if so take the filename */ - strecpy(name, info.name, lastof(name)); - this->SendGRFIdentifier(&packet, &info.ident); - packet.Send_string(name); - } - - this->SendPacket(&packet, client_addr); + flush_response(); } ///*** Communication with servers (we are client) ***/ @@ -439,6 +448,8 @@ void ClientNetworkUDPSocketHandler::Receive_SERVER_RESPONSE_Common(Packet *p, Ne this->SendPacket(&packet, &item->address); + DEBUG(net, 4, "[udp] sent newgrf data request (%u) to %s", in_request_count, client_addr->GetAddressAsString()); + in_request_count = 0; }; @@ -511,9 +522,9 @@ void ClientNetworkUDPSocketHandler::Receive_SERVER_NEWGRFS(Packet *p, NetworkAdd uint8 num_grfs; uint i; - DEBUG(net, 6, "[udp] newgrf data reply from %s", client_addr->GetAddressAsString()); - num_grfs = p->Recv_uint8 (); + DEBUG(net, 6, "[udp] newgrf data reply (%u) from %s", num_grfs, client_addr->GetAddressAsString()); + if (num_grfs > NETWORK_MAX_GRF_COUNT) return; for (i = 0; i < num_grfs; i++) {