]> git.seodisparate.com - UDPConnection/commitdiff
Add function to drop connection to specific addr
authorStephen Seo <seo.disparate@gmail.com>
Wed, 17 Apr 2019 06:15:19 +0000 (15:15 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 17 Apr 2019 06:15:19 +0000 (15:15 +0900)
Also added minor note about ctx->idMap

src/UDPConnection.c
src/UDPConnection.h

index 969e4c3afe4edb187ed86d444e69fd9b35da0bb1..7278486f11d43b9e00314ba47a82d52297a52e0b 100644 (file)
@@ -452,6 +452,28 @@ void UDPC_set_accept_new_connections(UDPC_Context *ctx, int isAccepting)
     if(ctx->isThreaded != 0) { mtx_unlock(&ctx->tCVMtx); }
 }
 
+int UDPC_drop_connection(UDPC_Context *ctx, uint32_t addr)
+{
+    int wasDropped = 0;
+
+    if(ctx->isThreaded != 0) { mtx_lock(&ctx->tCVMtx); }
+
+    UDPC_INTERNAL_ConnectionData *cd = UDPC_HashMap_get(ctx->conMap, addr);
+    if(cd)
+    {
+        if((cd->flags & 0x10) != 0)
+        {
+            UDPC_HashMap_remove(ctx->idMap, cd->id);
+        }
+        UDPC_HashMap_remove(ctx->conMap, addr);
+        wasDropped = 1;
+    }
+
+    if(ctx->isThreaded != 0) { mtx_unlock(&ctx->tCVMtx); }
+
+    return wasDropped;
+}
+
 uint32_t UDPC_get_protocol_id(UDPC_Context *ctx)
 {
     if(ctx->isThreaded != 0) { mtx_lock(&ctx->tCVMtx); }
index eafb0667f0263142c100003b092bd0148c4bdc16..1f7a94116f9b37a32803f59bf2e865700ba2e100 100644 (file)
@@ -123,6 +123,7 @@ typedef struct {
     mtx_t tflagsMtx;
     cnd_t threadCV;
     UDPC_HashMap *conMap;
+    // Clients intentionally do not use idMap at all
     UDPC_HashMap *idMap;
     struct timespec lastUpdated;
     char atostrBuf[UDPC_ATOSTR_BUF_SIZE];
@@ -227,6 +228,12 @@ int UDPC_get_accept_new_connections(UDPC_Context *ctx);
  */
 void UDPC_set_accept_new_connections(UDPC_Context *ctx, int isAccepting);
 
+/// Drops a connection specified by addr
+/*!
+ * \return non-zero if the connection existed and was dropped
+ */
+int UDPC_drop_connection(UDPC_Context *ctx, uint32_t addr);
+
 /// Gets the currently set protocol id
 uint32_t UDPC_get_protocol_id(UDPC_Context *ctx);