From f5cfbe41d5c9ca9979ad0a8952b968acdcac0ce0 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 27 Aug 2019 14:06:27 +0900 Subject: [PATCH] Fix warnings, some impl, still WIP --- cpp_impl/src/UDPC_Defines.hpp | 1 + cpp_impl/src/UDPConnection.cpp | 47 ++++++++++++++++++++++++++++------ cpp_impl/src/UDPConnection.h | 4 +-- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/cpp_impl/src/UDPC_Defines.hpp b/cpp_impl/src/UDPC_Defines.hpp index 2950d11..d43abd1 100644 --- a/cpp_impl/src/UDPC_Defines.hpp +++ b/cpp_impl/src/UDPC_Defines.hpp @@ -95,6 +95,7 @@ struct Context { Context(bool isThreaded); uint_fast32_t _contextIdentifier; + char recvBuf[UDPC_PACKET_MAX_SIZE]; /* * 0 - is threaded diff --git a/cpp_impl/src/UDPConnection.cpp b/cpp_impl/src/UDPConnection.cpp index d7f8590..7a2611b 100644 --- a/cpp_impl/src/UDPConnection.cpp +++ b/cpp_impl/src/UDPConnection.cpp @@ -147,6 +147,7 @@ float UDPC::durationToFSec( void *UDPC_init(uint16_t listenPort, uint32_t listenAddr, int isClient) { UDPC::Context *ctx = new UDPC::Context(false); + ctx->flags.set(1, isClient); // create socket ctx->socketHandle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); @@ -223,7 +224,6 @@ void UDPC_update(void *ctx) { return; } - const auto prevNow = std::move(c->lastUpdated); const auto now = std::chrono::steady_clock::now(); c->lastUpdated = now; @@ -432,7 +432,7 @@ void UDPC_update(void *ctx) { iter->second.rseq, iter->second.ack, &iter->second.lseq, - pInfo.flags & 0xC); + (pInfo.flags & 0x4) | (isResending ? 0x8 : 0)); std::memcpy(buf.get() + 20, pInfo.data, pInfo.dataSize); struct sockaddr_in destinationInfo; @@ -703,17 +703,42 @@ int UDPC_get_queue_send_available(void *ctx, uint32_t addr) { if(!c) { return 0; } - // TODO impl - return 0; + + auto iter = c->conMap.find(addr); + if(iter != c->conMap.end()) { + return iter->second.sendPkts.capacity() - iter->second.sendPkts.size(); + } else { + return 0; + } } -void UDPC_queue_send(void *ctx, uint32_t destAddr, uint16_t destPort, +void UDPC_queue_send(void *ctx, uint32_t destAddr, uint32_t isChecked, void *data, uint32_t size) { + if(size == 0 || !data) { + return; + } + UDPC::Context *c = UDPC::verifyContext(ctx); if(!c) { return; } - // TODO impl + + auto iter = c->conMap.find(destAddr); + if(iter == c->conMap.end()) { + // TODO log failed to add packet to queue; unknown recipient + return; + } + + UDPC_PacketInfo sendInfo; + std::memcpy(sendInfo.data, data, size); + sendInfo.dataSize = size; + sendInfo.sender = UDPC::LOCAL_ADDR; + sendInfo.senderPort = c->socketInfo.sin_port; + sendInfo.receiver = destAddr; + sendInfo.receiverPort = iter->second.port; + sendInfo.flags = (isChecked ? 0x0 : 0x4); + + iter->second.sendPkts.push(sendInfo); } int UDPC_set_accept_new_connections(void *ctx, int isAccepting) { @@ -724,12 +749,18 @@ int UDPC_set_accept_new_connections(void *ctx, int isAccepting) { return c->isAcceptNewConnections.exchange(isAccepting == 0 ? false : true); } -int UDPC_drop_connection(void *ctx, uint32_t addr, uint16_t port) { +int UDPC_drop_connection(void *ctx, uint32_t addr) { UDPC::Context *c = UDPC::verifyContext(ctx); if(!c) { return 0; } - // TODO impl + + auto iter = c->conMap.find(addr); + if(iter != c->conMap.end()) { + c->conMap.erase(iter); + return 1; + } + return 0; } diff --git a/cpp_impl/src/UDPConnection.h b/cpp_impl/src/UDPConnection.h index bbd5160..94a2383 100644 --- a/cpp_impl/src/UDPConnection.h +++ b/cpp_impl/src/UDPConnection.h @@ -73,12 +73,12 @@ void UDPC_update(void *ctx); int UDPC_get_queue_send_available(void *ctx, uint32_t addr); -void UDPC_queue_send(void *ctx, uint32_t destAddr, uint16_t destPort, +void UDPC_queue_send(void *ctx, uint32_t destAddr, uint32_t isChecked, void *data, uint32_t size); int UDPC_set_accept_new_connections(void *ctx, int isAccepting); -int UDPC_drop_connection(void *ctx, uint32_t addr, uint16_t port); +int UDPC_drop_connection(void *ctx, uint32_t addr); uint32_t UDPC_set_protocol_id(void *ctx, uint32_t id);