(Network) Get rid of the timeout_enable parameter for socket_connect (#14351)

This commit is contained in:
Cthulhu-throwaway 2022-08-24 23:40:19 -03:00 committed by GitHub
parent 23f649b050
commit e45958b25a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 59 additions and 63 deletions

View file

@ -119,20 +119,16 @@ static void *network_gfx_init(const video_info_t *video,
try_connect:
fd = socket_init((void**)&addr, network->port, network->address, SOCKET_TYPE_STREAM, 0);
next_addr = addr;
while (fd >= 0)
for (next_addr = addr; fd >= 0; fd = socket_next((void**)&next_addr))
{
if (socket_connect_with_timeout(fd, next_addr, 5000))
{
int ret = socket_connect(fd, (void*)next_addr, true);
if (ret >= 0) /* && socket_nonblock(fd)) */
/* socket_connect_with_timeout makes the socket non-blocking. */
if (socket_set_block(fd, true))
break;
socket_close(fd);
}
fd = socket_next((void**)&next_addr);
socket_close(fd);
}
if (addr)
@ -140,11 +136,7 @@ try_connect:
network->fd = fd;
#if 0
socket_nonblock(network->fd);
#endif
if (network->fd > 0)
if (network->fd >= 0)
RARCH_LOG("[Network]: Connected to host.\n");
else
{
@ -156,11 +148,6 @@ try_connect:
RARCH_LOG("[Network]: Init complete.\n");
return network;
error:
if (network)
free(network);
return NULL;
}
static bool network_gfx_frame(void *data, const void *frame,

View file

@ -101,7 +101,7 @@ ssize_t socket_receive_all_nonblocking(int fd, bool *error,
bool socket_bind(int fd, void *data);
int socket_connect(int fd, void *data, bool timeout_enable);
int socket_connect(int fd, void *data);
bool socket_connect_with_timeout(int fd, void *data, int timeout);

View file

@ -431,37 +431,35 @@ static int net_http_new_socket(struct http_connection_t *conn)
#ifdef HAVE_SSL
if (conn->sock_state.ssl)
{
if (fd < 0)
goto done;
if (!(conn->sock_state.ssl_ctx = ssl_socket_init(fd, conn->domain)))
return -1;
}
#endif
next_addr = addr;
while (fd >= 0)
{
#ifdef HAVE_SSL
if (conn->sock_state.ssl)
{
if (ssl_socket_connect(conn->sock_state.ssl_ctx,
(void*)next_addr, true, true) >= 0)
break;
ssl_socket_close(conn->sock_state.ssl_ctx);
}
else
#endif
{
if ( socket_connect(fd, (void*)next_addr, true) >= 0
&& socket_nonblock(fd))
break;
socket_close(fd);
fd = -1;
goto done;
}
if (ssl_socket_connect(conn->sock_state.ssl_ctx, addr, true, true)
< 0)
{
fd = -1;
goto done;
}
}
else
#endif
for (next_addr = addr; fd >= 0; fd = socket_next((void**)&next_addr))
{
if (socket_connect_with_timeout(fd, next_addr, 5000))
break;
fd = socket_next((void**)&next_addr);
socket_close(fd);
}
#ifdef HAVE_SSL
done:
#endif
if (addr)
freeaddrinfo_retro(addr);
@ -864,7 +862,7 @@ error:
conn->postdatacopy = NULL;
}
#ifdef HAVE_SSL
if (conn && conn->sock_state.ssl && conn->sock_state.ssl_ctx && fd >= 0)
if (conn && conn->sock_state.ssl_ctx)
{
ssl_socket_close(conn->sock_state.ssl_ctx);
ssl_socket_free(conn->sock_state.ssl_ctx);

View file

@ -660,21 +660,10 @@ bool socket_bind(int fd, void *data)
return !bind(fd, addr->ai_addr, addr->ai_addrlen);
}
int socket_connect(int fd, void *data, bool timeout_enable)
int socket_connect(int fd, void *data)
{
struct addrinfo *addr = (struct addrinfo*)data;
#if !defined(_WIN32) && !defined(VITA) && !defined(WIIU) && !defined(_3DS)
if (timeout_enable)
{
struct timeval timeout;
timeout.tv_sec = 4;
timeout.tv_usec = 0;
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout));
}
#endif
#ifdef WIIU
{
int op = 1;

View file

@ -222,8 +222,19 @@ int ssl_socket_connect(void *state_data,
struct ssl_state *state = (struct ssl_state*)state_data;
unsigned bearstate;
if (socket_connect(state->fd, data, timeout_enable))
return -1;
if (timeout_enable)
{
if (!socket_connect_with_timeout(state->fd, data, 5000))
return -1;
/* socket_connect_with_timeout makes the socket non-blocking. */
if (!socket_set_block(state->fd, true))
return -1;
}
else
{
if (socket_connect(state->fd, data))
return -1;
}
for (;;)
{

View file

@ -115,8 +115,19 @@ int ssl_socket_connect(void *state_data,
int ret, flags;
struct ssl_state *state = (struct ssl_state*)state_data;
if (socket_connect(state->net_ctx.fd, data, timeout_enable))
return -1;
if (timeout_enable)
{
if (!socket_connect_with_timeout(state->net_ctx.fd, data, 5000))
return -1;
/* socket_connect_with_timeout makes the socket non-blocking. */
if (!socket_set_block(state->net_ctx.fd, true))
return -1;
}
else
{
if (socket_connect(state->net_ctx.fd, data))
return -1;
}
if (mbedtls_ssl_config_defaults(&state->conf,
MBEDTLS_SSL_IS_CLIENT,

View file

@ -3207,7 +3207,7 @@ static bool netplay_tunnel_connect(int fd, const struct addrinfo *addr)
SET_TCP_NODELAY(fd)
SET_FD_CLOEXEC(fd)
result = socket_connect(fd, (void*)addr, false);
result = socket_connect(fd, (void*)addr);
if (result && !isinprogress(result) && !isagain(result))
return false;

View file

@ -277,7 +277,7 @@ int main(int argc, char **argv)
return 1;
}
if (socket_connect(sock, addr, false) < 0)
if (socket_connect(sock, addr) < 0)
{
perror("connect");
return 1;