1
0
Fork 0

Codechange: remove pointless close call due to resolving virtual functions statically in destructors

In the destructors of many of the network related classes Close() is called, just like the
top class in that hierarchy. However, due to virtual functions getting resolved statically
in the destructor it would always call the empty Close() of the top class.
Document the other cases where a virtual call is resolved statically.
pull/9261/head
Rubidium 2021-05-12 23:06:35 +02:00 committed by rubidium42
parent 7755f81bb8
commit 187a3f20bf
7 changed files with 18 additions and 12 deletions

View File

@ -46,10 +46,7 @@ public:
NetworkSocketHandler() { this->has_quit = false; }
/** Close the socket when destructing the socket handler */
virtual ~NetworkSocketHandler() { this->Close(); }
/** Really close the socket */
virtual void Close() {}
virtual ~NetworkSocketHandler() {}
/**
* Close the current connection; for TCP this will be mostly equivalent

View File

@ -29,7 +29,8 @@ NetworkTCPSocketHandler::NetworkTCPSocketHandler(SOCKET s) :
NetworkTCPSocketHandler::~NetworkTCPSocketHandler()
{
this->CloseConnection();
/* Virtual functions get called statically in destructors, so make it explicit to remove any confusion. */
this->NetworkTCPSocketHandler::CloseConnection();
if (this->sock != INVALID_SOCKET) closesocket(this->sock);
this->sock = INVALID_SOCKET;

View File

@ -137,9 +137,11 @@ const char *ContentInfo::GetTextfile(TextfileType type) const
return ::GetTextfile(type, GetContentInfoSubDir(this->type), tmp);
}
void NetworkContentSocketHandler::Close()
/**
* Close the actual socket.
*/
void NetworkContentSocketHandler::CloseSocket()
{
CloseConnection();
if (this->sock == INVALID_SOCKET) return;
closesocket(this->sock);

View File

@ -21,7 +21,7 @@
/** Base socket handler for all Content TCP sockets */
class NetworkContentSocketHandler : public NetworkTCPSocketHandler {
protected:
void Close() override;
void CloseSocket();
bool ReceiveInvalidPacket(PacketContentType type);
@ -124,7 +124,11 @@ public:
}
/** On destructing of this class, the socket needs to be closed */
virtual ~NetworkContentSocketHandler() { this->Close(); }
virtual ~NetworkContentSocketHandler()
{
/* Virtual functions get called statically in destructors, so make it explicit to remove any confusion. */
this->CloseSocket();
}
bool ReceivePackets();
};

View File

@ -190,7 +190,7 @@ public:
virtual ~NetworkUDPSocketHandler() { this->Close(); }
bool Listen();
void Close() override;
void Close();
void SendPacket(Packet *p, NetworkAddress *recv, bool all = false, bool broadcast = false);
void ReceivePackets();

View File

@ -784,7 +784,9 @@ void ClientNetworkContentSocketHandler::Connect()
void ClientNetworkContentSocketHandler::Close()
{
if (this->sock == INVALID_SOCKET) return;
NetworkContentSocketHandler::Close();
this->CloseConnection();
this->CloseSocket();
this->OnDisconnect();
}

View File

@ -107,7 +107,7 @@ public:
void Connect();
void SendReceive();
void Close() override;
void Close();
void RequestContentList(ContentType type);
void RequestContentList(uint count, const ContentID *content_ids);