]> git.seodisparate.com - UDPConnection/commitdiff
More impl, fix move for TSQueue
authorStephen Seo <seo.disparate@gmail.com>
Thu, 22 Aug 2019 07:08:05 +0000 (16:08 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 22 Aug 2019 07:08:05 +0000 (16:08 +0900)
cpp_impl/src/TSQueue.hpp
cpp_impl/src/UDPC_Defines.hpp
cpp_impl/src/UDPConnection.cpp

index bef1fa54a65250a1f179358b675ee0f76785a587..8b7d65c9c214bb34a2ef9398234c7f79f2f41588 100644 (file)
@@ -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 <typename T>
+TSQueue<T>::TSQueue(TSQueue &&other) :
+TSQueue<T>::TSQueue(other.rb.getCapacity())
+{
+    std::lock_guard<std::mutex> lock(other.mutex);
+    for(unsigned int i = 0; i < other.rb.getSize(); ++i) {
+        rb.push(other.rb[i]);
+    }
+}
+
+template <typename T>
+TSQueue<T>& TSQueue<T>::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 <typename T>
 TSQueue<T>::~TSQueue()
 {}
index 5fc3a1210398e2d3221dd0eccebaae827b663056..3a70e866cc2b16a1b94f41bf00ba8d88172b2e96 100644 (file)
@@ -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<UDPC_PacketInfo> sentPkts;
     TSQueue<UDPC_PacketInfo> sendPkts;
     TSQueue<UDPC_PacketInfo> priorityPkts;
index 8cfd391a634dec6f9090058c57f58ab69359139a..c1731c4fab948266c1555aa447636310161ee8ea 100644 (file)
@@ -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
 }