Network: Avoid racy use of flags in TCPConnecter::CheckCallbacks

This commit is contained in:
Jonathan G Rennison
2020-05-06 23:41:25 +01:00
parent ef7e658dee
commit 58c02030c2
2 changed files with 8 additions and 5 deletions

View File

@@ -17,6 +17,7 @@
#include <deque>
#include <memory>
#include <atomic>
/** The states of sending the packets. */
enum SendPacketsState {
@@ -70,8 +71,8 @@ public:
*/
class TCPConnecter {
private:
bool connected; ///< Whether we succeeded in making the connection
bool aborted; ///< Whether we bailed out (i.e. connection making failed)
std::atomic<bool> connected;///< Whether we succeeded in making the connection
std::atomic<bool> aborted; ///< Whether we bailed out (i.e. connection making failed)
bool killed; ///< Whether we got killed
SOCKET sock; ///< The socket we're connecting with

View File

@@ -66,19 +66,21 @@ void TCPConnecter::Connect()
{
for (auto iter = _tcp_connecters.begin(); iter < _tcp_connecters.end(); /* nothing */) {
TCPConnecter *cur = *iter;
if ((cur->connected || cur->aborted) && cur->killed) {
const bool connected = cur->connected.load();
const bool aborted = cur->aborted.load();
if ((connected || aborted) && cur->killed) {
iter = _tcp_connecters.erase(iter);
if (cur->sock != INVALID_SOCKET) closesocket(cur->sock);
delete cur;
continue;
}
if (cur->connected) {
if (connected) {
iter = _tcp_connecters.erase(iter);
cur->OnConnect(cur->sock);
delete cur;
continue;
}
if (cur->aborted) {
if (aborted) {
iter = _tcp_connecters.erase(iter);
cur->OnFailure();
delete cur;