mirror of https://github.com/OpenTTD/OpenTTD.git
Codechange: Swap SocketList map key/value around.
This map is used store socket and address together, and, other than checking that the address does not already have a socket, the data layout does not seem particularly important. However, as address is the key, technically it should not be modified, and address may self-modify itself during comparisons.
This commit is contained in:
parent
f454ec8d63
commit
72018badff
|
@ -259,7 +259,7 @@ SOCKET NetworkAddress::Resolve(int family, int socktype, int flags, SocketList *
|
|||
* of course totally unneeded ;) */
|
||||
if (sockets != nullptr) {
|
||||
NetworkAddress address(runp->ai_addr, (int)runp->ai_addrlen);
|
||||
if (sockets->Contains(address)) continue;
|
||||
if (std::any_of(sockets->begin(), sockets->end(), [&address](const auto &p) { return p.second == address; })) continue;
|
||||
}
|
||||
sock = func(runp);
|
||||
if (sock == INVALID_SOCKET) continue;
|
||||
|
@ -284,7 +284,7 @@ SOCKET NetworkAddress::Resolve(int family, int socktype, int flags, SocketList *
|
|||
}
|
||||
|
||||
NetworkAddress addr(runp->ai_addr, (int)runp->ai_addrlen);
|
||||
(*sockets)[addr] = sock;
|
||||
(*sockets)[sock] = addr;
|
||||
sock = INVALID_SOCKET;
|
||||
}
|
||||
freeaddrinfo (ai);
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
|
||||
class NetworkAddress;
|
||||
typedef std::vector<NetworkAddress> NetworkAddressList; ///< Type for a list of addresses.
|
||||
typedef SmallMap<NetworkAddress, SOCKET> SocketList; ///< Type for a mapping between address and socket.
|
||||
typedef SmallMap<SOCKET, NetworkAddress> SocketList; ///< Type for a mapping between address and socket.
|
||||
|
||||
/**
|
||||
* Wrapper for (un)resolved network addresses; there's no reason to transform
|
||||
|
|
|
@ -114,7 +114,7 @@ public:
|
|||
|
||||
/* take care of listener port */
|
||||
for (auto &s : sockets) {
|
||||
FD_SET(s.second, &read_fd);
|
||||
FD_SET(s.first, &read_fd);
|
||||
}
|
||||
|
||||
tv.tv_sec = tv.tv_usec = 0; // don't block at all.
|
||||
|
@ -122,7 +122,7 @@ public:
|
|||
|
||||
/* accept clients.. */
|
||||
for (auto &s : sockets) {
|
||||
if (FD_ISSET(s.second, &read_fd)) AcceptClient(s.second);
|
||||
if (FD_ISSET(s.first, &read_fd)) AcceptClient(s.first);
|
||||
}
|
||||
|
||||
/* read stuff from clients */
|
||||
|
@ -164,7 +164,7 @@ public:
|
|||
static void CloseListeners()
|
||||
{
|
||||
for (auto &s : sockets) {
|
||||
closesocket(s.second);
|
||||
closesocket(s.first);
|
||||
}
|
||||
sockets.clear();
|
||||
Debug(net, 5, "[{}] Closed listeners", Tsocket::GetName());
|
||||
|
|
|
@ -59,7 +59,7 @@ bool NetworkUDPSocketHandler::Listen()
|
|||
void NetworkUDPSocketHandler::CloseSocket()
|
||||
{
|
||||
for (auto &s : this->sockets) {
|
||||
closesocket(s.second);
|
||||
closesocket(s.first);
|
||||
}
|
||||
this->sockets.clear();
|
||||
}
|
||||
|
@ -81,20 +81,20 @@ void NetworkUDPSocketHandler::SendPacket(Packet *p, NetworkAddress *recv, bool a
|
|||
NetworkAddress send(*recv);
|
||||
|
||||
/* Not the same type */
|
||||
if (!send.IsFamily(s.first.GetAddress()->ss_family)) continue;
|
||||
if (!send.IsFamily(s.second.GetAddress()->ss_family)) continue;
|
||||
|
||||
p->PrepareToSend();
|
||||
|
||||
if (broadcast) {
|
||||
/* Enable broadcast */
|
||||
unsigned long val = 1;
|
||||
if (setsockopt(s.second, SOL_SOCKET, SO_BROADCAST, (char *) &val, sizeof(val)) < 0) {
|
||||
if (setsockopt(s.first, SOL_SOCKET, SO_BROADCAST, (char *) &val, sizeof(val)) < 0) {
|
||||
Debug(net, 1, "Setting broadcast mode failed: {}", NetworkError::GetLast().AsString());
|
||||
}
|
||||
}
|
||||
|
||||
/* Send the buffer */
|
||||
ssize_t res = p->TransferOut<int>(sendto, s.second, 0, (const struct sockaddr *)send.GetAddress(), send.GetAddressLength());
|
||||
ssize_t res = p->TransferOut<int>(sendto, s.first, 0, (const struct sockaddr *)send.GetAddress(), send.GetAddressLength());
|
||||
Debug(net, 7, "sendto({})", send.GetAddressAsString());
|
||||
|
||||
/* Check for any errors, but ignore it otherwise */
|
||||
|
@ -119,8 +119,8 @@ void NetworkUDPSocketHandler::ReceivePackets()
|
|||
socklen_t client_len = sizeof(client_addr);
|
||||
|
||||
/* Try to receive anything */
|
||||
SetNonBlocking(s.second); // Some OSes seem to lose the non-blocking status of the socket
|
||||
ssize_t nbytes = p.TransferIn<int>(recvfrom, s.second, 0, (struct sockaddr *)&client_addr, &client_len);
|
||||
SetNonBlocking(s.first); // Some OSes seem to lose the non-blocking status of the socket
|
||||
ssize_t nbytes = p.TransferIn<int>(recvfrom, s.first, 0, (struct sockaddr *)&client_addr, &client_len);
|
||||
|
||||
/* Did we get the bytes for the base header of the packet? */
|
||||
if (nbytes <= 0) break; // No data, i.e. no packet
|
||||
|
|
Loading…
Reference in New Issue