diff --git a/cpp_impl/src/TSQueue.hpp b/cpp_impl/src/TSQueue.hpp index bef1fa5..8b7d65c 100644 --- a/cpp_impl/src/TSQueue.hpp +++ b/cpp_impl/src/TSQueue.hpp @@ -18,9 +18,9 @@ class TSQueue { // disable copy TSQueue(const TSQueue &other) = delete; TSQueue &operator=(const TSQueue &other) = delete; - // disable move - TSQueue(TSQueue &&other) = delete; - TSQueue &operator=(TSQueue &&other) = delete; + // enable move + TSQueue(TSQueue &&other); + TSQueue &operator=(TSQueue &&other); bool push(const T &data); T top(); @@ -43,6 +43,27 @@ rb(capacity) rb.setResizePolicy(false); } +template +TSQueue::TSQueue(TSQueue &&other) : +TSQueue::TSQueue(other.rb.getCapacity()) +{ + std::lock_guard lock(other.mutex); + for(unsigned int i = 0; i < other.rb.getSize(); ++i) { + rb.push(other.rb[i]); + } +} + +template +TSQueue& TSQueue::operator =(TSQueue &&other) +{ + std::scoped_lock lock(other.mutex, mutex); + rb.resize(0); + rb.changeCapacity(other.rb.getCapacity()); + for(unsigned int i = 0; i < other.rb.getSize(); ++i) { + rb.push(other.rb[i]); + } +} + template TSQueue::~TSQueue() {} diff --git a/cpp_impl/src/UDPC_Defines.hpp b/cpp_impl/src/UDPC_Defines.hpp index 5fc3a12..3a70e86 100644 --- a/cpp_impl/src/UDPC_Defines.hpp +++ b/cpp_impl/src/UDPC_Defines.hpp @@ -37,6 +37,14 @@ struct ConnectionData { ConnectionData(); ConnectionData(bool isServer, Context *ctx); + // copy + ConnectionData(const ConnectionData& other) = delete; + ConnectionData& operator=(const ConnectionData& other) = delete; + + // move + ConnectionData(ConnectionData&& other) = default; + ConnectionData& operator=(ConnectionData&& other) = default; + /* * 0 - trigger send * 1 - is good mode @@ -54,7 +62,7 @@ struct ConnectionData { float toggleTimer; float toggledTimer; uint32_t addr; // in network order - uint16_t port; + uint16_t port; // in native order std::deque sentPkts; TSQueue sendPkts; TSQueue priorityPkts; diff --git a/cpp_impl/src/UDPConnection.cpp b/cpp_impl/src/UDPConnection.cpp index 8cfd391..c1731c4 100644 --- a/cpp_impl/src/UDPConnection.cpp +++ b/cpp_impl/src/UDPConnection.cpp @@ -9,11 +9,16 @@ UDPC::ConnectionData::ConnectionData() : flags(), +timer(0.0f), +toggleT(30.0f), +toggleTimer(0.0f), +toggledTimer(0.0f), sentPkts(), sendPkts(UDPC_QUEUED_PKTS_MAX_SIZE), priorityPkts(UDPC_QUEUED_PKTS_MAX_SIZE), received(std::chrono::steady_clock::now()), -sent(std::chrono::steady_clock::now()) +sent(std::chrono::steady_clock::now()), +rtt(0.0f) { } @@ -504,11 +509,38 @@ void UDPC_update(void *ctx) { // is receiving as server, connection did not already exist // TODO log establishing connection with client peer UDPC::ConnectionData newConnection(true, c); - // TODO update idMap and conMap with new CD - // TODO impl establish connection with client peer as server + newConnection.addr = receivedData.sin_addr.s_addr; + newConnection.port = ntohs(receivedData.sin_port); + + c->idMap.insert(std::make_pair(newConnection.id, newConnection.addr)); + c->conMap.insert(std::make_pair(newConnection.addr, std::move(newConnection))); + // TODO trigger event server established connection with client + } else if (c->flags.test(1)) { + // is client + auto iter = c->conMap.find(receivedData.sin_addr.s_addr); + if(iter == c->conMap.end() || !iter->second.flags.test(3)) { + return; + } + iter->second.flags.reset(3); + iter->second.id = conID; + iter->second.flags.set(4); + // TODO trigger event client established connection with server } + return; } + auto iter = c->conMap.find(receivedData.sin_addr.s_addr); + if(iter == c->conMap.end() || iter->second.flags.test(3) + || !iter->second.flags.test(4) || iter->second.id != conID) { + return; + } + else if(isPing) { + iter->second.flags.set(0); + } + + // packet is valid + // TODO log received valid packet + // TODO impl }