summaryrefslogtreecommitdiff
path: root/src/network/network_udp.cpp
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2010-12-05 22:21:13 +0000
committerrubidium <rubidium@openttd.org>2010-12-05 22:21:13 +0000
commit9139a6c858a6af95e2fd9a1abacec520c62a8bd0 (patch)
treeaaf375eb0eff1ab12871d8867f80a23941a94b01 /src/network/network_udp.cpp
parent440a529701a8a838cab57d41d97b9c0bfad0d43d (diff)
downloadopenttd-9139a6c858a6af95e2fd9a1abacec520c62a8bd0.tar.xz
(svn r21405) -Codechange: prepare sending of company information in the UDP packet for longer company names (in bytes), by truncating the names if needed
Diffstat (limited to 'src/network/network_udp.cpp')
-rw-r--r--src/network/network_udp.cpp33
1 files changed, 32 insertions, 1 deletions
diff --git a/src/network/network_udp.cpp b/src/network/network_udp.cpp
index 11e27d749..ed7cd3581 100644
--- a/src/network/network_udp.cpp
+++ b/src/network/network_udp.cpp
@@ -29,6 +29,8 @@
#include "../thread/thread.h"
#include "../rev.h"
#include "../newgrf_text.h"
+#include "../strings_func.h"
+#include "table/strings.h"
#include "core/udp.h"
@@ -138,11 +140,40 @@ DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_DETAIL_INFO)
NetworkCompanyStats company_stats[MAX_COMPANIES];
NetworkPopulateCompanyStats(company_stats);
+ /* The minimum company information "blob" size. */
+ static const uint MIN_CI_SIZE = 54;
+ uint max_cname_length = NETWORK_COMPANY_NAME_LENGTH;
+
+ if (Company::GetNumItems() * (MIN_CI_SIZE + NETWORK_COMPANY_NAME_LENGTH) >= (uint)packet.size - packet.pos) {
+ /* Assume we can at least put the company information in the packets. */
+ assert(Company::GetNumItems() * MIN_CI_SIZE < (uint)packet.size - packet.pos);
+
+ /* At this moment the company names might not fit in the
+ * packet. Check whether that is really the case. */
+
+ for (;;) {
+ int free = packet.size - packet.pos;
+ Company *company;
+ FOR_ALL_COMPANIES(company) {
+ char company_name[NETWORK_COMPANY_NAME_LENGTH];
+ SetDParam(0, company->index);
+ GetString(company_name, STR_COMPANY_NAME, company_name + max_cname_length - 1);
+ free -= MIN_CI_SIZE;
+ free -= strlen(company_name);
+ }
+ if (free >= 0) break;
+
+ /* Try again, with slightly shorter strings. */
+ assert(max_cname_length > 0);
+ max_cname_length--;
+ }
+ }
+
Company *company;
/* Go through all the companies */
FOR_ALL_COMPANIES(company) {
/* Send the information */
- this->SendCompanyInformation(&packet, company, &company_stats[company->index]);
+ this->SendCompanyInformation(&packet, company, &company_stats[company->index], max_cname_length);
}
this->SendPacket(&packet, client_addr);