author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 18 | 2f34d5167611 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#ifndef PEERWIRECLIENT_H |
|
42 |
#define PEERWIRECLIENT_H |
|
43 |
||
44 |
#include <QBitArray> |
|
45 |
#include <QList> |
|
46 |
#include <QTcpSocket> |
|
47 |
||
48 |
QT_BEGIN_NAMESPACE |
|
49 |
class QHostAddress; |
|
50 |
class QTimerEvent; |
|
51 |
QT_END_NAMESPACE |
|
52 |
class TorrentPeer; |
|
53 |
||
54 |
struct TorrentBlock |
|
55 |
{ |
|
56 |
inline TorrentBlock(int p, int o, int l) |
|
57 |
: pieceIndex(p), offset(o), length(l) |
|
58 |
{ |
|
59 |
} |
|
60 |
inline bool operator==(const TorrentBlock &other) const |
|
61 |
{ |
|
62 |
return pieceIndex == other.pieceIndex |
|
63 |
&& offset == other.offset |
|
64 |
&& length == other.length; |
|
65 |
} |
|
66 |
||
67 |
int pieceIndex; |
|
68 |
int offset; |
|
69 |
int length; |
|
70 |
}; |
|
71 |
||
72 |
class PeerWireClient : public QTcpSocket |
|
73 |
{ |
|
74 |
Q_OBJECT |
|
75 |
||
76 |
public: |
|
77 |
enum PeerWireStateFlag { |
|
78 |
ChokingPeer = 0x1, |
|
79 |
InterestedInPeer = 0x2, |
|
80 |
ChokedByPeer = 0x4, |
|
81 |
PeerIsInterested = 0x8 |
|
82 |
}; |
|
83 |
Q_DECLARE_FLAGS(PeerWireState, PeerWireStateFlag) |
|
84 |
||
85 |
PeerWireClient(const QByteArray &peerId, QObject *parent = 0); |
|
86 |
void initialize(const QByteArray &infoHash, int pieceCount); |
|
87 |
||
88 |
void setPeer(TorrentPeer *peer); |
|
89 |
TorrentPeer *peer() const; |
|
90 |
||
91 |
// State |
|
92 |
inline PeerWireState peerWireState() const { return pwState; } |
|
93 |
QBitArray availablePieces() const; |
|
94 |
QList<TorrentBlock> incomingBlocks() const; |
|
95 |
||
96 |
// Protocol |
|
97 |
void chokePeer(); |
|
98 |
void unchokePeer(); |
|
99 |
void sendInterested(); |
|
100 |
void sendKeepAlive(); |
|
101 |
void sendNotInterested(); |
|
102 |
void sendPieceNotification(int piece); |
|
103 |
void sendPieceList(const QBitArray &bitField); |
|
104 |
void requestBlock(int piece, int offset, int length); |
|
105 |
void cancelRequest(int piece, int offset, int length); |
|
106 |
void sendBlock(int piece, int offset, const QByteArray &data); |
|
107 |
||
108 |
// Rate control |
|
109 |
qint64 writeToSocket(qint64 bytes); |
|
110 |
qint64 readFromSocket(qint64 bytes); |
|
111 |
qint64 downloadSpeed() const; |
|
112 |
qint64 uploadSpeed() const; |
|
113 |
||
114 |
bool canTransferMore() const; |
|
115 |
qint64 bytesAvailable() const { return incomingBuffer.size() + QTcpSocket::bytesAvailable(); } |
|
116 |
qint64 socketBytesAvailable() const { return socket.bytesAvailable(); } |
|
117 |
qint64 socketBytesToWrite() const { return socket.bytesToWrite(); } |
|
118 |
||
119 |
void setReadBufferSize(int size); |
|
120 |
||
121 |
signals: |
|
122 |
void infoHashReceived(const QByteArray &infoHash); |
|
123 |
void readyToTransfer(); |
|
124 |
||
125 |
void choked(); |
|
126 |
void unchoked(); |
|
127 |
void interested(); |
|
128 |
void notInterested(); |
|
129 |
||
130 |
void piecesAvailable(const QBitArray &pieces); |
|
131 |
void blockRequested(int pieceIndex, int begin, int length); |
|
132 |
void blockReceived(int pieceIndex, int begin, const QByteArray &data); |
|
133 |
||
134 |
void bytesReceived(qint64 size); |
|
135 |
||
136 |
protected slots: |
|
137 |
void connectToHostImplementation(const QString &hostName, |
|
138 |
quint16 port, OpenMode openMode = ReadWrite); |
|
139 |
void diconnectFromHostImplementation(); |
|
140 |
||
141 |
protected: |
|
142 |
void timerEvent(QTimerEvent *event); |
|
143 |
||
144 |
qint64 readData(char *data, qint64 maxlen); |
|
145 |
qint64 readLineData(char *data, qint64 maxlen); |
|
146 |
qint64 writeData(const char *data, qint64 len); |
|
147 |
||
148 |
private slots: |
|
149 |
void sendHandShake(); |
|
150 |
void processIncomingData(); |
|
151 |
void socketStateChanged(QAbstractSocket::SocketState state); |
|
152 |
||
153 |
private: |
|
154 |
// Data waiting to be read/written |
|
155 |
QByteArray incomingBuffer; |
|
156 |
QByteArray outgoingBuffer; |
|
157 |
||
158 |
struct BlockInfo { |
|
159 |
int pieceIndex; |
|
160 |
int offset; |
|
161 |
int length; |
|
162 |
QByteArray block; |
|
163 |
}; |
|
164 |
QList<BlockInfo> pendingBlocks; |
|
165 |
int pendingBlockSizes; |
|
166 |
QList<TorrentBlock> incoming; |
|
167 |
||
168 |
enum PacketType { |
|
169 |
ChokePacket = 0, |
|
170 |
UnchokePacket = 1, |
|
171 |
InterestedPacket = 2, |
|
172 |
NotInterestedPacket = 3, |
|
173 |
HavePacket = 4, |
|
174 |
BitFieldPacket = 5, |
|
175 |
RequestPacket = 6, |
|
176 |
PiecePacket = 7, |
|
177 |
CancelPacket = 8 |
|
178 |
}; |
|
179 |
||
180 |
// State |
|
181 |
PeerWireState pwState; |
|
182 |
bool receivedHandShake; |
|
183 |
bool gotPeerId; |
|
184 |
bool sentHandShake; |
|
185 |
int nextPacketLength; |
|
186 |
||
187 |
// Upload/download speed records |
|
188 |
qint64 uploadSpeedData[8]; |
|
189 |
qint64 downloadSpeedData[8]; |
|
190 |
int transferSpeedTimer; |
|
191 |
||
192 |
// Timeout handling |
|
193 |
int timeoutTimer; |
|
194 |
int pendingRequestTimer; |
|
195 |
bool invalidateTimeout; |
|
196 |
int keepAliveTimer; |
|
197 |
||
198 |
// Checksum, peer ID and set of available pieces |
|
199 |
QByteArray infoHash; |
|
200 |
QByteArray peerIdString; |
|
201 |
QBitArray peerPieces; |
|
202 |
TorrentPeer *torrentPeer; |
|
203 |
||
204 |
QTcpSocket socket; |
|
205 |
}; |
|
206 |
||
207 |
Q_DECLARE_OPERATORS_FOR_FLAGS(PeerWireClient::PeerWireState) |
|
208 |
||
209 |
#endif |