Update socket
authorJeroen van der Heijden <jeroen@transceptor.technology>
Fri, 24 Jan 2020 14:09:48 +0000 (15:09 +0100)
committerJeroen van der Heijden <jeroen@transceptor.technology>
Fri, 24 Jan 2020 14:09:48 +0000 (15:09 +0100)
include/siri/db/tee.h
src/siri/db/tee.c

index 620a21d7a0ab3cfb694432122ce2f233c42dc019..6317120cf574ec6a4b791b495e66dd0b5cafa9e4 100644 (file)
@@ -9,7 +9,8 @@ typedef struct siridb_tee_s siridb_tee_t;
 enum
 {
     SIRIDB_TEE_FLAG_INIT = 1<<0,
-    SIRIDB_TEE_FLAG_CONNECTED = 1<<1,
+    SIRIDB_TEE_FLAG_CONNECTING = 1<<1,
+    SIRIDB_TEE_FLAG_CONNECTED = 1<<2,
     SIRIDB_TEE_FLAG = 1<<31,
 };
 
index f40630b11081acfd4792f3e934a48cb872fb0633..37e03587ca360c4c5a043b33ee1972b2007ecfb4 100644 (file)
@@ -49,7 +49,11 @@ void siridb_tee_free(siridb_tee_t * tee)
 
 int siridb_tee_connect(siridb_tee_t * tee)
 {
-    LOGC("TEE CONNECT0");
+    if (tee->flags & SIRIDB_TEE_FLAG_CONNECTING)
+    {
+        return 0;
+    }
+    tee->flags |= SIRIDB_TEE_FLAG_CONNECTING;
     uv_connect_t * req = malloc(sizeof(uv_connect_t));
     if (req == NULL)
     {
@@ -60,19 +64,19 @@ int siridb_tee_connect(siridb_tee_t * tee)
 
     if (uv_pipe_init(siri.loop, &tee->pipe, 0))
     {
+        tee->flags &= ~SIRIDB_TEE_FLAG_CONNECTING;
+        free(req);
         return -1;
     }
     tee->flags |= SIRIDB_TEE_FLAG_INIT;
     tee->pipe.data = tee;
 
     uv_pipe_connect(req, &tee->pipe, tee->pipe_name_, tee__on_connect);
-    LOGC("TEE CONNECT2");
     return 0;
 }
 
 int siridb_tee_set_pipe_name(siridb_tee_t * tee, const char * pipe_name)
 {
-    LOGC("SET NAME0");
     free(tee->pipe_name_);
     free(tee->err_msg_);
     tee->err_msg_ = NULL;
@@ -101,20 +105,17 @@ int siridb_tee_set_pipe_name(siridb_tee_t * tee, const char * pipe_name)
     {
         tee__runtime_init(&tee->pipe);
     }
-    LOGC("SET NAME5");
     return 0;
 }
 
 void siridb_tee_write(siridb_tee_t * tee, sirinet_promise_t * promise)
 {
-    LOGC("TEE_WRITE0");
     uv_write_t * req = malloc(sizeof(uv_write_t));
     if (!req)
     {
         log_error("Cannot allocate memory for tee request");
         return;
     }
-    LOGC("TEE_WRITE1");
 
     req->data = promise;
     sirinet_promise_incref(promise);
@@ -128,7 +129,6 @@ void siridb_tee_write(siridb_tee_t * tee, sirinet_promise_t * promise)
         log_error("Cannot write to tee");
         sirinet_promise_decref(promise);
     }
-    LOGC("TEE_WRITE2");
 }
 
 const char * tee_str(siridb_tee_t * tee)
@@ -147,7 +147,6 @@ const char * tee_str(siridb_tee_t * tee)
 
 static void tee__runtime_init(uv_pipe_t * pipe)
 {
-    LOGC("ON_INIT0");
     siridb_tee_t * tee = pipe->data;
 
     tee->flags &= ~SIRIDB_TEE_FLAG_INIT;
@@ -157,37 +156,30 @@ static void tee__runtime_init(uv_pipe_t * pipe)
      {
          log_error("Could not connect to tee at runtime");
      }
-     LOGC("ON_INIT2");
 }
 
 static void tee__close_cb(uv_pipe_t * pipe)
 {
-    LOGC("ONCLOSE0");
     siridb_tee_t * tee = pipe->data;
 
-    LOGC("ONCLOSE1");
     tee->flags &= ~SIRIDB_TEE_FLAG_INIT;
+    tee->flags &= ~SIRIDB_TEE_FLAG_CONNECTING;
     tee->flags &= ~SIRIDB_TEE_FLAG_CONNECTED;
-    LOGC("ONCLOSE2");
 }
 
 static void tee__write_cb(uv_write_t * req, int status)
 {
-    LOGC("ON_WRITE0");
     sirinet_promise_t * promise = req->data;
     sirinet_promise_decref(promise);
-    LOGC("ON_WRITE1");
     if (status)
     {
         log_error("Socket (tee) write error: %s", uv_strerror(status));
     }
     free(req);
-    LOGC("ON_WRITE2");
 }
 
 static void tee__on_connect(uv_connect_t * req, int status)
 {
-    LOGC("ON_CONN0");
     siridb_tee_t * tee = req->data;
 
     if (status == 0)
@@ -207,7 +199,6 @@ static void tee__on_connect(uv_connect_t * req, int status)
         tee->flags |= SIRIDB_TEE_FLAG_CONNECTED;
         goto done;
     }
-    LOGC("ON_CONN1");
 
     free(tee->err_msg_);
     tee->err_msg_ = NULL;
@@ -220,14 +211,11 @@ static void tee__on_connect(uv_connect_t * req, int status)
     {
         log_warning(tee->err_msg_);
     }
-    LOGC("HERE0");
 
 fail:
     uv_close((uv_handle_t *) req->handle, (uv_close_cb) tee__close_cb);
-    LOGC("HERE0.1");
 done:
     free(req);
-    LOGC("HERE1");
 }
 
 static void tee__alloc_buffer(
@@ -235,10 +223,8 @@ static void tee__alloc_buffer(
     size_t suggsz __attribute__((unused)),
     uv_buf_t * buf)
 {
-    LOGC("ALLOC0");
     buf->base = tee__buf;
     buf->len = TEE__BUF_SZ;
-    LOGC("ALLOC1");
 }
 
 
@@ -248,7 +234,6 @@ static void tee__on_data(
     ssize_t nread,
     const uv_buf_t * buf __attribute__((unused)))
 {
-    LOGC("ON_DATA0");
     if (nread < 0)
     {
         if (nread != UV_EOF)
@@ -260,12 +245,9 @@ static void tee__on_data(
         log_info("Disconnected from tee");
         uv_close((uv_handle_t *) client, (uv_close_cb) tee__close_cb);
     }
-    LOGC("ON_DATA1");
 
     if (nread > 0)
     {
         log_debug("Got %zd bytes on tee which will be ignored", nread);
     }
-
-    LOGC("ON_DATA2");
 }