Check msgid on receive and update caches

This commit is contained in:
Juhani Krekelä 2019-07-10 14:00:44 +03:00
parent 2d43d3273a
commit ab73580c23
1 changed files with 36 additions and 1 deletions

View File

@ -446,7 +446,42 @@ void handle_message(const unsigned char source_mac[6], const unsigned char *data
// TODO: Check that the message is valid utf-8 with newline as the only control char
// TODO: Check whether we've received this message already and update msgid cache
// See whether we've already received this message and update the next msgid if so
ssize_t cache_index = msgid_cache_lookup(source_mac);
if (cache_index == -1) {
// No cache entry -> add an empty one
cache_index = msgid_cache_add(source_mac);
}
if (!msgid_cache[cache_index].know_receive) {
// We have a cache entry, but no idea what next message to receive
// Assume this is not a repeat transmission, and accept the message
// The easiest way to do this is to set this message's msgid as the
// one we're looking to receive
msgid_cache[cache_index].next_receive = msgid;
msgid_cache[cache_index].know_receive = true;
}
if (msgid_cache[cache_index].next_receive - msgid < 0x8000) {
// The msgid counter can wrap around, so a simple larger than
// comparison will not work. Instead, we look at the distance
// between the message's msgid and the one we're expecting to
// receive. If it is from 0 to 0x8000, we consider it to be
// in the forwards direction, and if it is more, we consider it
// to be in the backwards one. This leaves equally sized (2^15)
// ranges for values in both cases.
// In this case the msgid is considered to be of a message we
// haven't yet seen. Therefore we'll continue processing, and
// update the next msgid we're expecting to receive to this one
// plus 1
msgid_cache[cache_index].next_receive = msgid + 1;
} else {
// In this case we consider the message already received
// We will send an ack, but not process it any further
send_ack(source_mac, msgid);
return;
}
char mac[18];
format_mac(source_mac, mac);