From 0f0cc67d621fbddae1768013f91b16bd421666c8 Mon Sep 17 00:00:00 2001 From: rubidium Date: Wed, 8 Apr 2009 21:11:16 +0000 Subject: (svn r15990) -Codechange: provide a thread safe variant of GetAddressAsString. --- src/network/core/address.cpp | 22 +++++++++++++--------- src/network/core/address.h | 9 +++++++++ src/network/network_udp.cpp | 2 +- 3 files changed, 23 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/network/core/address.cpp b/src/network/core/address.cpp index d14d1dab6..9a4bf18fb 100644 --- a/src/network/core/address.cpp +++ b/src/network/core/address.cpp @@ -53,15 +53,12 @@ void NetworkAddress::SetPort(uint16 port) } } -const char *NetworkAddress::GetAddressAsString(bool with_family) +void NetworkAddress::GetAddressAsString(char *buffer, const char *last, bool with_family) { - /* 6 = for the : and 5 for the decimal port number */ - static char buf[NETWORK_HOSTNAME_LENGTH + 6 + 7]; - char *p = buf; - if (this->GetAddress()->ss_family == AF_INET6) p = strecpy(p, "[", lastof(buf)); - p = strecpy(p, this->GetHostname(), lastof(buf)); - if (this->GetAddress()->ss_family == AF_INET6) p = strecpy(p, "]", lastof(buf)); - p += seprintf(p, lastof(buf), ":%d", this->GetPort()); + if (this->GetAddress()->ss_family == AF_INET6) buffer = strecpy(buffer, "[", last); + buffer = strecpy(buffer, this->GetHostname(), last); + if (this->GetAddress()->ss_family == AF_INET6) buffer = strecpy(buffer, "]", last); + buffer += seprintf(buffer, last, ":%d", this->GetPort()); if (with_family) { char family; @@ -70,8 +67,15 @@ const char *NetworkAddress::GetAddressAsString(bool with_family) case AF_INET6: family = '6'; break; default: family = '?'; break; } - seprintf(p, lastof(buf), " (IPv%c)", family); + seprintf(buffer, last, " (IPv%c)", family); } +} + +const char *NetworkAddress::GetAddressAsString(bool with_family) +{ + /* 6 = for the : and 5 for the decimal port number */ + static char buf[NETWORK_HOSTNAME_LENGTH + 6 + 7]; + this->GetAddressAsString(buf, lastof(buf), with_family); return buf; } diff --git a/src/network/core/address.h b/src/network/core/address.h index a2fac9845..a51339085 100644 --- a/src/network/core/address.h +++ b/src/network/core/address.h @@ -120,10 +120,19 @@ public: */ const char *GetHostname(); + /** + * Get the address as a string, e.g. 127.0.0.1:12345. + * @param buffer the buffer to write to + * @param last the last element in the buffer + * @param with_family whether to add the family (e.g. IPvX). + */ + void GetAddressAsString(char *buffer, const char *last, bool with_family = true); + /** * Get the address as a string, e.g. 127.0.0.1:12345. * @param with_family whether to add the family (e.g. IPvX). * @return the address + * @note NOT thread safe */ const char *GetAddressAsString(bool with_family = true); diff --git a/src/network/network_udp.cpp b/src/network/network_udp.cpp index b59ebfb7d..003900802 100644 --- a/src/network/network_udp.cpp +++ b/src/network/network_udp.cpp @@ -416,7 +416,7 @@ void NetworkUDPQueryServerThread(void *pntr) /* Clear item in gamelist */ NetworkGameList *item = CallocT(1); item->address = *info; - strecpy(item->info.server_name, info->GetAddressAsString(), lastof(item->info.server_name)); + info->GetAddressAsString(item->info.server_name, lastof(item->info.server_name)); strecpy(item->info.hostname, info->GetHostname(), lastof(item->info.hostname)); item->manually = info->manually; NetworkGameListAddItemDelayed(item); -- cgit v1.2.3-70-g09d2