Skip to content

Commit

Permalink
fix wsacleanup called more times than it should (#598)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Parfenov <a1994ndrey@gmail.com>

Signed-off-by: Andrey Parfenov <a1994ndrey@gmail.com>
  • Loading branch information
Andrey1994 committed Jan 22, 2023
1 parent acc39c8 commit 2396b0f
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 15 deletions.
12 changes: 11 additions & 1 deletion src/utils/broadcast_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@ BroadCastClient::BroadCastClient (int port)
this->port = port;
connect_socket = INVALID_SOCKET;
memset (&socket_addr, 0, sizeof (socket_addr));
wsa_initialized = false;
}

int BroadCastClient::init ()
{
if (wsa_initialized)
{
return (int)BroadCastClientReturnCodes::SOCKET_ALREADY_CREATED_ERROR;
}
WSADATA wsadata;
int res = WSAStartup (MAKEWORD (2, 2), &wsadata);
if (res != 0)
{
return (int)BroadCastClientReturnCodes::WSA_STARTUP_ERROR;
}
wsa_initialized = true;
socket_addr.sin_family = AF_INET;
socket_addr.sin_port = htons (port);
socket_addr.sin_addr.s_addr = htonl (INADDR_ANY);
Expand Down Expand Up @@ -65,7 +71,11 @@ void BroadCastClient::close ()
{
closesocket (connect_socket);
connect_socket = INVALID_SOCKET;
WSACleanup ();
if (wsa_initialized)
{
WSACleanup ();
wsa_initialized = false;
}
}

///////////////////////////////
Expand Down
13 changes: 12 additions & 1 deletion src/utils/broadcast_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ BroadCastServer::BroadCastServer (int port)
connect_socket = INVALID_SOCKET;
memset (&socket_addr, 0, sizeof (socket_addr));
strcpy (address, "255.255.255.255");
wsa_initialized = false;
}

BroadCastServer::BroadCastServer (const char *address, int port)
Expand All @@ -31,16 +32,22 @@ BroadCastServer::BroadCastServer (const char *address, int port)
connect_socket = INVALID_SOCKET;
memset (&socket_addr, 0, sizeof (socket_addr));
strcpy (this->address, address);
wsa_initialized = false;
}

int BroadCastServer::init ()
{
WSADATA wsadata;
if (wsa_initialized)
{
return (int)BroadCastServerReturnCodes::SOCKET_ALREADY_CREATED_ERROR;
}
int res = WSAStartup (MAKEWORD (2, 2), &wsadata);
if (res != 0)
{
return (int)BroadCastServerReturnCodes::WSA_STARTUP_ERROR;
}
wsa_initialized = true;
socket_addr.sin_family = AF_INET;
socket_addr.sin_port = htons (port);
if (inet_pton (AF_INET, address, &socket_addr.sin_addr) == 0)
Expand Down Expand Up @@ -99,7 +106,11 @@ void BroadCastServer::close ()
{
closesocket (connect_socket);
connect_socket = INVALID_SOCKET;
WSACleanup ();
if (wsa_initialized)
{
WSACleanup ();
wsa_initialized = false;
}
}

///////////////////////////////
Expand Down
4 changes: 3 additions & 1 deletion src/utils/inc/broadcast_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ enum class BroadCastClientReturnCodes : int
STATUS_OK = 0,
WSA_STARTUP_ERROR = 1,
CREATE_SOCKET_ERROR = 2,
INIT_ERROR = 3
INIT_ERROR = 3,
SOCKET_ALREADY_CREATED_ERROR = 4
};


Expand Down Expand Up @@ -45,6 +46,7 @@ class BroadCastClient
#ifdef _WIN32
SOCKET connect_socket;
struct sockaddr_in socket_addr;
bool wsa_initialized;
#else
int connect_socket;
struct sockaddr_in socket_addr;
Expand Down
4 changes: 3 additions & 1 deletion src/utils/inc/broadcast_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ enum class BroadCastServerReturnCodes : int
STATUS_OK = 0,
WSA_STARTUP_ERROR = 1,
CREATE_SOCKET_ERROR = 2,
INIT_ERROR = 3
INIT_ERROR = 3,
SOCKET_ALREADY_CREATED_ERROR = 4
};


