X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=daemon%2Ftransfer.c;h=1863e7462270ab2d13c736e812f98bb301fc7656;hb=1c5e71ff5eb6058d1729e43cc22167b799c99bed;hp=9ad6efea235cf4fed4b27b60cd5cf156c011ea26;hpb=96be906d7e4bf2bfa84a926e1b1b149cdd708021;p=doldaconnect.git diff --git a/daemon/transfer.c b/daemon/transfer.c index 9ad6efe..1863e74 100644 --- a/daemon/transfer.c +++ b/daemon/transfer.c @@ -126,17 +126,98 @@ struct transfer *newtransfer(void) return(new); } +static void localread(struct socket *sk, struct transfer *transfer) +{ + void *buf; + size_t blen; + off_t curpos; + + if((transfer->datapipe != NULL) && (sockqueueleft(transfer->datapipe) > 0)) { + buf = sockgetinbuf(sk, &blen); + if((transfer->endpos >= 0) && (transfer->localpos + blen > transfer->endpos)) + blen = transfer->endpos - transfer->localpos; + sockqueue(transfer->datapipe, buf, blen); + free(buf); + time(&transfer->activity); + transfer->localpos += blen; + bytesupload += blen; + } + if((curpos = transfer->localpos - socktqueuesize(transfer->datapipe)) < 0) + curpos = 0; + if(curpos != transfer->curpos) { + transfer->curpos = curpos; + CBCHAINDOCB(transfer, trans_p, transfer); + } +} + +static void dataread(struct socket *sk, struct transfer *transfer) +{ + void *buf; + size_t blen; + + if((transfer->localend != NULL) && (sockqueueleft(transfer->localend) > 0)) { + buf = sockgetinbuf(sk, &blen); + if((transfer->endpos >= 0) && (transfer->curpos + blen > transfer->endpos)) + blen = transfer->endpos - transfer->curpos; + sockqueue(transfer->localend, buf, blen); + free(buf); + transfer->curpos += blen; + bytesdownload += blen; + CBCHAINDOCB(transfer, trans_p, transfer); + } +} + +static void localwrite(struct socket *sk, struct transfer *transfer) +{ + if(transfer->datapipe != NULL) + dataread(transfer->datapipe, transfer); +} + +static void datawrite(struct socket *sk, struct transfer *transfer) +{ + if(transfer->localend != NULL) + localread(transfer->localend, transfer); +} + +static void localerr(struct socket *sk, int errno, struct transfer *transfer) +{ + if(transfer->datapipe != NULL) + closesock(transfer->datapipe); +} + +static void dataerr(struct socket *sk, int errno, struct transfer *transfer) +{ + if(transfer->dir == TRNSD_DOWN) { + if(transfer->curpos >= transfer->size) { + transfersetstate(transfer, TRNS_DONE); + if(transfer->localend != NULL) { + closesock(transfer->localend); + quitsock(transfer->localend); + transfer->localend = NULL; + } + } else { + resettransfer(transfer); + } + } else if(transfer->dir == TRNSD_UP) { + transfer->close = 1; + } +} + void transferattach(struct transfer *transfer, struct socket *dpipe) { transferdetach(transfer); getsock(transfer->datapipe = dpipe); + dpipe->readcb = (void (*)(struct socket *, void *))dataread; + dpipe->writecb = (void (*)(struct socket *, void *))datawrite; + dpipe->errcb = (void (*)(struct socket *, int, void *))dataerr; + dpipe->data = transfer; } void transferdetach(struct transfer *transfer) { if(transfer->datapipe != NULL) { closesock(transfer->datapipe); - putsock(transfer->datapipe); + quitsock(transfer->datapipe); } transfer->datapipe = NULL; } @@ -228,95 +309,10 @@ static void transexpire(int cancelled, struct transfer *transfer) transfer->timeout = 0; } -static void localread(struct socket *sk, struct transfer *transfer) -{ - void *buf; - size_t blen; - - if(transfer->datapipe != NULL) { - buf = sockgetinbuf(sk, &blen); - sockqueue(transfer->datapipe, buf, blen); - if(sockqueuesize(transfer->datapipe) >= 65536) - sockblock(sk, 1); - else - sockblock(sk, 0); - } else { - if(sockgetdatalen(sk) >= 65536) - sockblock(sk, 1); - } -} - -static void localwrite(struct socket *sk, struct transfer *transfer) -{ - void *buf; - size_t blen; - - -} - -static void localerr(struct socket *sk, int errno, struct transfer *transfer) -{ - if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL)) - transfer->iface->endofdata(transfer, transfer->ifacedata); -} - -void transferputdata(struct transfer *transfer, void *buf, size_t size) -{ - time(&transfer->activity); - sockqueue(transfer->localend, buf, size); - transfer->curpos += size; - bytesdownload += size; - CBCHAINDOCB(transfer, trans_p, transfer); -} - -void transferendofdata(struct transfer *transfer) -{ - if(transfer->curpos >= transfer->size) - { - transfersetstate(transfer, TRNS_DONE); - transfer->localend->readcb = NULL; - transfer->localend->writecb = NULL; - transfer->localend->errcb = NULL; - putsock(transfer->localend); - transfer->localend = NULL; - } else { - resettransfer(transfer); - } -} - -ssize_t transferdatasize(struct transfer *transfer) -{ - return(sockqueueleft(transfer->localend)); -} - -void *transfergetdata(struct transfer *transfer, size_t *size) -{ - void *buf; - - if(transfer->localend == NULL) - return(NULL); - time(&transfer->activity); - if((buf = sockgetinbuf(transfer->localend, size)) == NULL) - return(NULL); - if((transfer->endpos >= 0) && (transfer->curpos + *size >= transfer->endpos)) - { - if((*size = transfer->endpos - transfer->curpos) == 0) { - free(buf); - buf = NULL; - } else { - buf = srealloc(buf, *size); - } - } - transfer->curpos += *size; - bytesupload += *size; - CBCHAINDOCB(transfer, trans_p, transfer); - return(buf); -} - void transferprepul(struct transfer *transfer, off_t size, off_t start, off_t end, struct socket *lesk) { transfersetsize(transfer, size); - transfer->curpos = start; + transfer->curpos = transfer->localpos = start; transfer->endpos = end; transfersetlocalend(transfer, lesk); } @@ -332,7 +328,7 @@ void transferstartul(struct transfer *transfer, struct socket *sk) transfersetstate(transfer, TRNS_MAIN); socksettos(sk, confgetint("transfer", "ultos")); if(transfer->localend != NULL) - transferread(transfer->localend, transfer); + localread(transfer->localend, transfer); } void transfersetlocalend(struct transfer *transfer, struct socket *sk) @@ -740,6 +736,7 @@ static int run(void) { struct transfer *transfer, *next; + /* for(transfer = transfers; transfer != NULL; transfer = transfer->next) { if((transfer->endpos >= 0) && (transfer->state == TRNS_MAIN) && (transfer->localend != NULL) && (transfer->localend->state == SOCK_EST) && (transfer->curpos >= transfer->endpos)) @@ -749,6 +746,7 @@ static int run(void) closesock(transfer->localend); } } + */ for(transfer = transfers; transfer != NULL; transfer = next) { next = transfer->next;