about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRichard Nyberg <rnyberg@murmeldjur.se>2005-07-03 09:41:31 +0000
committerRichard Nyberg <rnyberg@murmeldjur.se>2005-07-03 09:41:31 +0000
commit307b9a50aa502de2bed4113e4484faf12909b9a2 (patch)
tree9fa8c2cc1114322a838bba396e575718d0bb3bcc
parentb84c77f1f7303d4fe4cdeddcf4754bf00508983a (diff)
downloadbtpd-307b9a50aa502de2bed4113e4484faf12909b9a2.tar.gz
btpd-307b9a50aa502de2bed4113e4484faf12909b9a2.zip
Before, the count of uploaded torrent data was increases by the block size
as soon as the message head was written to the network. Now it's increased
for each byte of torrent data we've written.

-rw-r--r--btpd/net.c23
-rw-r--r--btpd/net.h1
2 files changed, 16 insertions, 8 deletions
diff --git a/btpd/net.c b/btpd/net.c
index 244e7e4..a7a9023 100644
--- a/btpd/net.c
+++ b/btpd/net.c
@@ -137,8 +137,8 @@ static struct iob_link *
 malloc_liob(size_t len)
 {
     struct iob_link *iol;
-    iol = (struct iob_link *)btpd_malloc(sizeof(*iol) + len);
-    iol->iob.buf = (char *)iol + sizeof(*iol);
+    iol = (struct iob_link *)btpd_calloc(1, sizeof(*iol) + len);
+    iol->iob.buf = (char *)(iol + 1);
     iol->iob.buf_len = len;
     iol->iob.buf_off = 0;
     iol->kill_buf = nokill_iob;
@@ -149,7 +149,7 @@ static struct iob_link *
 salloc_liob(char *buf, size_t len, void (*kill_buf)(struct io_buffer *))
 {
     struct iob_link *iol;
-    iol = (struct iob_link *)btpd_malloc(sizeof(*iol));
+    iol = (struct iob_link *)btpd_calloc(1, sizeof(*iol));
     iol->iob.buf = buf;
     iol->iob.buf_len = len;
     iol->iob.buf_off = 0;
@@ -235,26 +235,32 @@ again:
     }
 
     bcount = nwritten;
-    p->rate_from_me[btpd.seconds % RATEHISTORY] += nwritten;
 
     req = BTPDQ_FIRST(&p->p_reqs);
     iol = BTPDQ_FIRST(&p->outq);
     while (bcount > 0) {
+	unsigned long bufdelta = iol->iob.buf_len - iol->iob.buf_off;
 	if (req != NULL && req->head == iol) {
-	    struct iob_link *piece = BTPDQ_NEXT(req->head, entry);
 	    struct piece_req *next = BTPDQ_NEXT(req, entry);
 	    BTPDQ_REMOVE(&p->p_reqs, req, entry);
 	    free(req);
 	    req = next;
-	    p->tp->uploaded += piece->iob.buf_len;
 	}
-	if (bcount >= iol->iob.buf_len - iol->iob.buf_off) {
-	    bcount -= iol->iob.buf_len - iol->iob.buf_off;
+	if (bcount >= bufdelta) {
+	    if (iol->upload) {
+		p->tp->uploaded += bufdelta;
+		p->rate_from_me[btpd.seconds % RATEHISTORY] += bufdelta;
+	    }
+	    bcount -= bufdelta;
 	    BTPDQ_REMOVE(&p->outq, iol, entry);
 	    iol->kill_buf(&iol->iob);
 	    free(iol);
 	    iol = BTPDQ_FIRST(&p->outq);
 	} else {
+	    if (iol->upload) {
+		p->tp->uploaded += bcount;
+		p->rate_from_me[btpd.seconds % RATEHISTORY] += bcount;
+	    }
 	    iol->iob.buf_off += bcount;
 	    bcount = 0;
 	}
@@ -306,6 +312,7 @@ net_send_piece(struct peer *p, uint32_t index, uint32_t begin,
     net_send(p, head);
 
     piece = salloc_liob(block, blen, kill_free_buf);
+    piece->upload = 1;
     net_send(p, piece);
 
     req = btpd_malloc(sizeof(*req));
diff --git a/btpd/net.h b/btpd/net.h
index 7ceff02..33137dd 100644
--- a/btpd/net.h
+++ b/btpd/net.h
@@ -20,6 +20,7 @@ struct bwlim {
 BTPDQ_HEAD(bwlim_tq, bwlim);
 
 struct iob_link {
+    int upload;
     BTPDQ_ENTRY(iob_link) entry;
     void (*kill_buf)(struct io_buffer *);
     struct io_buffer iob;