From ea8226cdf2b6ffa924cbaa875c04d016bc7d1b20 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 30 Jan 2019 15:46:52 +0900 Subject: [PATCH] Some work on UDPC --- src/UDPConnection.c | 14 +++++++++++--- src/UDPConnection.h | 13 +++++++++---- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/UDPConnection.c b/src/UDPConnection.c index 3bcf2f5..a763e45 100644 --- a/src/UDPConnection.c +++ b/src/UDPConnection.c @@ -2,12 +2,13 @@ #include -UDPC_Context* UDPC_init(uint16_t listenPort) +UDPC_Context* UDPC_init(uint16_t listenPort, int isClient) { UDPC_Context *context = malloc(sizeof(UDPC_Context)); context->error = UDPC_SUCCESS; context->flags = 0; context->threadFlags = 0; + if(isClient != 0) context->flags |= 0x2; // create socket context->socketHandle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); @@ -52,15 +53,21 @@ UDPC_Context* UDPC_init(uint16_t listenPort) CleanupSocket(context->socketHandle); context->socketHandle = 0; fprintf(stderr, "Failed to set non-blocking on socket\n"); +#if UDPC_PLATFORM == UDPC_PLATFORM_UNKNOWN + fprintf(stderr, "(Unknown platform)\n"); +#endif return context; } + context->connected = UDPC_Deque_init(sizeof(UDPC_INTERNAL_ConnectionData) + * (isClient != 0 ? 1 : UDPC_CD_AMOUNT)); + return context; } -UDPC_Context* UDPC_init_threaded_update(uint16_t listenPort) +UDPC_Context* UDPC_init_threaded_update(uint16_t listenPort, int isClient) { - UDPC_Context *context = UDPC_init(listenPort); + UDPC_Context *context = UDPC_init(listenPort, isClient); context->error = mtx_init(&context->tCVMtx, mtx_timed); if(context->error != thrd_success) @@ -118,6 +125,7 @@ UDPC_Context* UDPC_init_threaded_update(uint16_t listenPort) void UDPC_destroy(UDPC_Context *ctx) { CleanupSocket(ctx->socketHandle); + UDPC_Deque_destroy(ctx->connected); if((ctx->flags & 0x1) != 0) { diff --git a/src/UDPConnection.h b/src/UDPConnection.h index e28f8d1..27f459e 100644 --- a/src/UDPConnection.h +++ b/src/UDPConnection.h @@ -7,6 +7,7 @@ #include #include "UDPC_Defines.h" +#include "UDPC_Deque.h" #if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS #include @@ -21,6 +22,8 @@ #define CleanupSocket(x) close(x) #endif +#define UDPC_CD_AMOUNT 32 + // This struct should not be used outside of this library typedef struct { @@ -54,8 +57,8 @@ typedef struct float toggleTimer; float toggledTimer; uint16_t port; - UDPC_INTERNAL_PacketInfo *sentPkts; - UDPC_INTERNAL_PacketInfo *sendPktQueue; + UDPC_Deque *sentPkts; + UDPC_Deque *sendPktQueue; struct timespec received; struct timespec sent; struct timespec rtt; @@ -66,6 +69,7 @@ typedef struct { /* * 0x1 - is threaded + * 0x2 - is client */ uint32_t flags; /* @@ -79,11 +83,12 @@ typedef struct mtx_t tCVMtx; mtx_t tflagsMtx; cnd_t threadCV; + UDPC_Deque *connected; } UDPC_Context; -UDPC_Context* UDPC_init(uint16_t listenPort); +UDPC_Context* UDPC_init(uint16_t listenPort, int isClient); -UDPC_Context* UDPC_init_threaded_update(uint16_t listenPort); +UDPC_Context* UDPC_init_threaded_update(uint16_t listenPort, int isClient); void UDPC_destroy(UDPC_Context *ctx);