]> git.seodisparate.com - UDPConnection/commitdiff
Add get/set accept-new-connections fn
authorStephen Seo <seo.disparate@gmail.com>
Wed, 6 Mar 2019 11:13:38 +0000 (20:13 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 6 Mar 2019 11:13:38 +0000 (20:13 +0900)
Fix some functions not using mutex when threaded update is enabled.

src/UDPConnection.c
src/UDPConnection.h

index ad3966b05d049d9aba46bfdd75dc750b8eb2cdcc..d1bbc6b0aa8c3c2bb5e002028b9a88303e3b0104 100644 (file)
@@ -218,30 +218,39 @@ void UDPC_INTERNAL_destroy_conMap(void *unused, uint32_t addr, char *data)
 void UDPC_set_callback_connected(
     UDPC_Context *ctx, UDPC_callback_connected fptr, void *userData)
 {
+    if((ctx->flags & 0x1) != 0) { mtx_lock(&ctx->tCVMtx); }
+
     ctx->callbackConnected = fptr;
     ctx->callbackConnectedUserData = userData;
+
+    if((ctx->flags & 0x1) != 0) { mtx_unlock(&ctx->tCVMtx); }
 }
 
 void UDPC_set_callback_disconnected(
     UDPC_Context *ctx, UDPC_callback_disconnected fptr, void *userData)
 {
+    if((ctx->flags & 0x1) != 0) { mtx_lock(&ctx->tCVMtx); }
+
     ctx->callbackDisconnected = fptr;
     ctx->callbackDisconnectedUserData = userData;
+
+    if((ctx->flags & 0x1) != 0) { mtx_unlock(&ctx->tCVMtx); }
 }
 
 void UDPC_set_callback_received(
     UDPC_Context *ctx, UDPC_callback_received fptr, void *userData)
 {
+    if((ctx->flags & 0x1) != 0) { mtx_lock(&ctx->tCVMtx); }
+
     ctx->callbackReceived = fptr;
     ctx->callbackReceivedUserData = userData;
+
+    if((ctx->flags & 0x1) != 0) { mtx_unlock(&ctx->tCVMtx); }
 }
 
 void UDPC_check_events(UDPC_Context *ctx)
 {
-    if((ctx->flags & 0x1) != 0)
-    {
-        mtx_lock(&ctx->tCVMtx);
-    }
+    if((ctx->flags & 0x1) != 0) { mtx_lock(&ctx->tCVMtx); }
 
     if(ctx->callbackConnected)
     {
@@ -300,18 +309,12 @@ void UDPC_check_events(UDPC_Context *ctx)
         UDPC_Deque_clear(ctx->receivedPackets);
     }
 
-    if((ctx->flags & 0x1) != 0)
-    {
-        mtx_unlock(&ctx->tCVMtx);
-    }
+    if((ctx->flags & 0x1) != 0) { mtx_unlock(&ctx->tCVMtx); }
 }
 
 void UDPC_client_initiate_connection(UDPC_Context *ctx, uint32_t addr, uint16_t port)
 {
-    if((ctx->flags & 0x1) != 0)
-    {
-        mtx_lock(&ctx->tCVMtx);
-    }
+    if((ctx->flags & 0x1) != 0) { mtx_lock(&ctx->tCVMtx); }
 
     if((ctx->flags & 0x2) == 0 || UDPC_HashMap_has(ctx->conMap, addr) != 0)
     {
@@ -345,14 +348,13 @@ void UDPC_client_initiate_connection(UDPC_Context *ctx, uint32_t addr, uint16_t
 
     UDPC_HashMap_insert(ctx->conMap, addr, &cd);
 
-    if((ctx->flags & 0x1) != 0)
-    {
-        mtx_unlock(&ctx->tCVMtx);
-    }
+    if((ctx->flags & 0x1) != 0) { mtx_unlock(&ctx->tCVMtx); }
 }
 
 int UDPC_queue_send(UDPC_Context *ctx, uint32_t addr, uint32_t isChecked, void *data, uint32_t size)
 {
+    if((ctx->flags & 0x1) != 0) { mtx_lock(&ctx->tCVMtx); }
+
     UDPC_INTERNAL_ConnectionData *cd = UDPC_HashMap_get(ctx->conMap, addr);
     if(cd)
     {
@@ -371,16 +373,19 @@ int UDPC_queue_send(UDPC_Context *ctx, uint32_t addr, uint32_t isChecked, void *
             {
                 UDPC_INTERNAL_log(ctx, 1, "Not enough free space in send "
                     "packet queue, failed to queue packet for sending");
+                if((ctx->flags & 0x1) != 0) { mtx_unlock(&ctx->tCVMtx); }
                 return 0;
             }
             else
             {
+                if((ctx->flags & 0x1) != 0) { mtx_unlock(&ctx->tCVMtx); }
                 return 1;
             }
         }
         else
         {
             UDPC_INTERNAL_log(ctx, 0, "Failed to allocate memory to new send-packet queue entry");
+            if((ctx->flags & 0x1) != 0) { mtx_unlock(&ctx->tCVMtx); }
             return 0;
         }
     }
@@ -388,22 +393,59 @@ int UDPC_queue_send(UDPC_Context *ctx, uint32_t addr, uint32_t isChecked, void *
     {
         UDPC_INTERNAL_log(ctx, 0, "Cannot send to %s when connection has not been esablished",
             UDPC_INTERNAL_atostr(ctx, addr));
+        if((ctx->flags & 0x1) != 0) { mtx_unlock(&ctx->tCVMtx); }
         return 0;
     }
 }
 
 int UDPC_get_queue_send_available(UDPC_Context *ctx, uint32_t addr)
 {
+    if((ctx->flags & 0x1) != 0) { mtx_lock(&ctx->tCVMtx); }
+
     UDPC_INTERNAL_ConnectionData *cd = UDPC_HashMap_get(ctx->conMap, addr);
-    if(!cd) { return 0; }
+    if(!cd)
+    {
+        if((ctx->flags & 0x1) != 0) { mtx_unlock(&ctx->tCVMtx); }
+        return 0;
+    }
+
+    int available = UDPC_Deque_get_available(cd->sendPktQueue) / sizeof(UDPC_INTERNAL_PacketInfo);
 
-    return UDPC_Deque_get_available(cd->sendPktQueue) / sizeof(UDPC_INTERNAL_PacketInfo);
+    if((ctx->flags & 0x1) != 0) { mtx_unlock(&ctx->tCVMtx); }
+
+    return available;
+}
+
+int UDPC_get_accept_new_connections(UDPC_Context *ctx)
+{
+    if((ctx->flags & 0x1) != 0) { mtx_lock(&ctx->tCVMtx); }
+
+    int result = (ctx->flags & 0x40) != 0 ? 1 : 0;
+
+    if((ctx->flags & 0x1) != 0) { mtx_unlock(&ctx->tCVMtx); }
+
+    return result;
+}
+
+void UDPC_set_accept_new_connections(UDPC_Context *ctx, int isAccepting)
+{
+    if((ctx->flags & 0x1) != 0) { mtx_lock(&ctx->tCVMtx); }
+
+    if(isAccepting != 0) { ctx->flags |= 0x40; }
+    else { ctx->flags &= 0xFFFFFFBF; }
+
+    if((ctx->flags & 0x1) != 0) { mtx_unlock(&ctx->tCVMtx); }
 }
 
 uint32_t UDPC_get_error(UDPC_Context *ctx)
 {
+    if((ctx->flags & 0x1) != 0) { mtx_lock(&ctx->tCVMtx); }
+
     uint32_t error = ctx->error;
     ctx->error = 0;
+
+    if((ctx->flags & 0x1) != 0) { mtx_unlock(&ctx->tCVMtx); }
+
     return error;
 }
 
@@ -424,6 +466,8 @@ const char* UDPC_get_error_str(uint32_t error)
 
 void UDPC_set_logging_type(UDPC_Context *ctx, uint32_t logType)
 {
+    if((ctx->flags & 0x1) != 0) { mtx_lock(&ctx->tCVMtx); }
+
     switch(logType)
     {
     case 0:
@@ -445,6 +489,8 @@ void UDPC_set_logging_type(UDPC_Context *ctx, uint32_t logType)
         ctx->flags |= (0x4 | 0x8 | 0x10 | 0x20);
         break;
     }
+
+    if((ctx->flags & 0x1) != 0) { mtx_unlock(&ctx->tCVMtx); }
 }
 
 void UDPC_update(UDPC_Context *ctx)
index 5ebad773079dfd33775c33905df159c90e221f38..c8632403f6cf6fe9b155cfd7ecf887d2f53e3894 100644 (file)
@@ -174,8 +174,20 @@ int UDPC_queue_send(
  */
 int UDPC_get_queue_send_available(UDPC_Context *ctx, uint32_t addr);
 
+/// Returns non-zero if UDPC is accepting new connections
+int UDPC_get_accept_new_connections(UDPC_Context *ctx);
+
+/// Set isAccepting to non-zero to let UDPC accept new connections
+void UDPC_set_accept_new_connections(UDPC_Context *ctx, int isAccepting);
+
+/*!
+ * \brief Get the currently set error code, and clear it internally
+ * Error codes and their meanings are defined in UDPC_Defines.h .
+ * Use UDPC_get_error_str() to get a string describing the error.
+ */
 uint32_t UDPC_get_error(UDPC_Context *ctx);
 
+/// Returns a string describing the error code for UDPC
 const char* UDPC_get_error_str(uint32_t error);
 
 /*!