1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "peerwireclient.h"
52#include "ratecontroller.h"
53
54#include <QtCore>
55
56Q_GLOBAL_STATIC(RateController, rateController)
57
58RateController *RateController::instance()
59{
60 return rateController();
61}
62
63void RateController::addSocket(PeerWireClient *socket)
64{
65 connect(sender: socket, signal: &PeerWireClient::readyToTransfer,
66 receiver: this, slot: &RateController::scheduleTransfer);
67 socket->setReadBufferSize(downLimit * 4);
68 sockets << socket;
69 scheduleTransfer();
70}
71
72void RateController::removeSocket(PeerWireClient *socket)
73{
74 disconnect(sender: socket, signal: &PeerWireClient::readyToTransfer,
75 receiver: this, slot: &RateController::scheduleTransfer);
76 socket->setReadBufferSize(0);
77 sockets.remove(value: socket);
78}
79
80void RateController::setDownloadLimit(int bytesPerSecond)
81{
82 downLimit = bytesPerSecond;
83 for (PeerWireClient *socket : qAsConst(t&: sockets))
84 socket->setReadBufferSize(downLimit * 4);
85}
86
87void RateController::scheduleTransfer()
88{
89 if (transferScheduled)
90 return;
91 transferScheduled = true;
92 QTimer::singleShot(msec: 50, receiver: this, SLOT(transfer()));
93}
94
95void RateController::transfer()
96{
97 transferScheduled = false;
98 if (sockets.isEmpty())
99 return;
100
101 qint64 msecs = 1000;
102 if (stopWatch.isValid())
103 msecs = qMin(a: msecs, b: stopWatch.elapsed());
104
105 qint64 bytesToWrite = (upLimit * msecs) / 1000;
106 qint64 bytesToRead = (downLimit * msecs) / 1000;
107 if (bytesToWrite == 0 && bytesToRead == 0) {
108 scheduleTransfer();
109 return;
110 }
111
112 QSet<PeerWireClient *> pendingSockets;
113 for (PeerWireClient *client : qAsConst(t&: sockets)) {
114 if (client->canTransferMore())
115 pendingSockets << client;
116 }
117 if (pendingSockets.isEmpty())
118 return;
119
120 stopWatch.start();
121
122 bool canTransferMore;
123 do {
124 canTransferMore = false;
125 qint64 writeChunk = qMax<qint64>(a: 1, b: bytesToWrite / pendingSockets.size());
126 qint64 readChunk = qMax<qint64>(a: 1, b: bytesToRead / pendingSockets.size());
127
128 for (auto it = pendingSockets.begin(), end = pendingSockets.end(); it != end && (bytesToWrite > 0 || bytesToRead > 0); /*erasing*/) {
129 auto current = it++;
130 PeerWireClient *socket = *current;
131 if (socket->state() != QAbstractSocket::ConnectedState) {
132 pendingSockets.erase(i: current);
133 continue;
134 }
135
136 bool dataTransferred = false;
137 qint64 available = qMin<qint64>(a: socket->socketBytesAvailable(), b: readChunk);
138 if (available > 0) {
139 qint64 readBytes = socket->readFromSocket(bytes: qMin<qint64>(a: available, b: bytesToRead));
140 if (readBytes > 0) {
141 bytesToRead -= readBytes;
142 dataTransferred = true;
143 }
144 }
145
146 if (upLimit * 2 > socket->bytesToWrite()) {
147 qint64 chunkSize = qMin<qint64>(a: writeChunk, b: bytesToWrite);
148 qint64 toWrite = qMin(a: upLimit * 2 - socket->bytesToWrite(), b: chunkSize);
149 if (toWrite > 0) {
150 qint64 writtenBytes = socket->writeToSocket(bytes: toWrite);
151 if (writtenBytes > 0) {
152 bytesToWrite -= writtenBytes;
153 dataTransferred = true;
154 }
155 }
156 }
157
158 if (dataTransferred && socket->canTransferMore())
159 canTransferMore = true;
160 else
161 pendingSockets.erase(i: current);
162 }
163 } while (canTransferMore && (bytesToWrite > 0 || bytesToRead > 0) && !pendingSockets.isEmpty());
164
165 if (canTransferMore || bytesToWrite == 0 || bytesToRead == 0)
166 scheduleTransfer();
167}
168

source code of qtbase/examples/network/torrent/ratecontroller.cpp