summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-04-29 12:05:43 +0200
committerPatric Stout <github@truebrain.nl>2021-04-29 20:12:11 +0200
commit99f998805ba835f48a8061762fa19761760c7451 (patch)
tree4eef69f3bbf7e4a6d27ce47d581dd8bafb3728ab
parenta8afbe74bfdbe6aa9ceb88c8e70c63bfe37aeda0 (diff)
downloadopenttd-99f998805ba835f48a8061762fa19761760c7451.tar.xz
Codechange: use std::string over stack-based strings if possible
-rw-r--r--src/network/core/address.cpp32
-rw-r--r--src/network/network_gui.cpp5
2 files changed, 17 insertions, 20 deletions
diff --git a/src/network/core/address.cpp b/src/network/core/address.cpp
index e53566c0b..8b51e2c08 100644
--- a/src/network/core/address.cpp
+++ b/src/network/core/address.cpp
@@ -313,8 +313,7 @@ static SOCKET ConnectLoopProc(addrinfo *runp)
{
const char *type = NetworkAddress::SocketTypeAsString(runp->ai_socktype);
const char *family = NetworkAddress::AddressFamilyAsString(runp->ai_family);
- char address[NETWORK_HOSTNAME_LENGTH + 6 + 7];
- NetworkAddress(runp->ai_addr, (int)runp->ai_addrlen).GetAddressAsString(address, lastof(address));
+ std::string address = NetworkAddress(runp->ai_addr, (int)runp->ai_addrlen).GetAddressAsString();
SOCKET sock = socket(runp->ai_family, runp->ai_socktype, runp->ai_protocol);
if (sock == INVALID_SOCKET) {
@@ -328,7 +327,7 @@ static SOCKET ConnectLoopProc(addrinfo *runp)
int err = connect(sock, runp->ai_addr, (int)runp->ai_addrlen);
if (err != 0 && NetworkGetLastError() != EINPROGRESS) {
- DEBUG(net, 1, "[%s] could not connect to %s over %s: %s", type, address, family, NetworkGetLastErrorString());
+ DEBUG(net, 1, "[%s] could not connect to %s over %s: %s", type, address.c_str(), family, NetworkGetLastErrorString());
closesocket(sock);
return INVALID_SOCKET;
}
@@ -344,14 +343,14 @@ static SOCKET ConnectLoopProc(addrinfo *runp)
tv.tv_sec = DEFAULT_CONNECT_TIMEOUT_SECONDS;
int n = select(FD_SETSIZE, NULL, &write_fd, NULL, &tv);
if (n < 0) {
- DEBUG(net, 1, "[%s] could not connect to %s: %s", type, address, NetworkGetLastErrorString());
+ DEBUG(net, 1, "[%s] could not connect to %s: %s", type, address.c_str(), NetworkGetLastErrorString());
closesocket(sock);
return INVALID_SOCKET;
}
/* If no fd is selected, the timeout has been reached. */
if (n == 0) {
- DEBUG(net, 1, "[%s] timed out while connecting to %s", type, address);
+ DEBUG(net, 1, "[%s] timed out while connecting to %s", type, address.c_str());
closesocket(sock);
return INVALID_SOCKET;
}
@@ -359,13 +358,13 @@ static SOCKET ConnectLoopProc(addrinfo *runp)
/* Retrieve last error, if any, on the socket. */
err = GetSocketError(sock);
if (err != 0) {
- DEBUG(net, 1, "[%s] could not connect to %s: %s", type, address, NetworkGetErrorString(err));
+ DEBUG(net, 1, "[%s] could not connect to %s: %s", type, address.c_str(), NetworkGetErrorString(err));
closesocket(sock);
return INVALID_SOCKET;
}
/* Connection succeeded. */
- DEBUG(net, 1, "[%s] connected to %s", type, address);
+ DEBUG(net, 1, "[%s] connected to %s", type, address.c_str());
return sock;
}
@@ -390,48 +389,47 @@ static SOCKET ListenLoopProc(addrinfo *runp)
{
const char *type = NetworkAddress::SocketTypeAsString(runp->ai_socktype);
const char *family = NetworkAddress::AddressFamilyAsString(runp->ai_family);
- char address[NETWORK_HOSTNAME_LENGTH + 6 + 7];
- NetworkAddress(runp->ai_addr, (int)runp->ai_addrlen).GetAddressAsString(address, lastof(address));
+ std::string address = NetworkAddress(runp->ai_addr, (int)runp->ai_addrlen).GetAddressAsString();
SOCKET sock = socket(runp->ai_family, runp->ai_socktype, runp->ai_protocol);
if (sock == INVALID_SOCKET) {
- DEBUG(net, 0, "[%s] could not create %s socket on port %s: %s", type, family, address, NetworkGetLastErrorString());
+ DEBUG(net, 0, "[%s] could not create %s socket on port %s: %s", type, family, address.c_str(), NetworkGetLastErrorString());
return INVALID_SOCKET;
}
if (runp->ai_socktype == SOCK_STREAM && !SetNoDelay(sock)) {
- DEBUG(net, 3, "[%s] setting TCP_NODELAY failed for port %s", type, address);
+ DEBUG(net, 3, "[%s] setting TCP_NODELAY failed for port %s", type, address.c_str());
}
int on = 1;
/* The (const char*) cast is needed for windows!! */
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on)) == -1) {
- DEBUG(net, 3, "[%s] could not set reusable %s sockets for port %s: %s", type, family, address, NetworkGetLastErrorString());
+ DEBUG(net, 3, "[%s] could not set reusable %s sockets for port %s: %s", type, family, address.c_str(), NetworkGetLastErrorString());
}
#ifndef __OS2__
if (runp->ai_family == AF_INET6 &&
setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&on, sizeof(on)) == -1) {
- DEBUG(net, 3, "[%s] could not disable IPv4 over IPv6 on port %s: %s", type, address, NetworkGetLastErrorString());
+ DEBUG(net, 3, "[%s] could not disable IPv4 over IPv6 on port %s: %s", type, address.c_str(), NetworkGetLastErrorString());
}
#endif
if (bind(sock, runp->ai_addr, (int)runp->ai_addrlen) != 0) {
- DEBUG(net, 1, "[%s] could not bind on %s port %s: %s", type, family, address, NetworkGetLastErrorString());
+ DEBUG(net, 1, "[%s] could not bind on %s port %s: %s", type, family, address.c_str(), NetworkGetLastErrorString());
closesocket(sock);
return INVALID_SOCKET;
}
if (runp->ai_socktype != SOCK_DGRAM && listen(sock, 1) != 0) {
- DEBUG(net, 1, "[%s] could not listen at %s port %s: %s", type, family, address, NetworkGetLastErrorString());
+ DEBUG(net, 1, "[%s] could not listen at %s port %s: %s", type, family, address.c_str(), NetworkGetLastErrorString());
closesocket(sock);
return INVALID_SOCKET;
}
/* Connection succeeded */
- if (!SetNonBlocking(sock)) DEBUG(net, 0, "[%s] setting non-blocking mode failed for %s port %s", type, family, address);
+ if (!SetNonBlocking(sock)) DEBUG(net, 0, "[%s] setting non-blocking mode failed for %s port %s", type, family, address.c_str());
- DEBUG(net, 1, "[%s] listening on %s port %s", type, family, address);
+ DEBUG(net, 1, "[%s] listening on %s port %s", type, family, address.c_str());
return sock;
}
diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp
index 57127aee8..e1f9a791b 100644
--- a/src/network/network_gui.cpp
+++ b/src/network/network_gui.cpp
@@ -645,9 +645,8 @@ public:
DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_SERVER_VERSION); // server version
y += FONT_HEIGHT_NORMAL;
- char network_addr_buffer[NETWORK_HOSTNAME_LENGTH + 6 + 7];
- sel->address.GetAddressAsString(network_addr_buffer, lastof(network_addr_buffer));
- SetDParamStr(0, network_addr_buffer);
+ std::string address = sel->address.GetAddressAsString();
+ SetDParamStr(0, address.c_str());
DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_SERVER_ADDRESS); // server address
y += FONT_HEIGHT_NORMAL;