diff --git a/source/ftp.c b/source/ftp.c index 47e987c..4b054cf 100644 --- a/source/ftp.c +++ b/source/ftp.c @@ -700,7 +700,7 @@ static bool process_accept_events(s32 server) { s32 peer; struct sockaddr_in client_address; socklen_t addrlen = sizeof(client_address); - while ((peer = net_accept(server, (struct sockaddr *)&client_address, &addrlen)) != -EAGAIN) { + while ((peer = net_accept_nonblocking(server, (struct sockaddr *)&client_address, &addrlen)) != -EAGAIN) { if (peer < 0) { printf("Error accepting connection: [%i] %s\n", -peer, strerror(-peer)); return false; @@ -759,7 +759,7 @@ static void process_data_events(client_t *client) { if (client->passive_socket >= 0) { struct sockaddr_in data_peer_address; socklen_t addrlen = sizeof(data_peer_address); - result = net_accept(client->passive_socket, (struct sockaddr *)&data_peer_address ,&addrlen); + result = net_accept_nonblocking(client->passive_socket, (struct sockaddr *)&data_peer_address ,&addrlen); if (result >= 0) { client->data_socket = result; client->data_connection_connected = true; diff --git a/source/ftpii.c b/source/ftpii.c index 49a6167..ff43418 100644 --- a/source/ftpii.c +++ b/source/ftpii.c @@ -61,7 +61,6 @@ static void initialise_video() { } static void initialise_ftpii() { - initialise_video(); initialise_video(); PAD_Init(); initialise_reset_buttons(); diff --git a/source/net.c b/source/net.c index 2a36d35..98a530f 100644 --- a/source/net.c +++ b/source/net.c @@ -38,10 +38,14 @@ misrepresented as being the original software. static u32 NET_BUFFER_SIZE = MAX_NET_BUFFER_SIZE; void initialise_network() { + struct in_addr s_addr = {0}; + struct in_addr netmask = {0}; + struct in_addr gateway = {0}; + printf("Waiting for network to initialise...\n"); s32 result = -1; while (!check_reset_synchronous() && result < 0) { - while (!check_reset_synchronous() && (result = net_init()) == -EAGAIN); + result = if_configex(&s_addr, &netmask, &gateway, TRUE); if (result < 0) printf("net_init() failed: [%i] %s, retrying...\n", result, strerror(-result)); } if (result >= 0) { @@ -59,9 +63,8 @@ void initialise_network() { } s32 set_blocking(s32 s, bool blocking) { - s32 flags; - flags = net_fcntl(s, F_GETFL, 0); - if (flags >= 0) flags = net_fcntl(s, F_SETFL, blocking ? (flags&~4) : (flags|4)); + s32 flags = blocking; + net_ioctl(s, FIONBIO, &flags); return flags; } @@ -167,3 +170,17 @@ s32 recv_to_file(s32 s, FILE *f) { if (bytes_written < bytes_read) return -1; } } + +s32 net_accept_nonblocking(s32 s, struct sockaddr *addr, socklen_t *addrlen) { + struct timeval tv = {0}; + fd_set readset; + FD_ZERO(&readset); + FD_SET(s, &readset); + net_select(FD_SETSIZE, &readset, NULL, NULL, &tv); + + if (FD_ISSET(s, &readset)) { + return net_accept(s, addr, addrlen); + } else { + return -EAGAIN; + } +} diff --git a/source/net.h b/source/net.h index 9fbc18a..3c05314 100644 --- a/source/net.h +++ b/source/net.h @@ -40,4 +40,6 @@ s32 send_from_file(s32 s, FILE *f); s32 recv_to_file(s32 s, FILE *f); +s32 net_accept_nonblocking(s32 s, struct sockaddr *addr, socklen_t *addrlen); + #endif /* _NET_H_ */