Issues/issue 513 how to reinitialize a bacnet stack on windows (#514)

* Check init flag before cleaning up WSA for BACnet/IP

* Check for valid socket before cleaning up WSA for BACnet/IPv6

---------

Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
Steve Karg
2023-10-17 23:40:28 -05:00
committed by GitHub
parent e945c948d1
commit 20029d3f7a
2 changed files with 13 additions and 9 deletions
+7 -4
View File
@@ -48,6 +48,7 @@
/* Windows sockets */
static SOCKET BIP_Socket = INVALID_SOCKET;
static SOCKET BIP_Broadcast_Socket = INVALID_SOCKET;
static bool BIP_Initialized;
/* NOTE: we store address and port in network byte order
since BACnet/IP uses network byte order for all address byte arrays
@@ -258,18 +259,17 @@ static void print_last_error(const char *info)
*/
static void bip_init_windows(void)
{
static bool initialized = false;
int Result;
WSADATA wd;
if (!initialized) {
if (!BIP_Initialized) {
Result = WSAStartup((1 << 8) | 1, &wd);
/*Result = WSAStartup(MAKEWORD(2,2), &wd); */
if (Result != 0) {
print_last_error("TCP/IP stack initialization failed");
exit(1);
}
initialized = true;
BIP_Initialized = true;
atexit(bip_cleanup);
}
}
@@ -933,7 +933,10 @@ void bip_cleanup(void)
}
BIP_Broadcast_Socket = INVALID_SOCKET;
WSACleanup();
if (BIP_Initialized) {
BIP_Initialized = false;
WSACleanup();
}
return;
}
+6 -5
View File
@@ -375,7 +375,7 @@ int bip6_send_mpdu(BACNET_IP6_ADDRESS *dest, uint8_t *mtu, uint16_t mtu_len)
uint16_t addr16[8];
/* assumes that the driver has already been initialized */
if (BIP6_Socket < 0) {
if (BIP6_Socket == INVALID_SOCKET) {
return 0;
}
/* load destination IP address */
@@ -443,7 +443,7 @@ uint16_t bip6_receive(
uint16_t i = 0;
/* Make sure the socket is open */
if (BIP6_Socket < 0) {
if (BIP6_Socket == INVALID_SOCKET) {
return 0;
}
/* we could just use a non-blocking socket, but that consumes all
@@ -503,10 +503,11 @@ uint16_t bip6_receive(
*/
void bip6_cleanup(void)
{
if (BIP6_Socket != -1) {
closesocket(BIP6_Socket);
if (BIP6_Socket == INVALID_SOCKET) {
return;
}
BIP6_Socket = -1;
closesocket(BIP6_Socket);
BIP6_Socket = INVALID_SOCKET;
WSACleanup();
return;