1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the examples of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:BSD$
9
** You may use this file under the terms of the BSD license as follows:
11
** "Redistribution and use in source and binary forms, with or without
12
** modification, are permitted provided that the following conditions are
14
** * Redistributions of source code must retain the above copyright
15
** notice, this list of conditions and the following disclaimer.
16
** * Redistributions in binary form must reproduce the above copyright
17
** notice, this list of conditions and the following disclaimer in
18
** the documentation and/or other materials provided with the
20
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21
** of its contributors may be used to endorse or promote products derived
22
** from this software without specific prior written permission.
25
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39
****************************************************************************/
41
#ifndef PEERWIRECLIENT_H
42
#define PEERWIRECLIENT_H
56
inline TorrentBlock(int p, int o, int l)
57
: pieceIndex(p), offset(o), length(l)
60
inline bool operator==(const TorrentBlock &other) const
62
return pieceIndex == other.pieceIndex
63
&& offset == other.offset
64
&& length == other.length;
72
class PeerWireClient : public QTcpSocket
77
enum PeerWireStateFlag {
79
InterestedInPeer = 0x2,
81
PeerIsInterested = 0x8
83
Q_DECLARE_FLAGS(PeerWireState, PeerWireStateFlag)
85
explicit PeerWireClient(const QByteArray &peerId, QObject *parent = 0);
86
void initialize(const QByteArray &infoHash, int pieceCount);
88
void setPeer(TorrentPeer *peer);
89
TorrentPeer *peer() const;
92
inline PeerWireState peerWireState() const { return pwState; }
93
QBitArray availablePieces() const;
94
QList<TorrentBlock> incomingBlocks() const;
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);
109
qint64 writeToSocket(qint64 bytes);
110
qint64 readFromSocket(qint64 bytes);
111
qint64 downloadSpeed() const;
112
qint64 uploadSpeed() const;
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(); }
119
void setReadBufferSize(qint64 size);
122
void infoHashReceived(const QByteArray &infoHash);
123
void readyToTransfer();
128
void notInterested();
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);
134
void bytesReceived(qint64 size);
137
void connectToHostImplementation(const QString &hostName,
138
quint16 port, OpenMode openMode = ReadWrite);
139
void diconnectFromHostImplementation();
142
void timerEvent(QTimerEvent *event);
144
qint64 readData(char *data, qint64 maxlen);
145
qint64 readLineData(char *data, qint64 maxlen);
146
qint64 writeData(const char *data, qint64 len);
149
void sendHandShake();
150
void processIncomingData();
151
void socketStateChanged(QAbstractSocket::SocketState state);
154
// Data waiting to be read/written
155
QByteArray incomingBuffer;
156
QByteArray outgoingBuffer;
164
QList<BlockInfo> pendingBlocks;
165
int pendingBlockSizes;
166
QList<TorrentBlock> incoming;
171
InterestedPacket = 2,
172
NotInterestedPacket = 3,
181
PeerWireState pwState;
182
bool receivedHandShake;
185
int nextPacketLength;
187
// Upload/download speed records
188
qint64 uploadSpeedData[8];
189
qint64 downloadSpeedData[8];
190
int transferSpeedTimer;
194
int pendingRequestTimer;
195
bool invalidateTimeout;
198
// Checksum, peer ID and set of available pieces
200
QByteArray peerIdString;
201
QBitArray peerPieces;
202
TorrentPeer *torrentPeer;
207
Q_DECLARE_OPERATORS_FOR_FLAGS(PeerWireClient::PeerWireState)