Piece.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. namespace aria2 {
  44. class BitfieldMan;
  45. class WrDiskCache;
  46. class WrDiskCacheEntry;
  47. class DiskAdaptor;
  48. #ifdef ENABLE_MESSAGE_DIGEST
  49. class MessageDigest;
  50. #endif // ENABLE_MESSAGE_DIGEST
  51. class Piece {
  52. private:
  53. size_t index_;
  54. int32_t length_;
  55. int32_t blockLength_;
  56. BitfieldMan* bitfield_;
  57. std::vector<cuid_t> users_;
  58. bool usedBySegment_;
  59. WrDiskCacheEntry* wrCache_;
  60. #ifdef ENABLE_MESSAGE_DIGEST
  61. int32_t nextBegin_;
  62. std::string hashType_;
  63. std::unique_ptr<MessageDigest> mdctx_;
  64. #endif // ENABLE_MESSAGE_DIGEST
  65. Piece(const Piece& piece);
  66. Piece& operator=(const Piece& piece);
  67. public:
  68. static const int32_t BLOCK_LENGTH = 16*1024;
  69. Piece();
  70. Piece(size_t index, int32_t length, int32_t blockLength = BLOCK_LENGTH);
  71. ~Piece();
  72. bool operator==(const Piece& piece) const
  73. {
  74. return index_ == piece.index_;
  75. }
  76. bool operator<(const Piece& piece) const
  77. {
  78. return index_ < piece.index_;
  79. }
  80. // TODO This function only used by unit tests
  81. bool getMissingUnusedBlockIndex(size_t& index) const;
  82. // Appends at most n missing unused block index to indexes. For all
  83. // i in retrieved indexes, call bitfield->setUseBit(i). This
  84. // function just append index to indexes and it doesn't remove
  85. // anything from it. Returns the number of indexes to retrieved.
  86. size_t getMissingUnusedBlockIndex
  87. (std::vector<size_t>& indexes, size_t n) const;
  88. bool getFirstMissingBlockIndexWithoutLock(size_t& index) const;
  89. bool getAllMissingBlockIndexes(unsigned char* misbitfield,
  90. size_t mislen) const;
  91. void completeBlock(size_t blockIndex);
  92. void cancelBlock(size_t blockIndex);
  93. size_t countCompleteBlock() const;
  94. size_t countMissingBlock() const;
  95. bool hasBlock(size_t blockIndex) const;
  96. /**
  97. * Returns true if all blocks of this piece have been downloaded, otherwise
  98. * returns false.
  99. */
  100. bool pieceComplete() const;
  101. size_t countBlock() const;
  102. int32_t getBlockLength(size_t index) const;
  103. int32_t getBlockLength() const;
  104. size_t getIndex() const { return index_; }
  105. void setIndex(size_t index) { index_ = index; }
  106. int32_t getLength() const { return length_; }
  107. void setLength(int32_t length) { length_ = length; }
  108. const unsigned char* getBitfield() const;
  109. void setBitfield(const unsigned char* bitfield, size_t len);
  110. size_t getBitfieldLength() const;
  111. void clearAllBlock(WrDiskCache* diskCache);
  112. void setAllBlock();
  113. std::string toString() const;
  114. bool isBlockUsed(size_t index) const;
  115. // Calculates completed length
  116. int32_t getCompletedLength();
  117. #ifdef ENABLE_MESSAGE_DIGEST
  118. void setHashType(const std::string& hashType);
  119. // Updates hash value. This function compares begin and private variable
  120. // nextBegin_ and only when they are equal, hash is updated eating data and
  121. // returns true. Otherwise returns false.
  122. bool updateHash(int32_t begin, const unsigned char* data, size_t dataLength);
  123. bool isHashCalculated() const;
  124. // Returns raw hash value, not hex digest, which is calculated
  125. // by updateHash(). Please note that this function returns hash
  126. // value only once. Second invocation without updateHash() returns
  127. // empty string.
  128. std::string getDigest();
  129. void destroyHashContext();
  130. // Returns raw hash value, not hex digest, which is calculated using
  131. // cached data and data on disk.
  132. std::string getDigestWithWrCache(size_t pieceLength,
  133. const std::shared_ptr<DiskAdaptor>& adaptor);
  134. #endif // ENABLE_MESSAGE_DIGEST
  135. /**
  136. * Loses current bitfield state.
  137. */
  138. void reconfigure(int32_t length);
  139. void addUser(cuid_t cuid);
  140. void removeUser(cuid_t cuid);
  141. bool getUsed() const
  142. {
  143. return !users_.empty();
  144. }
  145. bool usedBy(cuid_t cuid) const;
  146. bool getUsedBySegment() const
  147. {
  148. return usedBySegment_;
  149. }
  150. void setUsedBySegment(bool f)
  151. {
  152. usedBySegment_ = f;
  153. }
  154. void initWrCache(WrDiskCache* diskCache,
  155. const std::shared_ptr<DiskAdaptor>& diskAdaptor);
  156. void flushWrCache(WrDiskCache* diskCache);
  157. void clearWrCache(WrDiskCache* diskCache);
  158. void updateWrCache(WrDiskCache* diskCache, unsigned char* data,
  159. size_t offset, size_t len, size_t capacity, int64_t goff);
  160. void updateWrCache(WrDiskCache* diskCache, unsigned char* data,
  161. size_t offset, size_t len, int64_t goff)
  162. {
  163. updateWrCache(diskCache, data, offset, len, len, goff);
  164. }
  165. size_t appendWrCache(WrDiskCache* diskCache, int64_t goff,
  166. const unsigned char* data, size_t len);
  167. void releaseWrCache(WrDiskCache* diskCache);
  168. WrDiskCacheEntry* getWrDiskCacheEntry() const
  169. {
  170. return wrCache_;
  171. }
  172. };
  173. } // namespace aria2
  174. #endif // D_PIECE_H