mirror of
https://github.com/Genymobile/scrcpy.git
synced 2025-08-03 14:49:29 +00:00
stopped_cond
This commit is contained in:
parent
e23daebe06
commit
34b92440e2
2 changed files with 33 additions and 5 deletions
|
@ -352,7 +352,8 @@ connect_and_read_byte(uint16_t port) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static sc_socket
|
static sc_socket
|
||||||
connect_to_server(uint16_t port, uint32_t attempts, uint32_t delay) {
|
connect_to_server(struct server *server, uint32_t attempts, sc_tick delay) {
|
||||||
|
uint16_t port = server->local_port;
|
||||||
do {
|
do {
|
||||||
LOGD("Remaining connection attempts: %d", (int) attempts);
|
LOGD("Remaining connection attempts: %d", (int) attempts);
|
||||||
sc_socket socket = connect_and_read_byte(port);
|
sc_socket socket = connect_and_read_byte(port);
|
||||||
|
@ -361,7 +362,16 @@ connect_to_server(uint16_t port, uint32_t attempts, uint32_t delay) {
|
||||||
return socket;
|
return socket;
|
||||||
}
|
}
|
||||||
if (attempts) {
|
if (attempts) {
|
||||||
SDL_Delay(delay);
|
sc_mutex_lock(&server->mutex);
|
||||||
|
// Ignore timedwait return (spurious wake ups are harmless)
|
||||||
|
sc_cond_timedwait(&server->stopped_cond, &server->mutex,
|
||||||
|
sc_tick_now() + delay);
|
||||||
|
bool stopped = server->stopped;
|
||||||
|
sc_mutex_unlock(&server->mutex);
|
||||||
|
if (stopped) {
|
||||||
|
LOGI("Connection attempt stopped");
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} while (--attempts > 0);
|
} while (--attempts > 0);
|
||||||
return SC_INVALID_SOCKET;
|
return SC_INVALID_SOCKET;
|
||||||
|
@ -390,7 +400,16 @@ server_init(struct server *server, const struct server_params *params) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ok = sc_cond_init(&server->stopped_cond);
|
||||||
|
if (!ok) {
|
||||||
|
sc_cond_destroy(&server->process_terminated_cond);
|
||||||
|
sc_mutex_destroy(&server->mutex);
|
||||||
|
server_params_destroy(&server->params);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
server->process_terminated = false;
|
server->process_terminated = false;
|
||||||
|
server->stopped = false;
|
||||||
|
|
||||||
server->server_socket = SC_INVALID_SOCKET;
|
server->server_socket = SC_INVALID_SOCKET;
|
||||||
server->video_socket = SC_INVALID_SOCKET;
|
server->video_socket = SC_INVALID_SOCKET;
|
||||||
|
@ -515,9 +534,8 @@ server_connect_to(struct server *server, char *device_name,
|
||||||
server->server_socket = SC_INVALID_SOCKET;
|
server->server_socket = SC_INVALID_SOCKET;
|
||||||
} else {
|
} else {
|
||||||
uint32_t attempts = 100;
|
uint32_t attempts = 100;
|
||||||
uint32_t delay = 100; // ms
|
sc_tick delay = SC_TICK_FROM_MS(100);
|
||||||
server->video_socket =
|
server->video_socket = connect_to_server(server, attempts, delay);
|
||||||
connect_to_server(server->local_port, attempts, delay);
|
|
||||||
if (server->video_socket == SC_INVALID_SOCKET) {
|
if (server->video_socket == SC_INVALID_SOCKET) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -540,6 +558,11 @@ server_connect_to(struct server *server, char *device_name,
|
||||||
|
|
||||||
void
|
void
|
||||||
server_stop(struct server *server) {
|
server_stop(struct server *server) {
|
||||||
|
sc_mutex_lock(&server->mutex);
|
||||||
|
server->stopped = true;
|
||||||
|
sc_cond_signal(&server->stopped_cond);
|
||||||
|
sc_mutex_unlock(&server->mutex);
|
||||||
|
|
||||||
if (server->server_socket != SC_INVALID_SOCKET) {
|
if (server->server_socket != SC_INVALID_SOCKET) {
|
||||||
if (!net_interrupt(server->server_socket)) {
|
if (!net_interrupt(server->server_socket)) {
|
||||||
LOGW("Could not interrupt server socket");
|
LOGW("Could not interrupt server socket");
|
||||||
|
@ -607,6 +630,7 @@ server_destroy(struct server *server) {
|
||||||
}
|
}
|
||||||
|
|
||||||
server_params_destroy(&server->params);
|
server_params_destroy(&server->params);
|
||||||
|
sc_cond_destroy(&server->stopped_cond);
|
||||||
sc_cond_destroy(&server->process_terminated_cond);
|
sc_cond_destroy(&server->process_terminated_cond);
|
||||||
sc_mutex_destroy(&server->mutex);
|
sc_mutex_destroy(&server->mutex);
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,9 +41,13 @@ struct server {
|
||||||
sc_thread wait_server_thread;
|
sc_thread wait_server_thread;
|
||||||
|
|
||||||
sc_mutex mutex;
|
sc_mutex mutex;
|
||||||
|
|
||||||
sc_cond process_terminated_cond;
|
sc_cond process_terminated_cond;
|
||||||
bool process_terminated;
|
bool process_terminated;
|
||||||
|
|
||||||
|
sc_cond stopped_cond;
|
||||||
|
bool stopped;
|
||||||
|
|
||||||
sc_socket server_socket; // only used if !tunnel_forward
|
sc_socket server_socket; // only used if !tunnel_forward
|
||||||
sc_socket video_socket;
|
sc_socket video_socket;
|
||||||
sc_socket control_socket;
|
sc_socket control_socket;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue