tOnly send HAVE messages when seeding - libeech - bittorrent library
 (HTM) git clone git://z3bra.org/libeech.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 34fe31c3c7b310cd8916e5f9c018293e4b0ffab9
 (DIR) parent be01a156aeef06d1f908824f63aa8339dd91992b
 (HTM) Author: z3bra <contactatz3bradotorg>
       Date:   Fri,  6 Jul 2018 19:34:31 +0200
       
       Only send HAVE messages when seeding
       
       By moving HAVE messages directly in the pwptx() function, they become
       independent from other messages, which means that we can send them
       anytime, and ONLY if the peer is consuming the message on the other
       side of the socket.
       
       One can also stop sending HAVE messages by clearing the SEEDING bit.
       
       Diffstat:
         M libeech.c                           |      29 +++++++++++++++++++++++++++--
       
       1 file changed, 27 insertions(+), 2 deletions(-)
       ---
 (DIR) diff --git a/libeech.c b/libeech.c
       t@@ -87,6 +87,9 @@ static int pwprxbf(struct peer *, size_t, char *);
        static int pwprxrq(struct torrent *, struct peer *, size_t, char *);
        static int pwprxpc(struct torrent *, struct peer *, size_t, char *);
        
       +/* various wrappers around PWP messages */
       +static int catchup(struct torrent *, struct peer *);
       +
        /* handle all received PWP messages */
        static int pwprxcb(struct torrent *, struct peer *, int, size_t, char *);
        
       t@@ -598,6 +601,7 @@ pwptxbf(struct torrent *t, struct peer *p)
        
                /* Bitfield */
                memcpy(m+5, t->bf, l);
       +        memcpy(p->view, t->bf, l);
        
                return pwpsend(p, m, l + 5);
        }
       t@@ -735,8 +739,6 @@ pwprxpc(struct torrent *t, struct peer *p, size_t sz, char *pl)
        
                if (!chkpiece(t, &p->piece, idx) && !writepiece(t, &(p->piece))) {
                        setbit(t->bf, p->piece.n);
       -                for (b = t->peers; b; b = b->next)
       -                        pwptxhv(b, p->piece.n);
                        p->piece.n = -1;
                }
        
       t@@ -747,6 +749,23 @@ pwprxpc(struct torrent *t, struct peer *p, size_t sz, char *pl)
        }
        
        /*
       + * Get a peer to catch up new pieces we had received
       + */
       +static int
       +catchup(struct torrent *t, struct peer *p)
       +{
       +        long i;
       +
       +        for (i = 0; i < t->npiece; i++) {
       +                if (bit(t->bf, i) && !bit(p->view, i)) {
       +                        pwptxhv(p, i);
       +                        setbit(p->view, i);
       +                }
       +        }
       +        return 0;
       +}
       +
       +/*
         * PWP message received callback
         * This function will run the appropriate function based on the message type
         */
       t@@ -799,6 +818,12 @@ pwptx(struct torrent *t, struct peer *p)
                                pwptxrq(t, p);
                }
        
       +        if (p->state & SEEDING) {
       +                /* send missing pieces as HAVE messages */
       +                if (memcmp(t->bf, p->view, PCENUM / 8 + !!(PCENUM % 8)))
       +                        catchup(t, p);
       +        }
       +
                return 0;
        }