diff --git a/src/UDPC_Defines.hpp b/src/UDPC_Defines.hpp index f5d0c41..bba5333 100644 --- a/src/UDPC_Defines.hpp +++ b/src/UDPC_Defines.hpp @@ -263,6 +263,8 @@ uint32_t generateConnectionID(Context &ctx); float durationToFSec(const std::chrono::steady_clock::duration& duration); +uint16_t durationToMS(const std::chrono::steady_clock::duration& duration); + float timePointsToFSec( const std::chrono::steady_clock::time_point& older, const std::chrono::steady_clock::time_point& newer); diff --git a/src/UDPConnection.cpp b/src/UDPConnection.cpp index 79788c9..9b5efd7 100644 --- a/src/UDPConnection.cpp +++ b/src/UDPConnection.cpp @@ -1657,11 +1657,12 @@ void UDPC::Context::update_impl() { "Received packet is out of order"); } - if((pktType == 0 && bytes > (int)UDPC_NSFULL_HEADER_SIZE) - | (pktType == 1 && bytes > (int)UDPC_LSFULL_HEADER_SIZE)) { + if(pktType == 0 && bytes > (int)UDPC_NSFULL_HEADER_SIZE) { UDPC_PacketInfo recPktInfo = UDPC::get_empty_pinfo(); - std::memcpy(recPktInfo.data, recvBuf, bytes); - recPktInfo.dataSize = bytes; + std::memcpy(recPktInfo.data + UDPC_NSFULL_HEADER_SIZE, + recvBuf, + bytes - UDPC_NSFULL_HEADER_SIZE); + recPktInfo.dataSize = bytes - UDPC_NSFULL_HEADER_SIZE; recPktInfo.flags = (isConnect ? 0x1 : 0) | (isPing ? 0x2 : 0) @@ -1671,6 +1672,25 @@ void UDPC::Context::update_impl() { recPktInfo.receiver.addr = in6addr_loopback; recPktInfo.sender.port = ntohs(receivedData.sin6_port); recPktInfo.receiver.port = ntohs(socketInfo.sin6_port); + recPktInfo.rtt = durationToMS(iter->second.rtt); + + receivedPkts.push(recPktInfo); + } else if(pktType == 1 && bytes > (int)UDPC_LSFULL_HEADER_SIZE) { + UDPC_PacketInfo recPktInfo = UDPC::get_empty_pinfo(); + std::memcpy(recPktInfo.data + UDPC_LSFULL_HEADER_SIZE, + recvBuf, + bytes - UDPC_LSFULL_HEADER_SIZE); + recPktInfo.dataSize = bytes - UDPC_LSFULL_HEADER_SIZE; + recPktInfo.flags = + (isConnect ? 0x1 : 0) + | (isPing ? 0x2 : 0) + | (isNotRecChecked ? 0x4 : 0) + | (isResending ? 0x8 : 0); + recPktInfo.sender.addr = receivedData.sin6_addr; + recPktInfo.receiver.addr = in6addr_loopback; + recPktInfo.sender.port = ntohs(receivedData.sin6_port); + recPktInfo.receiver.port = ntohs(socketInfo.sin6_port); + recPktInfo.rtt = durationToMS(iter->second.rtt); receivedPkts.push(recPktInfo); } else { @@ -1753,6 +1773,10 @@ float UDPC::durationToFSec(const std::chrono::steady_clock::duration& duration) / (float)std::decay_t::period::den; } +uint16_t UDPC::durationToMS(const std::chrono::steady_clock::duration& duration) { + return std::chrono::duration_cast(duration).count(); +} + float UDPC::timePointsToFSec( const std::chrono::steady_clock::time_point& older, const std::chrono::steady_clock::time_point& newer) { @@ -1766,6 +1790,7 @@ UDPC_PacketInfo UDPC::get_empty_pinfo() { {0}, // data (array) 0, // flags 0, // dataSize + 0, // rtt { // sender {0}, // ipv6 addr 0, // scope_id diff --git a/src/UDPConnection.h b/src/UDPConnection.h index d2066e1..fdda87a 100644 --- a/src/UDPConnection.h +++ b/src/UDPConnection.h @@ -168,6 +168,7 @@ typedef struct { * was received. */ uint16_t dataSize; + uint16_t rtt; /// The \p UDPC_ConnectionId of the sender UDPC_ConnectionId sender; /// The \p UDPC_ConnectionId of the receiver diff --git a/src/test/UDPC_NetworkTest.c b/src/test/UDPC_NetworkTest.c index 4add21a..a982482 100644 --- a/src/test/UDPC_NetworkTest.c +++ b/src/test/UDPC_NetworkTest.c @@ -271,11 +271,13 @@ int main(int argc, char **argv) { } do { received = UDPC_get_received(context, &size); +// printf("Received data size = %u\n", received.dataSize); if(received.dataSize == sizeof(unsigned int)) { if((received.flags & 0x8) != 0) { temp2 = ntohl(*((unsigned int*)received.data)); printf("Got out of order, data = %u\n", temp2); } +// printf("Got rtt %u\n", received.rtt); } } while (size > 0); }