summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ot_livesync.c20
1 files changed, 15 insertions, 5 deletions
diff --git a/ot_livesync.c b/ot_livesync.c
index 45271bc..e6db720 100644
--- a/ot_livesync.c
+++ b/ot_livesync.c
@@ -8,6 +8,7 @@
8#include <pthread.h> 8#include <pthread.h>
9#include <stdlib.h> 9#include <stdlib.h>
10#include <string.h> 10#include <string.h>
11#include <sys/socket.h>
11#include <sys/types.h> 12#include <sys/types.h>
12#include <unistd.h> 13#include <unistd.h>
13 14
@@ -31,6 +32,9 @@ ot_ip6 groupip_1 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 224, 0, 23, 5};
31 32
32#define LIVESYNC_MAXDELAY 15 /* seconds */ 33#define LIVESYNC_MAXDELAY 15 /* seconds */
33 34
35/* How long the worker may sit in a read before it looks at the running flag */
36#define LIVESYNC_WORKER_PATIENCE 1 /* seconds */
37
34enum { OT_SYNC_PEER4, OT_SYNC_PEER6 }; 38enum { OT_SYNC_PEER4, OT_SYNC_PEER6 };
35 39
36/* Forward declaration */ 40/* Forward declaration */
@@ -53,6 +57,7 @@ static sync_buffer g_v6_buf;
53static sync_buffer g_v4_buf; 57static sync_buffer g_v4_buf;
54 58
55static pthread_t thread_id; 59static pthread_t thread_id;
60static int thread_started;
56void livesync_init(void) { 61void livesync_init(void) {
57 uint32_t packet_type6 = htonl(OT_SYNC_PEER6); 62 uint32_t packet_type6 = htonl(OT_SYNC_PEER6);
58 uint32_t packet_type4 = htonl(OT_SYNC_PEER4); 63 uint32_t packet_type4 = htonl(OT_SYNC_PEER4);
@@ -73,16 +78,21 @@ void livesync_init(void) {
73 g_v6_buf.next_packet_time = time_future_seconds(LIVESYNC_MAXDELAY); 78 g_v6_buf.next_packet_time = time_future_seconds(LIVESYNC_MAXDELAY);
74 g_v4_buf.next_packet_time = time_future_seconds(LIVESYNC_MAXDELAY); 79 g_v4_buf.next_packet_time = time_future_seconds(LIVESYNC_MAXDELAY);
75 80
76 pthread_create(&thread_id, NULL, livesync_worker, NULL); 81 socket_set_timeout(g_socket_in, LIVESYNC_WORKER_PATIENCE);
82 thread_started = !pthread_create(&thread_id, NULL, livesync_worker, NULL);
77} 83}
78 84
79void livesync_deinit(void) { 85void livesync_deinit(void) {
86 if (thread_started) {
87 shutdown(g_socket_in, SHUT_RD);
88 pthread_join(thread_id, NULL);
89 thread_started = 0;
90 }
91
80 if (g_socket_in != -1) 92 if (g_socket_in != -1)
81 close(g_socket_in); 93 close(g_socket_in);
82 if (g_socket_out != -1) 94 if (g_socket_out != -1)
83 close(g_socket_out); 95 close(g_socket_out);
84
85 pthread_cancel(thread_id);
86} 96}
87 97
88void livesync_bind_mcast(ot_ip6 ip, uint16_t port) { 98void livesync_bind_mcast(ot_ip6 ip, uint16_t port) {
@@ -195,7 +205,7 @@ static void *livesync_worker(void *args) {
195 ws.inbuf = ws.request = malloc(LIVESYNC_INCOMING_BUFFSIZE); 205 ws.inbuf = ws.request = malloc(LIVESYNC_INCOMING_BUFFSIZE);
196 ws.outbuf = ws.reply = 0; 206 ws.outbuf = ws.reply = 0;
197 207
198 while (1) { 208 while (g_opentracker_running) {
199 uint32_t packet_type; 209 uint32_t packet_type;
200 210
201 ws.request_size = socket_recv(g_socket_in, ws.inbuf, LIVESYNC_INCOMING_BUFFSIZE, in_ip, &in_port); 211 ws.request_size = socket_recv(g_socket_in, ws.inbuf, LIVESYNC_INCOMING_BUFFSIZE, in_ip, &in_port);
@@ -224,7 +234,7 @@ static void *livesync_worker(void *args) {
224 } 234 }
225 } 235 }
226 236
227 /* Never returns. */ 237 free(ws.inbuf);
228 return NULL; 238 return NULL;
229} 239}
230 240