~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to examples/network/torrent/peerwireclient.h

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the examples of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:BSD$
 
9
** You may use this file under the terms of the BSD license as follows:
 
10
**
 
11
** "Redistribution and use in source and binary forms, with or without
 
12
** modification, are permitted provided that the following conditions are
 
13
** met:
 
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
 
19
**     distribution.
 
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.
 
23
**
 
24
**
 
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."
 
36
**
 
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
    explicit 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(qint64 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