-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer_manager.h
87 lines (67 loc) · 2.98 KB
/
peer_manager.h
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
#ifndef PEER_MANAGER_H
#define PEER_MANAGER_H
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include "torrent_runtime.h"
uint16_t DEBUG_CURRENTLY_DOWNLOADING;
struct Peer{
int socket;
uint16_t port; //Also in big endian format
uint8_t address[4]; //The corresponding 64bits version is in big endian format.
uint8_t am_choking, am_interested, peer_choking, peer_interested;
uint8_t *bitfield; //a dynamically allocated array,
int bitfield_length; //how many bytes the bitfield uses
int handshaked; //whether this peer has been handshaked
struct timeval last_received_message_time;
struct timeval last_sent_message_time;
struct Peer *next;
// Upload & Download Fields
struct timeval download_req_sent_time;
uint64_t download_rate;
uint8_t curr_dl; // 1=downloading, 0=not downloading
uint8_t curr_dl_begin; // did the download begin
uint32_t curr_dl_next_subpiece; // 0-based index of last subpiece downloaded
uint64_t curr_dl_piece_idx;
uint8_t curr_up; // 1=uploading, 0=not uploading
uint64_t curr_up_piece_idx;
uint8_t connect_to_use; // 1 for peer connecting with me, 0 for me connecting with them
};
//returns the root peer of the peer linked list
struct Peer *peer_manager_get_root_peer();
//number of bytes in each piece (integer)
uint8_t get_piece_length();
//get the list of four peers which we unchoked, used for uploading & downloading (choking algorithm)
struct Peer *get_am_unchoked();
//update the download rate for the peer, the peer manager doesnt call it
void update_download_rate(struct Peer *peer, uint64_t download_rate);
//returns the peer from the given socket
struct Peer *get_peer_from_socket(int socket);
//starts the peer manager
int start_peer_manager(Torrent *torrent);
//Returns 0 if theres no such peer; 1 otherwise
int peer_manager_inform_disconnect(struct Peer *peer);
/*
Called by the Piece Manager. This function will communicate with a peer
and request to download a piece by sending request message
Returns 1 if socket error / peer is choking us
Returns 0 if success
*/
int peer_manager_begin_download(struct Peer* peer, int pieceIndex);
/*
Params:
----------------------------------------------------------------
is_upload - 1 if this was an upload, 0 if it was a download
peer - A pointer to the peer
pieceIndex - The index of the piece we uploaded/downloaded successfully
Called by the Piece Manager when a piece has been downloaded from
a peer and we are no longer downloading from this peer. Updates the
Peer.curr_dl / Peer.curr_up / Peer.curr_dl_piece / Peer.curr_up_piece
fields and any choking algorithm.
*/
void peer_manager_upload_download_complete(uint8_t is_upload, struct Peer* peer, int piece_index);
//disconnects from all peers
void peer_manager_complete();
//update the download rate for the peer, the peer manager doesnt call it
int peer_manager_update_download_rate(struct Peer *peer, uint64_t download_rate);
#endif