libktorrent  2.2.0
piecedata.h
1 /***************************************************************************
2  * Copyright (C) 2008 by Joris Guisson and Ivan Vasic *
3  * joris.guisson@gmail.com *
4  * ivasic@gmail.com *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License as published by *
8  * the Free Software Foundation; either version 2 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this program; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20  ***************************************************************************/
21 #ifndef BTPIECEDATA_H
22 #define BTPIECEDATA_H
23 
24 #include <ktorrent_export.h>
25 #include <QSharedDataPointer>
26 #include <util/constants.h>
27 #ifndef Q_WS_WIN
28 #include <util/signalcatcher.h>
29 #endif
30 #include <diskio/cachefile.h>
31 
32 
33 namespace bt
34 {
35 
36  class File;
37  class Chunk;
38  class SHA1Hash;
39  class SHA1HashGen;
40 
45  class KTORRENT_EXPORT PieceData : public QSharedData, public MMappeable
46  {
47  public:
48  PieceData(Chunk* chunk, Uint32 off, Uint32 len, Uint8* ptr, CacheFile::Ptr cache_file, bool read_only);
49  ~PieceData() override;
50 
52  void unload();
53 
55  bool mapped() const {return cache_file != 0;}
56 
58  bool writeable() const {return !read_only;}
59 
61  Uint32 offset() const {return off;}
62 
64  Uint32 length() const {return len;}
65 
67  Uint8* data() {return ptr;}
68 
70  bool ok() const {return ptr != 0;}
71 
73  void setData(Uint8* p) {ptr = p;}
74 
76  Chunk* parentChunk() {return chunk;}
77 
87  Uint32 write(const Uint8* buf, Uint32 buf_size, Uint32 off = 0);
88 
98  Uint32 read(Uint8* buf, Uint32 to_read, Uint32 off = 0);
99 
108  Uint32 writeToFile(File & file, Uint32 size, Uint32 off = 0);
109 
118  Uint32 readFromFile(File & file, Uint32 size, Uint32 off = 0);
119 
125  void updateHash(SHA1HashGen & hg);
126 
132  SHA1Hash generateHash() const;
133 
134  typedef QExplicitlySharedDataPointer<PieceData> Ptr;
135 
137  bool inUse() const {return ref > 1;}
138 
139  private:
140  void unmapped() override;
141 
142  private:
143  Chunk* chunk;
144  Uint32 off;
145  Uint32 len;
146  Uint8* ptr;
147  CacheFile::Ptr cache_file;
148  bool read_only;
149  };
150 
151 
152 
153 }
154 
155 #endif
bt::Chunk
Keep track of a piece of the file.
Definition: chunk.h:64
bt::File
Wrapper class for stdio's FILE.
Definition: file.h:56
bt::SHA1HashGen
Definition: sha1hashgen.h:46
bt::SHA1Hash
Stores a SHA1 hash.
Definition: sha1hash.h:41