author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 15 Mar 2010 12:43:09 +0200 | |
branch | RCL_3 |
changeset 6 | dee5afe5301f |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
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 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
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 |