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_BT_PIECE_MESSAGE_H
36 #define D_BT_PIECE_MESSAGE_H
37 
38 #include "AbstractBtMessage.h"
39 
40 namespace aria2 {
41 
42 class Piece;
43 class DownloadContext;
44 class PeerStorage;
45 
46 class BtPieceMessage : public AbstractBtMessage {
47 private:
48   size_t index_;
49   int32_t begin_;
50   int32_t blockLength_;
51   const unsigned char* data_;
52   DownloadContext* downloadContext_;
53   PeerStorage* peerStorage_;
54 
55   bool checkPieceHash(const std::shared_ptr<Piece>& piece);
56 
57   void onNewPiece(const std::shared_ptr<Piece>& piece);
58 
59   void onWrongPiece(const std::shared_ptr<Piece>& piece);
60 
61   void pushPieceData(int64_t offset, int32_t length) const;
62 
63 public:
64   BtPieceMessage(size_t index = 0, int32_t begin = 0, int32_t blockLength = 0);
65 
66   virtual ~BtPieceMessage();
67 
68   static const uint8_t ID = 7;
69 
70   static const char NAME[];
71 
getIndex()72   size_t getIndex() const { return index_; }
73 
setIndex(size_t index)74   void setIndex(size_t index) { index_ = index; }
75 
getBegin()76   int32_t getBegin() const { return begin_; }
77 
setBegin(int32_t begin)78   void setBegin(int32_t begin) { begin_ = begin; }
79 
getBlock()80   const unsigned char* getBlock() const { return data_ + 9; }
81 
getBlockLength()82   int32_t getBlockLength() const { return blockLength_; }
83 
84   // Sets message payload data. Caller must not change or free data
85   // before doReceivedAction().
86   void setMsgPayload(const unsigned char* data);
87 
setBlockLength(int32_t blockLength)88   void setBlockLength(int32_t blockLength) { blockLength_ = blockLength; }
89 
90   void setDownloadContext(DownloadContext* downloadContext);
91 
92   void setPeerStorage(PeerStorage* peerStorage);
93 
94   static std::unique_ptr<BtPieceMessage> create(const unsigned char* data,
95                                                 size_t dataLength);
96 
97   virtual void doReceivedAction() CXX11_OVERRIDE;
98 
99   void createMessageHeader(unsigned char* msgHeader) const;
100 
101   size_t getMessageHeaderLength();
102 
103   virtual void send() CXX11_OVERRIDE;
104 
105   virtual std::string toString() const CXX11_OVERRIDE;
106 
107   virtual void onChokingEvent(const BtChokingEvent& event) CXX11_OVERRIDE;
108 
109   virtual void onCancelSendingPieceEvent(const BtCancelSendingPieceEvent& event)
110       CXX11_OVERRIDE;
111 };
112 
113 } // namespace aria2
114 
115 #endif // D_BT_PIECE_MESSAGE_H
116