int GSocket::Recv_Dgram(char *buffer, int size) { struct sockaddr from; WX_SOCKLEN_T fromlen = sizeof(from); int ret; GSocketError err; ret = recvfrom(m_fd, buffer, size, 0, &from, &fromlen); if (ret == SOCKET_ERROR) return SOCKET_ERROR; /* Translate a system address into a GSocket address */ if (!m_peer) { m_peer = GAddress_new(); if (!m_peer) { m_error = GSOCK_MEMERR; return -1; } } err = _GAddress_translate_from(m_peer, &from, fromlen); if (err != GSOCK_NOERROR) { GAddress_destroy(m_peer); m_peer = NULL; m_error = err; return -1; } return ret; }
GAddress *GSocket_GetLocal(GSocket *socket) { GAddress *address = NULL ; GSocketError err; InetAddress loc ; assert(socket != NULL); /* try to get it from the m_local var first */ if (socket->m_local) return GAddress_copy(socket->m_local); /* else, if the socket is initialized, try getsockname */ if (socket->m_endpoint == kOTInvalidEndpointRef) { socket->m_error = GSOCK_INVSOCK; return NULL; } /* we do not support multihoming with this code at the moment OTGetProtAddress will have to be used then - but we don't have a handy method to use right now */ { InetInterfaceInfo info; OTInetGetInterfaceInfo(&info, kDefaultInetInterface); loc.fHost = info.fAddress ; loc.fPort = 0 ; loc.fAddressType = AF_INET ; } /* got a valid address from getsockname, create a GAddress object */ address = GAddress_new(); if (address == NULL) { socket->m_error = GSOCK_MEMERR; return NULL; } err = _GAddress_translate_from(address, &loc); if (err != GSOCK_NOERROR) { GAddress_destroy(address); socket->m_error = err; return NULL; } return address; }
GAddress *GSocket::GetLocal() { GAddress *address; struct sockaddr addr; SOCKLEN_T size = sizeof(addr); GSocketError err; assert(this); /* try to get it from the m_local var first */ if (m_local) return GAddress_copy(m_local); /* else, if the socket is initialized, try getsockname */ if (m_fd == INVALID_SOCKET) { m_error = GSOCK_INVSOCK; return NULL; } if (getsockname(m_fd, &addr, (SOCKLEN_T *) &size) < 0) { m_error = GSOCK_IOERR; return NULL; } /* got a valid address from getsockname, create a GAddress object */ address = GAddress_new(); if (address == NULL) { m_error = GSOCK_MEMERR; return NULL; } err = _GAddress_translate_from(address, &addr, size); if (err != GSOCK_NOERROR) { GAddress_destroy(address); m_error = err; return NULL; } return address; }
int GSocket::Recv_Dgram(char *buffer, int size) { struct sockaddr from; SOCKLEN_T fromlen = sizeof(from); int ret; GSocketError err; fromlen = sizeof(from); do { ret = recvfrom(m_fd, buffer, size, 0, &from, (SOCKLEN_T *) &fromlen); } while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ if (ret == -1) return -1; /* Translate a system address into a GSocket address */ if (!m_peer) { m_peer = GAddress_new(); if (!m_peer) { m_error = GSOCK_MEMERR; return -1; } } err = _GAddress_translate_from(m_peer, &from, fromlen); if (err != GSOCK_NOERROR) { GAddress_destroy(m_peer); m_peer = NULL; m_error = err; return -1; } return ret; }
int _GSocket_Recv_Dgram(GSocket *socket, char *buffer, int size) { // TODO int ret = -1; #if 0 struct sockaddr from; WX_SOCKLEN_T fromlen = sizeof(from); GSocketError err; fromlen = sizeof(from); ret = recvfrom(socket->m_endpoint, buffer, size, 0, &from, (WX_SOCKLEN_T *) &fromlen); if (ret == -1) return -1; /* Translate a system address into a GSocket address */ if (!socket->m_peer) { socket->m_peer = GAddress_new(); if (!socket->m_peer) { socket->m_error = GSOCK_MEMERR; return -1; } } err = _GAddress_translate_from(socket->m_peer, &from, fromlen); if (err != GSOCK_NOERROR) { GAddress_destroy(socket->m_peer); socket->m_peer = NULL; socket->m_error = err; return -1; } #endif return ret; }
/* GSocket_WaitConnection: * Waits for an incoming client connection. Returns a pointer to * a GSocket object, or NULL if there was an error, in which case * the last error field will be updated for the calling GSocket. * * Error codes (set in the calling GSocket) * GSOCK_INVSOCK - the socket is not valid or not a server. * 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. */ GSocket *GSocket::WaitConnection() { struct sockaddr from; SOCKLEN_T fromlen = sizeof(from); GSocket *connection; GSocketError err; int arg = 1; assert(this); /* If the socket has already been created, we exit immediately */ if (m_fd == INVALID_SOCKET || !m_server) { m_error = GSOCK_INVSOCK; return NULL; } /* Create a GSocket object for the new connection */ connection = GSocket_new(); if (!connection) { m_error = GSOCK_MEMERR; return NULL; } /* Wait for a connection (with timeout) */ if (Input_Timeout() == GSOCK_TIMEDOUT) { delete connection; /* m_error set by _GSocket_Input_Timeout */ return NULL; } connection->m_fd = accept(m_fd, &from, (SOCKLEN_T *) &fromlen); /* Reenable CONNECTION events */ Enable(GSOCK_CONNECTION); if (connection->m_fd == INVALID_SOCKET) { if (errno == EWOULDBLOCK) m_error = GSOCK_WOULDBLOCK; else m_error = GSOCK_IOERR; delete connection; return NULL; } /* Initialize all fields */ connection->m_server = false; connection->m_stream = true; /* Setup the peer address field */ connection->m_peer = GAddress_new(); if (!connection->m_peer) { delete connection; m_error = GSOCK_MEMERR; return NULL; } err = _GAddress_translate_from(connection->m_peer, &from, fromlen); if (err != GSOCK_NOERROR) { delete connection; m_error = err; return NULL; } #if defined(__EMX__) || defined(__VISAGECPP__) ioctl(connection->m_fd, FIONBIO, (char*)&arg, sizeof(arg)); #else ioctl(connection->m_fd, FIONBIO, &arg); #endif gs_gui_functions->Enable_Events(connection); return connection; }
/* GSocket_WaitConnection: * Waits for an incoming client connection. Returns a pointer to * a GSocket object, or NULL if there was an error, in which case * the last error field will be updated for the calling GSocket. * * Error codes (set in the calling GSocket) * GSOCK_INVSOCK - the socket is not valid or not a server. * 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. */ GSocket *GSocket::WaitConnection() { GSocket *connection; struct sockaddr from; WX_SOCKLEN_T fromlen = sizeof(from); GSocketError err; u_long arg = 1; assert(this); /* Reenable CONNECTION events */ m_detected &= ~GSOCK_CONNECTION_FLAG; /* If the socket has already been created, we exit immediately */ if (m_fd == INVALID_SOCKET || !m_server) { m_error = GSOCK_INVSOCK; return NULL; } /* Create a GSocket object for the new connection */ connection = GSocket_new(); if (!connection) { m_error = GSOCK_MEMERR; return NULL; } /* Wait for a connection (with timeout) */ if (Input_Timeout() == GSOCK_TIMEDOUT) { delete connection; /* m_error set by _GSocket_Input_Timeout */ return NULL; } connection->m_fd = accept(m_fd, &from, &fromlen); if (connection->m_fd == INVALID_SOCKET) { if (WSAGetLastError() == WSAEWOULDBLOCK) m_error = GSOCK_WOULDBLOCK; else m_error = GSOCK_IOERR; delete connection; return NULL; } /* Initialize all fields */ connection->m_server = false; connection->m_stream = true; /* Setup the peer address field */ connection->m_peer = GAddress_new(); if (!connection->m_peer) { delete connection; m_error = GSOCK_MEMERR; return NULL; } err = _GAddress_translate_from(connection->m_peer, &from, fromlen); if (err != GSOCK_NOERROR) { GAddress_destroy(connection->m_peer); delete connection; m_error = err; return NULL; } ioctlsocket(connection->m_fd, FIONBIO, (u_long FAR *) &arg); gs_gui_functions->Enable_Events(connection); return connection; }
/* GSocket_WaitConnection: * Waits for an incoming client connection. Returns a pointer to * a GSocket object, or NULL if there was an error, in which case * the last error field will be updated for the calling GSocket. * * Error codes (set in the calling GSocket) * GSOCK_INVSOCK - the socket is not valid or not a server. * 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. */ GSocket *GSocket_WaitConnection(GSocket *socket) { GSocket *connection = NULL ; assert(socket != NULL); /* Reenable CONNECTION events */ socket->m_detected &= ~GSOCK_CONNECTION_FLAG; /* If the socket has already been created, we exit immediately */ if (socket->m_endpoint == kOTInvalidEndpointRef || !socket->m_server) { socket->m_error = GSOCK_INVSOCK; return NULL; } /* Create a GSocket object for the new connection */ connection = GSocket_new(); if (!connection) { socket->m_error = GSOCK_MEMERR; return NULL; } /* Wait for a connection (with timeout) */ if (_GSocket_Input_Timeout(socket) == GSOCK_TIMEDOUT) { GSocket_destroy(connection); /* socket->m_error set by _GSocket_Input_Timeout */ return NULL; } // TODO #if 0 connection->m_endpoint = accept(socket->m_endpoint, &from, (WX_SOCKLEN_T *) &fromlen); #endif if (connection->m_endpoint == kOTInvalidEndpointRef ) { if (errno == EWOULDBLOCK) socket->m_error = GSOCK_WOULDBLOCK; else socket->m_error = GSOCK_IOERR; GSocket_destroy(connection); return NULL; } /* 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(); if (!connection->m_peer) { GSocket_destroy(connection); socket->m_error = GSOCK_MEMERR; return NULL; } // TODO #if 0 err = _GAddress_translate_from(connection->m_peer, &from, fromlen); if (err != GSOCK_NOERROR) { GAddress_destroy(connection->m_peer); GSocket_destroy(connection); socket->m_error = err; return NULL; } ioctl(connection->m_endpoint, FIONBIO, &arg); #endif _GSocket_Enable_Events(connection); return connection; }