diff options
author | Patric Stout <truebrain@openttd.org> | 2021-05-13 11:46:51 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-13 11:46:51 +0200 |
commit | a403653805c6fd6022868c5f381e10107e1d2b20 (patch) | |
tree | b44e4df40d65cf1fecdadab090badb73d78e0a6d /src/network/core/tcp.cpp | |
parent | 86741ad489c3ee2d519eeb071be846721b90412c (diff) | |
download | openttd-a403653805c6fd6022868c5f381e10107e1d2b20.tar.xz |
Codechange: [Network] split CloseSocket and CloseConnection more clearly (#9261)
* Codechange: [Network] split CloseSocket and CloseConnection more clearly
- CloseSocket now closes the actual OS socket.
- CloseConnection frees up the resources to just before CloseSocket.
- dtors call CloseSocket / CloseConnection where needed.
Diffstat (limited to 'src/network/core/tcp.cpp')
-rw-r--r-- | src/network/core/tcp.cpp | 39 |
1 files changed, 30 insertions, 9 deletions
diff --git a/src/network/core/tcp.cpp b/src/network/core/tcp.cpp index 5c436edf0..d5754f69a 100644 --- a/src/network/core/tcp.cpp +++ b/src/network/core/tcp.cpp @@ -29,24 +29,45 @@ NetworkTCPSocketHandler::NetworkTCPSocketHandler(SOCKET s) : NetworkTCPSocketHandler::~NetworkTCPSocketHandler() { - /* Virtual functions get called statically in destructors, so make it explicit to remove any confusion. */ - this->NetworkTCPSocketHandler::CloseConnection(); + this->EmptyPacketQueue(); + this->CloseSocket(); +} + +/** + * Free all pending and partially received packets. + */ +void NetworkTCPSocketHandler::EmptyPacketQueue() +{ + while (this->packet_queue != nullptr) { + delete Packet::PopFromQueue(&this->packet_queue); + } + delete this->packet_recv; + this->packet_recv = nullptr; +} +/** + * Close the actual socket of the connection. + * Please make sure CloseConnection is called before CloseSocket, as + * otherwise not all resources might be released. + */ +void NetworkTCPSocketHandler::CloseSocket() +{ if (this->sock != INVALID_SOCKET) closesocket(this->sock); this->sock = INVALID_SOCKET; } +/** + * This will put this socket handler in a close state. It will not + * actually close the OS socket; use CloseSocket for this. + * @param error Whether we quit under an error condition or not. + * @return new status of the connection. + */ NetworkRecvStatus NetworkTCPSocketHandler::CloseConnection(bool error) { + this->MarkClosed(); this->writable = false; - NetworkSocketHandler::CloseConnection(error); - /* Free all pending and partially received packets */ - while (this->packet_queue != nullptr) { - delete Packet::PopFromQueue(&this->packet_queue); - } - delete this->packet_recv; - this->packet_recv = nullptr; + this->EmptyPacketQueue(); return NETWORK_RECV_STATUS_OKAY; } |