]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
After killing meshlink thread update the bool status threadstarted
[meshlink] / src / meshlink.c
index 9fc80153d48210b0cbb60f7c7793b673c4e090cd..99e1704b89c929e18a97fddf4f9abcd95f78a102 100644 (file)
@@ -740,7 +740,13 @@ static bool meshlink_setup(meshlink_handle_t *mesh) {
        return true;
 }
 
+
 meshlink_handle_t *meshlink_open(const char *confbase, const char *name) {
+       return meshlink_open_with_size(confbase, name, sizeof(meshlink_handle_t) );
+}
+
+
+meshlink_handle_t *meshlink_open_with_size(const char *confbase, const char *name, size_t size) {
        // Validate arguments provided by the application
        bool usingname = false;
 
@@ -763,10 +769,9 @@ meshlink_handle_t *meshlink_open(const char *confbase, const char *name) {
                } else { usingname = true;}
        }
 
-       meshlink_handle_t *mesh = xzalloc(sizeof *mesh);
+       meshlink_handle_t *mesh = xzalloc(size);
        mesh->confbase = xstrdup(confbase);
        if (usingname) mesh->name = xstrdup(name);
-       pthread_mutex_init ( &(mesh->outpacketqueue_mutex), NULL);
        pthread_mutex_init ( &(mesh->nodes_mutex), NULL);
        mesh->threadstarted = false;
        event_loop_init(&mesh->loop);
@@ -877,6 +882,7 @@ void meshlink_stop(meshlink_handle_t *mesh) {
        // Wait for the main thread to finish
 
        pthread_join(mesh->thread, NULL);
+       mesh->threadstarted = false;
 }
 
 void meshlink_close(meshlink_handle_t *mesh) {
@@ -951,19 +957,15 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const
                return false;
        }
 
-       /* If there is no outgoing list yet, create one. */
-
-       if(!mesh->outpacketqueue)
-               mesh->outpacketqueue = list_alloc(NULL);
-
        //add packet to the queue
        outpacketqueue_t *packet_in_queue = xzalloc(sizeof *packet_in_queue);
        packet_in_queue->destination=destination;
        packet_in_queue->data=data;
        packet_in_queue->len=len;
-       pthread_mutex_lock(&(mesh->outpacketqueue_mutex));
-       list_insert_head(mesh->outpacketqueue,packet_in_queue);
-       pthread_mutex_unlock(&(mesh->outpacketqueue_mutex));
+       if(!meshlink_queue_push(&mesh->outpacketqueue, packet_in_queue)) {
+               free(packet_in_queue);
+               return false;
+       }
 
        //notify event loop
        signal_trigger(&(mesh->loop),&(mesh->datafromapp));
@@ -974,10 +976,9 @@ void meshlink_send_from_queue(event_loop_t* el,meshlink_handle_t *mesh) {
        vpn_packet_t packet;
        meshlink_packethdr_t *hdr = (meshlink_packethdr_t *)packet.data;
 
-       outpacketqueue_t* p = list_get_tail(mesh->outpacketqueue);
-       if (p)
-       list_delete_tail(mesh->outpacketqueue);
-       else return ;
+       outpacketqueue_t* p = meshlink_queue_pop(&mesh->outpacketqueue);
+       if(!p)
+               return;
 
        if (sizeof(meshlink_packethdr_t) + p->len > MAXSIZE) {
                //log something