added an abstraction layer or tcp sockets, to be able to support socks proxys

This commit is contained in:
Arvid Norberg 2007-04-23 21:36:21 +00:00
parent 094dc1065c
commit d43a6b80bf
10 changed files with 629 additions and 34 deletions

View File

@ -166,7 +166,7 @@ namespace libtorrent
#endif
friend struct checker_impl;
friend class invariant_access;
typedef std::map<boost::shared_ptr<stream_socket>
typedef std::map<boost::shared_ptr<peer_connection::socket_type>
, boost::intrusive_ptr<peer_connection> >
connection_map;
typedef std::map<sha1_hash, boost::shared_ptr<torrent> > torrent_map;
@ -187,7 +187,7 @@ namespace libtorrent
void open_listen_port();
void async_accept();
void on_incoming_connection(boost::shared_ptr<stream_socket> const& s
void on_incoming_connection(boost::shared_ptr<peer_connection::socket_type> const& s
, boost::weak_ptr<socket_acceptor> const& as, asio::error_code const& e);
// must be locked to access the data
@ -205,7 +205,7 @@ namespace libtorrent
void close_connection(boost::intrusive_ptr<peer_connection> const& p);
void connection_completed(boost::intrusive_ptr<peer_connection> const& p);
void connection_failed(boost::shared_ptr<stream_socket> const& s
void connection_failed(boost::shared_ptr<peer_connection::socket_type> const& s
, tcp::endpoint const& a, char const* message);
void set_settings(session_settings const& s);

View File

@ -90,7 +90,7 @@ namespace libtorrent
bt_peer_connection(
aux::session_impl& ses
, boost::weak_ptr<torrent> t
, boost::shared_ptr<stream_socket> s
, boost::shared_ptr<socket_type> s
, tcp::endpoint const& remote
, policy::peer* peerinfo);
@ -98,7 +98,7 @@ namespace libtorrent
// know which torrent the connection belongs to
bt_peer_connection(
aux::session_impl& ses
, boost::shared_ptr<stream_socket> s
, boost::shared_ptr<socket_type> s
, policy::peer* peerinfo);
~bt_peer_connection();

View File

@ -72,6 +72,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/session.hpp"
#include "libtorrent/bandwidth_manager.hpp"
#include "libtorrent/policy.hpp"
#include "libtorrent/variant_stream.hpp"
// TODO: each time a block is 'taken over'
// from another peer. That peer must be given
@ -103,6 +104,8 @@ namespace libtorrent
friend void intrusive_ptr_release(peer_connection const*);
public:
typedef variant_stream<stream_socket> socket_type;
enum channels
{
upload_channel,
@ -116,7 +119,7 @@ namespace libtorrent
peer_connection(
aux::session_impl& ses
, boost::weak_ptr<torrent> t
, boost::shared_ptr<stream_socket> s
, boost::shared_ptr<socket_type> s
, tcp::endpoint const& remote
, tcp::endpoint const& proxy
, policy::peer* peerinfo);
@ -125,7 +128,7 @@ namespace libtorrent
// know which torrent the connection belongs to
peer_connection(
aux::session_impl& ses
, boost::shared_ptr<stream_socket> s
, boost::shared_ptr<socket_type> s
, policy::peer* peerinfo);
virtual ~peer_connection();
@ -216,7 +219,7 @@ namespace libtorrent
// is called once every second by the main loop
void second_tick(float tick_interval);
boost::shared_ptr<stream_socket> get_socket() const { return m_socket; }
boost::shared_ptr<socket_type> get_socket() const { return m_socket; }
tcp::endpoint const& remote() const { return m_remote; }
tcp::endpoint const& proxy() const { return m_remote_proxy; }
@ -490,7 +493,7 @@ namespace libtorrent
ptime m_last_receive;
ptime m_last_sent;
boost::shared_ptr<stream_socket> m_socket;
boost::shared_ptr<socket_type> m_socket;
// this is the peer we're actually talking to
// it may not necessarily be the peer we're
// connected to, in case we use a proxy

View File

@ -0,0 +1,598 @@
// Copyright Daniel Wallin and Arvid Norberg 2007.
// Use, modification and distribution is
// subject to the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef VARIANT_STREAM_070211_HPP
# define VARIANT_STREAM_070211_HPP
# include <boost/variant.hpp>
# include <boost/mpl/vector.hpp>
# include <boost/mpl/void.hpp>
# include <boost/mpl/remove.hpp>
# include <boost/mpl/transform.hpp>
# include <boost/mpl/size.hpp>
# include <boost/preprocessor/repetition/enum_params.hpp>
# include <boost/preprocessor/repetition/enum_binary_params.hpp>
# include <boost/preprocessor/facilities/intercept.hpp>
# include <boost/type_traits/add_pointer.hpp>
# include <boost/noncopyable.hpp>
#include <asio/io_service.hpp>
# define NETWORK_VARIANT_STREAM_LIMIT 5
namespace libtorrent {
namespace aux
{
struct delete_visitor
: boost::static_visitor<>
{
template <class T>
void operator()(T* p) const
{
delete p;
}
void operator()(boost::blank) const
{}
};
// -------------- async_connect -----------
template <class EndpointType, class Handler>
struct async_connect_visitor
: boost::static_visitor<>
{
async_connect_visitor(EndpointType const& endpoint, Handler const& handler)
: endpoint(endpoint)
, handler(handler)
{}
template <class T>
void operator()(T* p) const
{
p->async_connect(endpoint, handler);
}
void operator()(boost::blank) const
{}
EndpointType const& endpoint;
Handler const& handler;
};
// -------------- bind -----------
template <class EndpointType, class Error_Handler = boost::mpl::void_>
struct bind_visitor
: boost::static_visitor<>
{
bind_visitor(EndpointType const& ep, Error_Handler const& error_handler)
: endpoint(ep)
, error_handler(error_handler)
{}
template <class T>
void operator()(T* p) const
{
p->bind(endpoint, error_handler);
}
void operator()(boost::blank) const
{}
EndpointType const& endpoint;
Error_Handler const& error_handler;
};
template <class EndpointType>
struct bind_visitor<EndpointType, boost::mpl::void_>
: boost::static_visitor<>
{
bind_visitor(EndpointType const& ep)
: endpoint(ep)
{}
template <class T>
void operator()(T* p) const
{
p->bind(endpoint);
}
void operator()(boost::blank) const
{}
EndpointType const& endpoint;
};
// -------------- open -----------
template <class Protocol, class Error_Handler = boost::mpl::void_>
struct open_visitor
: boost::static_visitor<>
{
open_visitor(Protocol const& p, Error_Handler const& error_handler)
: proto(p)
, error_handler(error_handler)
{}
template <class T>
void operator()(T* p) const
{
p->open(proto, error_handler);
}
void operator()(boost::blank) const
{}
Protocol const& proto;
Error_Handler const& error_handler;
};
template <class Protocol>
struct open_visitor<Protocol, boost::mpl::void_>
: boost::static_visitor<>
{
open_visitor(Protocol const& p)
: proto(p)
{}
template <class T>
void operator()(T* p) const
{
p->open(proto);
}
void operator()(boost::blank) const
{}
Protocol const& proto;
};
// -------------- close -----------
template <class Error_Handler = boost::mpl::void_>
struct close_visitor
: boost::static_visitor<>
{
close_visitor(Error_Handler const& error_handler)
: error_handler(error_handler)
{}
template <class T>
void operator()(T* p) const
{
p->close(error_handler);
}
void operator()(boost::blank) const
{}
Error_Handler const& error_handler;
};
template <>
struct close_visitor<boost::mpl::void_>
: boost::static_visitor<>
{
template <class T>
void operator()(T* p) const
{
p->close();
}
void operator()(boost::blank) const
{}
};
// -------------- remote_endpoint -----------
template <class EndpointType, class Error_Handler = boost::mpl::void_>
struct remote_endpoint_visitor
: boost::static_visitor<EndpointType>
{
remote_endpoint_visitor(Error_Handler const& error_handler)
: error_handler(error_handler)
{}
template <class T>
EndpointType operator()(T* p) const
{
return p->remote_endpoint(error_handler);
}
EndpointType operator()(boost::blank) const
{
return EndpointType();
}
Error_Handler const& error_handler;
};
template <class EndpointType>
struct remote_endpoint_visitor<EndpointType, boost::mpl::void_>
: boost::static_visitor<EndpointType>
{
template <class T>
EndpointType operator()(T* p) const
{
return p->remote_endpoint();
}
EndpointType operator()(boost::blank) const
{
return EndpointType();
}
};
// -------------- local_endpoint -----------
template <class EndpointType, class Error_Handler = boost::mpl::void_>
struct local_endpoint_visitor
: boost::static_visitor<EndpointType>
{
local_endpoint_visitor(Error_Handler const& error_handler)
: error_handler(error_handler)
{}
template <class T>
EndpointType operator()(T* p) const
{
return p->local_endpoint(error_handler);
}
EndpointType operator()(boost::blank) const
{
return EndpointType();
}
Error_Handler const& error_handler;
};
template <class EndpointType>
struct local_endpoint_visitor<EndpointType, boost::mpl::void_>
: boost::static_visitor<EndpointType>
{
template <class T>
EndpointType operator()(T* p) const
{
return p->local_endpoint();
}
EndpointType operator()(boost::blank) const
{
return EndpointType();
}
};
// -------------- async_read_some -----------
template <class Mutable_Buffers, class Handler>
struct async_read_some_visitor
: boost::static_visitor<>
{
async_read_some_visitor(Mutable_Buffers const& buffers, Handler& handler)
: buffers(buffers)
, handler(handler)
{}
template <class T>
void operator()(T* p) const
{
p->async_read_some(buffers, handler);
}
void operator()(boost::blank) const
{}
Mutable_Buffers const& buffers;
Handler& handler;
};
// -------------- async_write_some -----------
template <class Const_Buffers, class Handler>
struct async_write_some_visitor
: boost::static_visitor<>
{
async_write_some_visitor(Const_Buffers const& buffers, Handler& handler)
: buffers(buffers)
, handler(handler)
{}
template <class T>
void operator()(T* p) const
{
p->async_write_some(buffers, handler);
}
void operator()(boost::blank) const
{}
Const_Buffers const& buffers;
Handler& handler;
};
// -------------- in_avail -----------
template <class Error_Handler = boost::mpl::void_>
struct in_avail_visitor
: boost::static_visitor<std::size_t>
{
in_avail_visitor(Error_Handler const& error_handler)
: error_handler(error_handler)
{}
template <class T>
std::size_t operator()(T* p) const
{
return p->in_avail(error_handler);
}
std::size_t operator()(boost::blank) const
{
return 0;
}
Error_Handler const& error_handler;
};
template <>
struct in_avail_visitor<boost::mpl::void_>
: boost::static_visitor<std::size_t>
{
template <class T>
std::size_t operator()(T* p) const
{
return p->in_avail();
}
void operator()(boost::blank) const
{}
};
// -------------- io_service -----------
template <class IOService>
struct io_service_visitor
: boost::static_visitor<IOService&>
{
template <class T>
IOService& operator()(T* p) const
{
return p->io_service();
}
IOService& operator()(boost::blank) const
{
return *(IOService*)0;
}
};
// -------------- lowest_layer -----------
template <class LowestLayer>
struct lowest_layer_visitor
: boost::static_visitor<LowestLayer&>
{
template <class T>
LowestLayer& operator()(T* p) const
{
return p->lowest_layer();
}
LowestLayer& operator()(boost::blank) const
{
return *(LowestLayer*)0;
}
};
} // namespace aux
template <
BOOST_PP_ENUM_BINARY_PARAMS(
NETWORK_VARIANT_STREAM_LIMIT, class S, = boost::mpl::void_ BOOST_PP_INTERCEPT
)
>
class variant_stream : boost::noncopyable
{
public:
typedef BOOST_PP_CAT(boost::mpl::vector, NETWORK_VARIANT_STREAM_LIMIT)<
BOOST_PP_ENUM_PARAMS(NETWORK_VARIANT_STREAM_LIMIT, S)
> types0;
typedef typename boost::mpl::remove<types0, boost::mpl::void_>::type types;
typedef typename boost::make_variant_over<
typename boost::mpl::push_back<
typename boost::mpl::transform<
types
, boost::add_pointer<boost::mpl::_>
>::type
, boost::blank
>::type
>::type variant_type;
typedef typename S0::lowest_layer_type lowest_layer_type;
typedef typename S0::endpoint_type endpoint_type;
typedef typename S0::protocol_type protocol_type;
explicit variant_stream(asio::io_service& io_service)
: m_io_service(io_service)
, m_variant(boost::blank())
{}
template <class S>
void instantiate()
{
std::auto_ptr<S> owned(new S(m_io_service));
boost::apply_visitor(aux::delete_visitor(), m_variant);
m_variant = owned.get();
owned.release();
}
template <class S>
S& get()
{
return *boost::get<S*>(m_variant);
}
bool instantiated() const
{
return m_variant.which() != boost::mpl::size<types>::value;
}
~variant_stream()
{
boost::apply_visitor(aux::delete_visitor(), m_variant);
}
template <class Mutable_Buffers, class Handler>
void async_read_some(Mutable_Buffers const& buffers, Handler handler)
{
assert(instantiated());
boost::apply_visitor(
aux::async_read_some_visitor<Mutable_Buffers, Handler>(buffers, handler)
, m_variant
);
}
template <class Const_Buffers, class Handler>
void async_write_some(Const_Buffers const& buffers, Handler handler)
{
assert(instantiated());
boost::apply_visitor(
aux::async_write_some_visitor<Const_Buffers, Handler>(buffers, handler)
, m_variant
);
}
template <class Handler>
void async_connect(endpoint_type const& endpoint, Handler handler)
{
assert(instantiated());
boost::apply_visitor(
aux::async_connect_visitor<endpoint_type, Handler>(endpoint, handler), m_variant
);
}
void bind(endpoint_type const& endpoint)
{
assert(instantiated());
boost::apply_visitor(aux::bind_visitor<endpoint_type>(endpoint), m_variant);
}
template <class Error_Handler>
void bind(endpoint_type const& endpoint, Error_Handler error_handler)
{
assert(instantiated());
boost::apply_visitor(
aux::bind_visitor<endpoint_type, Error_Handler>(endpoint, error_handler), m_variant
);
}
void open(protocol_type const& p)
{
assert(instantiated());
boost::apply_visitor(aux::open_visitor<protocol_type>(p), m_variant);
}
template <class Error_Handler>
void open(protocol_type const& p, Error_Handler error_handler)
{
assert(instantiated());
boost::apply_visitor(
aux::open_visitor<protocol_type, Error_Handler>(p, error_handler), m_variant
);
}
void close()
{
assert(instantiated());
boost::apply_visitor(aux::close_visitor<>(), m_variant);
}
template <class Error_Handler>
void close(Error_Handler error_handler)
{
assert(instantiated());
boost::apply_visitor(
aux::close_visitor<Error_Handler>(error_handler), m_variant
);
}
std::size_t in_avail()
{
assert(instantiated());
return boost::apply_visitor(aux::in_avail_visitor<>(), m_variant);
}
template <class Error_Handler>
std::size_t in_avail(Error_Handler error_handler)
{
assert(instantiated());
return boost::apply_visitor(
aux::in_avail_visitor<Error_Handler>(error_handler), m_variant
);
}
endpoint_type remote_endpoint()
{
assert(instantiated());
return boost::apply_visitor(aux::remote_endpoint_visitor<endpoint_type>(), m_variant);
}
template <class Error_Handler>
endpoint_type remote_endpoint(Error_Handler error_handler)
{
assert(instantiated());
return boost::apply_visitor(
aux::remote_endpoint_visitor<endpoint_type, Error_Handler>(error_handler), m_variant
);
}
endpoint_type local_endpoint()
{
assert(instantiated());
return boost::apply_visitor(aux::local_endpoint_visitor<endpoint_type>(), m_variant);
}
template <class Error_Handler>
endpoint_type local_endpoint(Error_Handler error_handler)
{
assert(instantiated());
return boost::apply_visitor(
aux::local_endpoint_visitor<endpoint_type, Error_Handler>(error_handler), m_variant
);
}
asio::io_service& io_service()
{
assert(instantiated());
return boost::apply_visitor(
aux::io_service_visitor<asio::io_service>(), m_variant
);
}
lowest_layer_type& lowest_layer()
{
assert(instantiated());
return boost::apply_visitor(
aux::lowest_layer_visitor<lowest_layer_type>(), m_variant
);
}
private:
asio::io_service& m_io_service;
variant_type m_variant;
};
} // namespace network
#endif // VARIANT_STREAM_070211_HPP

View File

@ -95,7 +95,7 @@ namespace libtorrent
web_peer_connection(
aux::session_impl& ses
, boost::weak_ptr<torrent> t
, boost::shared_ptr<stream_socket> s
, boost::shared_ptr<socket_type> s
, tcp::endpoint const& remote
, tcp::endpoint const& proxy
, std::string const& url

View File

@ -78,7 +78,7 @@ namespace libtorrent
bt_peer_connection::bt_peer_connection(
session_impl& ses
, boost::weak_ptr<torrent> tor
, shared_ptr<stream_socket> s
, shared_ptr<socket_type> s
, tcp::endpoint const& remote
, policy::peer* peerinfo)
: peer_connection(ses, tor, s, remote
@ -119,7 +119,7 @@ namespace libtorrent
bt_peer_connection::bt_peer_connection(
session_impl& ses
, boost::shared_ptr<stream_socket> s
, boost::shared_ptr<socket_type> s
, policy::peer* peerinfo)
: peer_connection(ses, s, peerinfo)
, m_state(read_protocol_length)
@ -1073,16 +1073,6 @@ namespace libtorrent
|| !std::equal(recv_buffer.begin, recv_buffer.end
, protocol_string))
{
const char cmd[] = "version";
if (recv_buffer.end - recv_buffer.begin == 7 && std::equal(
recv_buffer.begin, recv_buffer.end, cmd))
{
#ifdef TORRENT_VERBOSE_LOGGING
(*m_logger) << "sending libtorrent version\n";
#endif
asio::write(*get_socket(), asio::buffer("libtorrent version " LIBTORRENT_VERSION "\n", 27));
throw std::runtime_error("closing");
}
#ifdef TORRENT_VERBOSE_LOGGING
(*m_logger) << "incorrect protocol name\n";
#endif

View File

@ -76,7 +76,7 @@ namespace libtorrent
peer_connection::peer_connection(
session_impl& ses
, boost::weak_ptr<torrent> tor
, shared_ptr<stream_socket> s
, shared_ptr<socket_type> s
, tcp::endpoint const& remote
, tcp::endpoint const& proxy
, policy::peer* peerinfo)
@ -146,7 +146,7 @@ namespace libtorrent
peer_connection::peer_connection(
session_impl& ses
, boost::shared_ptr<stream_socket> s
, boost::shared_ptr<socket_type> s
, policy::peer* peerinfo)
:
#ifndef NDEBUG
@ -1551,10 +1551,9 @@ namespace libtorrent
}
void close_socket_ignore_error(boost::shared_ptr<stream_socket> s)
void close_socket_ignore_error(boost::shared_ptr<peer_connection::socket_type> s)
{
asio::error_code e;
s->close(e);
try { s->close(); } catch (std::exception& e) {}
}
void peer_connection::disconnect()

View File

@ -729,13 +729,14 @@ namespace libtorrent { namespace detail
void session_impl::async_accept()
{
shared_ptr<stream_socket> c(new stream_socket(m_io_service));
m_listen_socket->async_accept(*c
shared_ptr<peer_connection::socket_type> c(new peer_connection::socket_type(m_io_service));
c->instantiate<stream_socket>();
m_listen_socket->async_accept(c->get<stream_socket>()
, bind(&session_impl::on_incoming_connection, this, c
, weak_ptr<socket_acceptor>(m_listen_socket), _1));
}
void session_impl::on_incoming_connection(shared_ptr<stream_socket> const& s
void session_impl::on_incoming_connection(shared_ptr<peer_connection::socket_type> const& s
, weak_ptr<socket_acceptor> const& listen_socket, asio::error_code const& e) try
{
if (listen_socket.expired())
@ -796,7 +797,7 @@ namespace libtorrent { namespace detail
#endif
}
void session_impl::connection_failed(boost::shared_ptr<stream_socket> const& s
void session_impl::connection_failed(boost::shared_ptr<peer_connection::socket_type> const& s
, tcp::endpoint const& a, char const* message)
#ifndef NDEBUG
try

View File

@ -1488,7 +1488,9 @@ namespace libtorrent
else return;
}
boost::shared_ptr<stream_socket> s(new stream_socket(m_ses.m_io_service));
boost::shared_ptr<peer_connection::socket_type> s(
new peer_connection::socket_type(m_ses.m_io_service));
s->instantiate<stream_socket>();
boost::intrusive_ptr<peer_connection> c(new web_peer_connection(
m_ses, shared_from_this(), s, a, proxy, url, 0));
@ -1873,7 +1875,9 @@ namespace libtorrent
if (m_connections.find(a) != m_connections.end())
throw protocol_error("already connected to peer");
boost::shared_ptr<stream_socket> s(new stream_socket(m_ses.m_io_service));
boost::shared_ptr<peer_connection::socket_type> s(
new peer_connection::socket_type(m_ses.m_io_service));
s->instantiate<stream_socket>();
boost::intrusive_ptr<peer_connection> c(new bt_peer_connection(
m_ses, shared_from_this(), s, a, peerinfo));

View File

@ -59,7 +59,7 @@ namespace libtorrent
web_peer_connection::web_peer_connection(
session_impl& ses
, boost::weak_ptr<torrent> t
, boost::shared_ptr<stream_socket> s
, boost::shared_ptr<socket_type> s
, tcp::endpoint const& remote
, tcp::endpoint const& proxy
, std::string const& url