X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/c40158e40b45fd22c7a166ce1743345fb4d4a4d3..edc536d358374987326d93adb1012b6eda9435a0:/src/unix/gsocket.cpp diff --git a/src/unix/gsocket.cpp b/src/unix/gsocket.cpp index 100beaa553..874098d3b6 100644 --- a/src/unix/gsocket.cpp +++ b/src/unix/gsocket.cpp @@ -12,10 +12,6 @@ * ------------------------------------------------------------------------- */ -/* - * PLEASE don't put C++ comments here - this is a C source file. - */ - #if defined(__WATCOMC__) #include "wx/wxprec.h" #include @@ -23,17 +19,10 @@ #endif #ifndef __GSOCKET_STANDALONE__ -#include "wx/setup.h" -#endif - -#ifndef WXUNUSED -#define WXUNUSED(x) +#include "wx/defs.h" #endif #if defined(__VISAGECPP__) -/* Seems to be needed by Visual Age C++, though I don't see how it manages - to not break on including a C++ header into a plain C source file */ -#include "wx/defs.h" #define BSD_SELECT /* use Berkley Sockets select */ #endif @@ -118,26 +107,26 @@ int _System soclose(int); #endif #include -#ifndef SOCKLEN_T +#ifndef WX_SOCKLEN_T #ifdef VMS -# define SOCKLEN_T unsigned int +# define WX_SOCKLEN_T unsigned int #else # ifdef __GLIBC__ # if __GLIBC__ == 2 -# define SOCKLEN_T socklen_t +# define WX_SOCKLEN_T socklen_t # endif # elif defined(__WXMAC__) -# define SOCKLEN_T socklen_t +# define WX_SOCKLEN_T socklen_t # else -# define SOCKLEN_T int +# define WX_SOCKLEN_T int # endif #endif #endif /* SOCKLEN_T */ #ifndef SOCKOPTLEN_T -#define SOCKOPTLEN_T SOCKLEN_T +#define SOCKOPTLEN_T WX_SOCKLEN_T #endif /* @@ -166,12 +155,11 @@ int _System soclose(int); #define UNMASK_SIGNAL() } #else + extern "C" { typedef void (*wxSigHandler)(int); } #define MASK_SIGNAL() \ { \ - void (*old_handler)(int); \ - \ - old_handler = signal(SIGPIPE, SIG_IGN); + wxSigHandler old_handler = signal(SIGPIPE, SIG_IGN); #define UNMASK_SIGNAL() \ signal(SIGPIPE, old_handler); \ @@ -191,10 +179,14 @@ int _System soclose(int); #ifndef __GSOCKET_STANDALONE__ # include "wx/unix/gsockunx.h" +# include "wx/unix/private.h" # include "wx/gsocket.h" #else # include "gsockunx.h" # include "gsocket.h" +# ifndef WXUNUSED +# define WXUNUSED(x) +# endif #endif /* __GSOCKET_STANDALONE__ */ /* debugging helpers */ @@ -336,6 +328,9 @@ void GSocket::Shutdown() assert(this); + /* Don't allow events to fire after socket has been closed */ + gs_gui_functions->Disable_Events(this); + /* If socket has been created, shutdown it */ if (m_fd != INVALID_SOCKET) { @@ -415,7 +410,7 @@ GAddress *GSocket::GetLocal() { GAddress *address; struct sockaddr addr; - SOCKLEN_T size = sizeof(addr); + WX_SOCKLEN_T size = sizeof(addr); GSocketError err; assert(this); @@ -431,7 +426,7 @@ GAddress *GSocket::GetLocal() return NULL; } - if (getsockname(m_fd, &addr, (SOCKLEN_T *) &size) < 0) + if (getsockname(m_fd, &addr, (WX_SOCKLEN_T *) &size) < 0) { m_error = GSOCK_IOERR; return NULL; @@ -533,7 +528,7 @@ GSocketError GSocket::SetServer() if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) || (getsockname(m_fd, m_local->m_addr, - (SOCKLEN_T *) &m_local->m_len) != 0) || + (WX_SOCKLEN_T *) &m_local->m_len) != 0) || (listen(m_fd, 5) != 0)) { Close(); @@ -559,7 +554,7 @@ GSocketError GSocket::SetServer() GSocket *GSocket::WaitConnection() { struct sockaddr from; - SOCKLEN_T fromlen = sizeof(from); + WX_SOCKLEN_T fromlen = sizeof(from); GSocket *connection; GSocketError err; int arg = 1; @@ -590,7 +585,7 @@ GSocket *GSocket::WaitConnection() return NULL; } - connection->m_fd = accept(m_fd, &from, (SOCKLEN_T *) &fromlen); + connection->m_fd = accept(m_fd, &from, (WX_SOCKLEN_T *) &fromlen); /* Reenable CONNECTION events */ Enable(GSOCK_CONNECTION); @@ -721,9 +716,15 @@ GSocketError GSocket::Connect(GSocketStream stream) /* Connect it to the peer address, with a timeout (see below) */ ret = connect(m_fd, m_peer->m_addr, m_peer->m_len); - /* We only call Enable_Events if we know e aren't shutting down the socket */ + /* We only call Enable_Events if we know we aren't shutting down the socket. + * NB: Enable_Events needs to be called whether the socket is blocking or + * non-blocking, it just shouldn't be called prior to knowing there is a + * connection _if_ blocking sockets are being used. + * If connect above returns 0, we are already connected and need to make the + * call to Enable_Events now. + */ - if (m_non_blocking) + if (m_non_blocking || ret == 0) { gs_gui_functions->Enable_Events(this); } @@ -839,7 +840,7 @@ GSocketError GSocket::SetNonOriented() if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) || (getsockname(m_fd, m_local->m_addr, - (SOCKLEN_T *) &m_local->m_len) != 0)) + (WX_SOCKLEN_T *) &m_local->m_len) != 0)) { Close(); m_error = GSOCK_IOERR; @@ -868,23 +869,25 @@ int GSocket::Read(char *buffer, int size) Disable(GSOCK_INPUT); /* If the socket is blocking, wait for data (with a timeout) */ - if (Input_Timeout() == GSOCK_TIMEDOUT) - /* We no longer return here immediately, otherwise socket events would not be re-enabled! */ + if (Input_Timeout() == GSOCK_TIMEDOUT) { + m_error = GSOCK_TIMEDOUT; + /* Don't return here immediately, otherwise socket events would not be + * re-enabled! */ ret = -1; + } else { /* Read the data */ if (m_stream) ret = Recv_Stream(buffer, size); else ret = Recv_Dgram(buffer, size); - } - if (ret == -1) - { - if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) - m_error = GSOCK_WOULDBLOCK; - else - m_error = GSOCK_IOERR; + if (ret == -1) { + if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) + m_error = GSOCK_WOULDBLOCK; + else + m_error = GSOCK_IOERR; + } } /* Enable events again now that we are done processing */ @@ -970,17 +973,20 @@ GSocketEventFlags GSocket::Select(GSocketEventFlags flags) assert(this); + if (m_fd == -1) + return (GSOCK_LOST_FLAG & flags); + /* Do not use a static struct, Linux can garble it */ tv.tv_sec = m_timeout / 1000; tv.tv_usec = (m_timeout % 1000) * 1000; - FD_ZERO(&readfds); - FD_ZERO(&writefds); - FD_ZERO(&exceptfds); - FD_SET(m_fd, &readfds); + wxFD_ZERO(&readfds); + wxFD_ZERO(&writefds); + wxFD_ZERO(&exceptfds); + wxFD_SET(m_fd, &readfds); if (flags & GSOCK_OUTPUT_FLAG || flags & GSOCK_CONNECTION_FLAG) - FD_SET(m_fd, &writefds); - FD_SET(m_fd, &exceptfds); + wxFD_SET(m_fd, &writefds); + wxFD_SET(m_fd, &exceptfds); /* Check 'sticky' CONNECTION flag first */ result |= (GSOCK_CONNECTION_FLAG & m_detected); @@ -1003,7 +1009,7 @@ GSocketEventFlags GSocket::Select(GSocketEventFlags flags) } /* Check for readability */ - if (FD_ISSET(m_fd, &readfds)) + if (wxFD_ISSET(m_fd, &readfds)) { char c; @@ -1032,7 +1038,7 @@ GSocketEventFlags GSocket::Select(GSocketEventFlags flags) } /* Check for writability */ - if (FD_ISSET(m_fd, &writefds)) + if (wxFD_ISSET(m_fd, &writefds)) { if (m_establishing && !m_server) { @@ -1063,7 +1069,7 @@ GSocketEventFlags GSocket::Select(GSocketEventFlags flags) } /* Check for exceptions and errors (is this useful in Unices?) */ - if (FD_ISSET(m_fd, &exceptfds)) + if (wxFD_ISSET(m_fd, &exceptfds)) { m_establishing = false; m_detected = GSOCK_LOST_FLAG; @@ -1244,8 +1250,8 @@ GSocketError GSocket::Input_Timeout() if (!m_non_blocking) { - FD_ZERO(&readfds); - FD_SET(m_fd, &readfds); + wxFD_ZERO(&readfds); + wxFD_SET(m_fd, &readfds); ret = select(m_fd + 1, &readfds, NULL, NULL, &tv); if (ret == 0) { @@ -1285,8 +1291,8 @@ GSocketError GSocket::Output_Timeout() if (!m_non_blocking) { - FD_ZERO(&writefds); - FD_SET(m_fd, &writefds); + wxFD_ZERO(&writefds); + wxFD_SET(m_fd, &writefds); ret = select(m_fd + 1, NULL, &writefds, NULL, &tv); if (ret == 0) { @@ -1304,7 +1310,7 @@ GSocketError GSocket::Output_Timeout() m_error = GSOCK_TIMEDOUT; return GSOCK_TIMEDOUT; } - if ( ! FD_ISSET(m_fd, &writefds) ) { + if ( ! wxFD_ISSET(m_fd, &writefds) ) { GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" )); } else { @@ -1332,7 +1338,7 @@ int GSocket::Recv_Stream(char *buffer, int size) int GSocket::Recv_Dgram(char *buffer, int size) { struct sockaddr from; - SOCKLEN_T fromlen = sizeof(from); + WX_SOCKLEN_T fromlen = sizeof(from); int ret; GSocketError err; @@ -1340,7 +1346,7 @@ int GSocket::Recv_Dgram(char *buffer, int size) do { - ret = recvfrom(m_fd, buffer, size, 0, &from, (SOCKLEN_T *) &fromlen); + ret = recvfrom(m_fd, buffer, size, 0, &from, (WX_SOCKLEN_T *) &fromlen); } while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ if (ret == -1)