2010-01-12 03:57:25 +01:00
|
|
|
/*
|
|
|
|
|
2010-01-29 07:13:02 +01:00
|
|
|
Copyright (c) 2008, Arvid Norberg
|
2010-01-12 03:57:25 +01:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2010-01-29 07:13:02 +01:00
|
|
|
#include "libtorrent/peer_id.hpp"
|
|
|
|
#include "libtorrent/io_service.hpp"
|
|
|
|
#include "libtorrent/socket.hpp"
|
|
|
|
#include "libtorrent/address.hpp"
|
|
|
|
#include "libtorrent/error_code.hpp"
|
|
|
|
#include "libtorrent/io.hpp"
|
|
|
|
#include "libtorrent/torrent_info.hpp"
|
2011-04-22 03:12:28 +02:00
|
|
|
#include "libtorrent/create_torrent.hpp"
|
|
|
|
#include "libtorrent/hasher.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/socket_io.hpp"
|
2014-07-11 00:23:26 +02:00
|
|
|
#include "libtorrent/file_pool.hpp"
|
2017-03-18 17:19:49 +01:00
|
|
|
#include "libtorrent/string_view.hpp"
|
2016-10-03 06:17:14 +02:00
|
|
|
#include <random>
|
2010-01-29 07:13:02 +01:00
|
|
|
#include <cstring>
|
2016-05-01 00:54:23 +02:00
|
|
|
#include <thread>
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <functional>
|
2010-01-29 07:13:02 +01:00
|
|
|
#include <iostream>
|
2017-03-12 01:03:19 +01:00
|
|
|
#include <atomic>
|
2016-05-01 05:10:47 +02:00
|
|
|
#include <array>
|
2018-02-04 01:57:54 +01:00
|
|
|
#include <chrono>
|
2010-01-29 07:13:02 +01:00
|
|
|
|
2014-09-13 21:47:51 +02:00
|
|
|
#if BOOST_ASIO_DYN_LINK
|
|
|
|
#include <boost/asio/impl/src.hpp>
|
|
|
|
#endif
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
|
|
|
using namespace lt::detail; // for write_* and read_*
|
2010-01-29 07:13:02 +01:00
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
using namespace std::placeholders;
|
|
|
|
|
2017-05-05 14:54:02 +02:00
|
|
|
void generate_block(span<std::uint32_t> buffer, piece_index_t const piece
|
|
|
|
, int const offset)
|
2011-04-22 03:12:28 +02:00
|
|
|
{
|
2017-05-05 14:54:02 +02:00
|
|
|
std::uint32_t const fill = (static_cast<int>(piece) << 8) | ((offset / 0x4000) & 0xff);
|
|
|
|
for (auto& w : buffer) w = fill;
|
2011-04-22 03:12:28 +02:00
|
|
|
}
|
|
|
|
|
2011-07-23 23:09:10 +02:00
|
|
|
// in order to circumvent the restricton of only
|
|
|
|
// one connection per IP that most clients implement
|
|
|
|
// all sockets created by this tester are bound to
|
|
|
|
// uniqe local IPs in the range (127.0.0.1 - 127.255.255.255)
|
|
|
|
// it's only enabled if the target is also on the loopback
|
|
|
|
int local_if_counter = 0;
|
|
|
|
bool local_bind = false;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// when set to true, blocks downloaded are verified to match
|
|
|
|
// the test torrents
|
|
|
|
bool verify_downloads = false;
|
|
|
|
|
2013-04-13 23:16:04 +02:00
|
|
|
// if this is true, one block in 1000 will be sent corrupt.
|
|
|
|
// this only applies to dual and upload tests
|
|
|
|
bool test_corruption = false;
|
|
|
|
|
2011-11-27 22:15:33 +01:00
|
|
|
// number of seeds we've spawned. The test is terminated
|
|
|
|
// when this reaches zero, for dual tests
|
2017-03-12 01:03:19 +01:00
|
|
|
std::atomic<int> num_seeds(0);
|
2011-11-27 22:15:33 +01:00
|
|
|
|
|
|
|
// the kind of test to run. Upload sends data to a
|
|
|
|
// bittorrent client, download requests data from
|
|
|
|
// a client and dual uploads and downloads from a client
|
|
|
|
// at the same time (this is presumably the most realistic
|
|
|
|
// test)
|
2014-07-05 16:10:25 +02:00
|
|
|
enum test_mode_t{ none, upload_test, download_test, dual_test };
|
|
|
|
test_mode_t test_mode = none;
|
2011-11-27 22:15:33 +01:00
|
|
|
|
|
|
|
// the number of suggest messages received (total across all peers)
|
2017-03-12 01:03:19 +01:00
|
|
|
std::atomic<int> num_suggest(0);
|
2011-11-27 22:15:33 +01:00
|
|
|
|
|
|
|
// the number of requests made from suggested pieces
|
2017-03-12 01:03:19 +01:00
|
|
|
std::atomic<int> num_suggested_requests(0);
|
2011-11-27 22:15:33 +01:00
|
|
|
|
2015-01-02 12:11:09 +01:00
|
|
|
std::string leaf_path(std::string f)
|
|
|
|
{
|
|
|
|
if (f.empty()) return "";
|
|
|
|
char const* first = f.c_str();
|
|
|
|
char const* sep = strrchr(first, '/');
|
|
|
|
#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2)
|
|
|
|
char const* altsep = strrchr(first, '\\');
|
|
|
|
if (sep == 0 || altsep > sep) sep = altsep;
|
|
|
|
#endif
|
2016-07-09 22:26:26 +02:00
|
|
|
if (sep == nullptr) return f;
|
2015-01-02 12:11:09 +01:00
|
|
|
|
|
|
|
if (sep - first == int(f.size()) - 1)
|
|
|
|
{
|
|
|
|
// if the last character is a / (or \)
|
|
|
|
// ignore it
|
|
|
|
int len = 0;
|
|
|
|
while (sep > first)
|
|
|
|
{
|
|
|
|
--sep;
|
|
|
|
if (*sep == '/'
|
|
|
|
#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2)
|
|
|
|
|| *sep == '\\'
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
return std::string(sep + 1, len);
|
|
|
|
++len;
|
|
|
|
}
|
|
|
|
return std::string(first, len);
|
|
|
|
}
|
|
|
|
return std::string(sep + 1);
|
|
|
|
}
|
|
|
|
|
2016-10-03 06:17:14 +02:00
|
|
|
namespace {
|
|
|
|
std::random_device dev;
|
|
|
|
std::mt19937 rng(dev());
|
|
|
|
}
|
|
|
|
|
2010-01-30 02:33:34 +01:00
|
|
|
struct peer_conn
|
2010-01-12 03:57:25 +01:00
|
|
|
{
|
2010-01-30 02:33:34 +01:00
|
|
|
peer_conn(io_service& ios, int num_pieces, int blocks_pp, tcp::endpoint const& ep
|
2014-07-06 21:18:00 +02:00
|
|
|
, char const* ih, bool seed_, int churn_, bool corrupt_)
|
2010-01-30 02:33:34 +01:00
|
|
|
: s(ios)
|
|
|
|
, read_pos(0)
|
|
|
|
, state(handshaking)
|
2014-07-06 21:18:00 +02:00
|
|
|
, choked(true)
|
|
|
|
, current_piece(-1)
|
|
|
|
, current_piece_is_allowed(false)
|
2010-01-30 02:33:34 +01:00
|
|
|
, block(0)
|
|
|
|
, blocks_per_piece(blocks_pp)
|
|
|
|
, info_hash(ih)
|
|
|
|
, outstanding_requests(0)
|
2011-04-22 03:12:28 +02:00
|
|
|
, seed(seed_)
|
2013-04-13 01:18:49 +02:00
|
|
|
, fast_extension(false)
|
2011-04-22 09:38:46 +02:00
|
|
|
, blocks_received(0)
|
2011-04-27 06:33:03 +02:00
|
|
|
, blocks_sent(0)
|
2011-04-22 09:38:46 +02:00
|
|
|
, num_pieces(num_pieces)
|
2015-03-12 05:34:54 +01:00
|
|
|
, start_time(clock_type::now())
|
2014-07-06 21:18:00 +02:00
|
|
|
, churn(churn_)
|
|
|
|
, corrupt(corrupt_)
|
|
|
|
, endpoint(ep)
|
|
|
|
, restarting(false)
|
2010-01-29 07:13:02 +01:00
|
|
|
{
|
2013-04-13 23:16:04 +02:00
|
|
|
corruption_counter = rand() % 1000;
|
2011-11-27 22:15:33 +01:00
|
|
|
if (seed) ++num_seeds;
|
2011-04-22 09:38:46 +02:00
|
|
|
pieces.reserve(num_pieces);
|
2014-07-06 21:18:00 +02:00
|
|
|
start_conn();
|
|
|
|
}
|
|
|
|
|
|
|
|
void start_conn()
|
|
|
|
{
|
2011-07-23 23:09:10 +02:00
|
|
|
if (local_bind)
|
|
|
|
{
|
|
|
|
error_code ec;
|
2014-07-06 21:18:00 +02:00
|
|
|
s.open(endpoint.protocol(), ec);
|
2011-07-23 23:09:10 +02:00
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
close("ERROR OPEN: %s", ec);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tcp::endpoint bind_if(address_v4(
|
|
|
|
(127 << 24)
|
|
|
|
+ ((local_if_counter / 255) << 16)
|
|
|
|
+ ((local_if_counter % 255) + 1)), 0);
|
|
|
|
++local_if_counter;
|
|
|
|
s.bind(bind_if, ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
close("ERROR BIND: %s", ec);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
restarting = false;
|
2016-05-25 06:31:52 +02:00
|
|
|
s.async_connect(endpoint, std::bind(&peer_conn::on_connect, this, _1));
|
2010-01-12 03:57:25 +01:00
|
|
|
}
|
|
|
|
|
2015-06-06 19:49:18 +02:00
|
|
|
tcp::socket s;
|
2011-11-27 22:15:33 +01:00
|
|
|
char write_buf_proto[100];
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint32_t write_buffer[17*1024/4];
|
|
|
|
std::uint32_t buffer[17*1024/4];
|
2010-01-30 02:33:34 +01:00
|
|
|
int read_pos;
|
2013-04-13 23:16:04 +02:00
|
|
|
int corruption_counter;
|
2010-01-12 03:57:25 +01:00
|
|
|
|
2010-01-30 02:33:34 +01:00
|
|
|
enum state_t
|
2010-01-29 07:13:02 +01:00
|
|
|
{
|
2010-01-30 02:33:34 +01:00
|
|
|
handshaking,
|
|
|
|
sending_request,
|
|
|
|
receiving_message
|
|
|
|
};
|
|
|
|
int state;
|
2016-12-22 16:42:33 +01:00
|
|
|
std::vector<piece_index_t> pieces;
|
|
|
|
std::vector<piece_index_t> suggested_pieces;
|
|
|
|
std::vector<piece_index_t> allowed_fast;
|
2014-07-06 21:18:00 +02:00
|
|
|
bool choked;
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t current_piece; // the piece we're currently requesting blocks from
|
2014-07-06 21:18:00 +02:00
|
|
|
bool current_piece_is_allowed;
|
2010-01-30 02:33:34 +01:00
|
|
|
int block;
|
|
|
|
int blocks_per_piece;
|
|
|
|
char const* info_hash;
|
|
|
|
int outstanding_requests;
|
2011-04-22 03:12:28 +02:00
|
|
|
// if this is true, this connection is a seed
|
|
|
|
bool seed;
|
2011-07-24 11:29:26 +02:00
|
|
|
bool fast_extension;
|
2011-04-22 09:38:46 +02:00
|
|
|
int blocks_received;
|
2011-04-27 06:33:03 +02:00
|
|
|
int blocks_sent;
|
2011-04-22 09:38:46 +02:00
|
|
|
int num_pieces;
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point start_time;
|
|
|
|
time_point end_time;
|
2014-07-06 21:18:00 +02:00
|
|
|
int churn;
|
|
|
|
bool corrupt;
|
|
|
|
tcp::endpoint endpoint;
|
|
|
|
bool restarting;
|
2010-01-12 03:57:25 +01:00
|
|
|
|
2010-01-30 02:33:34 +01:00
|
|
|
void on_connect(error_code const& ec)
|
2010-01-29 07:13:02 +01:00
|
|
|
{
|
2010-01-30 02:33:34 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2011-04-27 06:33:03 +02:00
|
|
|
close("ERROR CONNECT: %s", ec);
|
2010-01-30 02:33:34 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char handshake[] = "\x13" "BitTorrent protocol\0\0\0\0\0\0\0\x04"
|
|
|
|
" " // space for info-hash
|
|
|
|
"aaaaaaaaaaaaaaaaaaaa" // peer-id
|
|
|
|
"\0\0\0\x01\x02"; // interested
|
|
|
|
char* h = (char*)malloc(sizeof(handshake));
|
|
|
|
memcpy(h, handshake, sizeof(handshake));
|
|
|
|
std::memcpy(h + 28, info_hash, 20);
|
|
|
|
std::generate(h + 48, h + 68, &rand);
|
2011-04-22 03:12:28 +02:00
|
|
|
// for seeds, don't send the interested message
|
2015-06-06 08:10:53 +02:00
|
|
|
boost::asio::async_write(s, boost::asio::buffer(h, (sizeof(handshake) - 1) - (seed ? 5 : 0))
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&peer_conn::on_handshake, this, h, _1, _2));
|
2010-01-29 07:13:02 +01:00
|
|
|
}
|
|
|
|
|
2016-05-02 18:36:21 +02:00
|
|
|
void on_handshake(char* h, error_code const& ec, size_t)
|
2010-01-29 07:13:02 +01:00
|
|
|
{
|
2010-01-30 02:33:34 +01:00
|
|
|
free(h);
|
|
|
|
if (ec)
|
|
|
|
{
|
2011-04-27 06:33:03 +02:00
|
|
|
close("ERROR SEND HANDSHAKE: %s", ec);
|
2010-01-30 02:33:34 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read handshake
|
2015-06-06 08:10:53 +02:00
|
|
|
boost::asio::async_read(s, boost::asio::buffer((char*)buffer, 68)
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&peer_conn::on_handshake2, this, _1, _2));
|
2010-01-29 07:13:02 +01:00
|
|
|
}
|
2010-01-12 03:57:25 +01:00
|
|
|
|
2016-05-02 18:36:21 +02:00
|
|
|
void on_handshake2(error_code const& ec, size_t)
|
2010-01-12 03:57:25 +01:00
|
|
|
{
|
2010-01-30 02:33:34 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2011-04-27 06:33:03 +02:00
|
|
|
close("ERROR READ HANDSHAKE: %s", ec);
|
2010-01-30 02:33:34 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-01-12 03:57:25 +01:00
|
|
|
|
2011-07-24 11:29:26 +02:00
|
|
|
// buffer is the full 68 byte handshake
|
|
|
|
// look at the extension bits
|
|
|
|
|
2016-04-20 06:45:32 +02:00
|
|
|
fast_extension = (((char*)buffer)[27] & 4) != 0;
|
2011-07-24 11:29:26 +02:00
|
|
|
|
2011-04-22 03:12:28 +02:00
|
|
|
if (seed)
|
|
|
|
{
|
|
|
|
write_have_all();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
work_download();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_have_all()
|
|
|
|
{
|
2011-07-24 11:29:26 +02:00
|
|
|
if (fast_extension)
|
|
|
|
{
|
2011-11-27 22:15:33 +01:00
|
|
|
char* ptr = write_buf_proto;
|
|
|
|
// have_all
|
|
|
|
write_uint32(1, ptr);
|
|
|
|
write_uint8(0xe, ptr);
|
|
|
|
// unchoke
|
|
|
|
write_uint32(1, ptr);
|
|
|
|
write_uint8(1, ptr);
|
2015-06-06 08:10:53 +02:00
|
|
|
boost::asio::async_write(s, boost::asio::buffer(write_buf_proto, ptr - write_buf_proto)
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&peer_conn::on_have_all_sent, this, _1, _2));
|
2011-07-24 11:29:26 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// bitfield
|
|
|
|
int len = (num_pieces + 7) / 8;
|
|
|
|
char* ptr = (char*)buffer;
|
|
|
|
write_uint32(len + 1, ptr);
|
|
|
|
write_uint8(5, ptr);
|
|
|
|
memset(ptr, 255, len);
|
|
|
|
ptr += len;
|
|
|
|
// unchoke
|
|
|
|
write_uint32(1, ptr);
|
|
|
|
write_uint8(1, ptr);
|
2015-06-06 08:10:53 +02:00
|
|
|
boost::asio::async_write(s, boost::asio::buffer((char*)buffer, len + 10)
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&peer_conn::on_have_all_sent, this, _1, _2));
|
2011-07-24 11:29:26 +02:00
|
|
|
}
|
2011-04-22 03:12:28 +02:00
|
|
|
}
|
|
|
|
|
2016-05-02 18:36:21 +02:00
|
|
|
void on_have_all_sent(error_code const& ec, size_t)
|
2011-04-22 03:12:28 +02:00
|
|
|
{
|
|
|
|
if (ec)
|
|
|
|
{
|
2011-04-27 06:33:03 +02:00
|
|
|
close("ERROR SEND HAVE ALL: %s", ec);
|
2011-04-22 03:12:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read message
|
2015-06-06 08:10:53 +02:00
|
|
|
boost::asio::async_read(s, boost::asio::buffer((char*)buffer, 4)
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&peer_conn::on_msg_length, this, _1, _2));
|
2010-01-30 02:33:34 +01:00
|
|
|
}
|
2010-01-29 07:13:02 +01:00
|
|
|
|
2011-11-27 22:15:33 +01:00
|
|
|
bool write_request()
|
2010-01-12 03:57:25 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
// if we're choked (and there are no allowed-fast pieces left)
|
|
|
|
if (choked && allowed_fast.empty() && !current_piece_is_allowed) return false;
|
|
|
|
|
|
|
|
// if there are no pieces left to request
|
2016-12-22 16:42:33 +01:00
|
|
|
if (pieces.empty() && suggested_pieces.empty()
|
|
|
|
&& current_piece == piece_index_t(-1))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-01-30 02:33:34 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
if (current_piece == piece_index_t(-1))
|
2011-11-27 22:15:33 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
// pick a new piece
|
|
|
|
if (choked && allowed_fast.size() > 0)
|
|
|
|
{
|
|
|
|
current_piece = allowed_fast.front();
|
|
|
|
allowed_fast.erase(allowed_fast.begin());
|
|
|
|
current_piece_is_allowed = true;
|
|
|
|
}
|
|
|
|
else if (suggested_pieces.size() > 0)
|
2011-11-27 22:15:33 +01:00
|
|
|
{
|
|
|
|
current_piece = suggested_pieces.front();
|
|
|
|
suggested_pieces.erase(suggested_pieces.begin());
|
|
|
|
++num_suggested_requests;
|
2014-07-06 21:18:00 +02:00
|
|
|
current_piece_is_allowed = false;
|
2011-11-27 22:15:33 +01:00
|
|
|
}
|
|
|
|
else if (pieces.size() > 0)
|
|
|
|
{
|
|
|
|
current_piece = pieces.front();
|
|
|
|
pieces.erase(pieces.begin());
|
2014-07-06 21:18:00 +02:00
|
|
|
current_piece_is_allowed = false;
|
2011-11-27 22:15:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-02 18:36:21 +02:00
|
|
|
TORRENT_ASSERT_FAIL();
|
2011-11-27 22:15:33 +01:00
|
|
|
}
|
|
|
|
}
|
2010-01-30 02:33:34 +01:00
|
|
|
char msg[] = "\0\0\0\xd\x06"
|
|
|
|
" " // piece
|
|
|
|
" " // offset
|
|
|
|
" "; // length
|
|
|
|
char* m = (char*)malloc(sizeof(msg));
|
|
|
|
memcpy(m, msg, sizeof(msg));
|
|
|
|
char* ptr = m + 5;
|
2016-12-22 16:42:33 +01:00
|
|
|
write_uint32(static_cast<int>(current_piece), ptr);
|
2010-01-30 02:33:34 +01:00
|
|
|
write_uint32(block * 16 * 1024, ptr);
|
|
|
|
write_uint32(16 * 1024, ptr);
|
2015-06-06 08:10:53 +02:00
|
|
|
boost::asio::async_write(s, boost::asio::buffer(m, sizeof(msg) - 1)
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&peer_conn::on_req_sent, this, m, _1, _2));
|
2011-11-27 22:15:33 +01:00
|
|
|
|
|
|
|
++outstanding_requests;
|
2010-01-30 02:33:34 +01:00
|
|
|
++block;
|
|
|
|
if (block == blocks_per_piece)
|
|
|
|
{
|
|
|
|
block = 0;
|
2016-12-22 16:42:33 +01:00
|
|
|
current_piece = piece_index_t(-1);
|
2014-07-06 21:18:00 +02:00
|
|
|
current_piece_is_allowed = false;
|
2010-01-30 02:33:34 +01:00
|
|
|
}
|
2011-11-27 22:15:33 +01:00
|
|
|
return true;
|
2010-01-29 07:13:02 +01:00
|
|
|
}
|
|
|
|
|
2016-05-02 18:36:21 +02:00
|
|
|
void on_req_sent(char* m, error_code const& ec, size_t)
|
2010-01-30 02:33:34 +01:00
|
|
|
{
|
|
|
|
free(m);
|
|
|
|
if (ec)
|
|
|
|
{
|
2011-04-27 06:33:03 +02:00
|
|
|
close("ERROR SEND REQUEST: %s", ec);
|
2010-01-30 02:33:34 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-22 03:12:28 +02:00
|
|
|
work_download();
|
2010-01-30 02:33:34 +01:00
|
|
|
}
|
|
|
|
|
2011-04-27 06:33:03 +02:00
|
|
|
void close(char const* fmt, error_code const& ec)
|
|
|
|
{
|
2015-03-12 05:34:54 +01:00
|
|
|
end_time = clock_type::now();
|
2011-04-27 06:33:03 +02:00
|
|
|
char tmp[1024];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(tmp, sizeof(tmp), fmt, ec.message().c_str());
|
2016-04-20 06:45:32 +02:00
|
|
|
int time = int(total_milliseconds(end_time - start_time));
|
2011-07-23 23:09:10 +02:00
|
|
|
if (time == 0) time = 1;
|
2016-06-18 20:01:38 +02:00
|
|
|
float up = (std::int64_t(blocks_sent) * 0x4000) / time / 1000.f;
|
|
|
|
float down = (std::int64_t(blocks_received) * 0x4000) / time / 1000.f;
|
2014-07-06 21:18:00 +02:00
|
|
|
error_code e;
|
2015-01-01 19:03:56 +01:00
|
|
|
|
|
|
|
char ep_str[200];
|
|
|
|
address const& addr = s.local_endpoint(e).address();
|
|
|
|
if (addr.is_v6())
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(ep_str, sizeof(ep_str), "[%s]:%d", addr.to_string(e).c_str()
|
2015-01-01 19:03:56 +01:00
|
|
|
, s.local_endpoint(e).port());
|
|
|
|
else
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(ep_str, sizeof(ep_str), "%s:%d", addr.to_string(e).c_str()
|
2015-01-01 19:03:56 +01:00
|
|
|
, s.local_endpoint(e).port());
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("%s ep: %s sent: %d received: %d duration: %d ms up: %.1fMB/s down: %.1fMB/s\n"
|
2015-01-01 19:03:56 +01:00
|
|
|
, tmp, ep_str, blocks_sent, blocks_received, time, up, down);
|
2011-11-27 22:15:33 +01:00
|
|
|
if (seed) --num_seeds;
|
2011-04-27 06:33:03 +02:00
|
|
|
}
|
|
|
|
|
2011-04-22 03:12:28 +02:00
|
|
|
void work_download()
|
2010-01-30 02:33:34 +01:00
|
|
|
{
|
2011-04-22 09:38:46 +02:00
|
|
|
if (pieces.empty()
|
2011-11-27 22:15:33 +01:00
|
|
|
&& suggested_pieces.empty()
|
2016-12-22 16:42:33 +01:00
|
|
|
&& current_piece == piece_index_t(-1)
|
2011-04-22 09:38:46 +02:00
|
|
|
&& outstanding_requests == 0
|
|
|
|
&& blocks_received >= num_pieces * blocks_per_piece)
|
2010-01-30 02:33:34 +01:00
|
|
|
{
|
2011-04-27 06:33:03 +02:00
|
|
|
close("COMPLETED DOWNLOAD", error_code());
|
2010-01-30 02:33:34 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-01-12 03:57:25 +01:00
|
|
|
|
2010-01-30 02:33:34 +01:00
|
|
|
// send requests
|
2011-11-27 22:15:33 +01:00
|
|
|
if (outstanding_requests < 40)
|
2010-01-30 02:33:34 +01:00
|
|
|
{
|
2011-11-27 22:15:33 +01:00
|
|
|
if (write_request()) return;
|
2010-01-30 02:33:34 +01:00
|
|
|
}
|
2010-01-12 03:57:25 +01:00
|
|
|
|
2010-01-30 02:33:34 +01:00
|
|
|
// read message
|
2015-06-06 08:10:53 +02:00
|
|
|
boost::asio::async_read(s, boost::asio::buffer((char*)buffer, 4)
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&peer_conn::on_msg_length, this, _1, _2));
|
2010-01-30 02:33:34 +01:00
|
|
|
}
|
2010-01-12 03:57:25 +01:00
|
|
|
|
2016-05-02 18:36:21 +02:00
|
|
|
void on_msg_length(error_code const& ec, size_t)
|
2010-01-29 07:13:02 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
if ((ec == boost::asio::error::operation_aborted || ec == boost::asio::error::bad_descriptor)
|
|
|
|
&& restarting)
|
|
|
|
{
|
|
|
|
start_conn();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-30 02:33:34 +01:00
|
|
|
if (ec)
|
2010-01-12 03:57:25 +01:00
|
|
|
{
|
2011-04-27 06:33:03 +02:00
|
|
|
close("ERROR RECEIVE MESSAGE PREFIX: %s", ec);
|
2010-01-30 02:33:34 +01:00
|
|
|
return;
|
2010-01-12 03:57:25 +01:00
|
|
|
}
|
2011-04-22 03:12:28 +02:00
|
|
|
char* ptr = (char*)buffer;
|
2010-01-30 02:33:34 +01:00
|
|
|
unsigned int length = read_uint32(ptr);
|
|
|
|
if (length > sizeof(buffer))
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "len: %d\n", length);
|
2011-04-27 06:33:03 +02:00
|
|
|
close("ERROR RECEIVE MESSAGE PREFIX: packet too big", error_code());
|
2010-01-30 02:33:34 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-06-06 08:10:53 +02:00
|
|
|
boost::asio::async_read(s, boost::asio::buffer((char*)buffer, length)
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&peer_conn::on_message, this, _1, _2));
|
2010-01-30 02:33:34 +01:00
|
|
|
}
|
2010-01-12 03:57:25 +01:00
|
|
|
|
2010-01-30 02:33:34 +01:00
|
|
|
void on_message(error_code const& ec, size_t bytes_transferred)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
if ((ec == boost::asio::error::operation_aborted || ec == boost::asio::error::bad_descriptor)
|
|
|
|
&& restarting)
|
|
|
|
{
|
|
|
|
start_conn();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-30 02:33:34 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2011-04-27 06:33:03 +02:00
|
|
|
close("ERROR RECEIVE MESSAGE: %s", ec);
|
2010-01-30 02:33:34 +01:00
|
|
|
return;
|
|
|
|
}
|
2011-04-22 03:12:28 +02:00
|
|
|
char* ptr = (char*)buffer;
|
2010-01-30 02:33:34 +01:00
|
|
|
int msg = read_uint8(ptr);
|
|
|
|
|
2011-11-27 22:15:33 +01:00
|
|
|
if (test_mode == dual_test && num_seeds == 0)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(!seed);
|
|
|
|
close("NO MORE SEEDS, test done", error_code());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
//std::printf("msg: %d len: %d\n", msg, int(bytes_transferred));
|
2011-04-22 09:38:46 +02:00
|
|
|
|
2011-04-22 03:12:28 +02:00
|
|
|
if (seed)
|
|
|
|
{
|
2011-08-10 09:36:59 +02:00
|
|
|
if (msg == 6)
|
2011-04-22 03:12:28 +02:00
|
|
|
{
|
2011-08-10 09:36:59 +02:00
|
|
|
if (bytes_transferred != 13)
|
|
|
|
{
|
|
|
|
close("REQUEST packet has invalid size", error_code());
|
|
|
|
return;
|
|
|
|
}
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t const piece = piece_index_t(detail::read_int32(ptr));
|
|
|
|
int const start = detail::read_int32(ptr);
|
|
|
|
int const length = detail::read_int32(ptr);
|
2011-04-22 03:12:28 +02:00
|
|
|
write_piece(piece, start, length);
|
|
|
|
}
|
2011-08-10 09:36:59 +02:00
|
|
|
else if (msg == 3) // not-interested
|
|
|
|
{
|
|
|
|
close("DONE", error_code());
|
|
|
|
return;
|
|
|
|
}
|
2011-04-22 03:12:28 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// read another message
|
2015-06-06 08:10:53 +02:00
|
|
|
boost::asio::async_read(s, boost::asio::buffer(buffer, 4)
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&peer_conn::on_msg_length, this, _1, _2));
|
2011-04-22 03:12:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-22 09:38:46 +02:00
|
|
|
if (msg == 0xe) // have_all
|
|
|
|
{
|
|
|
|
// build a list of all pieces and request them all!
|
|
|
|
pieces.resize(num_pieces);
|
2016-12-22 16:42:33 +01:00
|
|
|
for (piece_index_t i(0); i < piece_index_t(int(pieces.size())); ++i)
|
|
|
|
pieces[static_cast<int>(i)] = i;
|
2016-10-03 06:17:14 +02:00
|
|
|
std::shuffle(pieces.begin(), pieces.end(), rng);
|
2011-04-22 09:38:46 +02:00
|
|
|
}
|
|
|
|
else if (msg == 4) // have
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t const piece(detail::read_int32(ptr));
|
2011-04-22 09:38:46 +02:00
|
|
|
if (pieces.empty()) pieces.push_back(piece);
|
|
|
|
else pieces.insert(pieces.begin() + (rand() % pieces.size()), piece);
|
|
|
|
}
|
2011-07-24 11:29:26 +02:00
|
|
|
else if (msg == 5) // bitfield
|
2011-04-22 09:38:46 +02:00
|
|
|
{
|
2011-07-24 11:29:26 +02:00
|
|
|
pieces.reserve(num_pieces);
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t piece(0);
|
2014-07-05 16:10:25 +02:00
|
|
|
for (int i = 0; i < int(bytes_transferred); ++i)
|
2011-07-24 11:29:26 +02:00
|
|
|
{
|
|
|
|
int mask = 0x80;
|
|
|
|
for (int k = 0; k < 8; ++k)
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
if (piece > piece_index_t(num_pieces)) break;
|
2011-07-24 11:29:26 +02:00
|
|
|
if (*ptr & mask) pieces.push_back(piece);
|
|
|
|
mask >>= 1;
|
|
|
|
++piece;
|
|
|
|
}
|
|
|
|
++ptr;
|
|
|
|
}
|
2016-10-03 06:17:14 +02:00
|
|
|
std::shuffle(pieces.begin(), pieces.end(), rng);
|
2011-04-22 09:38:46 +02:00
|
|
|
}
|
2011-11-27 22:15:33 +01:00
|
|
|
else if (msg == 7) // piece
|
2011-04-22 09:38:46 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
if (verify_downloads)
|
2011-11-27 23:39:25 +01:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t const piece(read_uint32(ptr));
|
2011-11-27 23:39:25 +01:00
|
|
|
int start = read_uint32(ptr);
|
2016-04-25 23:22:09 +02:00
|
|
|
int size = int(bytes_transferred) - 9;
|
2011-11-27 23:39:25 +01:00
|
|
|
verify_piece(piece, start, ptr, size);
|
|
|
|
}
|
2011-04-22 09:38:46 +02:00
|
|
|
++blocks_received;
|
|
|
|
--outstanding_requests;
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t const piece = piece_index_t(detail::read_int32(ptr));
|
2011-11-27 22:15:33 +01:00
|
|
|
int start = detail::read_int32(ptr);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
if (churn && (blocks_received % churn) == 0) {
|
|
|
|
outstanding_requests = 0;
|
|
|
|
restarting = true;
|
|
|
|
s.close();
|
|
|
|
return;
|
|
|
|
}
|
2014-07-05 16:10:25 +02:00
|
|
|
if (int((start + bytes_transferred) / 0x4000) == blocks_per_piece)
|
2011-11-27 22:15:33 +01:00
|
|
|
{
|
|
|
|
write_have(piece);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (msg == 13) // suggest
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t const piece(detail::read_int32(ptr));
|
|
|
|
auto i = std::find(pieces.begin(), pieces.end(), piece);
|
2011-11-27 22:15:33 +01:00
|
|
|
if (i != pieces.end())
|
|
|
|
{
|
|
|
|
pieces.erase(i);
|
|
|
|
suggested_pieces.push_back(piece);
|
|
|
|
++num_suggest;
|
|
|
|
}
|
2011-04-22 09:38:46 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
else if (msg == 16) // reject request
|
2013-04-13 01:18:49 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t const piece(detail::read_int32(ptr));
|
2014-07-06 21:18:00 +02:00
|
|
|
int start = detail::read_int32(ptr);
|
|
|
|
int length = detail::read_int32(ptr);
|
|
|
|
|
|
|
|
// put it back!
|
|
|
|
if (current_piece != piece)
|
|
|
|
{
|
|
|
|
if (pieces.empty() || pieces.back() != piece)
|
|
|
|
pieces.push_back(piece);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-11 15:19:00 +02:00
|
|
|
block = std::min(start / 0x4000, block);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (block == 0)
|
|
|
|
{
|
|
|
|
pieces.push_back(current_piece);
|
2016-12-22 16:42:33 +01:00
|
|
|
current_piece = piece_index_t(-1);
|
2014-07-06 21:18:00 +02:00
|
|
|
current_piece_is_allowed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--outstanding_requests;
|
2016-12-22 16:42:33 +01:00
|
|
|
std::fprintf(stderr, "REJECT: [ piece: %d start: %d length: %d ]\n"
|
|
|
|
, static_cast<int>(piece), start, length);
|
2013-04-13 01:18:49 +02:00
|
|
|
}
|
|
|
|
else if (msg == 0) // choke
|
|
|
|
{
|
|
|
|
choked = true;
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
else if (msg == 1) // unchoke
|
|
|
|
{
|
|
|
|
choked = false;
|
|
|
|
}
|
|
|
|
else if (msg == 17) // allowed_fast
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t const piece = piece_index_t(detail::read_int32(ptr));
|
|
|
|
auto i = std::find(pieces.begin(), pieces.end(), piece);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (i != pieces.end())
|
|
|
|
{
|
|
|
|
pieces.erase(i);
|
|
|
|
allowed_fast.push_back(piece);
|
|
|
|
}
|
|
|
|
}
|
2011-04-22 03:12:28 +02:00
|
|
|
work_download();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
bool verify_piece(piece_index_t const piece, int start, char const* ptr, int size)
|
2011-11-27 23:39:25 +01:00
|
|
|
{
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint32_t* buf = (std::uint32_t*)ptr;
|
2016-12-22 16:42:33 +01:00
|
|
|
std::uint32_t const fill = (static_cast<int>(piece) << 8) | ((start / 0x4000) & 0xff);
|
2011-11-27 23:39:25 +01:00
|
|
|
for (int i = 0; i < size / 4; ++i)
|
|
|
|
{
|
|
|
|
if (buf[i] != fill)
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
std::fprintf(stderr, "received invalid block. piece %d block %d\n"
|
|
|
|
, static_cast<int>(piece), start / 0x4000);
|
2011-11-27 23:39:25 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
void write_piece(piece_index_t const piece, int start, int length)
|
2011-04-22 03:12:28 +02:00
|
|
|
{
|
2018-11-01 23:05:30 +01:00
|
|
|
generate_block({write_buffer, length / 4}
|
2017-05-05 14:54:02 +02:00
|
|
|
, piece, start);
|
2013-04-13 23:16:04 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (corrupt)
|
2013-04-13 23:16:04 +02:00
|
|
|
{
|
|
|
|
--corruption_counter;
|
|
|
|
if (corruption_counter == 0)
|
|
|
|
{
|
|
|
|
corruption_counter = 1000;
|
2019-11-05 20:14:46 +01:00
|
|
|
std::memset(write_buffer, 0, 10);
|
2013-04-13 23:16:04 +02:00
|
|
|
}
|
|
|
|
}
|
2011-11-27 22:15:33 +01:00
|
|
|
char* ptr = write_buf_proto;
|
2011-04-22 03:12:28 +02:00
|
|
|
write_uint32(9 + length, ptr);
|
|
|
|
assert(length == 0x4000);
|
2011-11-27 22:15:33 +01:00
|
|
|
write_uint8(7, ptr);
|
2016-12-22 16:42:33 +01:00
|
|
|
write_uint32(static_cast<int>(piece), ptr);
|
2011-04-22 03:12:28 +02:00
|
|
|
write_uint32(start, ptr);
|
2016-05-01 05:10:47 +02:00
|
|
|
std::array<boost::asio::const_buffer, 2> vec;
|
2015-06-06 08:10:53 +02:00
|
|
|
vec[0] = boost::asio::buffer(write_buf_proto, ptr - write_buf_proto);
|
|
|
|
vec[1] = boost::asio::buffer(write_buffer, length);
|
2016-05-25 06:31:52 +02:00
|
|
|
boost::asio::async_write(s, vec, std::bind(&peer_conn::on_have_all_sent, this, _1, _2));
|
2011-04-27 06:33:03 +02:00
|
|
|
++blocks_sent;
|
2014-07-06 21:18:00 +02:00
|
|
|
if (churn && (blocks_sent % churn) == 0 && seed) {
|
|
|
|
outstanding_requests = 0;
|
|
|
|
restarting = true;
|
|
|
|
s.close();
|
|
|
|
}
|
2010-01-29 07:13:02 +01:00
|
|
|
}
|
2011-11-27 22:15:33 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
void write_have(piece_index_t const piece)
|
2011-11-27 22:15:33 +01:00
|
|
|
{
|
|
|
|
char* ptr = write_buf_proto;
|
|
|
|
write_uint32(5, ptr);
|
|
|
|
write_uint8(4, ptr);
|
2016-12-22 16:42:33 +01:00
|
|
|
write_uint32(static_cast<int>(piece), ptr);
|
2016-05-25 06:31:52 +02:00
|
|
|
boost::asio::async_write(s, boost::asio::buffer(write_buf_proto, 9), std::bind(&peer_conn::on_have_all_sent, this, _1, _2));
|
2011-11-27 22:15:33 +01:00
|
|
|
}
|
2010-01-30 02:33:34 +01:00
|
|
|
};
|
2010-01-12 03:57:25 +01:00
|
|
|
|
2011-04-22 03:12:28 +02:00
|
|
|
void print_usage()
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "usage: connection_tester command [options]\n\n"
|
2011-04-22 03:12:28 +02:00
|
|
|
"command is one of:\n"
|
2013-04-13 23:16:04 +02:00
|
|
|
" gen-torrent generate a test torrent\n"
|
|
|
|
" options for this command:\n"
|
|
|
|
" -s <size> the size of the torrent in megabytes\n"
|
|
|
|
" -n <num-files> the number of files in the test torrent\n"
|
2018-01-22 00:06:35 +01:00
|
|
|
" -a introduce a lot of pad-files\n"
|
|
|
|
" (pad files are not supported for gen-data or upload)\n"
|
2014-07-12 08:20:16 +02:00
|
|
|
" -t <file> the file to save the .torrent file to\n"
|
|
|
|
" -T <name> the name of the torrent (and directory\n"
|
|
|
|
" its files are saved in)\n\n"
|
2013-04-13 23:16:04 +02:00
|
|
|
" gen-data generate the data file(s) for the test torrent\n"
|
|
|
|
" options for this command:\n"
|
|
|
|
" -t <file> the torrent file that was previously generated\n"
|
|
|
|
" -P <path> the path to where the data should be stored\n\n"
|
|
|
|
" gen-test-torrents generate many test torrents (cannot be used for up/down tests)\n"
|
|
|
|
" options for this command:\n"
|
|
|
|
" -N <num-torrents> number of torrents to generate\n"
|
|
|
|
" -n <num-files> number of files in each torrent\n"
|
|
|
|
" -t <name> base name of torrent files (index is appended)\n\n"
|
|
|
|
" upload start an uploader test\n"
|
|
|
|
" download start a downloader test\n"
|
|
|
|
" dual start a download and upload test\n"
|
|
|
|
" options for these commands:\n"
|
|
|
|
" -c <num-conns> the number of connections to make to the target\n"
|
|
|
|
" -d <dst> the IP address of the target\n"
|
|
|
|
" -p <dst-port> the port the target listens on\n"
|
2014-07-06 21:18:00 +02:00
|
|
|
" -t <torrent-file> the torrent file previously generated by gen-torrent\n"
|
|
|
|
" -C send corrupt pieces sometimes (applies to upload and dual)\n"
|
|
|
|
" -r <reconnects> churn - number of reconnects per second\n\n"
|
2011-04-22 03:12:28 +02:00
|
|
|
"examples:\n\n"
|
2013-04-13 23:16:04 +02:00
|
|
|
"connection_tester gen-torrent -s 1024 -n 4 -t test.torrent\n"
|
|
|
|
"connection_tester upload -c 200 -d 127.0.0.1 -p 6881 -t test.torrent\n"
|
|
|
|
"connection_tester download -c 200 -d 127.0.0.1 -p 6881 -t test.torrent\n"
|
|
|
|
"connection_tester dual -c 200 -d 127.0.0.1 -p 6881 -t test.torrent\n");
|
2011-04-22 03:12:28 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
void hasher_thread(lt::create_torrent* t, piece_index_t const start_piece
|
2016-12-22 16:42:33 +01:00
|
|
|
, piece_index_t const end_piece, int piece_size, bool print)
|
2011-08-10 09:36:59 +02:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
if (print) std::fprintf(stderr, "\n");
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint32_t piece[0x4000 / 4];
|
2016-12-22 16:42:33 +01:00
|
|
|
for (piece_index_t i = start_piece; i < end_piece; ++i)
|
2011-08-10 09:36:59 +02:00
|
|
|
{
|
|
|
|
hasher ph;
|
|
|
|
for (int j = 0; j < piece_size; j += 0x4000)
|
|
|
|
{
|
2017-05-05 14:54:02 +02:00
|
|
|
generate_block(piece, i, j);
|
|
|
|
ph.update(reinterpret_cast<char*>(piece), 0x4000);
|
2011-08-10 09:36:59 +02:00
|
|
|
}
|
|
|
|
t->set_hash(i, ph.final());
|
2016-12-22 16:42:33 +01:00
|
|
|
int const range = static_cast<int>(end_piece) - static_cast<int>(start_piece);
|
|
|
|
if (print && (static_cast<int>(i) & 1))
|
|
|
|
{
|
|
|
|
int const delta_piece = static_cast<int>(i) - static_cast<int>(start_piece);
|
|
|
|
std::fprintf(stderr, "\r%.1f %% ", float(delta_piece * 100) / float(range));
|
|
|
|
}
|
2011-08-10 09:36:59 +02:00
|
|
|
}
|
2016-05-17 15:24:06 +02:00
|
|
|
if (print) std::fprintf(stderr, "\n");
|
2011-08-10 09:36:59 +02:00
|
|
|
}
|
|
|
|
|
2011-07-10 22:17:32 +02:00
|
|
|
// size is in megabytes
|
2018-01-22 00:06:35 +01:00
|
|
|
void generate_torrent(std::vector<char>& buf, int num_pieces, int num_files
|
|
|
|
, char const* torrent_name, bool with_padding)
|
2011-04-22 03:12:28 +02:00
|
|
|
{
|
|
|
|
file_storage fs;
|
|
|
|
// 1 MiB piece size
|
|
|
|
const int piece_size = 1024 * 1024;
|
2016-06-18 20:01:38 +02:00
|
|
|
const std::int64_t total_size = std::int64_t(piece_size) * num_pieces;
|
2011-11-27 23:39:25 +01:00
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t s = total_size;
|
2018-10-10 16:52:38 +02:00
|
|
|
int file_index = 0;
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t file_size = total_size / num_files;
|
2011-08-01 02:03:43 +02:00
|
|
|
while (s > 0)
|
2011-07-24 11:29:26 +02:00
|
|
|
{
|
2012-05-03 08:04:59 +02:00
|
|
|
char b[100];
|
2018-10-10 16:52:38 +02:00
|
|
|
std::snprintf(b, sizeof(b), "%s/stress_test%d", torrent_name, file_index);
|
|
|
|
++file_index;
|
2018-04-11 15:19:00 +02:00
|
|
|
fs.add_file(b, std::min(s, file_size));
|
2011-11-27 23:39:25 +01:00
|
|
|
s -= file_size;
|
|
|
|
file_size += 200;
|
2011-07-24 11:29:26 +02:00
|
|
|
}
|
2011-11-27 23:39:25 +01:00
|
|
|
|
2018-01-27 09:41:54 +01:00
|
|
|
lt::create_torrent t(fs, piece_size, with_padding ? 100 : -1);
|
2018-01-22 00:06:35 +01:00
|
|
|
|
|
|
|
num_pieces = t.num_pieces();
|
2011-04-22 03:12:28 +02:00
|
|
|
|
2017-03-12 01:03:19 +01:00
|
|
|
int const num_threads = std::thread::hardware_concurrency()
|
|
|
|
? std::thread::hardware_concurrency() : 4;
|
|
|
|
std::printf("hashing in %d threads\n", num_threads);
|
2011-08-10 09:36:59 +02:00
|
|
|
|
2017-03-12 01:03:19 +01:00
|
|
|
std::vector<std::thread> threads;
|
|
|
|
threads.reserve(num_threads);
|
|
|
|
for (int i = 0; i < num_threads; ++i)
|
|
|
|
{
|
|
|
|
threads.emplace_back(&hasher_thread, &t
|
|
|
|
, piece_index_t(i * num_pieces / num_threads)
|
|
|
|
, piece_index_t((i + 1) * num_pieces / num_threads)
|
|
|
|
, piece_size
|
|
|
|
, i == 0);
|
|
|
|
}
|
2011-08-10 09:36:59 +02:00
|
|
|
|
2017-03-12 01:03:19 +01:00
|
|
|
for (auto& i : threads)
|
|
|
|
i.join();
|
2011-08-10 09:36:59 +02:00
|
|
|
|
2017-03-12 01:03:19 +01:00
|
|
|
std::back_insert_iterator<std::vector<char>> out(buf);
|
2011-08-10 09:36:59 +02:00
|
|
|
bencode(out, t.generate());
|
|
|
|
}
|
|
|
|
|
2014-07-11 00:23:26 +02:00
|
|
|
void generate_data(char const* path, torrent_info const& ti)
|
2011-08-10 09:36:59 +02:00
|
|
|
{
|
2014-07-11 00:23:26 +02:00
|
|
|
file_storage const& fs = ti.files();
|
2013-10-30 03:37:13 +01:00
|
|
|
|
2014-07-11 00:23:26 +02:00
|
|
|
file_pool fp;
|
|
|
|
|
2017-10-29 00:44:40 +02:00
|
|
|
aux::vector<download_priority_t, file_index_t> priorities;
|
2017-06-20 04:29:04 +02:00
|
|
|
sha1_hash info_hash;
|
|
|
|
storage_params params{
|
|
|
|
fs,
|
|
|
|
nullptr,
|
|
|
|
path,
|
|
|
|
storage_mode_sparse,
|
|
|
|
priorities,
|
|
|
|
info_hash
|
|
|
|
};
|
2014-07-11 00:23:26 +02:00
|
|
|
|
2017-04-07 00:11:24 +02:00
|
|
|
std::unique_ptr<storage_interface> st(default_storage_constructor(params, fp));
|
2014-07-11 00:23:26 +02:00
|
|
|
|
2016-05-02 18:36:21 +02:00
|
|
|
{
|
|
|
|
storage_error error;
|
|
|
|
st->initialize(error);
|
|
|
|
}
|
2011-08-10 09:36:59 +02:00
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint32_t piece[0x4000 / 4];
|
2016-12-22 16:42:33 +01:00
|
|
|
for (piece_index_t i(0); i < piece_index_t(ti.num_pieces()); ++i)
|
2011-04-22 03:12:28 +02:00
|
|
|
{
|
2014-07-11 00:23:26 +02:00
|
|
|
for (int j = 0; j < ti.piece_size(i); j += 0x4000)
|
2011-04-22 03:12:28 +02:00
|
|
|
{
|
2017-05-05 14:54:02 +02:00
|
|
|
generate_block(piece, i, j);
|
2016-10-04 16:31:07 +02:00
|
|
|
int const left_in_piece = ti.piece_size(i) - j;
|
2017-04-29 06:27:55 +02:00
|
|
|
iovec_t const b = { reinterpret_cast<char*>(piece)
|
2018-11-01 23:05:30 +01:00
|
|
|
, std::min(left_in_piece, 0x4000)};
|
2014-07-11 00:23:26 +02:00
|
|
|
storage_error error;
|
2017-07-15 02:59:20 +02:00
|
|
|
st->writev(b, i, j, open_mode::write_only, error);
|
2014-07-11 00:23:26 +02:00
|
|
|
if (error)
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "storage error: %s\n", error.ec.message().c_str());
|
2011-04-22 03:12:28 +02:00
|
|
|
}
|
2016-12-22 16:42:33 +01:00
|
|
|
if (static_cast<int>(i) & 1)
|
|
|
|
{
|
|
|
|
std::fprintf(stderr, "\r%.1f %% ", float(static_cast<int>(i) * 100) / float(ti.num_pieces()));
|
|
|
|
}
|
2011-04-22 03:12:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-24 11:29:26 +02:00
|
|
|
void io_thread(io_service* ios)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
ios->run(ec);
|
2016-05-17 15:24:06 +02:00
|
|
|
if (ec) std::fprintf(stderr, "ERROR: %s\n", ec.message().c_str());
|
2011-07-24 11:29:26 +02:00
|
|
|
}
|
|
|
|
|
2011-02-24 05:25:35 +01:00
|
|
|
int main(int argc, char* argv[])
|
2010-01-29 07:13:02 +01:00
|
|
|
{
|
2011-04-22 03:12:28 +02:00
|
|
|
if (argc <= 1) print_usage();
|
|
|
|
|
2013-04-13 23:16:04 +02:00
|
|
|
char const* command = argv[1];
|
|
|
|
int size = 1000;
|
|
|
|
int num_files = 10;
|
|
|
|
int num_torrents = 1;
|
|
|
|
char const* torrent_file = "benchmark.torrent";
|
|
|
|
char const* data_path = ".";
|
|
|
|
int num_connections = 50;
|
|
|
|
char const* destination_ip = "127.0.0.1";
|
|
|
|
int destination_port = 6881;
|
2014-07-06 21:18:00 +02:00
|
|
|
int churn = 0;
|
2018-01-22 00:06:35 +01:00
|
|
|
bool gen_pad_files = false;
|
2013-04-13 23:16:04 +02:00
|
|
|
|
|
|
|
argv += 2;
|
|
|
|
argc -= 2;
|
|
|
|
|
|
|
|
while (argc > 0)
|
2010-01-29 07:13:02 +01:00
|
|
|
{
|
2013-04-13 23:16:04 +02:00
|
|
|
char const* optname = argv[0];
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
|
|
|
|
if (optname[0] != '-' || strlen(optname) != 2)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "unknown option: %s\n", optname);
|
2013-04-13 23:16:04 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// options with no arguments
|
|
|
|
switch (optname[1])
|
|
|
|
{
|
|
|
|
case 'C': test_corruption = true; continue;
|
2018-01-22 00:06:35 +01:00
|
|
|
case 'a': gen_pad_files = true; continue;
|
2013-04-13 23:16:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "missing argument for option: %s\n", optname);
|
2013-04-13 23:16:04 +02:00
|
|
|
break;
|
|
|
|
}
|
2011-04-22 03:12:28 +02:00
|
|
|
|
2013-04-13 23:16:04 +02:00
|
|
|
char const* optarg = argv[0];
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
|
|
|
|
switch (optname[1])
|
|
|
|
{
|
|
|
|
case 's': size = atoi(optarg); break;
|
|
|
|
case 'n': num_files = atoi(optarg); break;
|
|
|
|
case 'N': num_torrents = atoi(optarg); break;
|
|
|
|
case 't': torrent_file = optarg; break;
|
|
|
|
case 'P': data_path = optarg; break;
|
|
|
|
case 'c': num_connections = atoi(optarg); break;
|
|
|
|
case 'p': destination_port = atoi(optarg); break;
|
|
|
|
case 'd': destination_ip = optarg; break;
|
2014-07-06 21:18:00 +02:00
|
|
|
case 'r': churn = atoi(optarg); break;
|
2016-05-17 15:24:06 +02:00
|
|
|
default: std::fprintf(stderr, "unknown option: %s\n", optname);
|
2013-04-13 23:16:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 17:19:49 +01:00
|
|
|
if (command == "gen-torrent"_sv)
|
2013-04-13 23:16:04 +02:00
|
|
|
{
|
2011-04-22 03:12:28 +02:00
|
|
|
std::vector<char> tmp;
|
2015-01-02 12:11:09 +01:00
|
|
|
std::string name = leaf_path(torrent_file);
|
2014-07-06 21:18:00 +02:00
|
|
|
name = name.substr(0, name.find_last_of('.'));
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("generating torrent: %s\n", name.c_str());
|
2014-07-12 08:20:16 +02:00
|
|
|
generate_torrent(tmp, size ? size : 1024, num_files ? num_files : 1
|
2018-01-22 00:06:35 +01:00
|
|
|
, name.c_str(), gen_pad_files);
|
2011-04-22 03:12:28 +02:00
|
|
|
|
|
|
|
FILE* output = stdout;
|
2017-03-18 17:19:49 +01:00
|
|
|
if ("-"_sv != torrent_file)
|
2013-10-30 03:37:13 +01:00
|
|
|
{
|
2016-07-09 22:26:26 +02:00
|
|
|
if( (output = std::fopen(torrent_file, "wb+")) == nullptr)
|
2013-10-30 03:37:13 +01:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "Could not open file '%s' for writing: %s\n"
|
|
|
|
, torrent_file, std::strerror(errno));
|
2013-10-30 03:37:13 +01:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
}
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "writing file to: %s\n", torrent_file);
|
2011-04-22 03:12:28 +02:00
|
|
|
fwrite(&tmp[0], 1, tmp.size(), output);
|
|
|
|
if (output != stdout)
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fclose(output);
|
2011-04-22 03:12:28 +02:00
|
|
|
|
|
|
|
return 0;
|
2010-01-29 07:13:02 +01:00
|
|
|
}
|
2017-03-18 17:19:49 +01:00
|
|
|
else if (command == "gen-data"_sv)
|
2011-08-10 09:36:59 +02:00
|
|
|
{
|
|
|
|
error_code ec;
|
2013-04-13 23:16:04 +02:00
|
|
|
torrent_info ti(torrent_file, ec);
|
2011-08-10 09:36:59 +02:00
|
|
|
if (ec)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "ERROR LOADING .TORRENT: %s\n", ec.message().c_str());
|
2011-08-10 09:36:59 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2014-07-11 00:23:26 +02:00
|
|
|
generate_data(data_path, ti);
|
2011-10-12 08:50:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-03-18 17:19:49 +01:00
|
|
|
else if (command == "gen-test-torrents"_sv)
|
2011-10-12 08:50:26 +02:00
|
|
|
{
|
|
|
|
std::vector<char> buf;
|
|
|
|
for (int i = 0; i < num_torrents; ++i)
|
|
|
|
{
|
|
|
|
char torrent_name[100];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(torrent_name, sizeof(torrent_name), "%s-%d.torrent", torrent_file, i);
|
2011-10-12 08:50:26 +02:00
|
|
|
|
|
|
|
file_storage fs;
|
|
|
|
for (int j = 0; j < num_files; ++j)
|
|
|
|
{
|
|
|
|
char file_name[100];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(file_name, sizeof(file_name), "%s-%d/file-%d", torrent_file, i, j);
|
2016-06-18 20:01:38 +02:00
|
|
|
fs.add_file(file_name, std::int64_t(j + i + 1) * 251);
|
2011-10-12 08:50:26 +02:00
|
|
|
}
|
|
|
|
// 1 MiB piece size
|
|
|
|
const int piece_size = 1024 * 1024;
|
2017-04-12 20:05:53 +02:00
|
|
|
lt::create_torrent t(fs, piece_size);
|
2016-07-09 22:26:26 +02:00
|
|
|
sha1_hash zero(nullptr);
|
2018-06-02 19:29:40 +02:00
|
|
|
for (auto const k : fs.piece_range())
|
2016-05-02 18:36:21 +02:00
|
|
|
t.set_hash(k, zero);
|
2011-10-12 08:50:26 +02:00
|
|
|
|
|
|
|
buf.clear();
|
2016-10-25 23:27:48 +02:00
|
|
|
std::back_insert_iterator<std::vector<char>> out(buf);
|
2011-10-12 08:50:26 +02:00
|
|
|
bencode(out, t.generate());
|
2016-05-17 15:24:06 +02:00
|
|
|
FILE* f = std::fopen(torrent_name, "w+");
|
2016-07-09 22:26:26 +02:00
|
|
|
if (f == nullptr)
|
2011-10-12 08:50:26 +02:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "Could not open file '%s' for writing: %s\n"
|
|
|
|
, torrent_name, std::strerror(errno));
|
2011-10-12 08:50:26 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2019-11-05 20:14:46 +01:00
|
|
|
size_t ret = fwrite(buf.data(), 1, buf.size(), f);
|
2011-10-12 08:50:26 +02:00
|
|
|
if (ret != buf.size())
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "write returned: %d (expected %d)\n", int(ret), int(buf.size()));
|
|
|
|
std::fclose(f);
|
2011-10-12 08:50:26 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("wrote %s\n", torrent_name);
|
|
|
|
std::fclose(f);
|
2011-10-12 08:50:26 +02:00
|
|
|
}
|
|
|
|
return 0;
|
2011-08-10 09:36:59 +02:00
|
|
|
}
|
2017-03-18 17:19:49 +01:00
|
|
|
else if (command == "upload"_sv)
|
2011-04-22 03:12:28 +02:00
|
|
|
{
|
2011-04-22 09:38:46 +02:00
|
|
|
test_mode = upload_test;
|
2011-04-22 03:12:28 +02:00
|
|
|
}
|
2017-03-18 17:19:49 +01:00
|
|
|
else if (command == "download"_sv)
|
2011-04-22 03:12:28 +02:00
|
|
|
{
|
2011-04-22 09:38:46 +02:00
|
|
|
test_mode = download_test;
|
|
|
|
}
|
2017-03-18 17:19:49 +01:00
|
|
|
else if (command == "dual"_sv)
|
2011-04-22 09:38:46 +02:00
|
|
|
{
|
|
|
|
test_mode = dual_test;
|
2011-04-22 03:12:28 +02:00
|
|
|
}
|
2013-04-13 23:16:04 +02:00
|
|
|
else
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "unknown command: %s\n\n", command);
|
2013-04-13 23:16:04 +02:00
|
|
|
print_usage();
|
|
|
|
}
|
2011-04-22 03:12:28 +02:00
|
|
|
|
2011-02-25 18:00:36 +01:00
|
|
|
error_code ec;
|
2013-04-13 23:16:04 +02:00
|
|
|
address_v4 addr = address_v4::from_string(destination_ip, ec);
|
2011-02-25 18:00:36 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "ERROR RESOLVING %s: %s\n", destination_ip, ec.message().c_str());
|
2011-02-25 18:00:36 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2016-06-18 20:01:38 +02:00
|
|
|
tcp::endpoint ep(addr, std::uint16_t(destination_port));
|
2015-07-11 09:12:55 +02:00
|
|
|
|
2011-08-10 09:36:59 +02:00
|
|
|
#if !defined __APPLE__
|
|
|
|
// apparently darwin doesn't seems to let you bind to
|
|
|
|
// loopback on any other IP than 127.0.0.1
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint32_t const ip = addr.to_ulong();
|
2011-07-23 23:09:10 +02:00
|
|
|
if ((ip & 0xff000000) == 0x7f000000)
|
|
|
|
{
|
|
|
|
local_bind = true;
|
|
|
|
}
|
2011-08-10 09:36:59 +02:00
|
|
|
#endif
|
2011-07-23 23:09:10 +02:00
|
|
|
|
2013-04-13 23:16:04 +02:00
|
|
|
torrent_info ti(torrent_file, ec);
|
2010-01-29 07:13:02 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "ERROR LOADING .TORRENT: %s\n", ec.message().c_str());
|
2010-01-29 07:13:02 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
std::vector<peer_conn*> conns;
|
|
|
|
conns.reserve(num_connections);
|
2016-05-01 00:54:23 +02:00
|
|
|
int const num_threads = 2;
|
2011-08-10 09:36:59 +02:00
|
|
|
io_service ios[num_threads];
|
2010-01-29 07:13:02 +01:00
|
|
|
for (int i = 0; i < num_connections; ++i)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
bool corrupt = test_corruption && (i & 1) == 0;
|
2011-04-22 09:38:46 +02:00
|
|
|
bool seed = false;
|
|
|
|
if (test_mode == upload_test) seed = true;
|
|
|
|
else if (test_mode == dual_test) seed = (i & 1);
|
2011-08-10 09:36:59 +02:00
|
|
|
conns.push_back(new peer_conn(ios[i % num_threads], ti.num_pieces(), ti.piece_length() / 16 / 1024
|
2014-07-06 21:18:00 +02:00
|
|
|
, ep, (char const*)&ti.info_hash()[0], seed, churn, corrupt));
|
2018-02-04 01:57:54 +01:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
2011-08-10 09:36:59 +02:00
|
|
|
ios[i % num_threads].poll_one(ec);
|
2011-02-25 18:00:36 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "ERROR: %s\n", ec.message().c_str());
|
2011-02-25 18:00:36 +01:00
|
|
|
break;
|
|
|
|
}
|
2010-01-29 07:13:02 +01:00
|
|
|
}
|
2010-01-12 03:57:25 +01:00
|
|
|
|
2016-05-01 00:54:23 +02:00
|
|
|
std::thread t1(&io_thread, &ios[0]);
|
|
|
|
std::thread t2(&io_thread, &ios[1]);
|
2015-07-11 09:12:55 +02:00
|
|
|
|
2011-07-24 11:29:26 +02:00
|
|
|
t1.join();
|
|
|
|
t2.join();
|
2011-04-27 06:33:03 +02:00
|
|
|
|
|
|
|
float up = 0.f;
|
|
|
|
float down = 0.f;
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint64_t total_sent = 0;
|
|
|
|
std::uint64_t total_received = 0;
|
2015-07-11 09:12:55 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
for (std::vector<peer_conn*>::iterator i = conns.begin()
|
2011-04-27 06:33:03 +02:00
|
|
|
, end(conns.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
peer_conn* p = *i;
|
2016-04-20 06:45:32 +02:00
|
|
|
int time = int(total_milliseconds(p->end_time - p->start_time));
|
2011-07-23 23:09:10 +02:00
|
|
|
if (time == 0) time = 1;
|
2011-07-24 11:29:26 +02:00
|
|
|
total_sent += p->blocks_sent;
|
2016-06-18 20:01:38 +02:00
|
|
|
up += (std::int64_t(p->blocks_sent) * 0x4000) / time / 1000.f;
|
|
|
|
down += (std::int64_t(p->blocks_received) * 0x4000) / time / 1000.f;
|
2011-04-27 06:33:03 +02:00
|
|
|
delete p;
|
|
|
|
}
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("=========================\n"
|
2011-11-27 22:15:33 +01:00
|
|
|
"suggests: %d suggested-requests: %d\n"
|
2011-08-10 09:36:59 +02:00
|
|
|
"total sent: %.1f %% received: %.1f %%\n"
|
|
|
|
"rate sent: %.1f MB/s received: %.1f MB/s\n"
|
2011-11-27 22:15:33 +01:00
|
|
|
, int(num_suggest), int(num_suggested_requests)
|
2011-08-10 09:36:59 +02:00
|
|
|
, total_sent * 0x4000 * 100.f / float(ti.total_size())
|
|
|
|
, total_received * 0x4000 * 100.f / float(ti.total_size())
|
|
|
|
, up, down);
|
2010-01-12 03:57:25 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|