Menu

[c3f7b3]: / dcpp / Transfer.cpp  Maximize  Restore  History

Download this file

108 lines (88 with data), 3.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
/*
* Copyright (C) 2001-2025 Jacek Sieka, arnetheduck on gmail point com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "stdinc.h"
#include "Transfer.h"
#include "UserConnection.h"
#include "ClientManager.h"
#include "format.h"
namespace dcpp {
const string Transfer::names[] = {
"file", "file", "list", "tthl"
};
const string Transfer::USER_LIST_NAME = "files.xml";
const string Transfer::USER_LIST_NAME_BZ = "files.xml.bz2";
Transfer::Transfer(UserConnection& conn, const string& path_, const TTHValue& tth_) : segment(0, -1), type(TYPE_FILE), start(0),
path(path_), tth(tth_), actual(0), pos(0), userConnection(conn) { }
void Transfer::tick() {
Lock l(cs);
uint64_t t = GET_TICK();
if(samples.size() >= 1) {
int64_t tdiff = samples.back().first - samples.front().first;
if((tdiff / 1000) > MIN_SECS) {
while(samples.size() >= MIN_SAMPLES) {
samples.pop_front();
}
}
}
if(samples.size() > 1) {
if(samples.back().second == pos) {
// Position hasn't changed, just update the time
samples.back().first = t;
return;
}
}
samples.emplace_back(t, pos);
}
double Transfer::getAverageSpeed() const {
Lock l(cs);
if(samples.size() < 2) {
return 0;
}
uint64_t ticks = samples.back().first - samples.front().first;
int64_t bytes = samples.back().second - samples.front().second;
return ticks > 0 ? (static_cast<double>(bytes) / ticks) * 1000.0 : 0;
}
void Transfer::getParams(const UserConnection& aSource, ParamMap& params) {
params["userCID"] = aSource.getUser()->getCID().toBase32();
params["userNI"] = Util::toString(ClientManager::getInstance()->getNicks(aSource.getUser()->getCID()));
params["userI4"] = aSource.getRemoteIp();
StringList hubNames = ClientManager::getInstance()->getHubNames(aSource.getUser()->getCID());
if(hubNames.empty())
hubNames.push_back(_("Offline"));
params["hub"] = Util::toString(hubNames);
StringList hubs = ClientManager::getInstance()->getHubUrls(aSource.getUser()->getCID());
if(hubs.empty())
hubs.push_back(_("Offline"));
params["hubURL"] = Util::toString(hubs);
params["fileSI"] = std::to_string(getSize());
params["fileSIshort"] = Util::formatBytes(getSize());
params["fileSIactual"] = std::to_string(getActual());
params["fileSIactualshort"] = Util::formatBytes(getActual());
params["speed"] = str(F_("%1%/s") % Util::formatBytes(getAverageSpeed()));
params["time"] = Util::formatSeconds((GET_TICK() - getStart()) / 1000);
params["fileTR"] = getTTH().toBase32();
}
UserPtr Transfer::getUser() {
return getUserConnection().getUser();
}
const UserPtr Transfer::getUser() const {
return getUserConnection().getUser();
}
HintedUser Transfer::getHintedUser() const {
return getUserConnection().getHintedUser();
}
} // namespace dcpp
MongoDB Logo MongoDB