From: Guus Sliepen Date: Thu, 7 Aug 2014 14:27:43 +0000 (+0200) Subject: Allow meshlink_start() to work after meshlink_stop(). X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=f8be16a427a3eeb89c3489975ba5099943262c59;p=meshlink Allow meshlink_start() to work after meshlink_stop(). The way the MeshLink thread is signaled that it has to quit using meshlink_stop() is by shutting down one of the listening sockets. However, it has to be repaired to allow meshlink_start() to work again. Maybe not the prettiest solution. --- diff --git a/src/meshlink.c b/src/meshlink.c index 99e1704b..0da4cbbe 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -832,7 +832,9 @@ static void *meshlink_main_loop(void *arg) { try_outgoing_connections(mesh); + fprintf(stderr, "Starting main_loop...\n"); main_loop(mesh); + fprintf(stderr, "main_loop returned.\n"); return NULL; } @@ -872,17 +874,26 @@ void meshlink_stop(meshlink_handle_t *mesh) { return; } - // Shut down the listening sockets to signal the main thread to shut down + listen_socket_t *s = &mesh->listen_socket[0]; - for(int i = 0; i < mesh->listen_sockets; i++) { - shutdown(mesh->listen_socket[i].tcp.fd, SHUT_RDWR); - shutdown(mesh->listen_socket[i].udp.fd, SHUT_RDWR); - } + // Shut down a listening socket to signal the main thread to shut down + + shutdown(s->tcp.fd, SHUT_RDWR); // Wait for the main thread to finish pthread_join(mesh->thread, NULL); mesh->threadstarted = false; + + // Fix the socket + + closesocket(s->tcp.fd); + io_del(&mesh->loop, &s->tcp); + s->tcp.fd = setup_listen_socket(&s->sa); + if(s->tcp.fd < 0) + logger(DEBUG_ALWAYS, LOG_ERR, "Could not repair listenen socket!"); + else + io_add(&mesh->loop, &s->tcp, handle_new_meta_connection, s, s->tcp.fd, IO_READ); } void meshlink_close(meshlink_handle_t *mesh) {