File: tracker.h

package info (click to toggle)
ctorrent 1.3.4.dnh3.3.2-5
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,240 kB
  • sloc: cpp: 13,161; sh: 675; ansic: 383; makefile: 6
file content (118 lines) | stat: -rw-r--r-- 3,146 bytes parent folder | download | duplicates (3)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef TRACKER_H
#define TRACKER_H

#include "./def.h"
#include <sys/types.h>

#include "./bufio.h"

#ifdef WINDOWS
#include <Winsock2.h>

#else
#include <unistd.h>
#include <netdb.h>   // Solaris defines MAXHOSTNAMELEN here.
#include <stdio.h>   // autoconf manual: Darwin + others prereq for stdlib.h
#include <stdlib.h>  // autoconf manual: Darwin prereq for sys/socket.h
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/param.h>
#endif

#include <time.h>

#include "btconfig.h"

#define T_FREE 		0
#define T_CONNECTING	1
#define T_READY		2
#define T_FINISHED	3

class btTracker
{
 private:
  char m_host[MAXHOSTNAMELEN];
  char m_path[MAXPATHLEN];
  int m_port;
  char m_key[9];
  char m_trackerid[PEER_ID_LEN+1];

  struct sockaddr_in m_sin;

  unsigned char m_status:2;
  unsigned char m_f_started:1;
  unsigned char m_f_stoped:1;
  unsigned char m_f_completed:1;
  unsigned char m_f_restart:1;
  unsigned char m_f_boguspeercnt:1;
  unsigned char m_reserved:1;

  time_t m_interval;		// ��Trackerͨ�ŵ�ʱ����
  time_t m_default_interval;		// interval that the tracker tells us to wait
  time_t m_last_timestamp;	// ���һ�γɹ���Trackerͨ�ŵ�ʱ��
  size_t m_connect_refuse_click;

  size_t m_ok_click;	// tracker ok response counter
  size_t m_peers_count;	// total number of peers
  size_t m_seeds_count;	// total number of seeds
  size_t m_prevpeers;	// number of peers previously seen
  time_t m_report_time;
  uint64_t m_totaldl, m_totalul, m_report_dl, m_report_ul;

  SOCKET m_sock;
  BufIo m_request_buffer, m_reponse_buffer;
  
  int _IPsin(char *h, int p, struct sockaddr_in *psin);
  int _s2sin(char *h,int p,struct sockaddr_in *psin);
  int _UpdatePeerList(char *buf,size_t bufsiz);
  int IsPrivateAddress(uint32_t addr);

  int BuildBaseRequest();
  int Connect();
  int SendRequest();
  int CheckReponse();
  void Restart();

 public:
  btTracker();
  ~btTracker();

  int Initial();

  void Reset(time_t new_interval);

  unsigned char GetStatus() { return m_status; }
  void SetStatus(unsigned char s) { m_status = s; }

  SOCKET GetSocket() { return m_sock; }

  void RestartTracker();
  void SetRestart() { m_f_restart = 1; }
  void ClearRestart() { m_f_restart = 0; }
  int IsRestarting() const { return m_f_restart; }
  int IsQuitting() const { return m_f_stoped; }
  void SetStoped();

  int IntervalCheck(fd_set* rfdp, fd_set *wfdp);
  int SocketReady(fd_set *rfdp, fd_set *wfdp, int *nfds,
    fd_set *rfdnextp, fd_set *wfdnextp);

  size_t GetRefuseClick() const { return m_connect_refuse_click; }
  size_t GetOkClick() const { return m_ok_click; }
  size_t GetPeersCount() const;
  size_t GetSeedsCount() const;
  void AdjustPeersCount() {
    if(m_f_boguspeercnt && m_peers_count) m_peers_count--; }
  time_t GetInterval() const { return m_default_interval; }

  time_t GetReportTime() const { return m_report_time; }
  uint64_t GetReportDL() const { return m_report_dl; }
  uint64_t GetReportUL() const { return m_report_ul; }
  void CountDL(size_t nbytes) { m_totaldl += nbytes; }
  void CountUL(size_t nbytes) { m_totalul += nbytes; }
};

extern btTracker Tracker;

#endif