int GSocket::Send_Dgram(const char *buffer, int size) { struct sockaddr *addr; int len, ret; GSocketError err; if (!m_peer) { m_error = GSOCK_INVADDR; return -1; } err = _GAddress_translate_to(m_peer, &addr, &len); if (err != GSOCK_NOERROR) { m_error = err; return -1; } #ifndef __VISAGECPP__ MASK_SIGNAL(); #endif do { ret = sendto(m_fd, (char *)buffer, size, 0, addr, len); } while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ #ifndef __VISAGECPP__ UNMASK_SIGNAL(); #endif /* Frees memory allocated from _GAddress_translate_to */ free(addr); return ret; }
int GSocket::Send_Dgram(const char *buffer, int size) { struct sockaddr *addr; int len, ret; GSocketError err; if (!m_peer) { m_error = GSOCK_INVADDR; return -1; } err = _GAddress_translate_to(m_peer, &addr, &len); if (err != GSOCK_NOERROR) { m_error = err; return -1; } ret = sendto(m_fd, buffer, size, 0, addr, len); /* Frees memory allocated by _GAddress_translate_to */ free(addr); return ret; }
int _GSocket_Send_Dgram(GSocket *socket, const char *buffer, int size) { int ret = -1 ; // TODO #if 0 struct sockaddr *addr; int len ; GSocketError err; if (!socket->m_peer) { socket->m_error = GSOCK_INVADDR; return -1; } err = _GAddress_translate_to(socket->m_peer, &addr, &len); if (err != GSOCK_NOERROR) { socket->m_error = err; return -1; } ret = sendto(socket->m_endpoint, buffer, size, 0, addr, len); /* Frees memory allocated from _GAddress_translate_to */ free(addr); #endif return ret; }
/* GSocket_Connect: * For stream (connection oriented) sockets, GSocket_Connect() tries * to establish a client connection to a server using the peer address * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the * connection has been successfully established, or one of the error * codes listed below. Note that for nonblocking sockets, a return * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection * request can be completed later; you should use GSocket_Select() * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the * corresponding asynchronous events. * * For datagram (non connection oriented) sockets, GSocket_Connect() * just sets the peer address established with GSocket_SetPeer() as * default destination. * * Error codes: * GSOCK_INVSOCK - the socket is in use or not valid. * GSOCK_INVADDR - the peer address has not been established. * 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. */ GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream) { InetAddress addr ; TEndpointInfo info; OSStatus err = kOTNoError; TCall peer ; assert(sck != NULL); /* Enable CONNECTION events (needed for nonblocking connections) */ sck->m_detected &= ~GSOCK_CONNECTION_FLAG; if (sck->m_endpoint != kOTInvalidEndpointRef ) { sck->m_error = GSOCK_INVSOCK; return GSOCK_INVSOCK; } if (!sck->m_peer) { sck->m_error = GSOCK_INVADDR; return GSOCK_INVADDR; } /* Streamed or dgram socket? */ sck->m_stream = (stream == GSOCK_STREAMED); sck->m_oriented = TRUE; sck->m_server = FALSE; /* Create the socket */ #if TARGET_CARBON sck->m_endpoint = OTOpenEndpointInContext( OTCreateConfiguration( kTCPName) , 0 , &info , &err , NULL ) ; #else sck->m_endpoint = OTOpenEndpoint( OTCreateConfiguration( kTCPName) , 0 , &info , &err ) ; #endif if ( sck->m_endpoint == kOTInvalidEndpointRef || err != kOTNoError ) { sck->m_endpoint = kOTInvalidEndpointRef ; sck->m_error = GSOCK_IOERR; return GSOCK_IOERR; } err = OTBind( sck->m_endpoint , nil , nil ) ; if ( err != kOTNoError ) { return GSOCK_IOERR; } SetDefaultEndpointModes( sck->m_endpoint , sck ) ; // TODO #if 0 ioctl(sck->m_endpoint, FIONBIO, &arg); #endif _GSocket_Enable_Events(sck); _GAddress_translate_to( sck->m_peer , &addr ) ; memset( &peer , 0 , sizeof( TCall ) ) ; peer.addr.len = sizeof( InetAddress ) ; peer.addr.buf = (unsigned char*) &addr ; err = OTConnect( sck->m_endpoint , &peer , nil ) ; if ( err != noErr ) { /* If connect failed with EINPROGRESS and the GSocket object * is in blocking mode, we select() for the specified timeout * checking for writability to see if the connection request * completes. */ if ((err == kOTNoDataErr ) && (!sck->m_non_blocking)) { if (_GSocket_Output_Timeout(sck) == GSOCK_TIMEDOUT) { OTSndOrderlyDisconnect( sck->m_endpoint ) ; sck->m_endpoint = kOTInvalidEndpointRef ; /* sck->m_error is set in _GSocket_Output_Timeout */ return GSOCK_TIMEDOUT; } else { /* int error; WX_SOCKLEN_T len = sizeof(error); getsockopt(sck->m_endpoint, SOL_SOCKET, SO_ERROR, (void*) &error, &len); if (!error) */ return GSOCK_NOERROR; } } /* If connect failed with EINPROGRESS and the GSocket object * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK * (and return GSOCK_WOULDBLOCK) but we don't close the socket; * this way if the connection completes, a GSOCK_CONNECTION * event will be generated, if enabled. */ if ((err == kOTNoDataErr) && (sck->m_non_blocking)) { sck->m_error = GSOCK_WOULDBLOCK; return GSOCK_WOULDBLOCK; } /* If connect failed with an error other than EINPROGRESS, * then the call to GSocket_Connect has failed. */ OTSndOrderlyDisconnect( sck->m_endpoint ) ; sck->m_endpoint = kOTInvalidEndpointRef ; sck->m_error = GSOCK_IOERR; return GSOCK_IOERR; } // OTInetEventHandler(sck, T_CONNECT , kOTNoError , NULL ) ; return GSOCK_NOERROR; }