diff --git a/src/message_queue.c b/src/message_queue.c index a19e417ae..fbaf9183e 100644 --- a/src/message_queue.c +++ b/src/message_queue.c @@ -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; @@ -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; @@ -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 /* @@ -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); @@ -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;