tUse enums for message types - libeech - bittorrent library
 (HTM) git clone git://z3bra.org/libeech.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 87b942d302df90a66247babfad7dbaf08602d9ac
 (DIR) parent 0fe563fb8981489d3dec676c091bd1628dffb252
 (HTM) Author: z3bra <contactatz3bradotorg>
       Date:   Fri, 29 Jun 2018 22:28:56 +0200
       
       Use enums for message types
       
       Diffstat:
         M libeech.c                           |      25 +++++++++++++++++++------
       
       1 file changed, 19 insertions(+), 6 deletions(-)
       ---
 (DIR) diff --git a/libeech.c b/libeech.c
       t@@ -21,6 +21,19 @@
        /* return a 4 byte array as a 32 bit integer */
        #define U32(s) ((uint32_t)((s)[0])<<24|((s)[1])<<16|((s)[2])<<8|((s)[3]))
        
       +/* Associate message types with their numerical value */
       +enum {
       +        CHOKE = 0,
       +        UNCHOKE,
       +        INTERESTED,
       +        UNINTERESTED,
       +        HAVE,
       +        BITFIELD,
       +        REQUEST,
       +        PIECE,
       +        CANCEL
       +};
       +
        struct peerfds {
                struct peer **p;
                struct pollfd *fds;
       t@@ -53,7 +66,7 @@ static ssize_t pwphsrecv(struct peer *);
        static int pwphsck(struct torrent *, char *, long);
        
        static ssize_t pwpbf(struct torrent *, struct peer *);
       -static ssize_t pwpinterest(struct peer *, int);
       +static ssize_t pwpchst(struct peer *, int);
        
        static int pwptx(struct torrent *, struct peer *);
        static int pwprx(struct torrent *, struct peer *);
       t@@ -418,23 +431,23 @@ pwpbf(struct torrent *t, struct peer *p)
                m[off++] = ((l + 1) >> 0)  & 0xff;
        
                /* Message type */
       -        m[off++] = 5;
       +        m[off++] = BITFIELD;
        
                return send(p->fd, m, l + 5, MSG_NOSIGNAL);
        }
        
        static ssize_t
       -pwpinterest(struct peer *p, int interested)
       +pwpchst(struct peer *p, int t)
        {
                char m[MSGSIZ];
        
       -        memset(m, 0, MSGSIZ);
       +        memset(m, 0, 5);
        
                /* Message length */
                m[3] = 1;
        
                /* Message type */
       -        m[4] = interested ? 2 : 3;
       +        m[4] = t;
        
                return send(p->fd, m, 5, MSG_NOSIGNAL);
        }
       t@@ -450,7 +463,7 @@ pwptx(struct torrent *t, struct peer *p)
                }
        
                if (p->state & LEECHING && !(p->state & AMINTERESTED)) {
       -                pwpinterest(p, 1);
       +                pwpchst(p, INTERESTED);
                        p->state |= AMINTERESTED;
                }