From b082b52407e8132d04821bfaa31eb6a9a870b054 Mon Sep 17 00:00:00 2001 From: David Elliott Date: Tue, 27 Jul 2004 02:15:32 +0000 Subject: [PATCH] Incorporated src/unix/gsocket.c 1.86 to 1.92 changes (except 1.90 to 1.91 which was applied by Vadim as 1.9 to 1.10 of src/unix/gsocket.cpp) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@28515 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/unix/gsockunx.h | 12 ++++++ src/unix/gsocket.cpp | 79 ++++++++++++++++++++++++++------------ 2 files changed, 67 insertions(+), 24 deletions(-) diff --git a/include/wx/unix/gsockunx.h b/include/wx/unix/gsockunx.h index fe88b7ded3..fd851a70c0 100644 --- a/include/wx/unix/gsockunx.h +++ b/include/wx/unix/gsockunx.h @@ -44,6 +44,7 @@ public: GAddress *GetPeer(); GSocketError SetServer(); GSocket *WaitConnection(); + int SetReusable(); GSocketError Connect(GSocketStream stream); GSocketError SetNonOriented(); int Read(char *buffer, int size); @@ -55,6 +56,9 @@ public: void SetCallback(GSocketEventFlags flags, GSocketCallback callback, char *cdata); void UnsetCallback(GSocketEventFlags flags); + GSocketError GetSockOpt(int level, int optname, void *optval, int *optlen); + GSocketError SetSockOpt(int level, int optname, + const void *optval, int optlen); /* API compatibility functions */ static void _GSocket_Detected_Read(GSocket *socket); static void _GSocket_Detected_Write(GSocket *socket); @@ -172,6 +176,8 @@ inline GSocketError GSocket_SetServer(GSocket *socket) { return socket->SetServer(); } inline GSocket *GSocket_WaitConnection(GSocket *socket) { return socket->WaitConnection(); } +inline int GSocket_SetReusable(GSocket *socket) +{ return socket->SetReusable(); } inline GSocketError GSocket_Connect(GSocket *socket, GSocketStream stream) { return socket->Connect(stream); } inline GSocketError GSocket_SetNonOriented(GSocket *socket) @@ -191,6 +197,12 @@ inline void GSocket_SetCallback(GSocket *socket, GSocketEventFlags flags, { socket->SetCallback(flags,fallback,cdata); } inline void GSocket_UnsetCallback(GSocket *socket, GSocketEventFlags flags) { socket->UnsetCallback(flags); } +inline GSocketError GSocket_GetSockOpt(GSocket *socket, int level, int optname, + void *optval, int *optlen) +{ return socket->GetSockOpt(level,optname,optval,optlen); } +inline GSocketError GSocket_SetSockOpt(GSocket *socket, int level, int optname, + const void *optval, int optlen) +{ return socket->SetSockOpt(level,optname,optval,optlen); } #endif /* def wxUSE_GSOCKET_CPLUSPLUS */ diff --git a/src/unix/gsocket.cpp b/src/unix/gsocket.cpp index bc9217aadd..0a73055c7b 100644 --- a/src/unix/gsocket.cpp +++ b/src/unix/gsocket.cpp @@ -167,7 +167,7 @@ void GSocket_SetGUIFunctions(struct GSocketGUIFunctionsTable *guifunc) { GSocketBSDGUIShim::ms_gui_functions = guifunc; } - + inline bool GSocketBSDGUIShim::UseGUI() { return ms_gui_functions; @@ -427,11 +427,11 @@ GAddress *GSocketBSD::GetPeer() * Sets up this socket as a server. The local address must have been * set with GSocket_SetLocal() before GSocket_SetServer() is called. * Returns GSOCK_NOERROR on success, one of the following otherwise: - * + * * Error codes: * GSOCK_INVSOCK - the socket is in use. * GSOCK_INVADDR - the local address has not been set. - * GSOCK_IOERR - low-level error. + * GSOCK_IOERR - low-level error. */ GSocketError GSocketBSD::SetServer() { @@ -475,7 +475,8 @@ GSocketError GSocketBSD::SetServer() /* allow a socket to re-bind if the socket is in the TIME_WAIT state after being previously closed. */ - setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(u_long)); + if(m_reusable) + setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(u_long)); /* Bind to the local address, * retrieve the actual address bound, @@ -505,7 +506,7 @@ GSocketError GSocketBSD::SetServer() * GSOCK_TIMEDOUT - timeout, no incoming connections. * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking. * GSOCK_MEMERR - couldn't allocate memory. - * GSOCK_IOERR - low-level error. + * GSOCK_IOERR - low-level error. */ GSocket *GSocketBSD::WaitConnection() { @@ -588,6 +589,16 @@ GSocket *GSocketBSD::WaitConnection() return connection; } +int GSocketBSD::SetReusable() +{ + /* socket must not be null, and must not be in use/already bound */ + if (NULL != this && m_fd == INVALID_SOCKET) { + m_reusable = TRUE; + return TRUE; + } + return FALSE; +} + /* Client specific parts */ /* GSocket_Connect: @@ -611,7 +622,7 @@ GSocket *GSocketBSD::WaitConnection() * GSOCK_TIMEDOUT - timeout, the connection failed. * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only) * GSOCK_MEMERR - couldn't allocate memory. - * GSOCK_IOERR - low-level error. + * GSOCK_IOERR - low-level error. */ GSocketError GSocketBSD::Connect(GSocketStream stream) { @@ -795,7 +806,7 @@ int GSocketBSD::Read(char *buffer, int size) /* Disable events during query of socket status */ 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! */ @@ -807,7 +818,7 @@ int GSocketBSD::Read(char *buffer, int size) else ret = Recv_Dgram(buffer, size); } - + if (ret == -1) { if (errno == EWOULDBLOCK) @@ -815,7 +826,7 @@ int GSocketBSD::Read(char *buffer, int size) else m_error = GSOCK_IOERR; } - + /* Enable events again now that we are done processing */ Enable(GSOCK_INPUT); @@ -823,11 +834,11 @@ int GSocketBSD::Read(char *buffer, int size) } int GSocketBSD::Write(const char *buffer, int size) -{ +{ int ret; assert(this); - + GSocket_Debug(( "GSocket_Write #1, size %d\n", size )); if (m_fd == INVALID_SOCKET || m_server) @@ -849,7 +860,7 @@ int GSocketBSD::Write(const char *buffer, int size) ret = Send_Stream(buffer, size); else ret = Send_Dgram(buffer, size); - + GSocket_Debug(( "GSocket_Write #4, size %d\n", size )); if (ret == -1) @@ -873,7 +884,7 @@ int GSocketBSD::Write(const char *buffer, int size) Enable(GSOCK_OUTPUT); return -1; } - + GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size, ret )); return ret; @@ -951,7 +962,7 @@ GSocketEventFlags GSocketBSD::Select(GSocketEventFlags flags) { m_detected = GSOCK_LOST_FLAG; m_establishing = FALSE; - + /* LOST event: Abort any further processing */ return (GSOCK_LOST_FLAG & flags); } @@ -1056,7 +1067,7 @@ GSocketError GSocketBSD::GetError() * operation, there is still data available, the callback function will * be called again. * GSOCK_OUTPUT: - * The socket is available for writing. That is, the next write call + * The socket is available for writing. That is, the next write call * won't block. This event is generated only once, when the connection is * first established, and then only if a call failed with GSOCK_WOULDBLOCK, * when the output buffer empties again. This means that the app should @@ -1117,6 +1128,26 @@ void GSocketBSD::UnsetCallback(GSocketEventFlags flags) } +GSocketError GSocketBSD::GetSockOpt(int level, int optname, + void *optval, int *optlen) +{ + if (getsockopt(m_fd, level, optname, optval, optlen) == 0) + { + return GSOCK_NOERROR; + } + return GSOCK_OPTERR; +} + +GSocketError GSocketBSD::SetSockOpt(int level, int optname, + const void *optval, int optlen) +{ + if (setsockopt(m_fd, level, optname, optval, optlen) == 0) + { + return GSOCK_NOERROR; + } + return GSOCK_OPTERR; +} + #define CALL_CALLBACK(event) { \ Disable(event); \ if (m_cbacks[event]) \ @@ -1683,7 +1714,7 @@ GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, address->m_error = GSOCK_INVPORT; return GSOCK_INVPORT; } - + se = getservbyname(port, protocol); if (!se) { @@ -1715,7 +1746,7 @@ GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port) assert(address != NULL); CHECK_ADDRESS(address, INET); - + addr = (struct sockaddr_in *)address->m_addr; addr->sin_port = htons(port); @@ -1728,7 +1759,7 @@ GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t char *addr_buf; struct sockaddr_in *addr; - assert(address != NULL); + assert(address != NULL); CHECK_ADDRESS(address, INET); addr = (struct sockaddr_in *)address->m_addr; @@ -1750,8 +1781,8 @@ unsigned long GAddress_INET_GetHostAddress(GAddress *address) { struct sockaddr_in *addr; - assert(address != NULL); - CHECK_ADDRESS_RETVAL(address, INET, 0); + assert(address != NULL); + CHECK_ADDRESS_RETVAL(address, INET, 0); addr = (struct sockaddr_in *)address->m_addr; @@ -1762,8 +1793,8 @@ unsigned short GAddress_INET_GetPort(GAddress *address) { struct sockaddr_in *addr; - assert(address != NULL); - CHECK_ADDRESS_RETVAL(address, INET, 0); + assert(address != NULL); + CHECK_ADDRESS_RETVAL(address, INET, 0); addr = (struct sockaddr_in *)address->m_addr; return ntohs(addr->sin_port); @@ -1800,9 +1831,9 @@ GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path) { struct sockaddr_un *addr; - assert(address != NULL); + assert(address != NULL); - CHECK_ADDRESS(address, UNIX); + CHECK_ADDRESS(address, UNIX); addr = ((struct sockaddr_un *)address->m_addr); strncpy(addr->sun_path, path, UNIX_SOCK_PATHLEN); -- 2.45.2