commit b4eab5af3aaec8118546802632e50215944ed4e7
parent ed1ed8cffe1dc358818693ff3be399dadebbbb80
Author: Michael Savage <mikejsavage@gmail.com>
Date: Fri, 7 Sep 2018 12:01:10 +0300
Trim out some of the unused networking stuff
Diffstat:
6 files changed, 7 insertions(+), 195 deletions(-)
diff --git a/src/platform_network.cc b/src/platform_network.cc
@@ -78,81 +78,7 @@ static void setsockoptone( OSSocket fd, int level, int opt ) {
}
}
-UDPSocket net_new_udp( NonblockingBool nonblocking, u16 port ) {
- UDPSocket sock;
-
- sock.ipv4 = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
- if( sock.ipv4 == INVALID_SOCKET ) {
- FATAL( "socket" );
- }
- sock.ipv6 = socket( AF_INET6, SOCK_DGRAM, IPPROTO_UDP );
- if( sock.ipv6 == INVALID_SOCKET ) {
- FATAL( "socket" );
- }
-
- if( nonblocking == NET_NONBLOCKING ) {
- make_socket_nonblocking( sock.ipv4 );
- make_socket_nonblocking( sock.ipv6 );
- }
-
- if( port != 0 ) {
- setsockoptone( sock.ipv4, SOL_SOCKET, SO_REUSEADDR );
- setsockoptone( sock.ipv6, SOL_SOCKET, SO_REUSEADDR );
- setsockoptone( sock.ipv6, IPPROTO_IPV6, IPV6_V6ONLY );
- }
-
- platform_init_sock( sock.ipv4 );
- platform_init_sock( sock.ipv6 );
-
- {
- sockaddr_in my_addr4;
- my_addr4.sin_family = AF_INET;
- my_addr4.sin_port = htons( port );
- my_addr4.sin_addr.s_addr = htonl( INADDR_ANY );
- int ok = bind( sock.ipv4, ( struct sockaddr * ) &my_addr4, sizeof( my_addr4 ) );
- if( ok == SOCKET_ERROR ) {
- FATAL( "bind" );
- }
- }
-
- {
- sockaddr_in6 my_addr6;
- my_addr6.sin6_family = AF_INET6;
- my_addr6.sin6_port = htons( port );
- my_addr6.sin6_addr = in6addr_any;
- int ok = bind( sock.ipv6, ( struct sockaddr * ) &my_addr6, sizeof( my_addr6 ) );
- if( ok == SOCKET_ERROR ) {
- FATAL( "bind" );
- }
- }
-
- return sock;
-}
-
-void net_send( UDPSocket sock, const void * data, size_t len, const NetAddress & addr ) {
- struct sockaddr_storage ss = netaddress_to_sockaddr( addr );
- socklen_t ss_size = sockaddr_size( ss );
- OSSocket fd = addr.type == NET_IPV4 ? sock.ipv4 : sock.ipv6;
- ssize_t ok = sendto( fd, ( const char * ) data, checked_cast< int >( len ), NET_SEND_FLAGS, ( struct sockaddr * ) &ss, ss_size );
- if( ok == SOCKET_ERROR ) {
- FATAL( "sendto" );
- }
-}
-
-void net_destroy( UDPSocket * sock ) {
- int ok4 = closesocket( sock->ipv4 );
- if( ok4 == -1 ) {
- FATAL( "closesocket" );
- }
- int ok6 = closesocket( sock->ipv6 );
- if( ok6 == -1 ) {
- FATAL( "closesocket" );
- }
- sock->ipv4 = INVALID_SOCKET;
- sock->ipv6 = INVALID_SOCKET;
-}
-
-bool net_new_tcp( TCPSocket * sock, const NetAddress & addr, NonblockingBool nonblocking ) {
+bool net_new_tcp( TCPSocket * sock, const NetAddress & addr ) {
struct sockaddr_storage ss = netaddress_to_sockaddr( addr );
socklen_t ss_size = sockaddr_size( ss );
@@ -171,10 +97,6 @@ bool net_new_tcp( TCPSocket * sock, const NetAddress & addr, NonblockingBool non
return false;
}
- if( nonblocking == NET_NONBLOCKING ) {
- make_socket_nonblocking( sock->fd );
- }
-
setsockoptone( sock->fd, SOL_SOCKET, SO_KEEPALIVE );
platform_init_sock( sock->fd );
@@ -188,28 +110,8 @@ bool net_send( TCPSocket sock, const void * data, size_t len ) {
return checked_cast< size_t >( sent ) == len;
}
-TCPRecvResult net_recv( TCPSocket sock, void * buf, size_t buf_size, size_t * bytes_read, u32 timeout_ms ) {
+TCPRecvResult net_recv( TCPSocket sock, void * buf, size_t buf_size, size_t * bytes_read ) {
while( true ) {
- if( timeout_ms > 0 ) {
- fd_set fds;
- FD_ZERO( &fds );
- FD_SET( sock.fd, &fds );
-
- struct timeval tv;
- tv.tv_sec = timeout_ms / 1000;
- tv.tv_usec = ( timeout_ms % 1000 ) * 1000;
-
- int ok = select( sock.fd + 1, &fds, NULL, NULL, &tv );
- // TODO: update timeout
- if( ok == 0 ) {
- return TCP_TIMEOUT;
- }
- if( ok == -1 ) {
- if( errno != EINTR ) continue;
- FATAL( "select" );
- }
- }
-
ssize_t r = recv( sock.fd, ( char * ) buf, buf_size, 0 );
// TODO: this is not right on windows
if( r == -1 ) {
diff --git a/src/platform_network.h b/src/platform_network.h
@@ -4,28 +4,21 @@
#include "common.h"
#if PLATFORM_WINDOWS
-typedef s64 OSSocket;
+typedef SOCKET OSSocket;
#elif PLATFORM_UNIX
typedef int OSSocket;
#else
#error new platform
#endif
-enum TransportProtocol { NET_UDP, NET_TCP };
enum IPvX { NET_IPV4, NET_IPV6 };
-enum NonblockingBool { NET_BLOCKING, NET_NONBLOCKING };
enum TCPRecvResult {
TCP_OK,
- TCP_TIMEOUT,
TCP_CLOSED,
TCP_ERROR,
};
-struct UDPSocket {
- OSSocket ipv4, ipv6;
-};
-
struct TCPSocket {
OSSocket fd;
};
@@ -48,14 +41,9 @@ bool operator!=( const NetAddress & lhs, const NetAddress & rhs );
void net_init();
void net_term();
-UDPSocket net_new_udp( NonblockingBool nonblocking, u16 port = 0 );
-void net_send( UDPSocket sock, const void * data, size_t len, const NetAddress & addr );
-bool net_tryrecv( UDPSocket sock, void * buf, size_t len, NetAddress * addr, size_t * bytes_received );
-void net_destroy( UDPSocket * sock );
-
-bool net_new_tcp( TCPSocket * sock, const NetAddress & addr, NonblockingBool nonblocking );
+bool net_new_tcp( TCPSocket * sock, const NetAddress & addr );
bool net_send( TCPSocket sock, const void * data, size_t len );
-TCPRecvResult net_recv( TCPSocket sock, void * buf, size_t buf_size, size_t * bytes_read, u32 timeout_ms = 0 );
+TCPRecvResult net_recv( TCPSocket sock, void * buf, size_t buf_size, size_t * bytes_read );
void net_destroy( TCPSocket * sock );
bool dns_first( const char * host, NetAddress * address );
diff --git a/src/unix_network.cc b/src/unix_network.cc
@@ -3,7 +3,6 @@
#include <sys/select.h>
#include <sys/socket.h>
#include <errno.h>
-#include <fcntl.h>
#include <netdb.h>
#include <unistd.h>
@@ -21,43 +20,8 @@ static int NET_SEND_FLAGS = MSG_NOSIGNAL;
void net_init() { }
void net_term() { }
-static void make_socket_nonblocking( int fd ) {
- int flags = fcntl( fd, F_GETFL, 0 );
- if( flags == -1 ) FATAL( "fcntl F_GETFL" );
- int ok = fcntl( fd, F_SETFL, flags | O_NONBLOCK );
- if( ok == -1 ) FATAL( "fcntl F_SETFL" );
-}
-
static void platform_init_sock( int fd ) {
#if PLATFORM_OSX
setsockoptone( fd, SOL_SOCKET, SO_NOSIGPIPE );
#endif
}
-
-bool net_tryrecv( UDPSocket sock, void * buf, size_t len, NetAddress * addr, size_t * bytes_received ) {
- struct sockaddr_storage sa;
-
- socklen_t sa_size = sizeof( struct sockaddr_in );
- ssize_t received4 = recvfrom( sock.ipv4, buf, len, 0, ( struct sockaddr * ) &sa, &sa_size );
- if( received4 != -1 ) {
- *addr = sockaddr_to_netaddress( sa );
- *bytes_received = size_t( received4 );
- return true;
- }
- if( received4 == -1 && errno != EAGAIN ) {
- FATAL( "recvfrom" );
- }
-
- sa_size = sizeof( struct sockaddr_in6 );
- ssize_t received6 = recvfrom( sock.ipv6, buf, len, 0, ( struct sockaddr * ) &sa, &sa_size );
- if( received6 != -1 ) {
- *addr = sockaddr_to_netaddress( sa );
- *bytes_received = size_t( received6 );
- return true;
- }
- if( received6 == -1 && errno != EAGAIN ) {
- FATAL( "recvfrom" );
- }
-
- return false;
-}
diff --git a/src/win32.cc b/src/win32.cc
@@ -64,7 +64,7 @@ void * platform_connect( const char ** err, const char * host, int port ) {
addr.port = checked_cast< u16 >( port );
TCPSocket sock;
- bool ok = net_new_tcp( &sock, addr, NET_BLOCKING );
+ bool ok = net_new_tcp( &sock, addr );
if( !ok ) {
*err = "net_new_tcp";
return NULL;
diff --git a/src/win32_network.cc b/src/win32_network.cc
@@ -18,46 +18,4 @@ void net_term() {
}
}
-static void make_socket_nonblocking( SOCKET fd ) {
- u_long one = 1;
- int ok = ioctlsocket( fd, FIONBIO, &one );
- if( ok == SOCKET_ERROR ) {
- FATAL( "ioctlsocket" );
- }
-}
-
static void platform_init_sock( SOCKET fd ) { }
-
-bool net_tryrecv( UDPSocket sock, void * buf, size_t len, NetAddress * addr, size_t * bytes_received ) {
- struct sockaddr_storage sa;
-
- socklen_t sa_size = sizeof( struct sockaddr_in );
- ssize_t received4 = recvfrom( sock.ipv4, ( char * ) buf, len, 0, ( struct sockaddr * ) &sa, &sa_size );
- if( received4 != SOCKET_ERROR ) {
- *addr = sockaddr_to_netaddress( sa );
- *bytes_received = size_t( received4 );
- return true;
- }
- else {
- int error = WSAGetLastError();
- if( error != WSAEWOULDBLOCK && error != WSAECONNRESET ) {
- FATAL( "recvfrom" );
- }
- }
-
- sa_size = sizeof( struct sockaddr_in6 );
- ssize_t received6 = recvfrom( sock.ipv6, ( char * ) buf, len, 0, ( struct sockaddr * ) &sa, &sa_size );
- if( received6 != SOCKET_ERROR ) {
- *addr = sockaddr_to_netaddress( sa );
- *bytes_received = size_t( received6 );
- return true;
- }
- else {
- int error = WSAGetLastError();
- if( error != WSAEWOULDBLOCK && error != WSAECONNRESET ) {
- FATAL( "recvfrom" );
- }
- }
-
- return false;
-}
diff --git a/src/x11.cc b/src/x11.cc
@@ -51,7 +51,7 @@ void * platform_connect( const char ** err, const char * host, int port ) {
addr.port = checked_cast< u16 >( port );
TCPSocket sock;
- bool ok = net_new_tcp( &sock, addr, NET_BLOCKING );
+ bool ok = net_new_tcp( &sock, addr );
if( !ok ) {
*err = "net_new_tcp";
return NULL;