about summary refs log tree commit diff
path: root/btpd/torrent.h
blob: 1797a7386c61f2d6a27152aa61ae103414c19d00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef BTPD_TORRENT_H
#define BTPD_TORRENT_H

#define PIECE_BLOCKLEN (1 << 14)

struct block {
    struct piece *pc;
    struct net_buf *msg;
    struct block_request_tq reqs;
};

struct piece {
    struct torrent *tp;

    uint32_t index;

    unsigned nreqs;

    unsigned nblocks;
    unsigned ngot;
    unsigned nbusy;
    unsigned next_block;

    struct block *blocks;

    const uint8_t *have_field;
    uint8_t *down_field;

    BTPDQ_ENTRY(piece) entry;
};

BTPDQ_HEAD(piece_tq, piece);

enum torrent_state {
    T_INACTIVE,
    T_STARTING,
    T_ACTIVE,
    T_STOPPING
};

struct torrent {
    const char *relpath;
    struct metainfo meta;

    enum torrent_state state;

    struct content *cp;

    BTPDQ_ENTRY(torrent) entry;
    BTPDQ_ENTRY(torrent) net_entry;

    int net_active;

    uint8_t *busy_field;
    uint32_t npcs_busy;

    unsigned *piece_count;

    uint64_t uploaded, downloaded;

    unsigned long rate_up, rate_dwn;

    unsigned npeers;
    struct peer_tq peers;

    int endgame;
    struct piece_tq getlst;
};

BTPDQ_HEAD(torrent_tq, torrent);

int torrent_create(struct torrent **res, const char *path);
void torrent_activate(struct torrent *tp);
void torrent_deactivate(struct torrent *tp);

int torrent_has_peer(struct torrent *tp, const uint8_t *id);

off_t torrent_piece_size(struct torrent *tp, uint32_t index);
uint32_t torrent_block_size(struct piece *pc, uint32_t index);

enum cm_state {
    CM_STARTED,
    CM_STOPPED,
    CM_ERROR
};

void torrent_cm_cb(struct torrent *tp, enum cm_state state);

#endif