clean up udp-tracker unit tests
This commit is contained in:
@@ -760,177 +760,6 @@ setup_transfer(session* ses1, session* ses2, session* ses3
|
||||
return boost::make_tuple(tor1, tor2, tor3);
|
||||
}
|
||||
|
||||
boost::asio::io_service* tracker_ios = 0;
|
||||
boost::shared_ptr<libtorrent::thread> tracker_server;
|
||||
libtorrent::mutex tracker_lock;
|
||||
libtorrent::event tracker_initialized;
|
||||
|
||||
bool udp_failed = false;
|
||||
|
||||
void stop_tracker()
|
||||
{
|
||||
fprintf(stderr, "%s: stop_tracker()\n", time_now_string());
|
||||
if (tracker_ios)
|
||||
{
|
||||
tracker_ios->stop();
|
||||
}
|
||||
|
||||
if (tracker_server)
|
||||
{
|
||||
tracker_server->join();
|
||||
tracker_server.reset();
|
||||
}
|
||||
|
||||
if (tracker_ios)
|
||||
{
|
||||
delete tracker_ios;
|
||||
tracker_ios = 0;
|
||||
}
|
||||
fprintf(stderr, "%s: stop_tracker() done\n", time_now_string());
|
||||
}
|
||||
|
||||
void udp_tracker_thread(int* port);
|
||||
|
||||
int start_tracker()
|
||||
{
|
||||
stop_tracker();
|
||||
|
||||
{
|
||||
libtorrent::mutex::scoped_lock l(tracker_lock);
|
||||
tracker_initialized.clear(l);
|
||||
}
|
||||
|
||||
int port = 0;
|
||||
|
||||
delete tracker_ios;
|
||||
tracker_ios = new io_service;
|
||||
tracker_server.reset(new libtorrent::thread(boost::bind(&udp_tracker_thread, &port)));
|
||||
|
||||
{
|
||||
libtorrent::mutex::scoped_lock l(tracker_lock);
|
||||
tracker_initialized.wait(l);
|
||||
}
|
||||
// test_sleep(100);
|
||||
return port;
|
||||
}
|
||||
|
||||
boost::detail::atomic_count g_udp_tracker_requests(0);
|
||||
|
||||
void on_udp_receive(error_code const& ec, size_t bytes_transferred, udp::endpoint const* from, char* buffer, udp::socket* sock)
|
||||
{
|
||||
if (ec)
|
||||
{
|
||||
fprintf(stderr, "%s: UDP tracker, read failed: %s\n", time_now_string(), ec.message().c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
udp_failed = false;
|
||||
|
||||
if (bytes_transferred < 16)
|
||||
{
|
||||
fprintf(stderr, "%s: UDP message too short (from: %s)\n", time_now_string(), print_endpoint(*from).c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
fprintf(stderr, "%s: UDP message %d bytes\n", time_now_string(), int(bytes_transferred));
|
||||
|
||||
char* ptr = buffer;
|
||||
detail::read_uint64(ptr);
|
||||
boost::uint32_t action = detail::read_uint32(ptr);
|
||||
boost::uint32_t transaction_id = detail::read_uint32(ptr);
|
||||
|
||||
error_code e;
|
||||
|
||||
switch (action)
|
||||
{
|
||||
case 0: // connect
|
||||
|
||||
fprintf(stderr, "%s: UDP connect from %s\n", time_now_string(), print_endpoint(*from).c_str());
|
||||
ptr = buffer;
|
||||
detail::write_uint32(0, ptr); // action = connect
|
||||
detail::write_uint32(transaction_id, ptr); // transaction_id
|
||||
detail::write_uint64(10, ptr); // connection_id
|
||||
sock->send_to(asio::buffer(buffer, 16), *from, 0, e);
|
||||
if (e) fprintf(stderr, "%s: send_to failed. ERROR: %s\n", time_now_string(), e.message().c_str());
|
||||
break;
|
||||
|
||||
case 1: // announce
|
||||
|
||||
++g_udp_tracker_requests;
|
||||
fprintf(stderr, "%s: UDP announce [%d]\n", time_now_string(), int(g_udp_tracker_requests));
|
||||
ptr = buffer;
|
||||
detail::write_uint32(1, ptr); // action = announce
|
||||
detail::write_uint32(transaction_id, ptr); // transaction_id
|
||||
detail::write_uint32(1800, ptr); // interval
|
||||
detail::write_uint32(1, ptr); // incomplete
|
||||
detail::write_uint32(1, ptr); // complete
|
||||
// 0 peers
|
||||
sock->send_to(asio::buffer(buffer, 20), *from, 0, e);
|
||||
if (e) fprintf(stderr, "%s: send_to failed. ERROR: %s\n", time_now_string(), e.message().c_str());
|
||||
break;
|
||||
case 2:
|
||||
// ignore scrapes
|
||||
fprintf(stderr, "%s: UDP scrape\n", time_now_string());
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "%s: UDP unknown message: %d\n", time_now_string(), action);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void udp_tracker_thread(int* port)
|
||||
{
|
||||
udp::socket acceptor(*tracker_ios);
|
||||
error_code ec;
|
||||
acceptor.open(udp::v4(), ec);
|
||||
if (ec)
|
||||
{
|
||||
fprintf(stderr, "Error opening listen UDP socket: %s\n", ec.message().c_str());
|
||||
libtorrent::mutex::scoped_lock l(tracker_lock);
|
||||
tracker_initialized.signal(l);
|
||||
return;
|
||||
}
|
||||
acceptor.bind(udp::endpoint(address_v4::any(), 0), ec);
|
||||
if (ec)
|
||||
{
|
||||
fprintf(stderr, "Error binding UDP socket to port 0: %s\n", ec.message().c_str());
|
||||
libtorrent::mutex::scoped_lock l(tracker_lock);
|
||||
tracker_initialized.signal(l);
|
||||
return;
|
||||
}
|
||||
*port = acceptor.local_endpoint().port();
|
||||
|
||||
fprintf(stderr, "%s: UDP tracker initialized on port %d\n", time_now_string(), *port);
|
||||
|
||||
{
|
||||
libtorrent::mutex::scoped_lock l(tracker_lock);
|
||||
tracker_initialized.signal(l);
|
||||
}
|
||||
|
||||
char buffer[2000];
|
||||
|
||||
for (;;)
|
||||
{
|
||||
error_code ec;
|
||||
udp::endpoint from;
|
||||
udp_failed = true;
|
||||
acceptor.async_receive_from(
|
||||
asio::buffer(buffer, sizeof(buffer)), from, boost::bind(
|
||||
&on_udp_receive, _1, _2, &from, &buffer[0], &acceptor));
|
||||
tracker_ios->run_one(ec);
|
||||
if (udp_failed) return;
|
||||
|
||||
if (ec)
|
||||
{
|
||||
fprintf(stderr, "%s: Error receiving on UDP socket: %s\n", time_now_string(), ec.message().c_str());
|
||||
libtorrent::mutex::scoped_lock l(tracker_lock);
|
||||
tracker_initialized.signal(l);
|
||||
return;
|
||||
}
|
||||
tracker_ios->reset();
|
||||
}
|
||||
}
|
||||
|
||||
pid_type web_server_pid = 0;
|
||||
|
||||
int start_web_server(bool ssl, bool chunked_encoding)
|
||||
|
Reference in New Issue
Block a user