Expand All @@ -43,6 +44,7 @@ class BroadCastServer
#ifdef _WIN32
SOCKET connect_socket;
struct sockaddr_in socket_addr;
bool wsa_initialized;
#else
int connect_socket;
struct sockaddr_in socket_addr;
Expand Down
4 changes: 3 additions & 1 deletion src/utils/inc/multicast_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ enum class MultiCastReturnCodes : int
CREATE_SOCKET_ERROR = 2,
BIND_ERROR = 3,
PTON_ERROR = 4,
SET_OPT_ERROR = 5
SET_OPT_ERROR = 5,
SOCKET_ALREADY_CREATED_ERROR = 6
};

class MultiCastClient
Expand All @@ -43,6 +44,7 @@ class MultiCastClient
#ifdef _WIN32
SOCKET client_socket;
struct sockaddr_in socket_addr;
bool wsa_initialized;
#else
int client_socket;
struct sockaddr_in socket_addr;
Expand Down
1 change: 1 addition & 0 deletions src/utils/inc/multicast_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class MultiCastServer

#ifdef _WIN32
volatile SOCKET server_socket;
bool wsa_initialized;
#else
volatile int server_socket;
#endif
Expand Down
4 changes: 3 additions & 1 deletion src/utils/inc/socket_client_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ enum class SocketClientTCPReturnCodes : int
WSA_STARTUP_ERROR = 1,
CREATE_SOCKET_ERROR = 2,
CONNECT_ERROR = 3,
PTON_ERROR = 4
PTON_ERROR = 4,
SOCKET_ALREADY_CREATED_ERROR = 5
};

class SocketClientTCP
Expand Down Expand Up @@ -52,6 +53,7 @@ class SocketClientTCP
#ifdef _WIN32
SOCKET connect_socket;
struct sockaddr_in socket_addr;
bool wsa_initialized;
#else
int connect_socket;
struct sockaddr_in socket_addr;
Expand Down
1 change: 1 addition & 0 deletions src/utils/inc/socket_client_udp.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class SocketClientUDP
#ifdef _WIN32
SOCKET connect_socket;
struct sockaddr_in socket_addr;
bool wsa_initialized;
#else
int connect_socket;
struct sockaddr_in socket_addr;
Expand Down
4 changes: 3 additions & 1 deletion src/utils/inc/socket_server_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ enum class SocketServerTCPReturnCodes : int
WSA_STARTUP_ERROR = 1,
CREATE_SOCKET_ERROR = 2,
CONNECT_ERROR = 3,
PTON_ERROR = 4
PTON_ERROR = 4,
SOCKET_ALREADY_CREATED_ERROR = 5
};


Expand Down Expand Up @@ -56,6 +57,7 @@ class SocketServerTCP
#ifdef _WIN32
volatile SOCKET server_socket;
volatile SOCKET connected_socket;
bool wsa_initialized;
#else
volatile int server_socket;
volatile int connected_socket;
Expand Down
4 changes: 3 additions & 1 deletion src/utils/inc/socket_server_udp.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ enum class SocketServerUDPReturnCodes
STATUS_OK = 0,
WSA_STARTUP_ERROR = 1,
CREATE_SOCKET_ERROR = 2,
BIND_ERROR = 3
BIND_ERROR = 3,
SOCKET_ALREADY_CREATED_ERROR = 4
};

class SocketServerUDP
Expand All @@ -38,6 +39,7 @@ class SocketServerUDP

#ifdef _WIN32
SOCKET server_socket;
bool wsa_initialized;
#else
int server_socket;
#endif
Expand Down
12 changes: 11 additions & 1 deletion src/utils/multicast_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ MultiCastClient::MultiCastClient (const char *ip_addr, int port)
this->port = port;
client_socket = INVALID_SOCKET;
memset (&socket_addr, 0, sizeof (socket_addr));
wsa_initialized = false;
}

int MultiCastClient::init ()
{
if (wsa_initialized)
{
return (int)MultiCastReturnCodes::SOCKET_ALREADY_CREATED_ERROR;
}
WSADATA wsadata;
int res = WSAStartup (MAKEWORD (2, 2), &wsadata);
if (res != 0)
{
return (int)MultiCastReturnCodes::WSA_STARTUP_ERROR;
}
wsa_initialized = true;
socket_addr.sin_family = AF_INET;
socket_addr.sin_port = htons (port);
socket_addr.sin_addr.s_addr = htonl (INADDR_ANY);
Expand Down Expand Up @@ -101,7 +107,11 @@ void MultiCastClient::close ()
closesocket (client_socket);
}
client_socket = INVALID_SOCKET;
WSACleanup ();
if (wsa_initialized)
{
WSACleanup ();
wsa_initialized = false;
}
}

