Some work on UDPC

This commit is contained in:
Stephen Seo 2019-01-30 15:46:52 +09:00
parent bfc704d429
commit ea8226cdf2
2 changed files with 20 additions and 7 deletions

View file

@ -2,12 +2,13 @@
#include <stdlib.h> #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)); UDPC_Context *context = malloc(sizeof(UDPC_Context));
context->error = UDPC_SUCCESS; context->error = UDPC_SUCCESS;
context->flags = 0; context->flags = 0;
context->threadFlags = 0; context->threadFlags = 0;
if(isClient != 0) context->flags |= 0x2;
// create socket // create socket
context->socketHandle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); context->socketHandle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
@ -52,15 +53,21 @@ UDPC_Context* UDPC_init(uint16_t listenPort)
CleanupSocket(context->socketHandle); CleanupSocket(context->socketHandle);
context->socketHandle = 0; context->socketHandle = 0;
fprintf(stderr, "Failed to set non-blocking on socket\n"); fprintf(stderr, "Failed to set non-blocking on socket\n");
#if UDPC_PLATFORM == UDPC_PLATFORM_UNKNOWN
fprintf(stderr, "(Unknown platform)\n");
#endif
return context; return context;
} }
context->connected = UDPC_Deque_init(sizeof(UDPC_INTERNAL_ConnectionData)
* (isClient != 0 ? 1 : UDPC_CD_AMOUNT));
return context; 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); context->error = mtx_init(&context->tCVMtx, mtx_timed);
if(context->error != thrd_success) if(context->error != thrd_success)
@ -118,6 +125,7 @@ UDPC_Context* UDPC_init_threaded_update(uint16_t listenPort)
void UDPC_destroy(UDPC_Context *ctx) void UDPC_destroy(UDPC_Context *ctx)
{ {
CleanupSocket(ctx->socketHandle); CleanupSocket(ctx->socketHandle);
UDPC_Deque_destroy(ctx->connected);
if((ctx->flags & 0x1) != 0) if((ctx->flags & 0x1) != 0)
{ {

View file

@ -7,6 +7,7 @@
#include <stdint.h> #include <stdint.h>
#include "UDPC_Defines.h" #include "UDPC_Defines.h"
#include "UDPC_Deque.h"
#if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS #if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
#include <winsock2.h> #include <winsock2.h>
@ -21,6 +22,8 @@
#define CleanupSocket(x) close(x) #define CleanupSocket(x) close(x)
#endif #endif
#define UDPC_CD_AMOUNT 32
// This struct should not be used outside of this library // This struct should not be used outside of this library
typedef struct typedef struct
{ {
@ -54,8 +57,8 @@ typedef struct
float toggleTimer; float toggleTimer;
float toggledTimer; float toggledTimer;
uint16_t port; uint16_t port;
UDPC_INTERNAL_PacketInfo *sentPkts; UDPC_Deque *sentPkts;
UDPC_INTERNAL_PacketInfo *sendPktQueue; UDPC_Deque *sendPktQueue;
struct timespec received; struct timespec received;
struct timespec sent; struct timespec sent;
struct timespec rtt; struct timespec rtt;
@ -66,6 +69,7 @@ typedef struct
{ {
/* /*
* 0x1 - is threaded * 0x1 - is threaded
* 0x2 - is client
*/ */
uint32_t flags; uint32_t flags;
/* /*
@ -79,11 +83,12 @@ typedef struct
mtx_t tCVMtx; mtx_t tCVMtx;
mtx_t tflagsMtx; mtx_t tflagsMtx;
cnd_t threadCV; cnd_t threadCV;
UDPC_Deque *connected;
} UDPC_Context; } 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); void UDPC_destroy(UDPC_Context *ctx);