Some fixes

Fix id of sent packet stored in network byte order instead of native
order.

Fix failing to send packet not stopping storing sent packet data.
This commit is contained in:
Stephen Seo 2019-08-28 15:54:18 +09:00
parent 35bc629b1b
commit 8dba751929
2 changed files with 13 additions and 6 deletions

View file

@ -145,9 +145,9 @@ struct Context {
struct sockaddr_in socketInfo; struct sockaddr_in socketInfo;
std::chrono::steady_clock::time_point lastUpdated; std::chrono::steady_clock::time_point lastUpdated;
// ipv4 address and port to ConnectionData // ipv4 address and port (as ConnectionIdentifier) to ConnectionData
std::unordered_map<ConnectionIdentifier, ConnectionData, ConnectionIdentifier::Hasher> conMap; std::unordered_map<ConnectionIdentifier, ConnectionData, ConnectionIdentifier::Hasher> conMap;
// id to ipv4 address // id to ipv4 address and port (as ConnectionIdentifier)
std::unordered_map<uint32_t, ConnectionIdentifier> idMap; std::unordered_map<uint32_t, ConnectionIdentifier> idMap;
std::default_random_engine rng_engine; std::default_random_engine rng_engine;

View file

@ -78,8 +78,11 @@ UDPC::ConnectionData::ConnectionData()
void UDPC::ConnectionData::cleanupSentPkts() { void UDPC::ConnectionData::cleanupSentPkts() {
uint32_t id; uint32_t id;
while(sentPkts.size() > UDPC_SENT_PKTS_MAX_SIZE) { while(sentPkts.size() > UDPC_SENT_PKTS_MAX_SIZE) {
id = ntohl(*((uint32_t*)(sentPkts.front().data + 8))); id = *((uint32_t*)(sentPkts.front().data + 8));
sentInfoMap.erase(id); auto iter = sentInfoMap.find(id);
assert(iter != sentInfoMap.end()
&& "Sent packet must have correspoding entry in sentInfoMap");
sentInfoMap.erase(iter);
sentPkts.pop_front(); sentPkts.pop_front();
} }
} }
@ -367,6 +370,7 @@ void UDPC_update(void *ctx) {
sizeof(struct sockaddr_in)); sizeof(struct sockaddr_in));
if(sentBytes != 20) { if(sentBytes != 20) {
// TODO log fail of sending connection-initiate-packet // TODO log fail of sending connection-initiate-packet
continue;
} }
} else { } else {
// is server, initiate connection to client // is server, initiate connection to client
@ -396,6 +400,7 @@ void UDPC_update(void *ctx) {
sizeof(struct sockaddr_in)); sizeof(struct sockaddr_in));
if(sentBytes != 20) { if(sentBytes != 20) {
// TODO log fail send init connection packet as server // TODO log fail send init connection packet as server
continue;
} }
} }
continue; continue;
@ -432,6 +437,7 @@ void UDPC_update(void *ctx) {
sizeof(struct sockaddr_in)); sizeof(struct sockaddr_in));
if(sentBytes != 20) { if(sentBytes != 20) {
// TODO log fail send heartbeat packet // TODO log fail send heartbeat packet
continue;
} }
UDPC_PacketInfo pInfo{{0}, 0, 0, 0, 0, 0, 0}; UDPC_PacketInfo pInfo{{0}, 0, 0, 0, 0, 0, 0};
@ -439,7 +445,7 @@ void UDPC_update(void *ctx) {
pInfo.receiver = iter->first.getAddr(); pInfo.receiver = iter->first.getAddr();
pInfo.senderPort = c->socketInfo.sin_port; pInfo.senderPort = c->socketInfo.sin_port;
pInfo.receiverPort = iter->second.port; pInfo.receiverPort = iter->second.port;
*((uint32_t*)(pInfo.data + 8)) = htonl(iter->second.lseq - 1); *((uint32_t*)(pInfo.data + 8)) = iter->second.lseq - 1;
iter->second.sentPkts.push_back(std::move(pInfo)); iter->second.sentPkts.push_back(std::move(pInfo));
iter->second.cleanupSentPkts(); iter->second.cleanupSentPkts();
@ -485,6 +491,7 @@ void UDPC_update(void *ctx) {
sizeof(struct sockaddr_in)); sizeof(struct sockaddr_in));
if(sentBytes != 20 + pInfo.dataSize) { if(sentBytes != 20 + pInfo.dataSize) {
// TODO log fail send packet // TODO log fail send packet
continue;
} }
if((pInfo.flags & 0x4) == 0) { if((pInfo.flags & 0x4) == 0) {
@ -509,7 +516,7 @@ void UDPC_update(void *ctx) {
sentPInfo.receiver = iter->first.getAddr(); sentPInfo.receiver = iter->first.getAddr();
sentPInfo.senderPort = c->socketInfo.sin_port; sentPInfo.senderPort = c->socketInfo.sin_port;
sentPInfo.receiverPort = iter->second.port; sentPInfo.receiverPort = iter->second.port;
*((uint32_t*)(sentPInfo.data + 8)) = htonl(iter->second.lseq - 1); *((uint32_t*)(sentPInfo.data + 8)) = iter->second.lseq - 1;
iter->second.sentPkts.push_back(std::move(pInfo)); iter->second.sentPkts.push_back(std::move(pInfo));
iter->second.cleanupSentPkts(); iter->second.cleanupSentPkts();