]> git.seodisparate.com - UDPConnection/commitdiff
Some work on UDPC
authorStephen Seo <seo.disparate@gmail.com>
Wed, 30 Jan 2019 06:46:52 +0000 (15:46 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 30 Jan 2019 06:46:52 +0000 (15:46 +0900)
src/UDPConnection.c
src/UDPConnection.h

index 3bcf2f5ae3fa458c50ec113ba3797f5281e51885..a763e456a4acf3dc47497fe9ef8159d1e39c142e 100644 (file)
@@ -2,12 +2,13 @@
 
 #include <stdlib.h>
 
-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)
     {
index e28f8d1d51b42437183cbd8be5f3057a53772de9..27f459e8c563dcb959d921b9aa2322387123fac4 100644 (file)
@@ -7,6 +7,7 @@
 #include <stdint.h>
 
 #include "UDPC_Defines.h"
+#include "UDPC_Deque.h"
 
 #if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
   #include <winsock2.h>
@@ -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);