⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 btcontent.h

📁 ctorrent源码
💻 H
字号:
#ifndef BTCONTENT_H
#define BTCONTENT_H

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

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

typedef struct _btcache             //为提高磁盘性能,类btContent维护一个BTCACHE链表,缺省为16MB。
{                                   //客户端程序从磁盘读的数据或想要向磁盘写的数据会先放到BTCACHE链表中,直至链表满了才写入磁盘。
  u_int64_t bc_off;                 //此BTCACHE链表成员的绝对偏移地址。            
  size_t bc_len;                    //此BTCACHE链表成员的长度。每个成员的长度不一定相同,取决于客户端想读或写的数据量。
  
  unsigned char bc_f_flush:1;       //flush标志,若为1则此BTCACHE链表成员中的数据会被写入磁盘。若为0则表示此数据是从磁盘读出的。
  unsigned char bc_f_reserved:7;    //保留项
  
  time_t bc_last_timestamp;         //最后一次读或写(由bc_f_flush为1或0决定)的时间。
  
  char *bc_buf;                     //存储的数据

  struct _btcache *bc_next;         //下一个节点
}BTCACHE;

class btContent                              //btContent是有关数据文件和种子文件的类
{
  //METAINFO成员
  char *m_announce;                             //m_announce存储了tracker服务器的announce URL
  unsigned char *m_hash_table;                  //m_hash_table存储了所有piece的SHA1 hash值,它的长度是m_hashtable_length。
  unsigned char m_shake_buffer[68];             //m_shake_buffer存储了client和tracker或peer握手时的数据。
  size_t m_hashtable_length;
  size_t m_piece_length;                        //m_piece_length是piece的长度,制作种子文件时由用户指定,一般为256KB。
  size_t m_npieces;                             //m_npieces则是所有piece的总数了。一般最后一个piece的长度会小一点。

  time_t m_create_date, m_seed_timestamp, m_start_timestamp;

  u_int64_t m_left_bytes;             //客户端缺少的字节数               
  btFiles m_btfiles;                    //m_btfiles储存了种子文件中列出的供用户下载的数据文件的信息。

  BTCACHE *m_cache;
  size_t m_cache_size, m_cache_used;
  
  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 CacheConfigure();
  void CacheClean();
  u_int64_t max_u_int64_t(u_int64_t a,u_int64_t b) { return (a > b) ? a : b; }
  u_int64_t min_u_int64_t(u_int64_t a,u_int64_t b) { return (a > b) ? b : a; }
  ssize_t CacheIO(char *buf, u_int64_t off, size_t len, int method);
  
 public:
  BitField *pBF;
  BitField *pBFilter;
  char *global_piece_buffer;
  
  btContent();
  ~btContent();
  
  void FlushCache();
  
  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);

  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; }

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

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

  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 SeedTimeout(const time_t *pnow);


  void SetFilter();
  void SetTmpFilter(int nfile, BitField *pFilter) { m_btfiles.SetFilter(nfile, pFilter, m_piece_length); }
  size_t getFilePieces(size_t nfile);

  BTFILE *GetNextFile(BTFILE *file) { return m_btfiles.GetNextFile(file); }
  time_t GetStartTime() { return m_start_timestamp; }
  time_t GetSeedTime() { return m_seed_timestamp; }
};

extern btContent BTCONTENT;

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -