some work on getting simulations to work

This commit is contained in:
arvidn
2019-01-30 12:20:42 +01:00
committed by Arvid Norberg
parent 6348b5def5
commit 3494961a68
20 changed files with 128 additions and 134 deletions

View File

@ -57,10 +57,12 @@ namespace libtorrent {
using boost::asio::ip::address;
using boost::asio::ip::address_v4;
using boost::asio::ip::address_v6;
using boost::asio::ip::network_v4;
using boost::asio::ip::v4_mapped;
#endif // SIMULATOR
using boost::asio::ip::network_v4;
using boost::asio::ip::make_network_v4;
using boost::asio::ip::v4_mapped;
#if defined TORRENT_BUILD_SIMULATOR
using sim::asio::ip::make_address;
using sim::asio::ip::make_address_v4;

View File

@ -1179,7 +1179,7 @@ namespace aux {
work_thread_t(work_thread_t const&) = delete;
work_thread_t& operator=(work_thread_t const&) = delete;
boost::asio::io_context ios;
io_context ios;
executor_work_guard<io_context::executor_type> work;
std::thread thread;
};

View File

@ -39,6 +39,9 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/asio/ts/io_context.hpp>
#include <boost/asio/executor_work_guard.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/defer.hpp>
#include <boost/asio/dispatch.hpp>
#include "libtorrent/aux_/disable_warnings_pop.hpp"
#endif // SIMULATOR
@ -48,8 +51,13 @@ namespace libtorrent {
using io_context = sim::asio::io_context;
#else
using boost::asio::io_context;
using boost::asio::executor_work_guard;
#endif
using boost::asio::executor_work_guard;
using boost::asio::make_work_guard;
using boost::asio::post;
using boost::asio::dispatch;
using boost::asio::defer;
}
#endif

View File

@ -53,7 +53,7 @@ using namespace sim;
struct fake_peer
{
fake_peer(simulation& sim, char const* ip)
: m_ios(sim, asio::ip::address::from_string(ip))
: m_ioc(sim, asio::ip::make_address(ip))
{
boost::system::error_code ec;
m_acceptor.open(asio::ip::tcp::v4(), ec);
@ -68,7 +68,7 @@ struct fake_peer
using namespace std::placeholders;
if (ec) return;
asio::async_read(m_socket, asio::mutable_buffers_1(&m_out_buffer[0], 68)
asio::async_read(m_socket, asio::buffer(m_out_buffer.data(), 68)
, std::bind(&fake_peer::read_handshake, this, _1, _2));
m_accepted = true;
@ -149,11 +149,11 @@ private:
" " // space for info-hash
"aaaaaaaaaaaaaaaaaaaa"; // peer-id
int const len = sizeof(handshake) - 1;
memcpy(m_out_buffer, handshake, len);
memcpy(m_out_buffer.data(), handshake, len);
memcpy(&m_out_buffer[28], ih.data(), 20);
asio::async_write(m_socket, asio::const_buffers_1(&m_out_buffer[0]
, len), [this, ep](boost::system::error_code const& ec
asio::async_write(m_socket, asio::buffer(m_out_buffer.data(), len)
, [this, ep](boost::system::error_code const& ec
, size_t /* bytes_transferred */)
{
std::printf("fake_peer::write_handshake(%s) -> (%d) %s\n"
@ -161,13 +161,12 @@ private:
, ec.message().c_str());
if (!m_send_buffer.empty())
{
asio::async_write(m_socket, asio::const_buffers_1(
m_send_buffer.data(), m_send_buffer.size())
asio::async_write(m_socket, asio::buffer(m_send_buffer)
, std::bind(&fake_peer::write_send_buffer, this, _1, _2));
}
else
{
asio::async_read(m_socket, asio::mutable_buffers_1(&m_out_buffer[0], 68)
asio::async_read(m_socket, asio::buffer(m_out_buffer.data(), 68)
, std::bind(&fake_peer::read_handshake, this, _1, _2));
}
});
@ -185,7 +184,7 @@ private:
return;
}
if (memcmp(&m_out_buffer[0], "\x13" "BitTorrent protocol", 20) != 0)
if (memcmp(m_out_buffer.data(), "\x13" "BitTorrent protocol", 20) != 0)
{
std::printf(" invalid protocol specifier\n");
m_socket.close();
@ -205,8 +204,7 @@ private:
m_connected = true;
// keep reading until we receie EOF, then set m_disconnected = true
m_socket.async_read_some(asio::mutable_buffers_1(&m_out_buffer[0]
, sizeof(m_out_buffer))
m_socket.async_read_some(asio::buffer(m_out_buffer)
, std::bind(&fake_peer::on_read, this, _1, _2));
}
@ -224,8 +222,8 @@ private:
return;
}
m_socket.async_read_some(asio::mutable_buffers_1(&m_out_buffer[0]
, sizeof(m_out_buffer))
m_socket.async_read_some(asio::buffer(m_out_buffer.data()
, m_out_buffer.size())
, std::bind(&fake_peer::on_read, this, _1, _2));
}
@ -237,15 +235,15 @@ private:
printf("fake_peer::write_send_buffer() -> (%d) %s\n"
, ec.value(), ec.message().c_str());
asio::async_read(m_socket, asio::mutable_buffers_1(&m_out_buffer[0], 68)
asio::async_read(m_socket, asio::buffer(m_out_buffer.data(), 68)
, std::bind(&fake_peer::read_handshake, this, _1, _2));
}
char m_out_buffer[300];
std::array<char, 300> m_out_buffer;
asio::io_service m_ios;
asio::ip::tcp::acceptor m_acceptor{m_ios};
asio::ip::tcp::socket m_socket{m_ios};
asio::io_context m_ioc;
asio::ip::tcp::acceptor m_acceptor{m_ioc};
asio::ip::tcp::socket m_socket{m_ioc};
lt::sha1_hash m_info_hash;
// set to true if this peer received an incoming connection
@ -266,7 +264,7 @@ inline void add_fake_peer(lt::torrent_handle& h, int const i)
char ep[30];
std::snprintf(ep, sizeof(ep), "60.0.0.%d", i);
h.connect_peer(lt::tcp::endpoint(
lt::address_v4::from_string(ep), 6881));
asio::ip::make_address_v4(ep), 6881));
}
inline void add_fake_peers(lt::torrent_handle& h, int const n = 5)
@ -282,7 +280,7 @@ struct udp_server
{
udp_server(simulation& sim, char const* ip, int port
, std::function<std::vector<char>(char const*, int)> handler)
: m_ios(sim, asio::ip::address::from_string(ip))
: m_ioc(sim, asio::ip::make_address(ip))
, m_handler(handler)
{
boost::system::error_code ec;
@ -296,7 +294,7 @@ struct udp_server
std::printf("udp_server::async_read_some\n");
using namespace std::placeholders;
m_socket.async_receive_from(boost::asio::buffer(m_in_buffer)
m_socket.async_receive_from(boost::asio::buffer(m_in_buffer.data(), m_in_buffer.size())
, m_from, 0, std::bind(&udp_server::on_read, this, _1, _2));
}
@ -315,7 +313,7 @@ private:
if (!send_buffer.empty())
{
lt::error_code err;
m_socket.send_to(boost::asio::buffer(send_buffer), m_from, 0, err);
m_socket.send_to(boost::asio::buffer(send_buffer, send_buffer.size()), m_from, 0, err);
if (err)
{
std::printf("send_to FAILED: %s\n", err.message().c_str());
@ -335,8 +333,8 @@ private:
std::array<char, 1500> m_in_buffer;
asio::io_service m_ios;
asio::ip::udp::socket m_socket{m_ios};
asio::io_context m_ioc;
asio::ip::udp::socket m_socket{m_ioc};
asio::ip::udp::endpoint m_from;
std::function<std::vector<char>(char const*, int)> m_handler;

View File

@ -31,7 +31,7 @@ POSSIBILITY OF SUCH DAMAGE.
*/
#include "libtorrent/kademlia/dht_settings.hpp"
#include "libtorrent/io_service.hpp"
#include "libtorrent/io_context.hpp"
#include "libtorrent/deadline_timer.hpp"
#include "libtorrent/address.hpp"
#include "libtorrent/time.hpp"
@ -90,24 +90,24 @@ struct dht_node final : lt::dht::socket_manager
{
dht_node(sim::simulation& sim, lt::dht::dht_settings const& sett, lt::counters& cnt
, int const idx, std::uint32_t const flags)
: m_io_service(sim, (flags & dht_network::bind_ipv6) ? addr6_from_int(idx) : addr_from_int(idx))
: m_io_context(sim, (flags & dht_network::bind_ipv6) ? addr6_from_int(idx) : addr_from_int(idx))
, m_dht_storage(lt::dht::dht_default_storage_constructor(sett))
, m_add_dead_nodes((flags & dht_network::add_dead_nodes) != 0)
, m_ipv6((flags & dht_network::bind_ipv6) != 0)
, m_socket(m_io_service)
, m_ls(sim_listen_socket(tcp::endpoint(m_io_service.get_ips().front(), 6881)))
, m_dht(m_ls, this, sett, id_from_addr(m_io_service.get_ips().front())
, m_socket(m_io_context)
, m_ls(sim_listen_socket(tcp::endpoint(m_io_context.get_ips().front(), 6881)))
, m_dht(m_ls, this, sett, id_from_addr(m_io_context.get_ips().front())
, nullptr, cnt
, [](lt::dht::node_id const&, std::string const&) -> lt::dht::node* { return nullptr; }
, *m_dht_storage)
{
m_dht_storage->update_node_ids({id_from_addr(m_io_service.get_ips().front())});
m_dht_storage->update_node_ids({id_from_addr(m_io_context.get_ips().front())});
sock().open(m_ipv6 ? asio::ip::udp::v6() : asio::ip::udp::v4());
sock().bind(asio::ip::udp::endpoint(
m_ipv6 ? lt::address(lt::address_v6::any()) : lt::address(lt::address_v4::any()), 6881));
sock().non_blocking(true);
sock().async_receive_from(asio::mutable_buffers_1(m_buffer, sizeof(m_buffer))
sock().async_receive_from(asio::buffer(m_buffer.data(), m_buffer.size())
, m_ep, [&](lt::error_code const& ec, std::size_t bytes_transferred)
{ this->on_read(ec, bytes_transferred); });
}
@ -136,7 +136,7 @@ struct dht_node final : lt::dht::socket_manager
// since the simulation is single threaded, we can get away with just
// allocating a single of these
static bdecode_node msg;
int const ret = bdecode(m_buffer, m_buffer + bytes_transferred, msg, err, &pos, 10, 500);
int const ret = bdecode(m_buffer.data(), m_buffer.data() + bytes_transferred, msg, err, &pos, 10, 500);
if (ret != 0) return;
if (msg.type() != bdecode_node::dict_t) return;
@ -144,7 +144,7 @@ struct dht_node final : lt::dht::socket_manager
lt::dht::msg m(msg, m_ep);
dht().incoming(m_ls, m);
sock().async_receive_from(asio::mutable_buffers_1(m_buffer, sizeof(m_buffer))
sock().async_receive_from(asio::buffer(m_buffer.data(), m_buffer.size())
, m_ep, [&](lt::error_code const& ec, std::size_t bytes_transferred)
{ this->on_read(ec, bytes_transferred); });
}
@ -158,14 +158,14 @@ struct dht_node final : lt::dht::socket_manager
send_buf.clear();
bencode(std::back_inserter(send_buf), e);
sock().send_to(boost::asio::const_buffers_1(send_buf.data(), int(send_buf.size())), addr);
sock().send_to(boost::asio::const_buffer(send_buf.data(), send_buf.size()), addr);
return true;
}
// the node_id and IP address of this node
std::pair<dht::node_id, lt::udp::endpoint> node_info() const
{
return std::make_pair(dht().nid(), lt::udp::endpoint(m_io_service.get_ips().front(), 6881));
return std::make_pair(dht().nid(), lt::udp::endpoint(m_io_context.get_ips().front(), 6881));
}
void bootstrap(std::vector<std::pair<dht::node_id, lt::udp::endpoint>> const& nodes)
@ -231,7 +231,7 @@ struct dht_node final : lt::dht::socket_manager
lt::dht::node const& dht() const { return m_dht; }
private:
asio::io_service m_io_service;
asio::io_context m_io_context;
std::shared_ptr<dht::dht_storage_interface> m_dht_storage;
bool const m_add_dead_nodes;
bool const m_ipv6;
@ -240,7 +240,7 @@ private:
std::shared_ptr<lt::aux::listen_socket_t> m_ls;
lt::dht::node m_dht;
lt::udp::endpoint m_ep;
char m_buffer[1300];
std::array<char, 1300> m_buffer;
};
dht_network::dht_network(sim::simulation& sim, int num_nodes, std::uint32_t flags)

View File

@ -31,7 +31,7 @@ POSSIBILITY OF SUCH DAMAGE.
*/
#include "libtorrent/session.hpp"
#include "libtorrent/io_service.hpp"
#include "libtorrent/io_context.hpp"
#include "libtorrent/deadline_timer.hpp"
#include "libtorrent/address.hpp"
#include "libtorrent/add_torrent_params.hpp"
@ -77,7 +77,7 @@ sim::route dsl_config::incoming_route(asio::ip::address ip)
auto it = m_incoming.find(ip);
if (it != m_incoming.end()) return sim::route().append(it->second);
it = m_incoming.insert(it, std::make_pair(ip, std::make_shared<queue>(
std::ref(m_sim->get_io_service())
m_sim->get_io_context()
, rate * 1000
, lt::duration_cast<duration>(milliseconds(rate / 2))
, 200 * 1000, "DSL modem in")));
@ -91,7 +91,7 @@ sim::route dsl_config::outgoing_route(asio::ip::address ip)
auto it = m_outgoing.find(ip);
if (it != m_outgoing.end()) return sim::route().append(it->second);
it = m_outgoing.insert(it, std::make_pair(ip, std::make_shared<queue>(
std::ref(m_sim->get_io_service()), rate * 1000
m_sim->get_io_context(), rate * 1000
, lt::duration_cast<duration>(milliseconds(rate / 2)), 200 * 1000, "DSL modem out")));
return sim::route().append(it->second);
}
@ -259,11 +259,11 @@ void setup_swarm(int num_nodes
, std::function<void(lt::alert const*, lt::session&)> on_alert
, std::function<bool(int, lt::session&)> terminate)
{
asio::io_service ios(sim);
asio::io_context ios(sim);
lt::time_point start_time(lt::clock_type::now());
std::vector<std::shared_ptr<lt::session>> nodes;
std::vector<std::shared_ptr<sim::asio::io_service>> io_service;
std::vector<std::shared_ptr<sim::asio::io_context>> io_context;
std::vector<lt::session_proxy> zombies;
lt::deadline_timer timer(ios);
@ -281,14 +281,14 @@ void setup_swarm(int num_nodes
// it's either a downloader or a seed
for (int i = 0; i < num_nodes; ++i)
{
// create a new io_service
// create a new io_context
std::vector<asio::ip::address> ips;
char ep[30];
std::snprintf(ep, sizeof(ep), "50.0.%d.%d", (i + 1) >> 8, (i + 1) & 0xff);
ips.push_back(addr(ep));
std::snprintf(ep, sizeof(ep), "2000::%X%X", (i + 1) >> 8, (i + 1) & 0xff);
ips.push_back(addr(ep));
io_service.push_back(std::make_shared<sim::asio::io_service>(sim, ips));
io_context.push_back(std::make_shared<sim::asio::io_context>(sim, ips));
lt::settings_pack pack = default_settings;
@ -299,7 +299,7 @@ void setup_swarm(int num_nodes
if (i == 0) new_session(pack);
std::shared_ptr<lt::session> ses =
std::make_shared<lt::session>(pack, *io_service.back());
std::make_shared<lt::session>(pack, *io_context.back());
init_session(*ses);
nodes.push_back(ses);
@ -332,7 +332,7 @@ void setup_swarm(int num_nodes
ses->set_alert_notify([&, i]() {
// this function is called inside libtorrent and we cannot perform work
// immediately in it. We have to notify the outside to pull all the alerts
io_service[i]->post([&,i]()
post(*io_context[i], [&,i]()
{
lt::session* ses = nodes[i].get();
@ -421,11 +421,11 @@ void setup_swarm(int num_nodes
++tick;
timer.expires_from_now(lt::seconds(1));
timer.expires_after(lt::seconds(1));
timer.async_wait(on_tick);
};
timer.expires_from_now(lt::seconds(1));
timer.expires_after(lt::seconds(1));
timer.async_wait(on_tick);
sim.run();

View File

@ -58,7 +58,7 @@ void run_test(Settings const& sett, Setup const& setup, Test const& test)
// setup the simulation
sim::default_config network_cfg;
sim::simulation sim{network_cfg};
std::unique_ptr<sim::asio::io_service> ios = make_io_service(sim, 0);
std::unique_ptr<sim::asio::io_context> ios = make_io_context(sim, 0);
lt::session_proxy zombie;
// setup settings pack to use for the session (customization point)

View File

@ -78,11 +78,11 @@ TORRENT_TEST(dht_bootstrap)
pack.set_bool(lt::settings_pack::enable_upnp, false);
pack.set_bool(lt::settings_pack::enable_natpmp, false);
pack.set_bool(lt::settings_pack::enable_dht, true);
sim::asio::io_service ios(sim, addr("10.0.0.1"));
sim::asio::io_context ios(sim, addr("10.0.0.1"));
std::shared_ptr<lt::session> ses = std::make_shared<lt::session>(pack, ios);
lt::deadline_timer timer(ios);
timer.expires_from_now(lt::seconds(10));
timer.expires_after(lt::seconds(10));
timer.async_wait([&](lt::error_code const&) {
zombies.push_back(ses->abort());
node.close();

View File

@ -101,17 +101,17 @@ TORRENT_TEST(dht_rate_limit)
default_config cfg;
simulation sim(cfg);
asio::io_service dht_ios(sim, address_v4::from_string("40.30.20.10"));
asio::io_context dht_ios(sim, make_address_v4("40.30.20.10"));
// receiver (the DHT under test)
lt::udp_socket sock(dht_ios);
obs o;
auto ls = std::make_shared<lt::aux::listen_socket_t>();
ls->external_address.cast_vote(address_v4::from_string("40.30.20.10")
ls->external_address.cast_vote(make_address_v4("40.30.20.10")
, lt::aux::session_interface::source_dht, lt::address());
ls->local_endpoint = tcp::endpoint(address_v4::from_string("40.30.20.10"), 8888);
ls->local_endpoint = tcp::endpoint(make_address_v4("40.30.20.10"), 8888);
error_code ec;
sock.bind(udp::endpoint(address_v4::from_string("40.30.20.10"), 8888), ec);
sock.bind(udp::endpoint(make_address_v4("40.30.20.10"), 8888), ec);
dht::dht_settings dhtsett;
dhtsett.block_ratelimit = 100000; // disable the DOS blocker
dhtsett.ignore_dark_internet = false;
@ -128,8 +128,8 @@ TORRENT_TEST(dht_rate_limit)
dht->new_socket(ls);
bool stop = false;
std::function<void(error_code const&, size_t)> on_read
= [&](error_code const& ec, size_t const /* bytes */)
std::function<void(error_code const&)> on_read
= [&](error_code const& ec)
{
if (ec) return;
udp_socket::packet p;
@ -143,7 +143,7 @@ TORRENT_TEST(dht_rate_limit)
// sender
int num_packets_sent = 0;
asio::io_service sender_ios(sim, address_v4::from_string("10.20.30.40"));
asio::io_context sender_ios(sim, make_address_v4("10.20.30.40"));
udp::socket sender_sock(sender_ios);
sender_sock.open(udp::v4());
sender_sock.bind(udp::endpoint(address_v4(), 4444));
@ -154,7 +154,7 @@ TORRENT_TEST(dht_rate_limit)
if (num_packets_sent == num_packets)
{
// we're done. shut down (a second from now, to let the dust settle)
timer.expires_from_now(chrono::seconds(1));
timer.expires_after(chrono::seconds(1));
timer.async_wait([&](error_code const&)
{
dht->stop();
@ -166,14 +166,14 @@ TORRENT_TEST(dht_rate_limit)
}
char const packet[] = "d1:ad2:id20:ababababababababababe1:y1:q1:q4:pinge";
sender_sock.send_to(asio::const_buffers_1(packet, sizeof(packet)-1)
, udp::endpoint(address_v4::from_string("40.30.20.10"), 8888));
sender_sock.send_to(asio::buffer(packet, sizeof(packet)-1)
, udp::endpoint(make_address_v4("40.30.20.10"), 8888));
++num_packets_sent;
timer.expires_from_now(chrono::milliseconds(10));
timer.expires_after(chrono::milliseconds(10));
timer.async_wait(sender_tick);
};
timer.expires_from_now(chrono::milliseconds(10));
timer.expires_after(chrono::milliseconds(10));
timer.async_wait(sender_tick);
udp::endpoint from;
@ -188,10 +188,10 @@ TORRENT_TEST(dht_rate_limit)
num_bytes_received += int(bytes);
++num_packets_received;
sender_sock.async_receive_from(asio::mutable_buffers_1(buffer, sizeof(buffer))
sender_sock.async_receive_from(asio::buffer(buffer, sizeof(buffer))
, from, on_receive);
};
sender_sock.async_receive_from(asio::mutable_buffers_1(buffer, sizeof(buffer))
sender_sock.async_receive_from(asio::buffer(buffer, sizeof(buffer))
, from, on_receive);
// run simulation
@ -228,17 +228,17 @@ TORRENT_TEST(dht_delete_socket)
sim::default_config cfg;
sim::simulation sim(cfg);
sim::asio::io_service dht_ios(sim, lt::address_v4::from_string("40.30.20.10"));
sim::asio::io_context dht_ios(sim, lt::make_address_v4("40.30.20.10"));
lt::udp_socket sock(dht_ios);
error_code ec;
sock.bind(udp::endpoint(address_v4::from_string("40.30.20.10"), 8888), ec);
sock.bind(udp::endpoint(make_address_v4("40.30.20.10"), 8888), ec);
obs o;
auto ls = std::make_shared<lt::aux::listen_socket_t>();
ls->external_address.cast_vote(address_v4::from_string("40.30.20.10")
ls->external_address.cast_vote(make_address_v4("40.30.20.10")
, lt::aux::session_interface::source_dht, lt::address());
ls->local_endpoint = tcp::endpoint(address_v4::from_string("40.30.20.10"), 8888);
ls->local_endpoint = tcp::endpoint(make_address_v4("40.30.20.10"), 8888);
dht::dht_settings dhtsett;
counters cnt;
dht::dht_state state;
@ -255,7 +255,7 @@ TORRENT_TEST(dht_delete_socket)
// to connection_timeout will be executed right after leaving
// the state of cancellable
asio::high_resolution_timer t1(dht_ios);
t1.expires_from_now(chrono::seconds(2));
t1.expires_after(chrono::seconds(2));
t1.async_wait([&](error_code const&)
{
dht->delete_socket(ls);
@ -263,7 +263,7 @@ TORRENT_TEST(dht_delete_socket)
// stop the DHT
asio::high_resolution_timer t2(dht_ios);
t2.expires_from_now(chrono::seconds(3));
t2.expires_after(chrono::seconds(3));
t2.async_wait([&](error_code const&) { dht->stop(); });
sim.run();

View File

@ -41,7 +41,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/kademlia/dht_storage.hpp"
#include "libtorrent/kademlia/dht_settings.hpp"
#include "libtorrent/io_service.hpp"
#include "libtorrent/io_context.hpp"
#include "libtorrent/address.hpp"
#include "libtorrent/aux_/time.hpp"
@ -100,14 +100,13 @@ void test_expiration(high_resolution_clock::duration const& expiry_time
{
default_config cfg;
simulation sim(cfg);
sim::asio::io_service ios(sim, addr("10.0.0.1"));
sim::asio::io_context ios(sim, addr("10.0.0.1"));
sim::asio::high_resolution_timer timer(ios);
timer.expires_from_now(expiry_time);
timer.expires_after(expiry_time);
timer.async_wait(std::bind(&timer_tick, s.get(), c, _1));
boost::system::error_code ec;
sim.run(ec);
sim.run();
}
TORRENT_TEST(dht_storage_counters)
@ -195,10 +194,10 @@ TORRENT_TEST(dht_storage_infohashes_sample)
default_config cfg;
simulation sim(cfg);
sim::asio::io_service ios(sim, addr("10.0.0.1"));
sim::asio::io_context ios(sim, addr("10.0.0.1"));
sim::asio::high_resolution_timer timer(ios);
timer.expires_from_now(hours(1)); // expiration of torrents
timer.expires_after(hours(1)); // expiration of torrents
timer.async_wait([&s](boost::system::error_code const&)
{
// tick here to trigger the torrents expiration
@ -209,8 +208,7 @@ TORRENT_TEST(dht_storage_infohashes_sample)
TEST_EQUAL(r, 0);
});
boost::system::error_code ec;
sim.run(ec);
sim.run();
}
#else
TORRENT_TEST(disabled) {}

View File

@ -80,8 +80,8 @@ void run_test(HandleAlerts const& on_alert, Test const& test)
// setup the simulation
sim::default_config network_cfg;
sim::simulation sim{network_cfg};
sim::asio::io_service ios0 { sim, peer0 };
sim::asio::io_service ios1 { sim, peer1 };
sim::asio::io_context ios0 { sim, peer0 };
sim::asio::io_context ios1 { sim, peer1 };
lt::session_proxy zombie[2];

View File

@ -406,11 +406,7 @@ void run_test(lt::aux::proxy_settings ps, std::string url, int expect_size, int
, expect_status, expect_error, ps, &counters[connect_handler]
, &counters[handler]);
error_code e;
sim.run(e);
if (e) std::cerr << " run failed: " << e.message() << std::endl;
TEST_EQUAL(e, error_code());
sim.run();
TEST_EQUAL(counters.size(), expect_counters.size());
for (int i = 0; i < int(counters.size()); ++i)
@ -489,9 +485,7 @@ TORRENT_TEST(http_connection_timeout_server_stalls)
, timed_out, lt::aux::proxy_settings()
, &connect_counter, &handler_counter);
error_code e;
sim.run(e);
TEST_CHECK(!e);
sim.run();
TEST_EQUAL(connect_counter, 2); // both endpoints are connected to
TEST_EQUAL(handler_counter, 1); // the handler only gets called once with error_code == timed_out
}
@ -541,9 +535,7 @@ TORRENT_TEST(http_connection_timeout_server_does_not_accept)
, timed_out, lt::aux::proxy_settings()
, &connect_counter, &handler_counter);
error_code e;
sim.run(e);
TEST_CHECK(!e);
sim.run();
TEST_EQUAL(connect_counter, 0); // no connection takes place
TEST_EQUAL(handler_counter, 1); // the handler only gets called once with error_code == timed_out
}
@ -582,11 +574,7 @@ void test_proxy_failure(lt::settings_pack::proxy_type_t proxy_type)
, error_condition(boost::system::errc::connection_refused, boost::system::generic_category())
, ps, &connect_counter, &handler_counter);
error_code e;
sim.run(e);
if (e) std::cerr << " run failed: " << e.message() << std::endl;
TEST_EQUAL(e, error_code());
sim.run();
}
// if we set up to user a proxy that does not exist, expect failure!
@ -640,13 +628,10 @@ TORRENT_TEST(http_connection_ssl_proxy)
h->start("10.0.0.2", 8080, seconds(1), 0, &ps, true /*ssl*/);
error_code e;
sim.run(e);
sim.run();
TEST_EQUAL(client_counter, 1);
TEST_EQUAL(proxy_counter, 1);
if (e) std::cerr << " run failed: " << e.message() << std::endl;
TEST_EQUAL(e, error_code());
}
// TODO: test http proxy with password

View File

@ -43,7 +43,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/alert_types.hpp"
#include "libtorrent/session.hpp"
#include "libtorrent/session_stats.hpp"
#include "libtorrent/io_service.hpp"
#include "libtorrent/io_context.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/deadline_timer.hpp"
@ -68,7 +68,7 @@ TORRENT_TEST(optimistic_unchoke)
dsl_config network_cfg;
sim::simulation sim{network_cfg};
io_service ios(sim, addr("50.1.0.0"));
io_context ios(sim, addr("50.1.0.0"));
lt::time_point start_time(lt::clock_type::now());
lt::add_torrent_params atp = ::create_torrent(0);
@ -87,7 +87,7 @@ TORRENT_TEST(optimistic_unchoke)
auto ses = std::make_shared<lt::session>(std::ref(pack), std::ref(ios));
ses->async_add_torrent(atp);
std::vector<std::shared_ptr<sim::asio::io_service>> io_service;
std::vector<std::shared_ptr<sim::asio::io_context>> io_context;
std::vector<std::shared_ptr<peer_conn>> peers;
print_alerts(*ses);
@ -96,12 +96,12 @@ TORRENT_TEST(optimistic_unchoke)
{
for (int i = 0; i < num_nodes; ++i)
{
// create a new io_service
// create a new io_context
char ep[30];
std::snprintf(ep, sizeof(ep), "50.0.%d.%d", (i + 1) >> 8, (i + 1) & 0xff);
io_service.push_back(std::make_shared<sim::asio::io_service>(
io_context.push_back(std::make_shared<sim::asio::io_context>(
std::ref(sim), addr(ep)));
peers.push_back(std::make_shared<peer_conn>(std::ref(*io_service.back())
peers.push_back(std::make_shared<peer_conn>(std::ref(*io_context.back())
, [&,i](int msg, char const* /* buf */, int /* len */)
{
choke_state& cs = peer_choke_state[i];

View File

@ -58,7 +58,7 @@ void run_test(Setup const& setup, Torrent const& torrent
// setup the simulation
sim::default_config network_cfg;
sim::simulation sim{network_cfg};
std::unique_ptr<sim::asio::io_service> ios = make_io_service(sim, 0);
std::unique_ptr<sim::asio::io_context> ios = make_io_context(sim, 0);
lt::session_proxy zombie;
// setup settings pack to use for the session (customization point)

View File

@ -62,10 +62,10 @@ void run_test(Setup const& setup
// setup the simulation
sim::default_config network_cfg;
sim::simulation sim{network_cfg};
std::unique_ptr<sim::asio::io_service> ios = make_io_service(sim, 0);
std::unique_ptr<sim::asio::io_context> ios = make_io_context(sim, 0);
lt::session_proxy zombie;
sim::asio::io_service proxy_ios{sim, addr("50.50.50.50") };
sim::asio::io_context proxy_ios{sim, addr("50.50.50.50") };
sim::socks_server socks4(proxy_ios, 4444, 4);
sim::socks_server socks5(proxy_ios, 5555, 5);
@ -128,7 +128,7 @@ TORRENT_TEST(socks5_tcp_announce)
[&tracker_port](sim::simulation& sim, lt::session&
, std::shared_ptr<lt::torrent_info> ti)
{
sim::asio::io_service web_server(sim, address_v4::from_string("2.2.2.2"));
sim::asio::io_context web_server(sim, make_address_v4("2.2.2.2"));
// listen on port 8080
sim::http_server http(web_server, 8080);
@ -254,10 +254,10 @@ TORRENT_TEST(socks5_udp_retry)
// setup the simulation
sim::default_config network_cfg;
sim::simulation sim{network_cfg};
std::unique_ptr<sim::asio::io_service> ios = make_io_service(sim, 0);
std::unique_ptr<sim::asio::io_context> ios = make_io_context(sim, 0);
lt::session_proxy zombie;
sim::asio::io_service proxy_ios{sim, addr("50.50.50.50") };
sim::asio::io_context proxy_ios{sim, addr("50.50.50.50") };
// close UDP associate connectons prematurely
sim::socks_server socks5(proxy_ios, 5555, 5, socks_flag::disconnect_udp_associate);

View File

@ -412,7 +412,7 @@ struct timeout_config : sim::default_config
auto it = m_incoming.find(ip);
if (it != m_incoming.end()) return sim::route().append(it->second);
it = m_incoming.insert(it, std::make_pair(ip, std::make_shared<queue>(
std::ref(m_sim->get_io_service())
std::ref(m_sim->get_io_context())
, 1000
, lt::duration_cast<lt::time_duration>(seconds(10))
, 1000, "packet-loss modem in")));
@ -424,7 +424,7 @@ struct timeout_config : sim::default_config
auto it = m_outgoing.find(ip);
if (it != m_outgoing.end()) return sim::route().append(it->second);
it = m_outgoing.insert(it, std::make_pair(ip, std::make_shared<queue>(
std::ref(m_sim->get_io_service()), 1000
std::ref(m_sim->get_io_context()), 1000
, lt::duration_cast<lt::time_duration>(seconds(5)), 200 * 1000, "packet-loss out")));
return sim::route().append(it->second);
}
@ -816,6 +816,6 @@ TORRENT_TEST(unchoke_slots_limit_negative)
// TODO: add test that makes sure a torrent in graceful pause mode won't accept
// incoming connections
// TODO: test the different storage allocation modes
// TODO: test contiguous buffers
// TODO: test contiguous buffer

View File

@ -33,15 +33,17 @@ POSSIBILITY OF SUCH DAMAGE.
#include "test.hpp"
#include "simulator/simulator.hpp"
#include "libtorrent/disk_io_thread_pool.hpp"
#include "libtorrent/io_context.hpp"
#include <condition_variable>
using lt::io_context;
struct test_threads : lt::pool_thread_interface
{
test_threads() {}
void notify_all() override { m_cond.notify_all(); }
void thread_fun(lt::disk_io_thread_pool&, lt::io_service::work) override
void thread_fun(lt::disk_io_thread_pool&, lt::executor_work_guard<io_context::executor_type>) override
{
std::unique_lock<std::mutex> l(m_mutex);
for (;;)
@ -114,7 +116,7 @@ TORRENT_TEST(disk_io_thread_pool_idle_reaping)
sim::simulation sim{ cfg };
test_threads threads;
sim::asio::io_service ios(sim);
sim::asio::io_context ios(sim);
lt::disk_io_thread_pool pool(threads, ios);
threads.m_pool = &pool;
pool.set_max_threads(3);
@ -128,7 +130,7 @@ TORRENT_TEST(disk_io_thread_pool_idle_reaping)
lt::deadline_timer idle_delay(ios);
// the thread will be killed the second time the reaper runs and we need
// to wait one extra minute to make sure the check runs after the reaper
idle_delay.expires_from_now(std::chrono::minutes(3));
idle_delay.expires_after(std::chrono::minutes(3));
idle_delay.async_wait([&](lt::error_code const&)
{
// this is a kludge to work around a race between the thread
@ -140,11 +142,11 @@ TORRENT_TEST(disk_io_thread_pool_idle_reaping)
sim.stop();
});
sim.run();
sim.reset();
sim.restart();
// now kill the rest
threads.set_active_threads(0);
idle_delay.expires_from_now(std::chrono::minutes(3));
idle_delay.expires_after(std::chrono::minutes(3));
idle_delay.async_wait([&](lt::error_code const&)
{
// see comment above about this kludge
@ -160,7 +162,7 @@ TORRENT_TEST(disk_io_thread_pool_abort_wait)
sim::simulation sim{ cfg };
test_threads threads;
sim::asio::io_service ios(sim);
sim::asio::io_context ios(sim);
lt::disk_io_thread_pool pool(threads, ios);
threads.m_pool = &pool;
pool.set_max_threads(3);
@ -171,7 +173,7 @@ TORRENT_TEST(disk_io_thread_pool_abort_wait)
}
#if 0
// disabled for now because io_service::work doesn't work under the simulator
// disabled for now because io_context::work doesn't work under the simulator
// and we need it to stop this test from exiting prematurely
TORRENT_TEST(disk_io_thread_pool_abort_no_wait)
{
@ -179,7 +181,7 @@ TORRENT_TEST(disk_io_thread_pool_abort_no_wait)
sim::simulation sim{ cfg };
test_threads threads;
sim::asio::io_service ios(sim);
sim::asio::io_context ios(sim);
lt::disk_io_thread_pool pool(threads, ios);
threads.m_pool = &pool;
pool.set_max_threads(3);
@ -197,7 +199,7 @@ TORRENT_TEST(disk_io_thread_pool_max_threads)
sim::simulation sim{ cfg };
test_threads threads;
sim::asio::io_service ios(sim);
sim::asio::io_context ios(sim);
lt::disk_io_thread_pool pool(threads, ios);
threads.m_pool = &pool;
// first check that the thread limit is respected when adding jobs

View File

@ -85,12 +85,12 @@ void run_test(
// setup the simulation
sim::default_config network_cfg;
sim::simulation sim{network_cfg};
sim::asio::io_service ios0 { sim, peer0 };
sim::asio::io_service ios1 { sim, peer1 };
sim::asio::io_context ios0 { sim, peer0 };
sim::asio::io_context ios1 { sim, peer1 };
lt::session_proxy zombie[2];
sim::asio::io_service proxy_ios{sim, proxy };
sim::asio::io_context proxy_ios{sim, proxy };
sim::socks_server socks4(proxy_ios, 4444, 4);
sim::socks_server socks5(proxy_ios, 5555, 5);

View File

@ -48,7 +48,7 @@ void utp_only(lt::session& ses);
void enable_enc(lt::session& ses);
void filter_ips(lt::session& ses);
std::unique_ptr<sim::asio::io_service> make_io_service(
std::unique_ptr<sim::asio::io_context> make_io_context(
sim::simulation& sim, int i);
enum flags_t

View File

@ -43,6 +43,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/random.hpp"
#include "libtorrent/debug.hpp"
#include "libtorrent/time.hpp"
#include "libtorrent/io_context.hpp"
#include <functional>
#include <string>