X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/70914290a18a1c95c16ff5148d53f3dea52d2940..57530dba8561c3133657ea946665b2d6a56279b8:/src/unix/gsocket.c diff --git a/src/unix/gsocket.c b/src/unix/gsocket.c index 85a38c699f..03bada003b 100644 --- a/src/unix/gsocket.c +++ b/src/unix/gsocket.c @@ -172,6 +172,13 @@ GSocket *GSocket_new(void) return socket; } +void GSocket_close(GSocket *socket) +{ + _GSocket_Disable_Events(socket); + close(socket->m_fd); + socket->m_fd = INVALID_SOCKET; +} + void GSocket_destroy(GSocket *socket) { assert(socket != NULL); @@ -208,8 +215,7 @@ void GSocket_Shutdown(GSocket *socket) if (socket->m_fd != INVALID_SOCKET) { shutdown(socket->m_fd, 2); - close(socket->m_fd); - socket->m_fd = INVALID_SOCKET; + GSocket_close(socket); } /* Disable GUI callbacks */ @@ -217,7 +223,6 @@ void GSocket_Shutdown(GSocket *socket) socket->m_cbacks[evt] = NULL; socket->m_detected = GSOCK_LOST_FLAG; - _GSocket_Disable_Events(socket); } /* Address handling */ @@ -396,8 +401,7 @@ GSocketError GSocket_SetServer(GSocket *sck) (SOCKLEN_T *) &sck->m_local->m_len) != 0) || (listen(sck->m_fd, 5) != 0)) { - close(sck->m_fd); - sck->m_fd = INVALID_SOCKET; + GSocket_close(sck); sck->m_error = GSOCK_IOERR; return GSOCK_IOERR; } @@ -577,8 +581,7 @@ GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream) { if (_GSocket_Output_Timeout(sck) == GSOCK_TIMEDOUT) { - close(sck->m_fd); - sck->m_fd = INVALID_SOCKET; + GSocket_close(sck); /* sck->m_error is set in _GSocket_Output_Timeout */ return GSOCK_TIMEDOUT; } @@ -610,8 +613,7 @@ GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream) /* If connect failed with an error other than EINPROGRESS, * then the call to GSocket_Connect has failed. */ - close(sck->m_fd); - sck->m_fd = INVALID_SOCKET; + GSocket_close(sck); sck->m_error = GSOCK_IOERR; return GSOCK_IOERR; } @@ -675,8 +677,7 @@ GSocketError GSocket_SetNonOriented(GSocket *sck) sck->m_local->m_addr, (SOCKLEN_T *) &sck->m_local->m_len) != 0)) { - close(sck->m_fd); - sck->m_fd = INVALID_SOCKET; + GSocket_close(sck); sck->m_error = GSOCK_IOERR; return GSOCK_IOERR; } @@ -1042,10 +1043,10 @@ GSocketError _GSocket_Input_Timeout(GSocket *socket) if (ret == -1) { GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" )); - if (errno == EBADF) GSocket_Debug(( "Invalid file descriptor\n" )); - if (errno == EINTR) GSocket_Debug(( "A non blocked signal was caught\n" )); - if (errno == EINVAL) GSocket_Debug(( "The highest number descriptor is negative\n" )); - if (errno == ENOMEM) GSocket_Debug(( "Not enough memory\n" )); + if (errno == EBADF) { GSocket_Debug(( "Invalid file descriptor\n" )); } + if (errno == EINTR) { GSocket_Debug(( "A non blocked signal was caught\n" )); } + if (errno == EINVAL) { GSocket_Debug(( "The highest number descriptor is negative\n" )); } + if (errno == ENOMEM) { GSocket_Debug(( "Not enough memory\n" )); } socket->m_error = GSOCK_TIMEDOUT; return GSOCK_TIMEDOUT; } @@ -1083,17 +1084,19 @@ GSocketError _GSocket_Output_Timeout(GSocket *socket) if (ret == -1) { GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" )); - if (errno == EBADF) GSocket_Debug(( "Invalid file descriptor\n" )); - if (errno == EINTR) GSocket_Debug(( "A non blocked signal was caught\n" )); - if (errno == EINVAL) GSocket_Debug(( "The highest number descriptor is negative\n" )); - if (errno == ENOMEM) GSocket_Debug(( "Not enough memory\n" )); + if (errno == EBADF) { GSocket_Debug(( "Invalid file descriptor\n" )); } + if (errno == EINTR) { GSocket_Debug(( "A non blocked signal was caught\n" )); } + if (errno == EINVAL) { GSocket_Debug(( "The highest number descriptor is negative\n" )); } + if (errno == ENOMEM) { GSocket_Debug(( "Not enough memory\n" )); } socket->m_error = GSOCK_TIMEDOUT; return GSOCK_TIMEDOUT; } - if ( ! FD_ISSET(socket->m_fd, &writefds) ) - GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" )); - else - GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" )); + if ( ! FD_ISSET(socket->m_fd, &writefds) ) { + GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" )); + } + else { + GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" )); + } } else { @@ -1299,7 +1302,7 @@ GAddress *GAddress_copy(GAddress *address) memcpy(addr2, address, sizeof(GAddress)); - if (address->m_addr) + if (address->m_addr && address->m_len > 0) { addr2->m_addr = (struct sockaddr *)malloc(addr2->m_len); if (addr2->m_addr == NULL) @@ -1638,3 +1641,5 @@ GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf) #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */ +/* vi:sts=4:sw=4:et */ +