diff --git a/configure.in b/configure.in index 458e074..8c56af6 100644 --- a/configure.in +++ b/configure.in @@ -1,5 +1,5 @@ -AC_INIT(libenet, 8-31-2007) -AM_INIT_AUTOMAKE(libenet.a, 8-31-2007) +AC_INIT(libenet, 10-02-2007) +AM_INIT_AUTOMAKE(libenet.a, 10-02-2007) AC_PROG_CC AC_PROG_RANLIB diff --git a/host.c b/host.c index 5633fb9..603ae32 100644 --- a/host.c +++ b/host.c @@ -45,6 +45,11 @@ enet_host_create (const ENetAddress * address, size_t peerCount, enet_uint32 inc return NULL; } + enet_socket_set_option (host -> socket, ENET_SOCKOPT_NONBLOCK, 1); + enet_socket_set_option (host -> socket, ENET_SOCKOPT_BROADCAST, 1); + enet_socket_set_option (host -> socket, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE); + enet_socket_set_option (host -> socket, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE); + if (address != NULL) host -> address = * address; diff --git a/include/enet/enet.h b/include/enet/enet.h index dc415c2..2ba8340 100644 --- a/include/enet/enet.h +++ b/include/enet/enet.h @@ -41,6 +41,14 @@ typedef enum ENET_SOCKET_WAIT_RECEIVE = (1 << 1) } ENetSocketWait; +typedef enum +{ + ENET_SOCKOPT_NONBLOCK = 1, + ENET_SOCKOPT_BROADCAST = 2, + ENET_SOCKOPT_RCVBUF = 3, + ENET_SOCKOPT_SNDBUF = 4 +} ENetSocketOption; + enum { ENET_HOST_ANY = 0, /**< specifies the default server host */ @@ -394,6 +402,7 @@ ENET_API int enet_socket_connect (ENetSocket, const ENetAddress *); ENET_API int enet_socket_send (ENetSocket, const ENetAddress *, const ENetBuffer *, size_t); ENET_API int enet_socket_receive (ENetSocket, ENetAddress *, ENetBuffer *, size_t); ENET_API int enet_socket_wait (ENetSocket, enet_uint32 *, enet_uint32); +ENET_API int enet_socket_set_option (ENetSocket, ENetSocketOption, int); ENET_API void enet_socket_destroy (ENetSocket); /** @} */ diff --git a/unix.c b/unix.c index 08ac249..e279bfe 100644 --- a/unix.c +++ b/unix.c @@ -156,30 +156,11 @@ ENetSocket enet_socket_create (ENetSocketType type, const ENetAddress * address) { ENetSocket newSocket = socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); - int receiveBufferSize = ENET_HOST_RECEIVE_BUFFER_SIZE, - sendBufferSize = ENET_HOST_SEND_BUFFER_SIZE, - allowBroadcasting = 1; -#ifndef HAS_FCNTL - int nonBlocking = 1; -#endif struct sockaddr_in sin; if (newSocket == ENET_SOCKET_NULL) return ENET_SOCKET_NULL; - if (type == ENET_SOCKET_TYPE_DATAGRAM) - { -#ifdef HAS_FCNTL - fcntl (newSocket, F_SETFL, O_NONBLOCK | fcntl (newSocket, F_GETFL)); -#else - ioctl (newSocket, FIONBIO, & nonBlocking); -#endif - - setsockopt (newSocket, SOL_SOCKET, SO_RCVBUF, (char *) & receiveBufferSize, sizeof (int)); - setsockopt (newSocket, SOL_SOCKET, SO_SNDBUF, (char *) & sendBufferSize, sizeof (int)); - setsockopt (newSocket, SOL_SOCKET, SO_BROADCAST, (char *) & allowBroadcasting, sizeof (int)); - } - if (address == NULL) return newSocket; @@ -204,6 +185,38 @@ enet_socket_create (ENetSocketType type, const ENetAddress * address) return newSocket; } +int +enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) +{ + int result = -1; + switch (option) + { + case ENET_SOCKOPT_NONBLOCK: +#ifdef HAS_FCNTL + result = fcntl (socket, F_SETFL, O_NONBLOCK | fcntl (socket, F_GETFL)); +#else + result = ioctl (socket, FIONBIO, & value); +#endif + break; + + case ENET_SOCKOPT_BROADCAST: + result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int)); + break; + + case ENET_SOCKOPT_RCVBUF: + result = setsockopt (socket, SOL_SOCKET, SO_RCVBUF, (char *) & value, sizeof (int)); + break; + + case ENET_SOCKOPT_SNDBUF: + result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int)); + break; + + default: + break; + } + return result == -1 ? -1 : 0; +} + int enet_socket_connect (ENetSocket socket, const ENetAddress * address) { diff --git a/win32.c b/win32.c index 9559154..27e7877 100644 --- a/win32.c +++ b/win32.c @@ -104,24 +104,11 @@ ENetSocket enet_socket_create (ENetSocketType type, const ENetAddress * address) { ENetSocket newSocket = socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); - u_long nonBlocking = 1; - int receiveBufferSize = ENET_HOST_RECEIVE_BUFFER_SIZE, - sendBufferSize = ENET_HOST_SEND_BUFFER_SIZE, - allowBroadcasting = 1; struct sockaddr_in sin; if (newSocket == ENET_SOCKET_NULL) return ENET_SOCKET_NULL; - if (type == ENET_SOCKET_TYPE_DATAGRAM) - { - ioctlsocket (newSocket, FIONBIO, & nonBlocking); - - setsockopt (newSocket, SOL_SOCKET, SO_RCVBUF, (char *) & receiveBufferSize, sizeof (int)); - setsockopt (newSocket, SOL_SOCKET, SO_SNDBUF, (char *) & sendBufferSize, sizeof (int)); - setsockopt (newSocket, SOL_SOCKET, SO_BROADCAST, (char *) & allowBroadcasting, sizeof (int)); - } - memset (& sin, 0, sizeof (struct sockaddr_in)); sin.sin_family = AF_INET; @@ -153,6 +140,37 @@ enet_socket_create (ENetSocketType type, const ENetAddress * address) return newSocket; } +int +enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) +{ + int result = SOCKET_ERROR; + switch (option) + { + case ENET_SOCKOPT_NONBLOCK: + { + u_long nonBlocking = (u_long) value; + result = ioctlsocket (socket, FIONBIO, & nonBlocking); + break; + } + + case ENET_SOCKOPT_BROADCAST: + result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int)); + break; + + case ENET_SOCKOPT_RCVBUF: + result = setsockopt (socket, SOL_SOCKET, SO_RCVBUF, (char *) & value, sizeof (int)); + break; + + case ENET_SOCKOPT_SNDBUF: + result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int)); + break; + + default: + break; + } + return result == SOCKET_ERROR ? -1 : 0; +} + int enet_socket_connect (ENetSocket socket, const ENetAddress * address) {