Piece.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef D_PIECE_H
  36. #define D_PIECE_H
  37. #include "common.h"
  38. #include <stdint.h>
  39. #include <vector>
  40. #include <string>
  41. #include <memory>
  42. #include "Command.h"
  43. #include "a2functional.h"
  44. namespace aria2 {
  45. class BitfieldMan;
  46. class WrDiskCache;
  47. class WrDiskCacheEntry;
  48. class DiskAdaptor;
  49. class MessageDigest;
  50. class Piece {
  51. private:
  52. std::unique_ptr<BitfieldMan> bitfield_;
  53. std::unique_ptr<WrDiskCacheEntry> wrCache_;
  54. std::unique_ptr<MessageDigest> mdctx_;
  55. std::vector<cuid_t> users_;
  56. std::string hashType_;
  57. size_t index_;
  58. int64_t length_;
  59. int64_t nextBegin_;
  60. bool usedBySegment_;
  61. Piece(const Piece& piece) = delete;
  62. Piece& operator=(const Piece& piece) = delete;
  63. public:
  64. static const int32_t BLOCK_LENGTH = 16_k;
  65. Piece();
  66. Piece(size_t index, int64_t length, int32_t blockLength = BLOCK_LENGTH);
  67. ~Piece();
  68. bool operator==(const Piece& piece) const { return index_ == piece.index_; }
  69. bool operator<(const Piece& piece) const { return index_ < piece.index_; }
  70. // TODO This function only used by unit tests
  71. bool getMissingUnusedBlockIndex(size_t& index) const;
  72. // Appends at most n missing unused block index to indexes. For all
  73. // i in retrieved indexes, call bitfield->setUseBit(i). This
  74. // function just append index to indexes and it doesn't remove
  75. // anything from it. Returns the number of indexes to retrieved.
  76. size_t getMissingUnusedBlockIndex(std::vector<size_t>& indexes,
  77. size_t n) const;
  78. bool getFirstMissingBlockIndexWithoutLock(size_t& index) const;
  79. bool getAllMissingBlockIndexes(unsigned char* misbitfield,
  80. size_t mislen) const;
  81. void completeBlock(size_t blockIndex);
  82. void cancelBlock(size_t blockIndex);
  83. size_t countCompleteBlock() const;
  84. size_t countMissingBlock() const;
  85. bool hasBlock(size_t blockIndex) const;
  86. /**
  87. * Returns true if all blocks of this piece have been downloaded, otherwise
  88. * returns false.
  89. */
  90. bool pieceComplete() const;
  91. size_t countBlock() const;
  92. int32_t getBlockLength(size_t index) const;
  93. int32_t getBlockLength() const;
  94. size_t getIndex() const { return index_; }
  95. void setIndex(size_t index) { index_ = index; }
  96. int64_t getLength() const { return length_; }
  97. void setLength(int64_t length) { length_ = length; }
  98. const unsigned char* getBitfield() const;
  99. void setBitfield(const unsigned char* bitfield, size_t len);
  100. size_t getBitfieldLength() const;
  101. void clearAllBlock(WrDiskCache* diskCache);
  102. void setAllBlock();
  103. std::string toString() const;
  104. bool isBlockUsed(size_t index) const;
  105. // Calculates completed length
  106. int64_t getCompletedLength();
  107. void setHashType(const std::string& hashType);
  108. // Updates hash value. This function compares begin and private variable
  109. // nextBegin_ and only when they are equal, hash is updated eating data and
  110. // returns true. Otherwise returns false.
  111. bool updateHash(int64_t begin, const unsigned char* data, size_t dataLength);
  112. bool isHashCalculated() const;
  113. // Returns raw hash value, not hex digest, which is calculated
  114. // by updateHash(). Please note that this function returns hash
  115. // value only once. Second invocation without updateHash() returns
  116. // empty string.
  117. std::string getDigest();
  118. void destroyHashContext();
  119. // Returns raw hash value, not hex digest, which is calculated using
  120. // cached data and data on disk.
  121. std::string getDigestWithWrCache(size_t pieceLength,
  122. const std::shared_ptr<DiskAdaptor>& adaptor);
  123. /**
  124. * Loses current bitfield state.
  125. */
  126. void reconfigure(int64_t length);
  127. void addUser(cuid_t cuid);
  128. void removeUser(cuid_t cuid);
  129. bool getUsed() const { return !users_.empty(); }
  130. bool usedBy(cuid_t cuid) const;
  131. bool getUsedBySegment() const { return usedBySegment_; }
  132. void setUsedBySegment(bool f) { usedBySegment_ = f; }
  133. void initWrCache(WrDiskCache* diskCache,
  134. const std::shared_ptr<DiskAdaptor>& diskAdaptor);
  135. void flushWrCache(WrDiskCache* diskCache);
  136. void clearWrCache(WrDiskCache* diskCache);
  137. void updateWrCache(WrDiskCache* diskCache, unsigned char* data, size_t offset,
  138. size_t len, size_t capacity, int64_t goff);
  139. void updateWrCache(WrDiskCache* diskCache, unsigned char* data, size_t offset,
  140. size_t len, int64_t goff)
  141. {
  142. updateWrCache(diskCache, data, offset, len, len, goff);
  143. }
  144. size_t appendWrCache(WrDiskCache* diskCache, int64_t goff,
  145. const unsigned char* data, size_t len);
  146. void releaseWrCache(WrDiskCache* diskCache);
  147. WrDiskCacheEntry* getWrDiskCacheEntry() const { return wrCache_.get(); }
  148. };
  149. } // namespace aria2
  150. #endif // D_PIECE_H