Skip to content

Commit

Permalink
Fix some bugs/issues with the message queue
Browse files Browse the repository at this point in the history
- It's no longer possible for messages to be sent out of order
- Check if logger is enabled before doing a pointless API call
  • Loading branch information
JFreegman committed Nov 12, 2020
1 parent 77ab71f commit 4460f09
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/message_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ static void cqueue_mark_read(ToxWindow *self, struct cqueue_msg *msg)
/* removes message with matching receipt from queue, writes to log and updates line to show the message was received. */
void cqueue_remove(ToxWindow *self, Tox *m, uint32_t receipt)
{
struct chatlog *log = self->chatwin->log;
struct chat_queue *q = self->chatwin->cqueue;
struct cqueue_msg *msg = q->root;

Expand All @@ -107,13 +108,16 @@ void cqueue_remove(ToxWindow *self, Tox *m, uint32_t receipt)
continue;
}

char selfname[TOX_MAX_NAME_LENGTH];
tox_self_get_name(m, (uint8_t *) selfname);
if (log->log_on) {
char selfname[TOX_MAX_NAME_LENGTH];
tox_self_get_name(m, (uint8_t *) selfname);

size_t len = tox_self_get_name_size(m);
selfname[len] = '\0';
size_t len = tox_self_get_name_size(m);
selfname[len] = 0;

write_to_log(msg->message, selfname, log, msg->type == OUT_ACTION);
}

write_to_log(msg->message, selfname, self->chatwin->log, msg->type == OUT_ACTION);
cqueue_mark_read(self, msg);

struct cqueue_msg *next = msg->next;
Expand All @@ -127,18 +131,16 @@ void cqueue_remove(ToxWindow *self, Tox *m, uint32_t receipt)
q->root = next;
} else {
struct cqueue_msg *prev = msg->prev;
free(msg);
prev->next = next;
free(msg);
}

return;
}
}

/* We use knowledge of toxcore internals (bad!) to determine that if we haven't received a read receipt
* for a sent packet after this amount of time, the connection has been severed and the packet needs
* to be re-sent despite toxcore not returning an error on its initial send.
*/
// We use knowledge of toxcore internals (bad!) to determine that if we haven't received a read receipt for a
// sent packet after this amount of time, the connection has been severed and the packet needs to be re-sent.
#define TRY_SEND_TIMEOUT 32

/*
Expand All @@ -150,8 +152,13 @@ void cqueue_try_send(ToxWindow *self, Tox *m)
struct chat_queue *q = self->chatwin->cqueue;
struct cqueue_msg *msg = q->root;

// if true we can no longer try to send unsent messages until we get receipts for our previous sent
// messages, but we continue to iterate the list, checking timestamps for any further successfully sent
// messages that have not yet gotten a receipt.
bool missed = false;

while (msg) {
if (msg->receipt == -1) {
if (msg->receipt == -1 && !missed) {
TOX_ERR_FRIEND_SEND_MESSAGE err;
Tox_Message_Type type = msg->type == OUT_MSG ? TOX_MESSAGE_TYPE_NORMAL : TOX_MESSAGE_TYPE_ACTION;
uint32_t receipt = tox_friend_send_message(m, self->num, type, (uint8_t *) msg->message, msg->len, &err);
Expand All @@ -162,8 +169,12 @@ void cqueue_try_send(ToxWindow *self, Tox *m)

msg->receipt = receipt;
msg->last_send_try = get_unix_time();
} else if (timed_out(msg->last_send_try, TRY_SEND_TIMEOUT)) { // unlikely but possible
msg->receipt = -1;
} else {
missed = true;

if (timed_out(msg->last_send_try, TRY_SEND_TIMEOUT)) {
msg->receipt = -1;
}
}

msg = msg->next;
Expand Down

0 comments on commit 4460f09

Please sign in to comment.