From 208fddb9f5ed2e454de7e248b15b1568472dabc6 Mon Sep 17 00:00:00 2001 From: Kevin Easton Date: Thu, 12 Apr 2018 00:20:13 +1000 Subject: [PATCH] Simplify dcc_send_socketread() There should be no change in behaviour. Replaces a code block of the form: if (a || b) { if (c && a) { /* one */ } else if (b) { /* two */ } } with the simpler equivalent: if (c && a) { /* one */ } else if (b) { /* two */ } --- source/dcc.c | 100 ++++++++++++++++++++++++++------------------------- 1 file changed, 52 insertions(+), 48 deletions(-) diff --git a/source/dcc.c b/source/dcc.c index 234cc37..c7c9056 100644 --- a/source/dcc.c +++ b/source/dcc.c @@ -1791,66 +1791,70 @@ void close_dcc_file(int snum) void BX_dcc_send_socketread(int snum) { -SocketList *s; -DCC_int *n; -u_32int_t bytes = 0; -int bytesread = 0; -char *buffer = alloca(MAX_DCC_BLOCK_SIZE+1); + SocketList *s; + DCC_int *n; + int bytesread = 0; + char buffer[MAX_DCC_BLOCK_SIZE+1]; + s = get_socket(snum); n = (DCC_int *)s->info; - if (n->readwaiting || n->eof) + + if (!(s->flags & DCC_TDCC) && n->readwaiting) { + /* n->readwaiting is set when we have sent a block and are waiting for an acknowledgement. */ int numbytes = 0; - if (!(s->flags & DCC_TDCC) && n->readwaiting) + u_32int_t bytes = 0; + + if ((ioctl(snum, FIONREAD, &numbytes)) == -1) { - if ((ioctl(snum, FIONREAD, &numbytes)) == -1) - { - erase_dcc_info(snum, 1, convert_output_format("$G %RDCC%n Remote $0 closed dcc send", "%s", s->server)); - close_socketread(snum); - return; - } - if (numbytes) - { - if (read(snum, &bytes, sizeof bytes) < (int)sizeof bytes) - { - erase_dcc_info(snum, 1, convert_output_format("$G %RDCC%n Remote closed dcc send", NULL)); - close_socketread(snum); - } - bytes = (unsigned long)ntohl(bytes); - get_time(&n->lasttime); - if (bytes == (n->filesize - n->transfer_orders.byteoffset)) - { - close_dcc_file(snum); - return; - } - else if (!n->dcc_fast && (bytes != n->bytes_sent)) - return; - n->readwaiting = 0; - } + erase_dcc_info(snum, 1, convert_output_format("$G %RDCC%n Remote $0 closed dcc send", "%s", s->server)); + close_socketread(snum); + return; } - else if (n->eof) - { - u_32int_t *buf; - n->readwaiting = 1; - if ((ioctl(snum, FIONREAD, &numbytes) == -1)) + if (numbytes) + { + if (read(snum, &bytes, sizeof bytes) < (int)sizeof bytes) + { + erase_dcc_info(snum, 1, convert_output_format("$G %RDCC%n Remote closed dcc send", NULL)); + close_socketread(snum); + } + bytes = (unsigned long)ntohl(bytes); + get_time(&n->lasttime); + if (bytes == (n->filesize - n->transfer_orders.byteoffset)) { close_dcc_file(snum); return; } - buf = alloca(numbytes+1); - numbytes = read(snum, buf, numbytes); - switch(numbytes) - { - case -1: - erase_dcc_info(snum, 1, convert_output_format("$G %RDCC%n Remote $0 closed dcc send", "%s", s->server)); - close_socketread(snum); - break; - case 0: - close_dcc_file(snum); - break; - } + else if (!n->dcc_fast && (bytes != n->bytes_sent)) + return; + n->readwaiting = 0; + } + } + else if (n->eof) + { + /* n->eof is set if read() on the file we are sending returns EOF or error. */ + int numbytes = 0; + u_32int_t *buf; + + n->readwaiting = 1; + if ((ioctl(snum, FIONREAD, &numbytes) == -1)) + { + close_dcc_file(snum); return; } + buf = alloca(numbytes+1); + numbytes = read(snum, buf, numbytes); + switch(numbytes) + { + case -1: + erase_dcc_info(snum, 1, convert_output_format("$G %RDCC%n Remote $0 closed dcc send", "%s", s->server)); + close_socketread(snum); + break; + case 0: + close_dcc_file(snum); + break; + } + return; } if ((bytesread = read(n->file, buffer, n->blocksize)) > 0)