project_files/frontlib/nonblocksockets.h
author Medo <smaxein@googlemail.com>
Thu, 31 May 2012 18:54:40 +0200
changeset 7160 c42949cfdd92
parent 7158 frontlib/nonblocksockets.h@a0573014ff4f
permissions -rw-r--r--
Moved frontlib into project_files

/*
 * nonblocksockets.h
 *
 *  Created on: 31.05.2012
 *      Author: simmax
 */

#ifndef NONBLOCKSOCKETS_H_
#define NONBLOCKSOCKETS_H_

#include <SDL_net.h>
#include <stdbool.h>

typedef struct {
	TCPsocket sock;
	SDLNet_SocketSet sockset;
} _NonBlockSocket;

typedef _NonBlockSocket *NonBlockSocket;

/**
 * Close the indicated socket, free its memory and set it to NULL.
 * If the socket is already NULL, nothing happens.
 */
void flib_nbsocket_close(NonBlockSocket *socket);

/**
 * Try to accept a connection from a listening socket.
 * if localOnly is true, this will only accept connections which came from 127.0.0.1
 * Returns NULL if nothing can be accepted.
 */
NonBlockSocket flib_nbsocket_accept(TCPsocket listensocket, bool localOnly);

/**
 * Attempt to receive up to maxlen bytes from the socket, but does not
 * block if nothing is available.
 * Returns the ammount of data received, 0 if there was nothing to receive,
 * or a negative number if the connection was closed or an error occurred.
 */
int flib_nbsocket_recv(NonBlockSocket sock, void *data, int maxlen);

/**
 * We can't do a nonblocking send over SDL_net, so this function just forwards
 * to SDLNet_TCP_Send for convenience, which blocks until all data is sent or an
 * error occurs. The ammount of data actually sent is returned, negative value on error.
 */
static inline int flib_nbsocket_blocksend(NonBlockSocket sock, void *data, int len) {
	return SDLNet_TCP_Send(sock->sock, data, len);
}

#endif /* NONBLOCKSOCKETS_H_ */