summaryrefslogtreecommitdiff
path: root/ratox.c
diff options
context:
space:
mode:
authorz3bra <contactatz3bradotorg>2016-11-05 15:14:58 +0100
committerz3bra <contactatz3bradotorg>2016-11-08 16:47:52 +0100
commit1e5d0f0e2b37d4281fdd2471d372fc88fadd642f (patch)
tree4ebc002a3da6d2b49921a988077a312c692f98a0 /ratox.c
parentc45129f12ce3fd6af68db7e2dc25888cb6a2ffb9 (diff)
Implement chunk_request callback for file transfer
Diffstat (limited to 'ratox.c')
-rw-r--r--ratox.c46
1 files changed, 43 insertions, 3 deletions
diff --git a/ratox.c b/ratox.c
index 2984dc4..260f828 100644
--- 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);