From c01566e8af611da3298d1d0edb6ca11bc76e07a6 Mon Sep 17 00:00:00 2001 From: sin Date: Tue, 23 Sep 2014 14:32:20 +0100 Subject: Cancel transfers properly --- ratox.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/ratox.c b/ratox.c index d50b8bc..fea810a 100644 --- a/ratox.c +++ b/ratox.c @@ -182,6 +182,7 @@ static void cbnamechange(Tox *, int32_t, const uint8_t *, uint16_t, void *); static void cbstatusmessage(Tox *, int32_t, const uint8_t *, uint16_t, void *); static void cbuserstatus(Tox *, int32_t, uint8_t, void *); static void cbfilecontrol(Tox *, int32_t, uint8_t, uint8_t, uint8_t, const uint8_t *, uint16_t, void *); +static void canceltransfer(struct friend *); static void sendfriendfile(struct friend *); static void sendfriendtext(struct friend *); static void removefriend(struct friend *); @@ -485,6 +486,21 @@ cbfilecontrol(Tox *m, int32_t fid, uint8_t rec_sen, uint8_t fnum, uint8_t ctrlty }; } +static void +canceltransfer(struct friend *f) +{ + if (f->t.state != TRANSFER_NONE) { + printout("Cancelling transfer to %s\n", + f->namestr[0] == '\0' ? "Anonymous" : f->namestr); + f->t.state = TRANSFER_NONE; + free(f->t.buf); + f->t.buf = NULL; + /* flush the FIFO */ + while (fiforead(f->dirfd, &f->fd[FFILE_IN], ffiles[FFILE_IN], + toilet, sizeof(toilet))); + } +} + static void sendfriendfile(struct friend *f) { @@ -919,7 +935,7 @@ frienddestroy(struct friend *f) } } rmdir(f->idstr); - /* T0D0: cancel transmissions */ + canceltransfer(f); TAILQ_REMOVE(&friendhead, f, entry); } @@ -1105,18 +1121,8 @@ loop(void) * in the middle of a transfer. */ TAILQ_FOREACH(f, &friendhead, entry) { - if (tox_get_friend_connection_status(tox, f->fid) == 0) { - if (f->t.state != TRANSFER_NONE) { - printout("Stale transfer detected, friend offline\n"); - f->t.state = TRANSFER_NONE; - free(f->t.buf); - f->t.buf = NULL; - - /* flush the FIFO */ - while (fiforead(f->dirfd, &f->fd[FFILE_IN], ffiles[FFILE_IN], - toilet, sizeof(toilet))); - } - } + if (tox_get_friend_connection_status(tox, f->fid) == 0) + canceltransfer(f); } /* If we hit the receiver too hard, we will run out of -- cgit v1.2.3