1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the examples of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #ifndef PEERWIRECLIENT_H
|
---|
43 | #define PEERWIRECLIENT_H
|
---|
44 |
|
---|
45 | #include <QBitArray>
|
---|
46 | #include <QList>
|
---|
47 | #include <QTcpSocket>
|
---|
48 |
|
---|
49 | QT_BEGIN_NAMESPACE
|
---|
50 | class QHostAddress;
|
---|
51 | class QTimerEvent;
|
---|
52 | QT_END_NAMESPACE
|
---|
53 | class TorrentPeer;
|
---|
54 |
|
---|
55 | struct TorrentBlock
|
---|
56 | {
|
---|
57 | inline TorrentBlock(int p, int o, int l)
|
---|
58 | : pieceIndex(p), offset(o), length(l)
|
---|
59 | {
|
---|
60 | }
|
---|
61 | inline bool operator==(const TorrentBlock &other) const
|
---|
62 | {
|
---|
63 | return pieceIndex == other.pieceIndex
|
---|
64 | && offset == other.offset
|
---|
65 | && length == other.length;
|
---|
66 | }
|
---|
67 |
|
---|
68 | int pieceIndex;
|
---|
69 | int offset;
|
---|
70 | int length;
|
---|
71 | };
|
---|
72 |
|
---|
73 | class PeerWireClient : public QTcpSocket
|
---|
74 | {
|
---|
75 | Q_OBJECT
|
---|
76 |
|
---|
77 | public:
|
---|
78 | enum PeerWireStateFlag {
|
---|
79 | ChokingPeer = 0x1,
|
---|
80 | InterestedInPeer = 0x2,
|
---|
81 | ChokedByPeer = 0x4,
|
---|
82 | PeerIsInterested = 0x8
|
---|
83 | };
|
---|
84 | Q_DECLARE_FLAGS(PeerWireState, PeerWireStateFlag)
|
---|
85 |
|
---|
86 | PeerWireClient(const QByteArray &peerId, QObject *parent = 0);
|
---|
87 | void initialize(const QByteArray &infoHash, int pieceCount);
|
---|
88 |
|
---|
89 | void setPeer(TorrentPeer *peer);
|
---|
90 | TorrentPeer *peer() const;
|
---|
91 |
|
---|
92 | // State
|
---|
93 | inline PeerWireState peerWireState() const { return pwState; }
|
---|
94 | QBitArray availablePieces() const;
|
---|
95 | QList<TorrentBlock> incomingBlocks() const;
|
---|
96 |
|
---|
97 | // Protocol
|
---|
98 | void chokePeer();
|
---|
99 | void unchokePeer();
|
---|
100 | void sendInterested();
|
---|
101 | void sendKeepAlive();
|
---|
102 | void sendNotInterested();
|
---|
103 | void sendPieceNotification(int piece);
|
---|
104 | void sendPieceList(const QBitArray &bitField);
|
---|
105 | void requestBlock(int piece, int offset, int length);
|
---|
106 | void cancelRequest(int piece, int offset, int length);
|
---|
107 | void sendBlock(int piece, int offset, const QByteArray &data);
|
---|
108 |
|
---|
109 | // Rate control
|
---|
110 | qint64 writeToSocket(qint64 bytes);
|
---|
111 | qint64 readFromSocket(qint64 bytes);
|
---|
112 | qint64 downloadSpeed() const;
|
---|
113 | qint64 uploadSpeed() const;
|
---|
114 |
|
---|
115 | bool canTransferMore() const;
|
---|
116 | qint64 bytesAvailable() const { return incomingBuffer.size() + QTcpSocket::bytesAvailable(); }
|
---|
117 | qint64 socketBytesAvailable() const { return socket.bytesAvailable(); }
|
---|
118 | qint64 socketBytesToWrite() const { return socket.bytesToWrite(); }
|
---|
119 |
|
---|
120 | void setReadBufferSize(int size);
|
---|
121 |
|
---|
122 | signals:
|
---|
123 | void infoHashReceived(const QByteArray &infoHash);
|
---|
124 | void readyToTransfer();
|
---|
125 |
|
---|
126 | void choked();
|
---|
127 | void unchoked();
|
---|
128 | void interested();
|
---|
129 | void notInterested();
|
---|
130 |
|
---|
131 | void piecesAvailable(const QBitArray &pieces);
|
---|
132 | void blockRequested(int pieceIndex, int begin, int length);
|
---|
133 | void blockReceived(int pieceIndex, int begin, const QByteArray &data);
|
---|
134 |
|
---|
135 | void bytesReceived(qint64 size);
|
---|
136 |
|
---|
137 | protected slots:
|
---|
138 | void connectToHostImplementation(const QString &hostName,
|
---|
139 | quint16 port, OpenMode openMode = ReadWrite);
|
---|
140 | void diconnectFromHostImplementation();
|
---|
141 |
|
---|
142 | protected:
|
---|
143 | void timerEvent(QTimerEvent *event);
|
---|
144 |
|
---|
145 | qint64 readData(char *data, qint64 maxlen);
|
---|
146 | qint64 readLineData(char *data, qint64 maxlen);
|
---|
147 | qint64 writeData(const char *data, qint64 len);
|
---|
148 |
|
---|
149 | private slots:
|
---|
150 | void sendHandShake();
|
---|
151 | void processIncomingData();
|
---|
152 | void socketStateChanged(QAbstractSocket::SocketState state);
|
---|
153 |
|
---|
154 | private:
|
---|
155 | // Data waiting to be read/written
|
---|
156 | QByteArray incomingBuffer;
|
---|
157 | QByteArray outgoingBuffer;
|
---|
158 |
|
---|
159 | struct BlockInfo {
|
---|
160 | int pieceIndex;
|
---|
161 | int offset;
|
---|
162 | int length;
|
---|
163 | QByteArray block;
|
---|
164 | };
|
---|
165 | QList<BlockInfo> pendingBlocks;
|
---|
166 | int pendingBlockSizes;
|
---|
167 | QList<TorrentBlock> incoming;
|
---|
168 |
|
---|
169 | enum PacketType {
|
---|
170 | ChokePacket = 0,
|
---|
171 | UnchokePacket = 1,
|
---|
172 | InterestedPacket = 2,
|
---|
173 | NotInterestedPacket = 3,
|
---|
174 | HavePacket = 4,
|
---|
175 | BitFieldPacket = 5,
|
---|
176 | RequestPacket = 6,
|
---|
177 | PiecePacket = 7,
|
---|
178 | CancelPacket = 8
|
---|
179 | };
|
---|
180 |
|
---|
181 | // State
|
---|
182 | PeerWireState pwState;
|
---|
183 | bool receivedHandShake;
|
---|
184 | bool gotPeerId;
|
---|
185 | bool sentHandShake;
|
---|
186 | int nextPacketLength;
|
---|
187 |
|
---|
188 | // Upload/download speed records
|
---|
189 | qint64 uploadSpeedData[8];
|
---|
190 | qint64 downloadSpeedData[8];
|
---|
191 | int transferSpeedTimer;
|
---|
192 |
|
---|
193 | // Timeout handling
|
---|
194 | int timeoutTimer;
|
---|
195 | int pendingRequestTimer;
|
---|
196 | bool invalidateTimeout;
|
---|
197 | int keepAliveTimer;
|
---|
198 |
|
---|
199 | // Checksum, peer ID and set of available pieces
|
---|
200 | QByteArray infoHash;
|
---|
201 | QByteArray peerIdString;
|
---|
202 | QBitArray peerPieces;
|
---|
203 | TorrentPeer *torrentPeer;
|
---|
204 |
|
---|
205 | QTcpSocket socket;
|
---|
206 | };
|
---|
207 |
|
---|
208 | Q_DECLARE_OPERATORS_FOR_FLAGS(PeerWireClient::PeerWireState)
|
---|
209 |
|
---|
210 | #endif
|
---|