* Guilhem Lavaux,
* Guillermo Rodriguez Garcia <guille@iies.es>
* Purpose: GSocket main Unix and OS/2 file
- * Licence: The wxWidgets licence
+ * Licence: The wxWindows licence
* CVSID: $Id$
* -------------------------------------------------------------------------
*/
{
GSocketBSDGUIShim::ms_gui_functions = guifunc;
}
-
+
inline bool GSocketBSDGUIShim::UseGUI()
{
return ms_gui_functions;
* 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()
{
/* Initialize all fields */
m_stream = TRUE;
m_server = TRUE;
- m_oriented = TRUE;
/* Create the socket */
m_fd = socket(m_local->m_realfamily, SOCK_STREAM, 0);
/* 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,
* 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()
{
/* Initialize all fields */
connection->m_server = FALSE;
connection->m_stream = TRUE;
- connection->m_oriented = TRUE;
/* Setup the peer address field */
connection->m_peer = GAddress_new();
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:
* 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)
{
/* Streamed or dgram socket? */
m_stream = (stream == GSOCK_STREAMED);
- m_oriented = TRUE;
m_server = FALSE;
m_establishing = FALSE;
/* Initialize all fields */
m_stream = FALSE;
m_server = FALSE;
- m_oriented = FALSE;
/* Create the socket */
m_fd = socket(m_local->m_realfamily, SOCK_DGRAM, 0);
assert(this);
- /* When using CFSocket we MUST NOT reenable events until we finish reading */
-#ifndef __DARWIN__
- /* Reenable INPUT events */
- Enable(GSOCK_INPUT);
-#endif
-
if (m_fd == INVALID_SOCKET || m_server)
{
m_error = GSOCK_INVSOCK;
return -1;
}
+ /* 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)
- return -1;
+ /* We no longer 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);
+ }
- /* Read the data */
- if (m_stream)
- ret = Recv_Stream(buffer, size);
- else
- ret = Recv_Dgram(buffer, size);
-
if (ret == -1)
{
if (errno == EWOULDBLOCK)
else
m_error = GSOCK_IOERR;
}
-
-#ifdef __DARWIN__
- /* Reenable INPUT events */
+
+ /* Enable events again now that we are done processing */
Enable(GSOCK_INPUT);
-#endif
return ret;
}
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)
ret = Send_Stream(buffer, size);
else
ret = Send_Dgram(buffer, size);
-
+
GSocket_Debug(( "GSocket_Write #4, size %d\n", size ));
if (ret == -1)
Enable(GSOCK_OUTPUT);
return -1;
}
-
+
GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size, ret ));
return ret;
fd_set exceptfds;
struct timeval tv;
+ assert(this);
+
/* Do not use a static struct, Linux can garble it */
tv.tv_sec = m_timeout / 1000;
tv.tv_usec = (m_timeout % 1000) / 1000;
- assert(this);
-
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&exceptfds);
FD_SET(m_fd, &readfds);
- if (flags & GSOCK_OUTPUT_FLAG)
+ if (flags & GSOCK_OUTPUT_FLAG || flags & GSOCK_CONNECTION_FLAG)
FD_SET(m_fd, &writefds);
FD_SET(m_fd, &exceptfds);
{
m_detected = GSOCK_LOST_FLAG;
m_establishing = FALSE;
-
+
/* LOST event: Abort any further processing */
return (GSOCK_LOST_FLAG & flags);
}
* 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
}
+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]) \
{
#else
/* Use gethostbyname by default */
+#ifndef __WXMAC__
int val = 1; //VA doesn't like constants in conditional expressions at all
if (val)
+#endif
{
#endif
struct in_addr *array_addr;
CHECK_ADDRESS(address, INET);
addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
- addr->s_addr = hostaddr;
+ addr->s_addr = htonl(hostaddr);
return GSOCK_NOERROR;
}
address->m_error = GSOCK_INVPORT;
return GSOCK_INVPORT;
}
-
+
se = getservbyname(port, protocol);
if (!se)
{
assert(address != NULL);
CHECK_ADDRESS(address, INET);
-
+
addr = (struct sockaddr_in *)address->m_addr;
addr->sin_port = htons(port);
char *addr_buf;
struct sockaddr_in *addr;
- assert(address != NULL);
+ assert(address != NULL);
CHECK_ADDRESS(address, INET);
addr = (struct sockaddr_in *)address->m_addr;
{
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 addr->sin_addr.s_addr;
+ return ntohl(addr->sin_addr.s_addr);
}
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);
{
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);