[ADD/WIP] Timeouts in stanza awaiting

Querying for a MUC will not take longer than 10 seconds, now.
This commit is contained in:
LDA 2024-07-09 16:26:35 +02:00
commit d449c8097e
4 changed files with 52 additions and 12 deletions

View file

@ -65,6 +65,7 @@ ParseeMXID(ParseeData *data)
":", data->config->homeserver_host);
}
/* Performs i;octet collation. */
static int
ICollate(unsigned char *cata, unsigned char *catb)
{
@ -1453,8 +1454,11 @@ ParseeXMPPThread(void *argp)
pthread_mutex_destroy(&info.lock);
return NULL;
}
#include <time.h>
#include <errno.h>
XMLElement *
ParseeAwaitStanza(char *identifier)
ParseeAwaitStanza(char *identifier, int64_t timeout)
{
/* TODO: Pthreads HATE me using Malloc here, so I'm abusing stackspace.
* Not *too much* of a problem, just a weird oddity. If anyone has a clue
@ -1462,11 +1466,24 @@ ParseeAwaitStanza(char *identifier)
* me know! */
XMPPAwait awa;
XMLElement *stanza;
struct timespec ts;
if (!identifier)
{
return NULL;
}
if (timeout > 0)
{
int64_t seconds = timeout / (1 SECONDS);
int64_t msecond = timeout % (1 SECONDS);
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += seconds ;
ts.tv_nsec+= msecond * 1000000 ;
}
pthread_mutex_lock(&await_lock);
pthread_cond_init(&awa.condition, NULL);
@ -1479,8 +1496,20 @@ ParseeAwaitStanza(char *identifier)
pthread_mutex_lock(&awa.cond_lock);
while (!awa.stanza)
{
/* TODO: Allow timeouts. */
pthread_cond_wait(&awa.condition, &awa.cond_lock);
int code;
if (timeout <= 0)
{
pthread_cond_wait(&awa.condition, &awa.cond_lock);
continue;
}
code = pthread_cond_timedwait(&awa.condition, &awa.cond_lock, &ts);
if (code == ETIMEDOUT)
{
/* Timeout detected, give up regardless of the status of our
* search. */
break;
}
}
stanza = awa.stanza;