///////////////////////////////
Expand Down
12 changes: 11 additions & 1 deletion src/utils/multicast_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@ MultiCastServer::MultiCastServer (const char *local_ip, int local_port)
this->local_port = local_port;
server_socket = INVALID_SOCKET;
memset (&server_addr, 0, sizeof (server_addr));
wsa_initialized = false;
}

int MultiCastServer::init ()
{
if (wsa_initialized)
{
return (int)MultiCastReturnCodes::SOCKET_ALREADY_CREATED_ERROR;
}
WSADATA wsadata;
int res = WSAStartup (MAKEWORD (2, 2), &wsadata);
if (res != 0)
{
return (int)MultiCastReturnCodes::WSA_STARTUP_ERROR;
}
wsa_initialized = true;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons (local_port);
if (inet_pton (AF_INET, local_ip, &server_addr.sin_addr) == 0)
Expand Down Expand Up @@ -69,7 +75,11 @@ void MultiCastServer::close ()
closesocket (server_socket);
server_socket = INVALID_SOCKET;
}
WSACleanup ();
if (wsa_initialized)
{
WSACleanup ();
wsa_initialized = false;
}
}

///////////////////////////////
Expand Down
12 changes: 11 additions & 1 deletion src/utils/socket_client_tcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@ SocketClientTCP::SocketClientTCP (const char *ip_addr, int port)
this->port = port;
connect_socket = INVALID_SOCKET;
memset (&socket_addr, 0, sizeof (socket_addr));
wsa_initialized = false;
}

int SocketClientTCP::connect ()
{
if (wsa_initialized)
{
return (int)SocketClientTCPReturnCodes::SOCKET_ALREADY_CREATED_ERROR;
}
WSADATA wsadata;
int res = WSAStartup (MAKEWORD (2, 2), &wsadata);
if (res != 0)
{
return (int)SocketClientTCPReturnCodes::WSA_STARTUP_ERROR;
}
wsa_initialized = true;
socket_addr.sin_family = AF_INET;
socket_addr.sin_port = htons (port);
if (inet_pton (AF_INET, ip_addr, &socket_addr.sin_addr) == 0)
Expand Down Expand Up @@ -79,7 +85,11 @@ void SocketClientTCP::close ()
{
closesocket (connect_socket);
connect_socket = INVALID_SOCKET;
WSACleanup ();
if (wsa_initialized)
{
WSACleanup ();
wsa_initialized = false;
}
}

///////////////////////////////
Expand Down
22 changes: 20 additions & 2 deletions src/utils/socket_client_udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ int SocketClientUDP::get_local_ip_addr (const char *connect_ip, int port, char *
char buffer[80];
SOCKET sock = INVALID_SOCKET;
struct sockaddr_in name;
bool wsa_initialized = false;
int res = WSAStartup (MAKEWORD (2, 2), &wsadata);
if (res != 0)
{
return_value = (int)SocketClientUDPReturnCodes::WSA_STARTUP_ERROR;
}
else
{
wsa_initialized = true;
}

if (return_value == (int)SocketClientUDPReturnCodes::STATUS_OK)
{
Expand Down Expand Up @@ -78,7 +83,10 @@ int SocketClientUDP::get_local_ip_addr (const char *connect_ip, int port, char *
}

closesocket (sock);
WSACleanup ();
if (wsa_initialized)
{
WSACleanup ();
}
return return_value;
}

Expand All @@ -88,16 +96,22 @@ SocketClientUDP::SocketClientUDP (const char *ip_addr, int port)
this->port = port;
connect_socket = INVALID_SOCKET;
memset (&socket_addr, 0, sizeof (socket_addr));
wsa_initialized = false;
}

int SocketClientUDP::connect ()
{
if (wsa_initialized)
{
return (int)SocketClientUDPReturnCodes::SOCKET_ALREADY_CREATED_ERROR;
}
WSADATA wsadata;
int res = WSAStartup (MAKEWORD (2, 2), &wsadata);
if (res != 0)
{
return (int)SocketClientUDPReturnCodes::WSA_STARTUP_ERROR;
}
wsa_initialized = true;
socket_addr.sin_family = AF_INET;
socket_addr.sin_port = htons (port);
if (inet_pton (AF_INET, ip_addr, &socket_addr.sin_addr) == 0)
Expand Down Expand Up @@ -217,7 +231,11 @@ void SocketClientUDP::close ()
{
closesocket (connect_socket);
connect_socket = INVALID_SOCKET;
WSACleanup ();
if (wsa_initialized)
{
WSACleanup ();
wsa_initialized = false;
}
}

///////////////////////////////
Expand Down

0 comments on commit 2396b0f

Please sign in to comment.