From 884a262f935d143a585c690baf0d1bf19dbf7bd6 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 4 Feb 2019 15:33:44 +0900 Subject: [PATCH] Fix UDPC_destroy, WIP impl of recieving packets --- src/UDPC_Defines.h | 2 ++ src/UDPConnection.c | 70 ++++++++++++++++++++++++++++++++++++++++++++- src/UDPConnection.h | 2 ++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/UDPC_Defines.h b/src/UDPC_Defines.h index 63a6e5a..77088e7 100644 --- a/src/UDPC_Defines.h +++ b/src/UDPC_Defines.h @@ -47,4 +47,6 @@ static const char *UDPC_ERR_THREADFAIL_STR = "Failed to create thread"; #define UDPC_SENT_PKTS_MAX_SIZE 34 #define UDPC_SENT_PKTS_ALLOC_SIZE 35 +#define UDPC_PACKET_MAX_SIZE 8192 + #endif diff --git a/src/UDPConnection.c b/src/UDPConnection.c index c5fae0e..bbd5478 100644 --- a/src/UDPConnection.c +++ b/src/UDPConnection.c @@ -133,6 +133,31 @@ UDPC_Context* UDPC_init_threaded_update(uint16_t listenPort, int isClient) void UDPC_destroy(UDPC_Context *ctx) { CleanupSocket(ctx->socketHandle); + for(int x = 0; x * sizeof(UDPC_INTERNAL_ConnectionData) < ctx->connected->size; ++x) + { + UDPC_INTERNAL_ConnectionData *cd = UDPC_Deque_index_ptr( + ctx->connected, sizeof(UDPC_INTERNAL_ConnectionData), x); + for(int y = 0; y * sizeof(UDPC_INTERNAL_PacketInfo) < cd->sentPkts->size; ++y) + { + UDPC_INTERNAL_PacketInfo *pinfo = UDPC_Deque_index_ptr( + cd->sentPkts, sizeof(UDPC_INTERNAL_PacketInfo), y); + if(pinfo->data) + { + free(pinfo->data); + } + } + UDPC_Deque_destroy(cd->sentPkts); + for(int y = 0; y * sizeof(UDPC_INTERNAL_PacketInfo) < cd->sendPktQueue->size; ++y) + { + UDPC_INTERNAL_PacketInfo *pinfo = UDPC_Deque_index_ptr( + cd->sendPktQueue, sizeof(UDPC_INTERNAL_PacketInfo), y); + if(pinfo->data) + { + free(pinfo->data); + } + } + UDPC_Deque_destroy(cd->sendPktQueue); + } UDPC_Deque_destroy(ctx->connected); if((ctx->flags & 0x1) != 0) @@ -424,7 +449,50 @@ void UDPC_update(UDPC_Context *ctx) } // receive packet - // TODO +#if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS + typedef int socklen_t; +#endif + struct sockaddr_in receivedData; + socklen_t receivedDataSize = sizeof(receivedData); + int bytes = recvfrom( + ctx->socketHandle, + ctx->recvBuf, + UDPC_PACKET_MAX_SIZE, + 0, + (struct sockaddr*) &receivedData, + &receivedDataSize); + if(bytes < 20) + { + UDPC_INTERNAL_log(ctx, 2, "Got invalid packet from %s port %d (too small)", + UDPC_INTERNAL_atostr(ctx, receivedData.sin_addr.s_addr), + ntohs(receivedData.sin_port)); + return; + } + + uint32_t temp = ntohl(*((uint32_t*)ctx->recvBuf)); + if(temp != UDPC_PKT_PROTOCOL_ID) + { + UDPC_INTERNAL_log(ctx, 2, "Got invalid packet from %s port %d (invalid protocol id)", + UDPC_INTERNAL_atostr(ctx, receivedData.sin_addr.s_addr), + ntohs(receivedData.sin_port)); + return; + } + + uint32_t conID = ntohl(*((uint32_t*)(ctx->recvBuf + 4))); + uint32_t seqID = ntohl(*((uint32_t*)(ctx->recvBuf + 8))); + uint32_t rseq = ntohl(*((uint32_t*)(ctx->recvBuf + 12))); + uint32_t ack = ntohl(*((uint32_t*)(ctx->recvBuf + 16))); + + int isConnect = conID & UDPC_ID_CONNECT; + int isPing = conID & UDPC_ID_PING; + int isNotRecvCheck = conID & UDPC_ID_NO_REC_CHK; + int isResent = conID & UDPC_ID_RESENDING; + conID &= 0x0FFFFFFF; + + if(isConnect != 0 && (ctx->flags & 0x40) != 0) + { + // TODO after impl hash map of connected + } } float UDPC_ts_diff_to_seconds(struct timespec *ts0, struct timespec *ts1) diff --git a/src/UDPConnection.h b/src/UDPConnection.h index ee32a3d..0ed0646 100644 --- a/src/UDPConnection.h +++ b/src/UDPConnection.h @@ -78,6 +78,7 @@ typedef struct * 0x8 - log warnings * 0x10 - log info * 0x20 - log verbose + * 0x40 - accept new connections */ uint32_t flags; /* @@ -94,6 +95,7 @@ typedef struct UDPC_Deque *connected; struct timespec lastUpdated; char atostrBuf[UDPC_ATOSTR_BUF_SIZE]; + char recvBuf[UDPC_PACKET_MAX_SIZE]; } UDPC_Context; UDPC_Context* UDPC_init(uint16_t listenPort, int isClient);