2013-06-13 08:32:13 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2013, Arvid Norberg
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libtorrent/bencode.hpp"
|
|
|
|
#include "libtorrent/entry.hpp"
|
|
|
|
#include "libtorrent/address.hpp"
|
|
|
|
#include "libtorrent/io_service.hpp"
|
|
|
|
#include "libtorrent/error_code.hpp"
|
|
|
|
#include "libtorrent/socket.hpp"
|
2015-03-12 05:34:54 +01:00
|
|
|
#include "libtorrent/aux_/time.hpp"
|
2015-07-12 05:01:27 +02:00
|
|
|
#include "libtorrent/io_service.hpp"
|
2013-06-24 05:34:45 +02:00
|
|
|
#include "peer_server.hpp"
|
2015-04-24 05:48:08 +02:00
|
|
|
#include "test_utils.hpp"
|
2013-06-13 08:32:13 +02:00
|
|
|
|
|
|
|
#include <boost/detail/atomic_count.hpp>
|
|
|
|
#include <boost/shared_ptr.hpp>
|
2016-05-01 00:54:23 +02:00
|
|
|
#include <boost/make_shared.hpp>
|
2013-06-13 08:32:13 +02:00
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <functional>
|
2016-05-01 00:54:23 +02:00
|
|
|
#include <thread>
|
|
|
|
#include <condition_variable>
|
|
|
|
|
2013-06-13 08:32:13 +02:00
|
|
|
using namespace libtorrent;
|
2016-05-25 06:31:52 +02:00
|
|
|
using namespace std::placeholders;
|
2013-06-13 08:32:13 +02:00
|
|
|
|
|
|
|
struct peer_server
|
|
|
|
{
|
|
|
|
|
2015-07-12 05:01:27 +02:00
|
|
|
libtorrent::io_service m_ios;
|
2013-06-13 08:32:13 +02:00
|
|
|
boost::detail::atomic_count m_peer_requests;
|
|
|
|
tcp::acceptor m_acceptor;
|
|
|
|
int m_port;
|
|
|
|
|
2016-05-01 00:54:23 +02:00
|
|
|
std::shared_ptr<std::thread> m_thread;
|
2013-06-13 08:32:13 +02:00
|
|
|
|
|
|
|
peer_server()
|
|
|
|
: m_peer_requests(0)
|
|
|
|
, m_acceptor(m_ios)
|
2014-01-18 07:53:47 +01:00
|
|
|
, m_port(0)
|
2013-06-13 08:32:13 +02:00
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
m_acceptor.open(tcp::v4(), ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "PEER Error opening peer listen socket: %s\n", ec.message().c_str());
|
2013-06-13 08:32:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_acceptor.bind(tcp::endpoint(address_v4::any(), 0), ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "PEER Error binding peer socket to port 0: %s\n", ec.message().c_str());
|
2013-06-13 08:32:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_port = m_acceptor.local_endpoint(ec).port();
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "PEER Error getting local endpoint of peer socket: %s\n", ec.message().c_str());
|
2013-06-13 08:32:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_acceptor.listen(10, ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "PEER Error listening on peer socket: %s\n", ec.message().c_str());
|
2013-06-13 08:32:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "%s: PEER peer initialized on port %d\n", time_now_string(), m_port);
|
2013-06-13 08:32:13 +02:00
|
|
|
|
2016-05-01 00:54:23 +02:00
|
|
|
m_thread = std::make_shared<std::thread>(&peer_server::thread_fun, this);
|
2013-06-13 08:32:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
~peer_server()
|
|
|
|
{
|
2013-06-17 07:18:56 +02:00
|
|
|
m_acceptor.cancel();
|
2013-06-13 08:32:13 +02:00
|
|
|
m_acceptor.close();
|
|
|
|
if (m_thread) m_thread->join();
|
|
|
|
}
|
|
|
|
|
|
|
|
int port() const { return m_port; }
|
|
|
|
|
|
|
|
int num_hits() const { return m_peer_requests; }
|
|
|
|
|
2013-06-17 07:18:56 +02:00
|
|
|
static void new_connection(error_code const& ec, error_code* ret, bool* done)
|
|
|
|
{
|
|
|
|
*ret = ec;
|
|
|
|
*done = true;
|
|
|
|
}
|
|
|
|
|
2013-06-13 08:32:13 +02:00
|
|
|
void thread_fun()
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
tcp::endpoint from;
|
|
|
|
tcp::socket socket(m_ios);
|
2016-05-01 00:54:23 +02:00
|
|
|
std::condition_variable cond;
|
2013-06-17 07:18:56 +02:00
|
|
|
bool done = false;
|
2016-05-25 06:31:52 +02:00
|
|
|
m_acceptor.async_accept(socket, from, std::bind(&new_connection, _1, &ec, &done));
|
2013-06-17 07:18:56 +02:00
|
|
|
while (!done)
|
|
|
|
{
|
2015-07-12 05:01:27 +02:00
|
|
|
m_ios.poll_one();
|
2013-06-17 07:18:56 +02:00
|
|
|
m_ios.reset();
|
|
|
|
}
|
2013-06-13 08:32:13 +02:00
|
|
|
|
|
|
|
if (ec == boost::asio::error::operation_aborted
|
|
|
|
|| ec == boost::asio::error::bad_descriptor) return;
|
|
|
|
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "PEER Error accepting connection on peer socket: %s\n", ec.message().c_str());
|
2013-06-13 08:32:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "%s: PEER incoming peer connection\n", time_now_string());
|
2013-06-13 08:32:13 +02:00
|
|
|
++m_peer_requests;
|
|
|
|
socket.close(ec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
boost::shared_ptr<peer_server> g_peer;
|
|
|
|
|
|
|
|
int start_peer()
|
|
|
|
{
|
|
|
|
g_peer.reset(new peer_server);
|
|
|
|
return g_peer->port();
|
|
|
|
}
|
|
|
|
|
|
|
|
// the number of DHT messages received
|
|
|
|
int num_peer_hits()
|
|
|
|
{
|
|
|
|
if (g_peer) return g_peer->num_hits();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop_peer()
|
|
|
|
{
|
|
|
|
g_peer.reset();
|
|
|
|
}
|
|
|
|
|