At least on Linux, one way to signal a sleeping select() call in
the thread is to call shutdown() on the listening sockets. A subsequent
accept() returns EINVAL, we use that as a cue to shut down the main
loop.
}
void meshlink_stop(meshlink_handle_t *mesh) {
- // TODO: close the listening sockets to signal the main thread to shut down
+ // Shut down the listening sockets to signal the main thread to shut down
+
+ 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);
+ }
// Wait for the main thread to finish
fd = accept(l->tcp.fd, &sa.sa, &len);
if(fd < 0) {
+ if(errno == EINVAL) { // TODO: check if Windows agrees
+ event_loop_stop(loop);
+ return;
+ }
+
logger(DEBUG_ALWAYS, LOG_ERR, "Accepting a new connection failed: %s", sockstrerror(sockerrno));
return;
}