File: btcontent.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 (198 lines) | stat: -rw-r--r-- 5,346 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#ifndef BTCONTENT_H
#define BTCONTENT_H

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

#include <time.h>
#include "bitfield.h"
#include "btfiles.h"

typedef struct _btcache{
  uint64_t bc_off;
  size_t bc_len;
  
  unsigned char bc_f_flush:1;
  unsigned char bc_f_reserved:7;
  
  char *bc_buf;

  struct _btcache *bc_next;
  struct _btcache *bc_prev;
  struct _btcache *age_next;
  struct _btcache *age_prev;
}BTCACHE;

typedef struct _btflush{
  size_t idx;
  struct _btflush *next;
}BTFLUSH;

typedef struct _bfnode{
  char *name;
  BitField bitfield;
  struct _bfnode *next;

  _bfnode(){
    name = (char *)0;
    next = (struct _bfnode *)0;
  }
  ~_bfnode(){
    if(name) delete []name;
  }
}BFNODE;

class btContent
{
  //METAINFO��Ա
  char *m_announce;
  unsigned char *m_hash_table;
  unsigned char m_shake_buffer[68];
  char *m_announcelist[9];
  char *m_comment, *m_created_by;

  size_t m_hashtable_length;
  size_t m_piece_length;
  size_t m_npieces, m_check_piece;
  time_t m_create_date, m_seed_timestamp, m_start_timestamp;
  size_t m_private;

  uint64_t m_left_bytes;
  btFiles m_btfiles;

  time_t m_flush_failed, m_flush_tried;

  BTCACHE **m_cache, *m_cache_oldest, *m_cache_newest;
  size_t m_cache_size, m_cache_used;
  size_t m_cache_hit, m_cache_miss, m_cache_pre;
  time_t m_cache_eval_time;
  BTFLUSH *m_flushq;

  BFNODE *m_filters, *m_current_filter;

  size_t m_prevdlrate;
  size_t m_hash_failures, m_dup_blocks, m_unwanted_blocks;

  void _Set_InfoHash(unsigned char buf[20]);
  char* _file2mem(const char *fname, size_t *psiz);
  
  void ReleaseHashTable(){
    if(m_hash_table){
      delete []m_hash_table;
      m_hash_table = (unsigned char*) 0;
    }
  }

  int CheckExist();
  void CacheClean(size_t need);
  void CacheEval();
  uint64_t max_uint64_t(uint64_t a,uint64_t b) { return (a > b) ? a : b; }
  uint64_t min_uint64_t(uint64_t a,uint64_t b) { return (a > b) ? b : a; }
  ssize_t CacheIO(char *buf, uint64_t off, size_t len, int method);
  void FlushEntry(BTCACHE *p);

 public:
  BitField *pBF;
  BitField *pBMasterFilter;
  BitField *pBRefer;
  BitField *pBChecked;
  BitField *pBMultPeer;
  char *global_piece_buffer;
  size_t global_buffer_size;
  
  btContent();
  ~btContent();
  
  void CacheConfigure();
  void FlushCache();
  void FlushPiece(size_t idx);
  void Uncache(size_t idx);
  void FlushQueue();
  int NeedFlush() const;
  int FlushFailed() const { return m_flush_failed ? 1 : 0 ; }

  int CreateMetainfoFile(const char *mifn);
  int InitialFromFS(const char *pathname, char *ann_url, size_t piece_length);
  int InitialFromMI(const char *metainfo_fname,const char *saveas);

  int CheckNextPiece();
  size_t CheckedPieces() const { return m_check_piece; }

  char* GetAnnounce() { return m_announce;}

  unsigned char* GetShakeBuffer() {return m_shake_buffer;}
  unsigned char* GetInfoHash() {return (m_shake_buffer + 28);}
  unsigned char* GetPeerId() {return (m_shake_buffer + 48); }

  size_t GetPieceLength(size_t idx);
  size_t GetPieceLength() const { return m_piece_length; }
  size_t GetNPieces() const { return m_npieces; }

  uint64_t GetTotalFilesLength() const { return m_btfiles.GetTotalLength(); }
  uint64_t GetLeftBytes() const { return m_left_bytes; }

  int APieceComplete(size_t idx);
  int GetHashValue(size_t idx,unsigned char *md);

  int CachePrep(size_t idx);
  ssize_t ReadSlice(char *buf,size_t idx,size_t off,size_t len);
  ssize_t WriteSlice(char *buf,size_t idx,size_t off,size_t len);
  ssize_t ReadPiece(char *buf,size_t idx);

  int PrintOut();
  int PrintFiles();
  int SeedTimeout();
  void CompletionCommand();
  void SaveBitfield();

  void CheckFilter();
  void SetFilter();
  BitField *GetFilter() const {
    return m_current_filter ? &(m_current_filter->bitfield) : (BitField *)0;
  }
  BitField *GetNextFilter() const { return GetNextFilter((BitField *)0); }
  BitField *GetNextFilter(BitField *pfilter) const;
  char *GetFilterName() const { return m_current_filter->name; }
  void SetTmpFilter(int nfile, BitField *pFilter){
    m_btfiles.SetFilter(nfile, pFilter, m_piece_length);
  }

  size_t GetNFiles() const { return m_btfiles.GetNFiles(); }
  char *GetFileName(size_t nfile) const {
    return m_btfiles.GetFileName(nfile);
  }
  uint64_t GetFileSize(size_t nfile) const {
    return m_btfiles.GetFileSize(nfile);
  }
  size_t GetFilePieces(size_t nfile) const {
    return m_btfiles.GetFilePieces(nfile);
  }

  time_t GetStartTime() const { return m_start_timestamp; }
  time_t GetSeedTime() const { return m_seed_timestamp; }

  size_t GetHashFailures() const { return m_hash_failures; }
  size_t GetDupBlocks() const { return m_dup_blocks; }
  size_t GetUnwantedBlocks() const { return m_unwanted_blocks; }
  void CountHashFailure() { m_hash_failures++; }
  void CountDupBlock(size_t len);
  void CountUnwantedBlock() { m_unwanted_blocks++; }

  int IsFull() const { return pBF->IsFull(); }
  int Seeding() const;

  size_t CacheHits() const { return m_cache_hit; }
  // "miss" does not count prefetch reads from disk
  size_t CacheMiss() const { return m_cache_miss; }
  // instead, "pre" does
  size_t CachePre() const { return m_cache_pre; }
  size_t CacheSize() const { return m_cache_size; }
  size_t CacheUsed() const { return m_cache_used; }

  void DumpCache();
};

extern btContent BTCONTENT;

#endif