2014-09-06 19:18:42 +02:00
|
|
|
/* message_queue.c
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Toxic All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Toxic.
|
|
|
|
*
|
|
|
|
* Toxic is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Toxic is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2014-09-07 08:43:53 +02:00
|
|
|
#include "line_info.h"
|
2014-09-11 07:36:33 +02:00
|
|
|
#include "log.h"
|
2020-04-18 02:00:00 +02:00
|
|
|
#include "message_queue.h"
|
|
|
|
#include "misc_tools.h"
|
|
|
|
#include "toxic.h"
|
|
|
|
#include "windows.h"
|
2014-09-06 19:18:42 +02:00
|
|
|
|
|
|
|
void cqueue_cleanup(struct chat_queue *q)
|
|
|
|
{
|
|
|
|
struct cqueue_msg *tmp1 = q->root;
|
|
|
|
|
|
|
|
while (tmp1) {
|
|
|
|
struct cqueue_msg *tmp2 = tmp1->next;
|
|
|
|
free(tmp1);
|
|
|
|
tmp1 = tmp2;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(q);
|
|
|
|
}
|
|
|
|
|
2017-06-01 22:46:12 +02:00
|
|
|
void cqueue_add(struct chat_queue *q, const char *msg, size_t len, uint8_t type, int line_id)
|
2014-09-06 19:18:42 +02:00
|
|
|
{
|
2017-06-01 22:46:12 +02:00
|
|
|
if (line_id < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-09 20:06:28 +02:00
|
|
|
struct cqueue_msg *new_m = malloc(sizeof(struct cqueue_msg));
|
2014-09-06 19:18:42 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (new_m == NULL) {
|
2015-07-04 07:19:16 +02:00
|
|
|
exit_toxic_err("failed in cqueue_message", FATALERR_MEMORY);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-09-06 19:18:42 +02:00
|
|
|
|
|
|
|
snprintf(new_m->message, sizeof(new_m->message), "%s", msg);
|
|
|
|
new_m->len = len;
|
|
|
|
new_m->type = type;
|
|
|
|
new_m->line_id = line_id;
|
2014-09-09 20:06:28 +02:00
|
|
|
new_m->last_send_try = 0;
|
2021-11-20 04:54:35 +01:00
|
|
|
new_m->time_added = get_unix_time();
|
2020-11-11 22:28:50 +01:00
|
|
|
new_m->receipt = -1;
|
2014-09-09 20:06:28 +02:00
|
|
|
new_m->next = NULL;
|
2014-09-06 19:18:42 +02:00
|
|
|
|
2014-09-09 20:06:28 +02:00
|
|
|
if (q->root == NULL) {
|
|
|
|
new_m->prev = NULL;
|
2014-09-06 19:18:42 +02:00
|
|
|
q->root = new_m;
|
2014-09-09 20:06:28 +02:00
|
|
|
} else {
|
|
|
|
new_m->prev = q->end;
|
2014-09-06 19:18:42 +02:00
|
|
|
q->end->next = new_m;
|
2014-09-09 20:06:28 +02:00
|
|
|
}
|
2014-09-06 19:18:42 +02:00
|
|
|
|
|
|
|
q->end = new_m;
|
|
|
|
}
|
|
|
|
|
2014-09-07 08:43:53 +02:00
|
|
|
/* update line to show receipt was received after queue removal */
|
2014-09-11 07:36:33 +02:00
|
|
|
static void cqueue_mark_read(ToxWindow *self, struct cqueue_msg *msg)
|
2014-09-07 08:43:53 +02:00
|
|
|
{
|
2021-11-20 04:54:35 +01:00
|
|
|
struct line_info *line = line_info_get(self, msg->line_id);
|
2014-09-07 08:43:53 +02:00
|
|
|
|
2021-11-20 04:54:35 +01:00
|
|
|
if (line == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-08 04:05:17 +02:00
|
|
|
|
2021-11-20 04:54:35 +01:00
|
|
|
line->type = msg->type == OUT_ACTION ? OUT_ACTION_READ : OUT_MSG_READ;
|
2014-09-07 08:43:53 +02:00
|
|
|
|
2021-11-20 04:54:35 +01:00
|
|
|
if (line->noread_flag) {
|
|
|
|
line->noread_flag = false;
|
|
|
|
line->read_flag = true;
|
|
|
|
flag_interface_refresh();
|
2014-09-07 08:43:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-11 07:36:33 +02:00
|
|
|
/* 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)
|
2014-09-06 19:18:42 +02:00
|
|
|
{
|
2020-11-12 17:39:19 +01:00
|
|
|
struct chatlog *log = self->chatwin->log;
|
2014-09-11 07:36:33 +02:00
|
|
|
struct chat_queue *q = self->chatwin->cqueue;
|
2014-09-09 20:06:28 +02:00
|
|
|
struct cqueue_msg *msg = q->root;
|
2014-09-07 08:43:53 +02:00
|
|
|
|
2014-09-09 20:06:28 +02:00
|
|
|
while (msg) {
|
|
|
|
if (msg->receipt != receipt) {
|
|
|
|
msg = msg->next;
|
|
|
|
continue;
|
|
|
|
}
|
2014-09-07 08:43:53 +02:00
|
|
|
|
2020-11-12 17:39:19 +01:00
|
|
|
if (log->log_on) {
|
|
|
|
char selfname[TOX_MAX_NAME_LENGTH];
|
|
|
|
tox_self_get_name(m, (uint8_t *) selfname);
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2020-11-12 17:39:19 +01:00
|
|
|
size_t len = tox_self_get_name_size(m);
|
|
|
|
selfname[len] = 0;
|
|
|
|
|
|
|
|
write_to_log(msg->message, selfname, log, msg->type == OUT_ACTION);
|
|
|
|
}
|
2014-09-11 07:36:33 +02:00
|
|
|
|
|
|
|
cqueue_mark_read(self, msg);
|
|
|
|
|
2014-09-09 20:06:28 +02:00
|
|
|
struct cqueue_msg *next = msg->next;
|
2014-09-06 19:18:42 +02:00
|
|
|
|
2014-09-09 20:06:28 +02:00
|
|
|
if (msg->prev == NULL) { /* root */
|
2018-07-18 17:33:16 +02:00
|
|
|
if (next) {
|
2014-09-09 20:06:28 +02:00
|
|
|
next->prev = NULL;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-09-09 20:06:28 +02:00
|
|
|
|
|
|
|
q->root = next;
|
|
|
|
} else {
|
2014-09-09 21:16:27 +02:00
|
|
|
struct cqueue_msg *prev = msg->prev;
|
|
|
|
prev->next = next;
|
2020-11-12 17:39:19 +01:00
|
|
|
next->prev = prev;
|
2014-09-09 20:06:28 +02:00
|
|
|
}
|
2014-09-07 08:43:53 +02:00
|
|
|
|
2020-11-12 17:39:19 +01:00
|
|
|
free(msg);
|
|
|
|
|
2014-09-06 19:18:42 +02:00
|
|
|
return;
|
2014-09-09 20:06:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-09-06 19:18:42 +02:00
|
|
|
|
2020-11-12 17:39:19 +01:00
|
|
|
// 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.
|
2020-11-11 22:28:50 +01:00
|
|
|
#define TRY_SEND_TIMEOUT 32
|
2014-09-10 22:18:37 +02:00
|
|
|
|
2020-11-12 17:39:19 +01:00
|
|
|
/*
|
|
|
|
* Marks all timed out messages in queue as unsent.
|
|
|
|
*/
|
|
|
|
static void cqueue_check_timeouts(struct cqueue_msg *msg)
|
|
|
|
{
|
|
|
|
while (msg) {
|
|
|
|
if (timed_out(msg->last_send_try, TRY_SEND_TIMEOUT)) {
|
|
|
|
msg->receipt = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = msg->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 04:54:35 +01:00
|
|
|
/*
|
|
|
|
* Sets the noread flag for messages sent to the peer associated with `self` which have not
|
|
|
|
* received a receipt after a period of time.
|
|
|
|
*/
|
|
|
|
#define NOREAD_TIMEOUT 5
|
|
|
|
void cqueue_check_unread(ToxWindow *self)
|
|
|
|
{
|
|
|
|
struct chat_queue *q = self->chatwin->cqueue;
|
|
|
|
struct cqueue_msg *msg = q->root;
|
|
|
|
|
|
|
|
while (msg) {
|
|
|
|
if (msg->noread_flag) {
|
|
|
|
msg = msg->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct line_info *line = line_info_get(self, msg->line_id);
|
|
|
|
|
|
|
|
if (line != NULL) {
|
|
|
|
if (timed_out(msg->time_added, NOREAD_TIMEOUT)) {
|
|
|
|
line->noread_flag = true;
|
|
|
|
msg->noread_flag = true;
|
|
|
|
flag_interface_refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = msg->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-11 22:28:50 +01:00
|
|
|
/*
|
|
|
|
* Tries to send all messages in the send queue in sequential order.
|
|
|
|
* If a message fails to send the function will immediately return.
|
|
|
|
*/
|
2014-09-10 22:18:37 +02:00
|
|
|
void cqueue_try_send(ToxWindow *self, Tox *m)
|
2014-09-09 20:06:28 +02:00
|
|
|
{
|
|
|
|
struct chat_queue *q = self->chatwin->cqueue;
|
|
|
|
struct cqueue_msg *msg = q->root;
|
2014-12-04 04:10:21 +01:00
|
|
|
|
2020-11-11 22:28:50 +01:00
|
|
|
while (msg) {
|
2020-11-12 17:39:19 +01:00
|
|
|
if (msg->receipt != -1) {
|
|
|
|
// 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.
|
|
|
|
cqueue_check_timeouts(msg);
|
|
|
|
return;
|
|
|
|
}
|
2014-12-04 04:10:21 +01:00
|
|
|
|
2022-03-07 20:20:35 +01:00
|
|
|
Tox_Err_Friend_Send_Message err;
|
2020-11-12 17:39:19 +01:00
|
|
|
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);
|
2014-09-07 08:43:53 +02:00
|
|
|
|
2020-11-12 17:39:19 +01:00
|
|
|
if (err != TOX_ERR_FRIEND_SEND_MESSAGE_OK) {
|
|
|
|
return;
|
2020-11-11 22:28:50 +01:00
|
|
|
}
|
|
|
|
|
2020-11-12 17:39:19 +01:00
|
|
|
msg->receipt = receipt;
|
|
|
|
msg->last_send_try = get_unix_time();
|
2020-11-11 22:28:50 +01:00
|
|
|
msg = msg->next;
|
|
|
|
}
|
2014-09-06 19:18:42 +02:00
|
|
|
}
|