From bf27c328a67e240544ac8c12f551841fff7ceff1 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sun, 18 Aug 2019 18:34:45 +0900 Subject: [PATCH] Impl sending queued packet TODO receiving packet --- cpp_impl/src/UDPConnection.cpp | 58 +++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/cpp_impl/src/UDPConnection.cpp b/cpp_impl/src/UDPConnection.cpp index 1a1dec9..2407077 100644 --- a/cpp_impl/src/UDPConnection.cpp +++ b/cpp_impl/src/UDPConnection.cpp @@ -365,10 +365,66 @@ void UDPC_update(void *ctx) { iter->second.sendPkts.pop(); } std::unique_ptr buf = std::make_unique(20 + pInfo.dataSize); - // TODO prepare and send packet + UDPC::preparePacket( + buf.get(), + c->protocolID, + iter->second.id, + iter->second.rseq, + iter->second.ack, + &iter->second.lseq, + pInfo.flags & 0xC); + std::memcpy(buf.get() + 20, pInfo.data, pInfo.dataSize); + + struct sockaddr_in destinationInfo; + destinationInfo.sin_family = AF_INET; + destinationInfo.sin_addr.s_addr = iter->first; + destinationInfo.sin_port = htons(iter->second.port); + long int sentBytes = sendto( + c->socketHandle, + buf.get(), + pInfo.dataSize + 20, + 0, + (struct sockaddr*) &destinationInfo, + sizeof(struct sockaddr_in)); + if(sentBytes != 20 + pInfo.dataSize) { + // TODO log fail send packet + } + + if((pInfo.flags & 0x4) == 0) { + // is check-received, store data in case packet gets lost + UDPC_PacketInfo sentPInfo; + std::memcpy(sentPInfo.data, buf.get(), 20 + pInfo.dataSize); + sentPInfo.flags = 0; + sentPInfo.dataSize = 20 + pInfo.dataSize; + sentPInfo.sender = UDPC::LOCAL_ADDR; + sentPInfo.receiver = iter->first; + sentPInfo.senderPort = c->socketInfo.sin_port; + sentPInfo.receiverPort = iter->second.port; + + iter->second.sentPkts.push_back(std::move(pInfo)); + while(iter->second.sentPkts.size() > UDPC_SENT_PKTS_MAX_SIZE) { + iter->second.sentPkts.pop_front(); + } + } else { + // is not check-received, no data stored but other data is kept + UDPC_PacketInfo sentPInfo; + sentPInfo.flags = 0x4; + sentPInfo.dataSize = 0; + sentPInfo.sender = UDPC::LOCAL_ADDR; + sentPInfo.receiver = iter->first; + sentPInfo.senderPort = c->socketInfo.sin_port; + sentPInfo.receiverPort = iter->second.port; + + iter->second.sentPkts.push_back(std::move(pInfo)); + while(iter->second.sentPkts.size() > UDPC_SENT_PKTS_MAX_SIZE) { + iter->second.sentPkts.pop_front(); + } + } } } + // TODO receive packet + // TODO impl }