summaryrefslogtreecommitdiff
path: root/ratatox.c
diff options
context:
space:
mode:
authorsin <sin@2f30.org>2014-09-16 13:26:03 +0100
committersin <sin@2f30.org>2014-09-16 13:26:03 +0100
commit0eaf525dfabe2610d4c8e04d5bfdf0ba70f416fd (patch)
treeeca2ea585d4de37eb242f588f3000d3c877c97dd /ratatox.c
parenta9d505c68ec181e031728b436d13e6e1dd660718 (diff)
Clean up send_friend_file()
Diffstat (limited to 'ratatox.c')
-rw-r--r--ratatox.c35
1 files changed, 19 insertions, 16 deletions
diff --git a/ratatox.c b/ratatox.c
index 50e9e6f..d9e3ab9 100644
--- a/ratatox.c
+++ b/ratatox.c
@@ -394,38 +394,41 @@ send_friend_file(struct friend *f)
{
ssize_t n;
- if (f->t.pending == 0) {
-again:
+ while (1) {
+ /* attempt to transmit the pending buffer */
+ if (f->t.pending == 1) {
+ if (tox_file_send_data(tox, f->fid, f->t.fnum, f->t.buf, f->t.n) == -1) {
+ /* bad luck - we will try again later */
+ break;
+ }
+ f->t.pending = 0;
+ break;
+ }
+ /* grab another buffer from the FIFO */
n = read(f->fd[FILE_IN_FIFO], f->t.buf, f->t.chunksz);
if (n < 0) {
if (errno == EINTR)
- goto again;
- /* go back to select until the fd is readable */
+ continue;
+ /* go back to select() until the fd is readable */
if (errno == EWOULDBLOCK)
- return;
+ break;
perror("read");
exit(EXIT_FAILURE);
}
+ /* we are done */
if (n == 0) {
tox_file_send_control(tox, f->fid, 0, f->t.fnum,
TOX_FILECONTROL_FINISHED, NULL, 0);
f->t.state = TRANSFER_DONE;
- return;
+ break;
}
+ /* store transfer size in case we can't send it right now */
f->t.n = n;
if (tox_file_send_data(tox, f->fid, f->t.fnum, f->t.buf, f->t.n) == -1) {
- /* remember to resend the last buffer */
+ /* ok we will have to send it later, flip state */
f->t.pending = 1;
return;
}
- goto again;
- } else {
- if (tox_file_send_data(tox, f->fid, f->t.fnum, f->t.buf, f->t.n) == -1) {
- /* we might be hitting here too hard, maybe relax()? */
- return;
- }
- f->t.pending = 0;
- goto again;
}
}
@@ -440,7 +443,7 @@ again:
if (n < 0) {
if (errno == EINTR)
goto again;
- /* go back to select until the fd is readable */
+ /* go back to select() until the fd is readable */
if (errno == EWOULDBLOCK)
return;
perror("read");