Set video and control sockets only on success

Store the video and control sockets only if server_connect_to() returns
successfully.
This commit is contained in:
Romain Vimont 2021-11-01 11:44:58 +01:00
parent e69356c550
commit ed19901db1

View file

@ -138,9 +138,10 @@ enable_tunnel_reverse_any_port(struct server *server,
// client can listen before starting the server app, so there is no // client can listen before starting the server app, so there is no
// need to try to connect until the server socket is listening on the // need to try to connect until the server socket is listening on the
// device. // device.
server->server_socket = listen_on_port(port); sc_socket server_socket = listen_on_port(port);
if (server->server_socket != SC_INVALID_SOCKET) { if (server_socket != SC_INVALID_SOCKET) {
// success // success
server->server_socket = server_socket;
server->local_port = port; server->local_port = port;
server->tunnel_enabled = true; server->tunnel_enabled = true;
return true; return true;
@ -432,16 +433,19 @@ device_read_info(sc_socket device_socket, struct server_info *info) {
bool bool
server_connect_to(struct server *server, struct server_info *info) { server_connect_to(struct server *server, struct server_info *info) {
assert(server->tunnel_enabled);
sc_socket video_socket = SC_INVALID_SOCKET;
sc_socket control_socket = SC_INVALID_SOCKET;
if (!server->tunnel_forward) { if (!server->tunnel_forward) {
server->video_socket = net_accept(server->server_socket); video_socket = net_accept(server->server_socket);
if (server->video_socket == SC_INVALID_SOCKET) { if (video_socket == SC_INVALID_SOCKET) {
return false; goto fail;
} }
server->control_socket = net_accept(server->server_socket); control_socket = net_accept(server->server_socket);
if (server->control_socket == SC_INVALID_SOCKET) { if (control_socket == SC_INVALID_SOCKET) {
// the video_socket will be cleaned up on destroy goto fail;
return false;
} }
// we don't need the server socket anymore // we don't need the server socket anymore
@ -453,17 +457,15 @@ server_connect_to(struct server *server, struct server_info *info) {
} else { } else {
uint32_t attempts = 100; uint32_t attempts = 100;
uint32_t delay = 100; // ms uint32_t delay = 100; // ms
server->video_socket = video_socket = connect_to_server(server->local_port, attempts, delay);
connect_to_server(server->local_port, attempts, delay); if (video_socket == SC_INVALID_SOCKET) {
if (server->video_socket == SC_INVALID_SOCKET) { goto fail;
return false;
} }
// we know that the device is listening, we don't need several attempts // we know that the device is listening, we don't need several attempts
server->control_socket = control_socket = net_connect(IPV4_LOCALHOST, server->local_port);
net_connect(IPV4_LOCALHOST, server->local_port); if (control_socket == SC_INVALID_SOCKET) {
if (server->control_socket == SC_INVALID_SOCKET) { goto fail;
return false;
} }
} }
@ -471,7 +473,33 @@ server_connect_to(struct server *server, struct server_info *info) {
disable_tunnel(server); // ignore failure disable_tunnel(server); // ignore failure
// The sockets will be closed on stop if device_read_info() fails // The sockets will be closed on stop if device_read_info() fails
return device_read_info(server->video_socket, info); bool ok = device_read_info(video_socket, info);
if (!ok) {
goto fail;
}
assert(video_socket != SC_INVALID_SOCKET);
assert(control_socket != SC_INVALID_SOCKET);
server->video_socket = video_socket;
server->control_socket = control_socket;
return true;
fail:
if (video_socket != SC_INVALID_SOCKET) {
if (!net_close(video_socket)) {
LOGW("Could not close video socket");
}
}
if (control_socket != SC_INVALID_SOCKET) {
if (!net_close(control_socket)) {
LOGW("Could not close control socket");
}
}
return false;
} }
void void