From a33004a4c92b98ffa0f669cd7a57ace14954cff6 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 17 Apr 2019 15:15:19 +0900 Subject: [PATCH] Add function to drop connection to specific addr Also added minor note about ctx->idMap --- src/UDPConnection.c | 22 ++++++++++++++++++++++ src/UDPConnection.h | 7 +++++++ 2 files changed, 29 insertions(+) diff --git a/src/UDPConnection.c b/src/UDPConnection.c index 969e4c3..7278486 100644 --- a/src/UDPConnection.c +++ b/src/UDPConnection.c @@ -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); } diff --git a/src/UDPConnection.h b/src/UDPConnection.h index eafb066..1f7a941 100644 --- a/src/UDPConnection.h +++ b/src/UDPConnection.h @@ -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);