ratox

FIFO based tox client
git clone git://git.2f30.org/ratox
Log | Files | Refs | README | LICENSE

commit 1e5d0f0e2b37d4281fdd2471d372fc88fadd642f
parent c45129f12ce3fd6af68db7e2dc25888cb6a2ffb9
Author: z3bra <contactatz3bradotorg>
Date:   Sat,  5 Nov 2016 15:14:58 +0100

Implement chunk_request callback for file transfer

Diffstat:
Mratox.c | 46+++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 43 insertions(+), 3 deletions(-)

diff --git a/ratox.c b/ratox.c @@ -197,7 +197,8 @@ static void cbstatusmessage(Tox *, uint32_t, const uint8_t *, size_t, void *); static void cbfriendstate(Tox *, uint32_t, TOX_USER_STATUS, void *); static void cbfilecontrol(Tox *, uint32_t, uint32_t, TOX_FILE_CONTROL, void *); static void cbfilesendreq(Tox *, uint32_t, uint32_t, uint32_t, uint64_t, const uint8_t *, size_t, void *); -static void cbfiledata(Tox *, int32_t, uint32_t, uint64_t, const uint8_t *, size_t, void *); +static void cbfiledatareq(Tox *, uint32_t, uint32_t, uint64_t, size_t, void *); +static void cbfiledata(Tox *, uint32_t, uint32_t, uint64_t, const uint8_t *, size_t, void *); static void canceltxtransfer(struct friend *); static void cancelrxtransfer(struct friend *); @@ -733,7 +734,7 @@ cbfilecontrol(Tox *m, uint32_t frnum, uint32_t fnum, TOX_FILE_CONTROL ctrltype, f->tx.state = TRANSFER_INPROGRESS; } else { f->tx.fnum = fnum; - f->tx.chunksz = tox_file_data_size(tox, fnum); + f->tx.chunksz = TOX_MAX_CUSTOM_PACKET_SIZE; f->tx.buf = malloc(f->tx.chunksz); if (!f->tx.buf) eprintf("malloc:"); @@ -769,6 +770,44 @@ cbfilecontrol(Tox *m, uint32_t frnum, uint32_t fnum, TOX_FILE_CONTROL ctrltype, } static void +cbfiledatareq(Tox *m, uint32_t frnum, uint32_t fnum, uint64_t pos, size_t flen, void *udata) +{ + struct friend *f; + ssize_t n; + + TAILQ_FOREACH(f, &friendhead, entry) + if (f->num == frnum) + break; + + /* Grab another buffer from the FIFO */ + if (!f->tx.pendingbuf) { + n = fiforead(f->dirfd, &f->fd[FFILE_IN], ffiles[FFILE_IN], f->tx.buf, + f->tx.chunksz); + f->tx.n = n; + f->tx.pendingbuf = 0; + } + + if (f->tx.n == 0) { + /* Signal transfer completion to other end */ + if (!tox_file_send_chunk(tox, f->num, f->tx.fnum, pos, NULL, 0, NULL)) + weprintf("Failed to signal transfer completion to the receiver\n"); + logmsg(": %s : Tx > Complete\n", f->name); + f->tx.state = TRANSFER_NONE; + free(f->tx.buf); + f->tx.buf = NULL; + return; + } + if (f->tx.n < 0) { + if (errno != EWOULDBLOCK) + weprintf("fiforead:"); + return; + } + + if (!tox_file_send_chunk(tox, f->num, f->tx.fnum, pos, f->tx.buf, f->tx.n, NULL)) + f->tx.pendingbuf = 1; +} + +static void cbfilesendreq(Tox *m, uint32_t frnum, uint32_t fnum, uint32_t kind, uint64_t fsz, const uint8_t *fname, size_t flen, void *udata) { @@ -801,7 +840,7 @@ cbfilesendreq(Tox *m, uint32_t frnum, uint32_t fnum, uint32_t kind, uint64_t fsz } static void -cbfiledata(Tox *m, int32_t frnum, uint32_t fnum, uint64_t pos, const uint8_t *data, size_t len, void *udata) +cbfiledata(Tox *m, uint32_t frnum, uint32_t fnum, uint64_t pos, const uint8_t *data, size_t len, void *udata) { struct friend *f; ssize_t n; @@ -1168,6 +1207,7 @@ toxinit(void) tox_callback_file_recv_control(tox, cbfilecontrol, NULL); tox_callback_file_recv(tox, cbfilesendreq, NULL); tox_callback_file_recv_chunk(tox, cbfiledata, NULL); + tox_callback_file_chunk_request(tox, cbfiledatareq, NULL); toxav_register_callstate_callback(toxav, cbcallinvite, av_OnInvite, NULL); toxav_register_callstate_callback(toxav, cbcallstart, av_OnStart, NULL);