From d027b1703a3ecf569fed3cc2a4d8257b6a2c8f78 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 29 Aug 2019 11:07:24 +0900 Subject: [PATCH] Add client_initiate_connection fn, some fixes --- cpp_impl/src/UDPC_Defines.hpp | 1 + cpp_impl/src/UDPConnection.cpp | 33 +++++++++++++++++++++++++++++++-- cpp_impl/src/UDPConnection.h | 7 +++++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/cpp_impl/src/UDPC_Defines.hpp b/cpp_impl/src/UDPC_Defines.hpp index 180e8b0..0bf1c22 100644 --- a/cpp_impl/src/UDPC_Defines.hpp +++ b/cpp_impl/src/UDPC_Defines.hpp @@ -29,6 +29,7 @@ namespace UDPC { static const auto ONE_SECOND = std::chrono::seconds(1); static const auto TEN_SECONDS = std::chrono::seconds(10); +static const auto THIRTY_SECONDS = std::chrono::seconds(30); static uint32_t LOCAL_ADDR = 0; static const auto INIT_PKT_INTERVAL_DT = std::chrono::seconds(5); diff --git a/cpp_impl/src/UDPConnection.cpp b/cpp_impl/src/UDPConnection.cpp index a32879f..0d5ced3 100644 --- a/cpp_impl/src/UDPConnection.cpp +++ b/cpp_impl/src/UDPConnection.cpp @@ -50,8 +50,12 @@ std::size_t UDPC::ConnectionIdentifier::Hasher::operator()(const ConnectionIdent UDPC::ConnectionData::ConnectionData() : flags(), +id(0), +lseq(0), +rseq(0), +ack(0xFFFFFFFF), timer(std::chrono::steady_clock::duration::zero()), -toggleT(std::chrono::seconds(30)), +toggleT(UDPC::THIRTY_SECONDS), toggleTimer(std::chrono::steady_clock::duration::zero()), toggledTimer(std::chrono::steady_clock::duration::zero()), sentPkts(), @@ -68,10 +72,12 @@ rtt(std::chrono::steady_clock::duration::zero()) UDPC::ConnectionData::ConnectionData(bool isServer, Context *ctx) : UDPC::ConnectionData::ConnectionData() { + flags.set(3); if(isServer) { - flags.set(3); id = UDPC::generateConnectionID(*ctx); flags.set(4); + } else { + lseq = 1; } } @@ -770,6 +776,29 @@ void UDPC_update(void *ctx) { } } +void UDPC_client_initiate_connection(void *ctx, uint32_t addr, uint16_t port) { + UDPC::Context *c = UDPC::verifyContext(ctx); + if(!c || !c->flags.test(1)) { + return; + } + + UDPC::ConnectionData newCon(false, c); + UDPC::ConnectionIdentifier identifier(addr, port); + + // TODO make thread safe by using mutex + c->conMap.insert(std::make_pair(identifier, std::move(newCon))); + auto addrConIter = c->addrConMap.find(addr); + if(addrConIter == c->addrConMap.end()) { + auto insertResult = c->addrConMap.insert(std::make_pair( + addr, + std::unordered_set{} + )); + assert(insertResult.second); + addrConIter = insertResult.first; + } + addrConIter->second.insert(identifier); +} + int UDPC_get_queue_send_available(void *ctx, uint32_t addr, uint16_t port) { UDPC::Context *c = UDPC::verifyContext(ctx); if(!c) { diff --git a/cpp_impl/src/UDPConnection.h b/cpp_impl/src/UDPConnection.h index 4dc10b2..55c37e3 100644 --- a/cpp_impl/src/UDPConnection.h +++ b/cpp_impl/src/UDPConnection.h @@ -73,10 +73,13 @@ void UDPC_destroy(void *ctx); void UDPC_update(void *ctx); -/// port must be in native byte order, addr must be in network byte order (big-endian) +/// addr must be in network byte order (big-endian), port must be in native byte order +void UDPC_client_initiate_connection(void *ctx, uint32_t addr, uint16_t port); + +/// addr must be in network byte order (big-endian), port must be in native byte order int UDPC_get_queue_send_available(void *ctx, uint32_t addr, uint16_t port); -/// destPort must be in native byte order, destAddr must be in network byte order (big-endian) +/// destAddr must be in network byte order (big-endian), destPort must be in native byte order void UDPC_queue_send(void *ctx, uint32_t destAddr, uint16_t destPort, uint32_t isChecked, void *data, uint32_t size);