tAdd glch_piececount() to the API - libeech - bittorrent library
(HTM) git clone git://z3bra.org/libeech.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 75028910f9e794ced6d97bb31f977a808e0b72e1
(DIR) parent ce235dc3c86e16ce041a209277ebfa9cc50848d2
(HTM) Author: z3bra <contactatz3bradotorg>
Date: Fri, 6 Jul 2018 08:22:43 +0200
Add glch_piececount() to the API
Diffstat:
M libeech.c | 11 +++++++++++
M libeech.h | 1 +
M torrent.c | 6 +++++-
3 files changed, 17 insertions(+), 1 deletion(-)
---
(DIR) diff --git a/libeech.c b/libeech.c
t@@ -987,3 +987,14 @@ glch_leech(struct torrent *t, int timeout)
{
return netloop(t, timeout);
}
+
+/* Return the number of currently downloaded pieces */
+long
+glch_piececount(struct torrent *t)
+{
+ long i, n;
+ for (i = n = 0; i < t->npiece; i++)
+ n += bit(t->bf, i);
+
+ return n;
+}
(DIR) diff --git a/libeech.h b/libeech.h
t@@ -66,3 +66,4 @@ struct torrent {
int glch_loadtorrent(struct torrent *, char *, size_t);
int glch_addpeer(struct torrent *, struct peer *, char *, int);
int glch_leech(struct torrent *, int);
+long glch_piececount(struct torrent *);
(DIR) diff --git a/torrent.c b/torrent.c
t@@ -83,8 +83,12 @@ main(int argc, char *argv[])
printf("\t%s\n", t.files[i].path);
glch_addpeer(&t, &peer, h, p);
- for (;;)
+ while (glch_piececount(&t) < t.npiece) {
glch_leech(&t, 5000);
+ printf("\rDownload: %2d%%", (100 * glch_piececount(&t)) / t.npiece);
+ fflush(stdout);
+ }
+ puts("\rDownload finished!");
return 0;
}