Beispiel #1
0
/* Send 'len' bytes of 'data' over the non-server socket 'sock'
   This function returns the actual amount of data sent.  If the return value
   is less than the amount of data sent, then either the remote connection was
   closed, or an unknown socket error occurred.
*/
int SDLNet_TCP_Send(TCPsocket sock, const void *datap, int len)
{
	const unsigned char *data = (const unsigned char *)datap;	/* For pointer arithmetic */
	int sent, left;
	/* Server sockets are for accepting connections only */
	if ( sock->iServerFlag ) {
		SDLNet_SetError("Server sockets cannot send");
		return(-1);
	}

	/* Keep sending data until it's sent or an error occurs */
	left = len;
	sent = 0;
	SDLNet_SetLastError(0);
	while ( (left > 0) && ((len > 0) || (SDLNet_GetLastError() == EINTR)) )
	{
		len = send(sock->channel, (const char *) data, left, 0);
		if ( len > 0 ) {
			sent += len;
			left -= len;
			data += len;
		}
	}

	return(sent);
}
Beispiel #2
0
/* Receive up to 'maxlen' bytes of data over the non-server socket 'sock',
   and store them in the buffer pointed to by 'data'.
   This function returns the actual amount of data received.  If the return
   value is less than or equal to zero, then either the remote connection was
   closed, or an unknown socket error occurred.
*/
int SDLNet_TCP_Recv(TCPsocket sock, void *data, int maxlen)
{
    int len;

    /* Server sockets are for accepting connections only */
    if ( sock->sflag ) {
        SDLNet_SetError("Server sockets cannot receive");
        return(-1);
    }

    SDLNet_SetLastError(0);
    do {
        len = recv(sock->channel, (char *) data, maxlen, 0);
    } while ( SDLNet_GetLastError() == EINTR );

    sock->ready = 0;
    return(len);
}
Beispiel #3
0
/* This function checks to see if data is available for reading on the
   given set of sockets.  If 'timeout' is 0, it performs a quick poll,
   otherwise the function returns when either data is available for
   reading, or the timeout in milliseconds has elapsed, which ever occurs
   first.  This function returns the number of sockets ready for reading,
   or -1 if there was an error with the select() system call.
*/
int SDLNet_CheckSockets(SDLNet_SocketSet set, Uint32 timeout)
{
    int i;
    SOCKET maxfd;
    int retval;
    struct timeval tv;
    fd_set mask;

    /* Find the largest file descriptor */
    maxfd = 0;
    for ( i=set->numsockets-1; i>=0; --i ) {
        if ( set->sockets[i]->channel > maxfd ) {
            maxfd = set->sockets[i]->channel;
        }
    }

    /* Check the file descriptors for available data */
    do {
        SDLNet_SetLastError(0);

        /* Set up the mask of file descriptors */
        FD_ZERO(&mask);
        for ( i=set->numsockets-1; i>=0; --i ) {
            FD_SET(set->sockets[i]->channel, &mask);
        }

        /* Set up the timeout */
        tv.tv_sec = timeout/1000;
        tv.tv_usec = (timeout%1000)*1000;

        /* Look! */
        retval = select(maxfd+1, &mask, NULL, NULL, &tv);
    } while ( SDLNet_GetLastError() == EINTR );

    /* Mark all file descriptors ready that have data available */
    if ( retval > 0 ) {
        for ( i=set->numsockets-1; i>=0; --i ) {
            if ( FD_ISSET(set->sockets[i]->channel, &mask) ) {
                set->sockets[i]->ready = 1;
            }
        }
    }
    return(retval);
}
Beispiel #4
0
/* Returns true if a socket is has data available for reading right now */
static int SocketReady(SOCKET sock)
{
    int retval = 0;
    struct timeval tv;
    fd_set mask;

    /* Check the file descriptors for available data */
    do {
        SDLNet_SetLastError(0);

        /* Set up the mask of file descriptors */
        FD_ZERO(&mask);
        FD_SET(sock, &mask);

        /* Set up the timeout */
        tv.tv_sec = 0;
        tv.tv_usec = 0;

        /* Look! */
        retval = select(sock+1, &mask, NULL, NULL, &tv);
    } while ( SDLNet_GetLastError() == EINTR );

    return(retval == 1);
}