2007-04-23 23:36:21 +02:00
|
|
|
// 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>
|
|
|
|
|
2008-05-20 08:03:46 +02:00
|
|
|
#if BOOST_VERSION < 103500
|
|
|
|
#include <asio/io_service.hpp>
|
|
|
|
#else
|
2008-05-03 18:05:42 +02:00
|
|
|
#include <boost/asio/io_service.hpp>
|
2008-05-20 08:03:46 +02:00
|
|
|
#endif
|
2007-04-23 23:36:21 +02:00
|
|
|
|
|
|
|
# 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
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2007-05-10 03:50:11 +02:00
|
|
|
// -------------- io_control -----------
|
|
|
|
|
|
|
|
template<class IO_Control_Command>
|
|
|
|
struct io_control_visitor_ec: boost::static_visitor<>
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
io_control_visitor_ec(IO_Control_Command& ioc, error_code& ec_)
|
2007-10-26 02:43:28 +02:00
|
|
|
: ioc(ioc), ec(ec_) {}
|
2007-05-10 03:50:11 +02:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void operator()(T* p) const
|
|
|
|
{
|
|
|
|
p->io_control(ioc, ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(boost::blank) const
|
|
|
|
{}
|
|
|
|
|
|
|
|
IO_Control_Command& ioc;
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code& ec;
|
2007-05-10 03:50:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class IO_Control_Command>
|
|
|
|
struct io_control_visitor
|
|
|
|
: boost::static_visitor<>
|
|
|
|
{
|
|
|
|
io_control_visitor(IO_Control_Command& ioc)
|
|
|
|
: ioc(ioc) {}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void operator()(T* p) const
|
|
|
|
{
|
|
|
|
p->io_control(ioc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(boost::blank) const
|
|
|
|
{}
|
|
|
|
|
|
|
|
IO_Control_Command& ioc;
|
|
|
|
};
|
2007-04-23 23:36:21 +02:00
|
|
|
// -------------- 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 -----------
|
|
|
|
|
2007-10-23 00:53:31 +02:00
|
|
|
template <class EndpointType>
|
|
|
|
struct bind_visitor_ec
|
2007-04-23 23:36:21 +02:00
|
|
|
: boost::static_visitor<>
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
bind_visitor_ec(EndpointType const& ep, error_code& ec_)
|
2007-04-23 23:36:21 +02:00
|
|
|
: endpoint(ep)
|
2007-10-23 00:53:31 +02:00
|
|
|
, ec(ec_)
|
2007-04-23 23:36:21 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void operator()(T* p) const
|
2007-10-23 00:53:31 +02:00
|
|
|
{ p->bind(endpoint, ec); }
|
2007-04-23 23:36:21 +02:00
|
|
|
|
2007-10-23 00:53:31 +02:00
|
|
|
void operator()(boost::blank) const {}
|
2007-04-23 23:36:21 +02:00
|
|
|
|
|
|
|
EndpointType const& endpoint;
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code& ec;
|
2007-04-23 23:36:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class EndpointType>
|
2007-10-23 00:53:31 +02:00
|
|
|
struct bind_visitor
|
2007-04-23 23:36:21 +02:00
|
|
|
: boost::static_visitor<>
|
|
|
|
{
|
|
|
|
bind_visitor(EndpointType const& ep)
|
|
|
|
: endpoint(ep)
|
|
|
|
{}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void operator()(T* p) const
|
2007-10-23 00:53:31 +02:00
|
|
|
{ p->bind(endpoint); }
|
2007-04-23 23:36:21 +02:00
|
|
|
|
2007-10-23 00:53:31 +02:00
|
|
|
void operator()(boost::blank) const {}
|
2007-04-23 23:36:21 +02:00
|
|
|
|
|
|
|
EndpointType const& endpoint;
|
|
|
|
};
|
|
|
|
|
|
|
|
// -------------- open -----------
|
|
|
|
|
2007-10-23 00:53:31 +02:00
|
|
|
template <class Protocol>
|
|
|
|
struct open_visitor_ec
|
2007-04-23 23:36:21 +02:00
|
|
|
: boost::static_visitor<>
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
open_visitor_ec(Protocol const& p, error_code& ec_)
|
2007-04-23 23:36:21 +02:00
|
|
|
: proto(p)
|
2007-10-23 00:53:31 +02:00
|
|
|
, ec(ec_)
|
2007-04-23 23:36:21 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void operator()(T* p) const
|
2007-10-23 00:53:31 +02:00
|
|
|
{ p->open(proto, ec); }
|
2007-04-23 23:36:21 +02:00
|
|
|
|
2007-10-23 00:53:31 +02:00
|
|
|
void operator()(boost::blank) const {}
|
2007-04-23 23:36:21 +02:00
|
|
|
|
|
|
|
Protocol const& proto;
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code& ec;
|
2007-04-23 23:36:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class Protocol>
|
2007-10-23 00:53:31 +02:00
|
|
|
struct open_visitor
|
2007-04-23 23:36:21 +02:00
|
|
|
: boost::static_visitor<>
|
|
|
|
{
|
|
|
|
open_visitor(Protocol const& p)
|
|
|
|
: proto(p)
|
|
|
|
{}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void operator()(T* p) const
|
2007-10-23 00:53:31 +02:00
|
|
|
{ p->open(proto); }
|
2007-04-23 23:36:21 +02:00
|
|
|
|
2007-10-23 00:53:31 +02:00
|
|
|
void operator()(boost::blank) const {}
|
2007-04-23 23:36:21 +02:00
|
|
|
|
|
|
|
Protocol const& proto;
|
|
|
|
};
|
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
// -------------- is_open -----------
|
|
|
|
|
|
|
|
struct is_open_visitor
|
|
|
|
: boost::static_visitor<bool>
|
|
|
|
{
|
|
|
|
is_open_visitor() {}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
bool operator()(T const* p) const
|
|
|
|
{ return p->is_open(); }
|
|
|
|
|
|
|
|
bool operator()(boost::blank) const { return false; }
|
|
|
|
};
|
|
|
|
|
2007-04-23 23:36:21 +02:00
|
|
|
// -------------- close -----------
|
|
|
|
|
2007-10-23 00:53:31 +02:00
|
|
|
struct close_visitor_ec
|
2007-04-23 23:36:21 +02:00
|
|
|
: boost::static_visitor<>
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
close_visitor_ec(error_code& ec_)
|
2007-10-26 02:43:28 +02:00
|
|
|
: ec(ec_)
|
2007-04-23 23:36:21 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void operator()(T* p) const
|
2007-10-23 00:53:31 +02:00
|
|
|
{ p->close(ec); }
|
2007-04-23 23:36:21 +02:00
|
|
|
|
2007-10-23 00:53:31 +02:00
|
|
|
void operator()(boost::blank) const {}
|
2007-04-23 23:36:21 +02:00
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code& ec;
|
2007-04-23 23:36:21 +02:00
|
|
|
};
|
|
|
|
|
2007-10-23 00:53:31 +02:00
|
|
|
struct close_visitor
|
2007-04-23 23:36:21 +02:00
|
|
|
: boost::static_visitor<>
|
|
|
|
{
|
|
|
|
template <class T>
|
|
|
|
void operator()(T* p) const
|
2007-10-23 00:53:31 +02:00
|
|
|
{ p->close(); }
|
2007-04-23 23:36:21 +02:00
|
|
|
|
2007-10-23 00:53:31 +02:00
|
|
|
void operator()(boost::blank) const {}
|
2007-04-23 23:36:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// -------------- remote_endpoint -----------
|
|
|
|
|
2007-09-22 18:21:07 +02:00
|
|
|
template <class EndpointType>
|
|
|
|
struct remote_endpoint_visitor_ec
|
2007-04-23 23:36:21 +02:00
|
|
|
: boost::static_visitor<EndpointType>
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
remote_endpoint_visitor_ec(error_code& ec_)
|
|
|
|
: ec(ec_)
|
2007-04-23 23:36:21 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
template <class T>
|
2008-01-27 23:39:50 +01:00
|
|
|
EndpointType operator()(T const* p) const
|
2008-05-03 18:05:42 +02:00
|
|
|
{ return p->remote_endpoint(ec); }
|
2007-04-23 23:36:21 +02:00
|
|
|
|
|
|
|
EndpointType operator()(boost::blank) const
|
2007-10-23 00:53:31 +02:00
|
|
|
{ return EndpointType(); }
|
2007-04-23 23:36:21 +02:00
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code& ec;
|
2007-04-23 23:36:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class EndpointType>
|
2007-09-22 18:21:07 +02:00
|
|
|
struct remote_endpoint_visitor
|
2007-04-23 23:36:21 +02:00
|
|
|
: boost::static_visitor<EndpointType>
|
|
|
|
{
|
|
|
|
template <class T>
|
2008-01-27 23:39:50 +01:00
|
|
|
EndpointType operator()(T const* p) const
|
2007-10-23 00:53:31 +02:00
|
|
|
{ return p->remote_endpoint(); }
|
2007-04-23 23:36:21 +02:00
|
|
|
|
|
|
|
EndpointType operator()(boost::blank) const
|
2007-10-23 00:53:31 +02:00
|
|
|
{ return EndpointType(); }
|
2007-04-23 23:36:21 +02:00
|
|
|
};
|
|
|
|
|
2008-03-12 17:58:23 +01:00
|
|
|
// -------------- set_option -----------
|
|
|
|
|
|
|
|
template <class SettableSocketOption>
|
|
|
|
struct set_option_visitor
|
|
|
|
: boost::static_visitor<>
|
|
|
|
{
|
|
|
|
set_option_visitor(SettableSocketOption const& opt)
|
|
|
|
: opt_(opt)
|
|
|
|
{}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void operator()(T* p) const
|
|
|
|
{ p->set_option(opt_); }
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void operator()(boost::blank) const {}
|
2008-03-12 17:58:23 +01:00
|
|
|
|
|
|
|
SettableSocketOption const& opt_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class SettableSocketOption>
|
|
|
|
struct set_option_visitor_ec
|
2008-05-03 18:05:42 +02:00
|
|
|
: boost::static_visitor<error_code>
|
2008-03-12 17:58:23 +01:00
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
set_option_visitor_ec(SettableSocketOption const& opt, error_code& ec)
|
2008-03-12 17:58:23 +01:00
|
|
|
: opt_(opt)
|
|
|
|
, ec_(ec)
|
|
|
|
{}
|
|
|
|
|
|
|
|
template <class T>
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code operator()(T* p) const
|
2008-03-12 17:58:23 +01:00
|
|
|
{ return p->set_option(opt_, ec_); }
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code operator()(boost::blank) const
|
2008-03-12 17:58:23 +01:00
|
|
|
{ return ec_; }
|
|
|
|
|
|
|
|
SettableSocketOption const& opt_;
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code& ec_;
|
2008-03-12 17:58:23 +01:00
|
|
|
};
|
|
|
|
|
2007-04-23 23:36:21 +02:00
|
|
|
// -------------- local_endpoint -----------
|
|
|
|
|
2007-09-22 18:21:07 +02:00
|
|
|
template <class EndpointType>
|
|
|
|
struct local_endpoint_visitor_ec
|
2007-04-23 23:36:21 +02:00
|
|
|
: boost::static_visitor<EndpointType>
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
local_endpoint_visitor_ec(error_code& ec_)
|
|
|
|
: ec(ec_)
|
2007-04-23 23:36:21 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
template <class T>
|
2008-01-27 23:39:50 +01:00
|
|
|
EndpointType operator()(T const* p) const
|
2007-04-23 23:36:21 +02:00
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
return p->local_endpoint(ec);
|
2007-04-23 23:36:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EndpointType operator()(boost::blank) const
|
|
|
|
{
|
|
|
|
return EndpointType();
|
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code& ec;
|
2007-04-23 23:36:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class EndpointType>
|
2007-09-22 18:21:07 +02:00
|
|
|
struct local_endpoint_visitor
|
2007-04-23 23:36:21 +02:00
|
|
|
: boost::static_visitor<EndpointType>
|
|
|
|
{
|
|
|
|
template <class T>
|
2008-01-27 23:39:50 +01:00
|
|
|
EndpointType operator()(T const* p) const
|
2007-04-23 23:36:21 +02:00
|
|
|
{
|
|
|
|
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<>
|
|
|
|
{
|
2007-04-25 20:26:35 +02:00
|
|
|
async_read_some_visitor(Mutable_Buffers const& buffers, Handler const& handler)
|
2007-04-23 23:36:21 +02:00
|
|
|
: 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;
|
2007-04-25 20:26:35 +02:00
|
|
|
Handler const& handler;
|
2007-04-23 23:36:21 +02:00
|
|
|
};
|
|
|
|
|
2007-05-10 03:50:11 +02:00
|
|
|
// -------------- read_some -----------
|
|
|
|
|
|
|
|
template <class Mutable_Buffers>
|
|
|
|
struct read_some_visitor
|
|
|
|
: boost::static_visitor<std::size_t>
|
|
|
|
{
|
|
|
|
read_some_visitor(Mutable_Buffers const& buffers)
|
|
|
|
: buffers(buffers)
|
|
|
|
{}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
std::size_t operator()(T* p) const
|
|
|
|
{ return p->read_some(buffers); }
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
std::size_t operator()(boost::blank) const
|
2007-05-10 03:50:11 +02:00
|
|
|
{ return 0; }
|
|
|
|
|
|
|
|
Mutable_Buffers const& buffers;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Mutable_Buffers>
|
|
|
|
struct read_some_visitor_ec
|
|
|
|
: boost::static_visitor<std::size_t>
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
read_some_visitor_ec(Mutable_Buffers const& buffers, error_code& ec_)
|
2007-05-10 03:50:11 +02:00
|
|
|
: buffers(buffers)
|
2007-10-26 02:43:28 +02:00
|
|
|
, ec(ec_)
|
2007-05-10 03:50:11 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
std::size_t operator()(T* p) const
|
|
|
|
{ return p->read_some(buffers, ec); }
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
std::size_t operator()(boost::blank) const
|
2007-05-10 03:50:11 +02:00
|
|
|
{ return 0; }
|
|
|
|
|
|
|
|
Mutable_Buffers const& buffers;
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code& ec;
|
2007-05-10 03:50:11 +02:00
|
|
|
};
|
|
|
|
|
2007-04-23 23:36:21 +02:00
|
|
|
// -------------- async_write_some -----------
|
|
|
|
|
|
|
|
template <class Const_Buffers, class Handler>
|
|
|
|
struct async_write_some_visitor
|
|
|
|
: boost::static_visitor<>
|
|
|
|
{
|
2007-04-25 20:26:35 +02:00
|
|
|
async_write_some_visitor(Const_Buffers const& buffers, Handler const& handler)
|
2007-04-23 23:36:21 +02:00
|
|
|
: 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;
|
2007-04-25 20:26:35 +02:00
|
|
|
Handler const& handler;
|
2007-04-23 23:36:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// -------------- in_avail -----------
|
|
|
|
|
2007-10-23 00:53:31 +02:00
|
|
|
struct in_avail_visitor_ec
|
2007-04-23 23:36:21 +02:00
|
|
|
: boost::static_visitor<std::size_t>
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
in_avail_visitor_ec(error_code& ec_)
|
2007-10-23 00:53:31 +02:00
|
|
|
: ec(ec_)
|
2007-04-23 23:36:21 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
template <class T>
|
2008-01-27 23:39:50 +01:00
|
|
|
std::size_t operator()(T const* p) const
|
2007-04-23 23:36:21 +02:00
|
|
|
{
|
2007-10-23 00:53:31 +02:00
|
|
|
return p->in_avail(ec);
|
2007-04-23 23:36:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t operator()(boost::blank) const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code& ec;
|
2007-04-23 23:36:21 +02:00
|
|
|
};
|
|
|
|
|
2007-10-23 00:53:31 +02:00
|
|
|
struct in_avail_visitor
|
2007-04-23 23:36:21 +02:00
|
|
|
: boost::static_visitor<std::size_t>
|
|
|
|
{
|
|
|
|
template <class T>
|
2008-01-27 23:39:50 +01:00
|
|
|
std::size_t operator()(T const* p) const
|
2007-04-23 23:36:21 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2008-01-27 23:39:50 +01:00
|
|
|
return p->get_io_service();
|
2007-04-23 23:36:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2007-04-25 20:26:35 +02:00
|
|
|
typedef typename S0::endpoint_type endpoint_type;
|
2007-04-23 23:36:21 +02:00
|
|
|
typedef typename S0::protocol_type protocol_type;
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
explicit variant_stream(io_service& ios)
|
2008-01-27 23:39:50 +01:00
|
|
|
: m_io_service(ios), m_variant(boost::blank()) {}
|
2007-04-23 23:36:21 +02:00
|
|
|
|
|
|
|
template <class S>
|
2008-05-03 18:05:42 +02:00
|
|
|
void instantiate(io_service& ios)
|
2007-04-23 23:36:21 +02:00
|
|
|
{
|
2008-01-27 23:39:50 +01:00
|
|
|
TORRENT_ASSERT(&ios == &m_io_service);
|
2007-10-22 06:17:26 +02:00
|
|
|
std::auto_ptr<S> owned(new S(ios));
|
2007-04-23 23:36:21 +02:00
|
|
|
boost::apply_visitor(aux::delete_visitor(), m_variant);
|
|
|
|
m_variant = owned.get();
|
|
|
|
owned.release();
|
|
|
|
}
|
|
|
|
|
2007-04-25 20:26:35 +02:00
|
|
|
template <class S>
|
2007-04-23 23:36:21 +02:00
|
|
|
S& get()
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
return *boost::get<S*>(m_variant);
|
2007-04-23 23:36:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool instantiated() const
|
|
|
|
{
|
|
|
|
return m_variant.which() != boost::mpl::size<types>::value;
|
|
|
|
}
|
|
|
|
|
|
|
|
~variant_stream()
|
|
|
|
{
|
|
|
|
boost::apply_visitor(aux::delete_visitor(), m_variant);
|
|
|
|
}
|
|
|
|
|
2007-05-10 03:50:11 +02:00
|
|
|
template <class Mutable_Buffers>
|
2008-05-03 18:05:42 +02:00
|
|
|
std::size_t read_some(Mutable_Buffers const& buffers, error_code& ec)
|
2007-05-10 03:50:11 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(instantiated());
|
2007-05-10 03:50:11 +02:00
|
|
|
return boost::apply_visitor(
|
|
|
|
aux::read_some_visitor_ec<Mutable_Buffers>(buffers, ec)
|
|
|
|
, m_variant
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Mutable_Buffers>
|
|
|
|
std::size_t read_some(Mutable_Buffers const& buffers)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(instantiated());
|
2007-05-10 03:50:11 +02:00
|
|
|
return boost::apply_visitor(
|
|
|
|
aux::read_some_visitor<Mutable_Buffers>(buffers)
|
|
|
|
, m_variant
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-04-23 23:36:21 +02:00
|
|
|
template <class Mutable_Buffers, class Handler>
|
2007-04-25 20:26:35 +02:00
|
|
|
void async_read_some(Mutable_Buffers const& buffers, Handler const& handler)
|
2007-04-23 23:36:21 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(instantiated());
|
2007-04-23 23:36:21 +02:00
|
|
|
boost::apply_visitor(
|
|
|
|
aux::async_read_some_visitor<Mutable_Buffers, Handler>(buffers, handler)
|
|
|
|
, m_variant
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Const_Buffers, class Handler>
|
2007-04-25 20:26:35 +02:00
|
|
|
void async_write_some(Const_Buffers const& buffers, Handler const& handler)
|
2007-04-23 23:36:21 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(instantiated());
|
2007-04-23 23:36:21 +02:00
|
|
|
boost::apply_visitor(
|
|
|
|
aux::async_write_some_visitor<Const_Buffers, Handler>(buffers, handler)
|
|
|
|
, m_variant
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Handler>
|
2007-04-25 20:26:35 +02:00
|
|
|
void async_connect(endpoint_type const& endpoint, Handler const& handler)
|
2007-04-23 23:36:21 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(instantiated());
|
2007-04-23 23:36:21 +02:00
|
|
|
boost::apply_visitor(
|
|
|
|
aux::async_connect_visitor<endpoint_type, Handler>(endpoint, handler), m_variant
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-05-10 03:50:11 +02:00
|
|
|
template <class IO_Control_Command>
|
|
|
|
void io_control(IO_Control_Command& ioc)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(instantiated());
|
2007-05-10 03:50:11 +02:00
|
|
|
boost::apply_visitor(
|
|
|
|
aux::io_control_visitor<IO_Control_Command>(ioc), m_variant
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class IO_Control_Command>
|
2008-05-03 18:05:42 +02:00
|
|
|
void io_control(IO_Control_Command& ioc, error_code& ec)
|
2007-05-10 03:50:11 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(instantiated());
|
2007-05-10 03:50:11 +02:00
|
|
|
boost::apply_visitor(
|
|
|
|
aux::io_control_visitor_ec<IO_Control_Command>(ioc, ec)
|
|
|
|
, m_variant
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-04-23 23:36:21 +02:00
|
|
|
void bind(endpoint_type const& endpoint)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(instantiated());
|
2007-04-23 23:36:21 +02:00
|
|
|
boost::apply_visitor(aux::bind_visitor<endpoint_type>(endpoint), m_variant);
|
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void bind(endpoint_type const& endpoint, error_code& ec)
|
2007-04-23 23:36:21 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(instantiated());
|
2007-04-23 23:36:21 +02:00
|
|
|
boost::apply_visitor(
|
2007-10-23 00:53:31 +02:00
|
|
|
aux::bind_visitor_ec<endpoint_type>(endpoint, ec), m_variant
|
2007-04-23 23:36:21 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void open(protocol_type const& p)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(instantiated());
|
2007-04-23 23:36:21 +02:00
|
|
|
boost::apply_visitor(aux::open_visitor<protocol_type>(p), m_variant);
|
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void open(protocol_type const& p, error_code& ec)
|
2007-04-23 23:36:21 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(instantiated());
|
2007-04-23 23:36:21 +02:00
|
|
|
boost::apply_visitor(
|
2007-10-23 00:53:31 +02:00
|
|
|
aux::open_visitor_ec<protocol_type>(p, ec), m_variant
|
2007-04-23 23:36:21 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
bool is_open() const
|
|
|
|
{
|
|
|
|
return boost::apply_visitor(aux::is_open_visitor(), m_variant);
|
|
|
|
}
|
|
|
|
|
2007-04-23 23:36:21 +02:00
|
|
|
void close()
|
|
|
|
{
|
2007-10-23 00:53:31 +02:00
|
|
|
if (!instantiated()) return;
|
|
|
|
boost::apply_visitor(aux::close_visitor(), m_variant);
|
2007-04-23 23:36:21 +02:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void close(error_code& ec)
|
2007-04-23 23:36:21 +02:00
|
|
|
{
|
2007-10-23 00:53:31 +02:00
|
|
|
if (!instantiated()) return;
|
2007-04-23 23:36:21 +02:00
|
|
|
boost::apply_visitor(
|
2007-10-23 00:53:31 +02:00
|
|
|
aux::close_visitor_ec(ec), m_variant
|
2007-04-23 23:36:21 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
std::size_t in_avail() const
|
2007-04-23 23:36:21 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(instantiated());
|
2007-10-23 00:53:31 +02:00
|
|
|
return boost::apply_visitor(aux::in_avail_visitor(), m_variant);
|
2007-04-23 23:36:21 +02:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
std::size_t in_avail(error_code& ec) const
|
2007-04-23 23:36:21 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(instantiated());
|
2007-04-23 23:36:21 +02:00
|
|
|
return boost::apply_visitor(
|
2007-10-23 00:53:31 +02:00
|
|
|
aux::in_avail_visitor_ec(ec), m_variant
|
2007-04-23 23:36:21 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
endpoint_type remote_endpoint() const
|
2007-04-23 23:36:21 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(instantiated());
|
2007-04-23 23:36:21 +02:00
|
|
|
return boost::apply_visitor(aux::remote_endpoint_visitor<endpoint_type>(), m_variant);
|
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
endpoint_type remote_endpoint(error_code& ec) const
|
2007-04-23 23:36:21 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(instantiated());
|
2007-04-23 23:36:21 +02:00
|
|
|
return boost::apply_visitor(
|
2007-09-22 18:21:07 +02:00
|
|
|
aux::remote_endpoint_visitor_ec<endpoint_type>(ec), m_variant
|
2007-04-23 23:36:21 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2008-03-12 17:58:23 +01:00
|
|
|
template <class SettableSocketOption>
|
|
|
|
void set_option(SettableSocketOption const& opt)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(instantiated());
|
|
|
|
boost::apply_visitor(aux::set_option_visitor<SettableSocketOption>(opt)
|
|
|
|
, m_variant);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class SettableSocketOption>
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code set_option(SettableSocketOption const& opt, error_code& ec)
|
2008-03-12 17:58:23 +01:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(instantiated());
|
|
|
|
return boost::apply_visitor(aux::set_option_visitor_ec<SettableSocketOption>(opt, ec)
|
|
|
|
, m_variant);
|
|
|
|
}
|
2008-05-03 18:05:42 +02:00
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
endpoint_type local_endpoint() const
|
2007-04-23 23:36:21 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(instantiated());
|
2007-04-23 23:36:21 +02:00
|
|
|
return boost::apply_visitor(aux::local_endpoint_visitor<endpoint_type>(), m_variant);
|
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
endpoint_type local_endpoint(error_code& ec) const
|
2007-04-23 23:36:21 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(instantiated());
|
2007-04-23 23:36:21 +02:00
|
|
|
return boost::apply_visitor(
|
2007-09-22 18:21:07 +02:00
|
|
|
aux::local_endpoint_visitor_ec<endpoint_type>(ec), m_variant
|
2007-04-23 23:36:21 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
io_service& get_io_service()
|
2007-04-23 23:36:21 +02:00
|
|
|
{
|
2008-01-27 23:39:50 +01:00
|
|
|
return m_io_service;
|
2007-04-23 23:36:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
lowest_layer_type& lowest_layer()
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(instantiated());
|
2007-04-23 23:36:21 +02:00
|
|
|
return boost::apply_visitor(
|
|
|
|
aux::lowest_layer_visitor<lowest_layer_type>(), m_variant
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2008-05-03 18:05:42 +02:00
|
|
|
io_service& m_io_service;
|
2007-04-23 23:36:21 +02:00
|
|
|
variant_type m_variant;
|
|
|
|
};
|
|
|
|
|
2007-04-25 20:26:35 +02:00
|
|
|
} // namespace libtorrent
|
2007-04-23 23:36:21 +02:00
|
|
|
|
|
|
|
#endif // VARIANT_STREAM_070211_HPP
|
|
|
|
|