2010-11-29 02:33:05 +01:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2009-2016, Arvid Norberg
|
2010-11-29 02:33:05 +01:00
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_UTP_STREAM_HPP_INCLUDED
|
|
|
|
#define TORRENT_UTP_STREAM_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include "libtorrent/proxy_base.hpp"
|
|
|
|
#include "libtorrent/udp_socket.hpp"
|
|
|
|
#include "libtorrent/io.hpp"
|
|
|
|
#include "libtorrent/packet_buffer.hpp"
|
|
|
|
#include "libtorrent/error_code.hpp"
|
2016-05-01 00:54:23 +02:00
|
|
|
#include "libtorrent/time.hpp"
|
2017-02-04 02:27:31 +01:00
|
|
|
#include "libtorrent/close_reason.hpp"
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <functional>
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2012-03-15 07:08:08 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2016-08-13 03:31:55 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
2012-03-15 07:08:08 +01:00
|
|
|
#include <boost/system/system_error.hpp>
|
2015-08-02 21:55:05 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
2016-08-13 03:31:55 +02:00
|
|
|
#endif
|
2015-08-02 21:55:05 +02:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
2015-08-13 05:00:28 +02:00
|
|
|
#ifndef TORRENT_UTP_LOG_ENABLE
|
|
|
|
#define TORRENT_UTP_LOG 0
|
|
|
|
#define TORRENT_VERBOSE_UTP_LOG 0
|
|
|
|
#else
|
|
|
|
#define TORRENT_UTP_LOG 1
|
|
|
|
#define TORRENT_VERBOSE_UTP_LOG 1
|
|
|
|
#endif
|
2015-08-13 03:36:20 +02:00
|
|
|
|
2015-08-13 06:19:41 +02:00
|
|
|
#if TORRENT_UTP_LOG
|
2016-08-03 06:35:40 +02:00
|
|
|
TORRENT_FORMAT(1, 2)
|
|
|
|
void utp_log(char const* fmt, ...);
|
2016-04-04 04:30:56 +02:00
|
|
|
TORRENT_EXPORT bool is_utp_stream_logging();
|
2015-08-13 03:36:20 +02:00
|
|
|
|
|
|
|
// This function should be used at the very beginning and very end of your program.
|
2016-04-04 04:30:56 +02:00
|
|
|
TORRENT_EXPORT void set_utp_stream_logging(bool enable);
|
2015-08-13 03:36:20 +02:00
|
|
|
#endif
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
TORRENT_EXTRA_EXPORT bool compare_less_wrap(std::uint32_t lhs
|
|
|
|
, std::uint32_t rhs, std::uint32_t mask);
|
2015-08-17 15:01:43 +02:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
struct utp_socket_manager;
|
|
|
|
|
2013-11-19 18:57:16 +01:00
|
|
|
// internal: the point of the bif_endian_int is two-fold
|
2016-04-04 04:30:56 +02:00
|
|
|
// one purpose is to not have any alignment requirements
|
|
|
|
// so that any buffer received from the network can be cast
|
2010-11-29 02:33:05 +01:00
|
|
|
// to it and read as an integer of various sizes without
|
|
|
|
// triggering a bus error. The other purpose is to convert
|
|
|
|
// from network byte order to host byte order when read and
|
|
|
|
// written, to offer a convenient interface to both interpreting
|
|
|
|
// and writing network packets
|
|
|
|
template <class T> struct big_endian_int
|
|
|
|
{
|
|
|
|
big_endian_int& operator=(T v)
|
|
|
|
{
|
|
|
|
char* p = m_storage;
|
2016-11-25 17:17:25 +01:00
|
|
|
detail::write_impl<T>(v, p);
|
2010-11-29 02:33:05 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
operator T() const
|
|
|
|
{
|
|
|
|
const char* p = m_storage;
|
|
|
|
return detail::read_impl(p, detail::type<T>());
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
char m_storage[sizeof(T)];
|
|
|
|
};
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
typedef big_endian_int<std::uint64_t> be_uint64;
|
|
|
|
typedef big_endian_int<std::uint32_t> be_uint32;
|
|
|
|
typedef big_endian_int<std::uint16_t> be_uint16;
|
|
|
|
typedef big_endian_int<std::int64_t> be_int64;
|
|
|
|
typedef big_endian_int<std::int32_t> be_int32;
|
|
|
|
typedef big_endian_int<std::int16_t> be_int16;
|
2010-11-29 02:33:05 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
uTP header from BEP 29
|
|
|
|
|
|
|
|
0 4 8 16 24 32
|
|
|
|
+-------+-------+---------------+---------------+---------------+
|
|
|
|
| type | ver | extension | connection_id |
|
|
|
|
+-------+-------+---------------+---------------+---------------+
|
|
|
|
| timestamp_microseconds |
|
|
|
|
+---------------+---------------+---------------+---------------+
|
|
|
|
| timestamp_difference_microseconds |
|
|
|
|
+---------------+---------------+---------------+---------------+
|
|
|
|
| wnd_size |
|
|
|
|
+---------------+---------------+---------------+---------------+
|
|
|
|
| seq_nr | ack_nr |
|
|
|
|
+---------------+---------------+---------------+---------------+
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-11-19 18:57:16 +01:00
|
|
|
// internal: the different kinds of uTP packets
|
2013-11-29 06:29:39 +01:00
|
|
|
enum utp_socket_state_t
|
|
|
|
{ ST_DATA, ST_FIN, ST_STATE, ST_RESET, ST_SYN, NUM_TYPES };
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2015-03-13 06:42:18 +01:00
|
|
|
// internal: extension headers. 2 is skipped because there is a deprecated
|
2015-02-28 20:51:15 +01:00
|
|
|
// extension with that number in the wild
|
|
|
|
enum utp_extensions_t
|
|
|
|
{ utp_no_extension = 0, utp_sack = 1, utp_close_reason = 3 };
|
|
|
|
|
2013-11-19 18:57:16 +01:00
|
|
|
struct utp_header
|
|
|
|
{
|
2016-12-13 16:30:36 +01:00
|
|
|
std::uint8_t type_ver;
|
|
|
|
std::uint8_t extension;
|
2013-11-19 18:57:16 +01:00
|
|
|
be_uint16 connection_id;
|
|
|
|
be_uint32 timestamp_microseconds;
|
|
|
|
be_uint32 timestamp_difference_microseconds;
|
|
|
|
be_uint32 wnd_size;
|
|
|
|
be_uint16 seq_nr;
|
|
|
|
be_uint16 ack_nr;
|
|
|
|
|
|
|
|
int get_type() const { return type_ver >> 4; }
|
|
|
|
int get_version() const { return type_ver & 0xf; }
|
|
|
|
};
|
2010-11-29 02:33:05 +01:00
|
|
|
|
|
|
|
struct utp_socket_impl;
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
utp_socket_impl* construct_utp_impl(std::uint16_t recv_id
|
|
|
|
, std::uint16_t send_id, void* userdata
|
2010-11-29 02:33:05 +01:00
|
|
|
, utp_socket_manager* sm);
|
|
|
|
void detach_utp_impl(utp_socket_impl* s);
|
|
|
|
void delete_utp_impl(utp_socket_impl* s);
|
|
|
|
bool should_delete(utp_socket_impl* s);
|
2015-03-12 05:34:54 +01:00
|
|
|
void tick_utp_impl(utp_socket_impl* s, time_point now);
|
2010-11-29 02:33:05 +01:00
|
|
|
void utp_init_mtu(utp_socket_impl* s, int link_mtu, int utp_mtu);
|
2016-07-22 18:31:42 +02:00
|
|
|
bool utp_incoming_packet(utp_socket_impl* s, span<char const> p
|
2016-04-28 14:20:10 +02:00
|
|
|
, udp::endpoint const& ep, time_point receive_time);
|
2016-06-18 20:01:38 +02:00
|
|
|
bool utp_match(utp_socket_impl* s, udp::endpoint const& ep, std::uint16_t id);
|
2010-11-29 02:33:05 +01:00
|
|
|
udp::endpoint utp_remote_endpoint(utp_socket_impl* s);
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint16_t utp_receive_id(utp_socket_impl* s);
|
2010-11-29 02:33:05 +01:00
|
|
|
int utp_socket_state(utp_socket_impl const* s);
|
2012-06-21 17:05:57 +02:00
|
|
|
void utp_send_ack(utp_socket_impl* s);
|
2013-02-06 05:38:30 +01:00
|
|
|
void utp_socket_drained(utp_socket_impl* s);
|
2012-07-01 20:44:46 +02:00
|
|
|
void utp_writable(utp_socket_impl* s);
|
2010-11-29 02:33:05 +01:00
|
|
|
|
|
|
|
// this is the user-level stream interface to utp sockets.
|
|
|
|
// the reason why it's split up in a utp_stream class and
|
|
|
|
// an implementation class is because the socket state has
|
|
|
|
// to be able to out-live the user level socket. For instance
|
|
|
|
// when sending data on a stream and then closing it, the
|
|
|
|
// state holding the send buffer has to be kept around until
|
|
|
|
// it has been flushed, which may be longer than the client
|
|
|
|
// will keep the utp_stream object around for.
|
|
|
|
// for more details, see utp_socket_impl, which is analogous
|
|
|
|
// to the kernel state for a socket. It's defined in utp_stream.cpp
|
2017-02-04 02:27:31 +01:00
|
|
|
struct TORRENT_EXTRA_EXPORT utp_stream
|
2010-11-29 02:33:05 +01:00
|
|
|
{
|
2017-02-04 02:27:31 +01:00
|
|
|
using lowest_layer_type = utp_stream ;
|
|
|
|
using endpoint_type = tcp::socket::endpoint_type;
|
|
|
|
using protocol_type = tcp::socket::protocol_type;
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2015-06-06 07:22:53 +02:00
|
|
|
explicit utp_stream(io_service& io_service);
|
2010-11-29 02:33:05 +01:00
|
|
|
~utp_stream();
|
2017-02-04 02:27:31 +01:00
|
|
|
utp_stream& operator=(utp_stream const&) = delete;
|
|
|
|
utp_stream(utp_stream const&) = delete;
|
|
|
|
utp_stream& operator=(utp_stream&&) = default;
|
|
|
|
utp_stream(utp_stream&&) = default;
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2011-09-10 07:52:07 +02:00
|
|
|
lowest_layer_type& lowest_layer() { return *this; }
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
// used for incoming connections
|
|
|
|
void set_impl(utp_socket_impl* s);
|
|
|
|
utp_socket_impl* get_impl();
|
|
|
|
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
template <class IO_Control_Command>
|
2014-07-07 08:28:48 +02:00
|
|
|
void io_control(IO_Control_Command&) {}
|
2010-11-29 02:33:05 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
template <class IO_Control_Command>
|
2014-07-07 08:28:48 +02:00
|
|
|
void io_control(IO_Control_Command&, error_code&) {}
|
2010-11-29 02:33:05 +01:00
|
|
|
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2012-05-06 02:40:31 +02:00
|
|
|
void bind(endpoint_type const& /*endpoint*/) {}
|
2010-11-29 02:33:05 +01:00
|
|
|
#endif
|
|
|
|
|
2014-07-07 08:28:48 +02:00
|
|
|
void bind(endpoint_type const&, error_code&);
|
2010-11-29 02:33:05 +01:00
|
|
|
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
template <class SettableSocketOption>
|
2014-07-07 08:28:48 +02:00
|
|
|
void set_option(SettableSocketOption const&) {}
|
2010-11-29 02:33:05 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
template <class SettableSocketOption>
|
2014-07-07 08:28:48 +02:00
|
|
|
error_code set_option(SettableSocketOption const&, error_code& ec) { return ec; }
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2013-09-03 10:39:30 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
template <class GettableSocketOption>
|
2014-07-07 08:28:48 +02:00
|
|
|
void get_option(GettableSocketOption&) {}
|
2013-09-03 10:39:30 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
template <class GettableSocketOption>
|
2015-02-28 20:51:15 +01:00
|
|
|
error_code get_option(GettableSocketOption&, error_code& ec)
|
|
|
|
{ return ec; }
|
2013-09-03 10:39:30 +02:00
|
|
|
|
2015-04-19 08:28:21 +02:00
|
|
|
error_code cancel(error_code&)
|
2014-10-06 05:03:01 +02:00
|
|
|
{
|
2015-06-06 07:22:53 +02:00
|
|
|
cancel_handlers(boost::asio::error::operation_aborted);
|
2014-10-06 05:03:01 +02:00
|
|
|
return error_code();
|
|
|
|
}
|
2013-09-03 10:39:30 +02:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
void close();
|
2017-02-04 02:27:31 +01:00
|
|
|
void close(error_code const&) { close(); }
|
2015-02-28 20:51:15 +01:00
|
|
|
|
2017-02-04 02:27:31 +01:00
|
|
|
void set_close_reason(close_reason_t code);
|
|
|
|
close_reason_t get_close_reason();
|
2015-02-28 20:51:15 +01:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
bool is_open() const { return m_open; }
|
|
|
|
|
|
|
|
int read_buffer_size() const;
|
2017-01-13 14:44:08 +01:00
|
|
|
static void on_read(void* self, std::size_t bytes_transferred
|
2015-02-28 20:51:15 +01:00
|
|
|
, error_code const& ec, bool kill);
|
2017-01-13 14:44:08 +01:00
|
|
|
static void on_write(void* self, std::size_t bytes_transferred
|
2015-02-28 20:51:15 +01:00
|
|
|
, error_code const& ec, bool kill);
|
2010-11-29 02:33:05 +01:00
|
|
|
static void on_connect(void* self, error_code const& ec, bool kill);
|
2017-02-04 02:27:31 +01:00
|
|
|
static void on_close_reason(void* self, close_reason_t reason);
|
2010-11-29 02:33:05 +01:00
|
|
|
|
|
|
|
void add_read_buffer(void* buf, size_t len);
|
2015-02-15 01:25:56 +01:00
|
|
|
void issue_read();
|
2010-11-29 02:33:05 +01:00
|
|
|
void add_write_buffer(void const* buf, size_t len);
|
2015-02-15 01:25:56 +01:00
|
|
|
void issue_write();
|
2010-11-29 02:33:05 +01:00
|
|
|
size_t read_some(bool clear_buffers);
|
2015-06-06 07:22:53 +02:00
|
|
|
|
2011-06-21 09:44:13 +02:00
|
|
|
int send_delay() const;
|
2011-09-28 02:03:12 +02:00
|
|
|
int recv_delay() const;
|
2011-06-21 09:44:13 +02:00
|
|
|
|
2015-02-15 01:25:56 +01:00
|
|
|
void do_connect(tcp::endpoint const& ep);
|
2010-11-29 02:33:05 +01:00
|
|
|
|
|
|
|
endpoint_type local_endpoint() const
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
return local_endpoint(ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint_type local_endpoint(error_code& ec) const;
|
|
|
|
|
|
|
|
endpoint_type remote_endpoint() const
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
return remote_endpoint(ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint_type remote_endpoint(error_code& ec) const;
|
|
|
|
|
|
|
|
std::size_t available() const;
|
2012-05-06 02:40:31 +02:00
|
|
|
std::size_t available(error_code& /*ec*/) const { return available(); }
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2015-06-06 07:22:53 +02:00
|
|
|
io_service& get_io_service() { return m_io_service; }
|
2010-11-29 02:33:05 +01:00
|
|
|
|
|
|
|
template <class Handler>
|
|
|
|
void async_connect(endpoint_type const& endpoint, Handler const& handler)
|
|
|
|
{
|
|
|
|
if (!endpoint.address().is_v4())
|
|
|
|
{
|
2016-05-25 06:31:52 +02:00
|
|
|
m_io_service.post(std::bind<void>(handler, boost::asio::error::operation_not_supported, 0));
|
2010-11-29 02:33:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_impl == 0)
|
|
|
|
{
|
2016-05-25 06:31:52 +02:00
|
|
|
m_io_service.post(std::bind<void>(handler, boost::asio::error::not_connected, 0));
|
2010-11-29 02:33:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_connect_handler = handler;
|
2015-02-15 01:25:56 +01:00
|
|
|
do_connect(endpoint);
|
2010-11-29 02:33:05 +01:00
|
|
|
}
|
2015-11-19 01:51:17 +01:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
template <class Mutable_Buffers, class Handler>
|
|
|
|
void async_read_some(Mutable_Buffers const& buffers, Handler const& handler)
|
|
|
|
{
|
|
|
|
if (m_impl == 0)
|
|
|
|
{
|
2016-05-25 06:31:52 +02:00
|
|
|
m_io_service.post(std::bind<void>(handler, boost::asio::error::not_connected, 0));
|
2010-11-29 02:33:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_ASSERT(!m_read_handler);
|
|
|
|
if (m_read_handler)
|
|
|
|
{
|
2016-05-25 06:31:52 +02:00
|
|
|
m_io_service.post(std::bind<void>(handler, boost::asio::error::operation_not_supported, 0));
|
2010-11-29 02:33:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-11-19 01:51:17 +01:00
|
|
|
std::size_t bytes_added = 0;
|
2010-11-29 02:33:05 +01:00
|
|
|
for (typename Mutable_Buffers::const_iterator i = buffers.begin()
|
|
|
|
, end(buffers.end()); i != end; ++i)
|
|
|
|
{
|
2013-08-31 22:19:03 +02:00
|
|
|
if (buffer_size(*i) == 0) continue;
|
2015-06-06 07:22:53 +02:00
|
|
|
using boost::asio::buffer_cast;
|
|
|
|
using boost::asio::buffer_size;
|
2010-11-29 02:33:05 +01:00
|
|
|
add_read_buffer(buffer_cast<void*>(*i), buffer_size(*i));
|
2013-08-31 22:19:03 +02:00
|
|
|
bytes_added += buffer_size(*i);
|
2010-11-29 02:33:05 +01:00
|
|
|
}
|
2013-08-31 22:19:03 +02:00
|
|
|
if (bytes_added == 0)
|
|
|
|
{
|
|
|
|
// if we're reading 0 bytes, post handler immediately
|
|
|
|
// asio's SSL layer depends on this behavior
|
2016-05-25 06:31:52 +02:00
|
|
|
m_io_service.post(std::bind<void>(handler, error_code(), 0));
|
2013-08-31 22:19:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
m_read_handler = handler;
|
2015-02-15 01:25:56 +01:00
|
|
|
issue_read();
|
2010-11-29 02:33:05 +01:00
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
template <class Handler>
|
2015-07-12 05:01:27 +02:00
|
|
|
void async_read_some(null_buffers const&, Handler const& handler)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
|
|
|
if (m_impl == 0)
|
|
|
|
{
|
2016-05-25 06:31:52 +02:00
|
|
|
m_io_service.post(std::bind<void>(handler, boost::asio::error::not_connected, 0));
|
2014-07-06 21:18:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_ASSERT(!m_read_handler);
|
|
|
|
if (m_read_handler)
|
|
|
|
{
|
2016-05-02 18:36:21 +02:00
|
|
|
TORRENT_ASSERT_FAIL(); // we should never do this!
|
2016-05-25 06:31:52 +02:00
|
|
|
m_io_service.post(std::bind<void>(handler, boost::asio::error::operation_not_supported, 0));
|
2014-07-06 21:18:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_read_handler = handler;
|
2015-02-15 01:25:56 +01:00
|
|
|
issue_read();
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
void do_async_connect(endpoint_type const& ep
|
2016-08-13 03:31:55 +02:00
|
|
|
, std::function<void(error_code const&)> const& handler);
|
2010-11-29 02:33:05 +01:00
|
|
|
|
|
|
|
template <class Protocol>
|
2014-07-07 08:28:48 +02:00
|
|
|
void open(Protocol const&, error_code&)
|
2010-11-29 02:33:05 +01:00
|
|
|
{ m_open = true; }
|
|
|
|
|
|
|
|
template <class Protocol>
|
2014-07-07 08:28:48 +02:00
|
|
|
void open(Protocol const&)
|
2010-11-29 02:33:05 +01:00
|
|
|
{ m_open = true; }
|
|
|
|
|
|
|
|
template <class Mutable_Buffers>
|
|
|
|
std::size_t read_some(Mutable_Buffers const& buffers, error_code& ec)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(!m_read_handler);
|
|
|
|
if (m_impl == 0)
|
|
|
|
{
|
2015-06-06 07:22:53 +02:00
|
|
|
ec = boost::asio::error::not_connected;
|
2010-11-29 02:33:05 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (read_buffer_size() == 0)
|
|
|
|
{
|
2015-06-06 07:22:53 +02:00
|
|
|
ec = boost::asio::error::would_block;
|
2010-11-29 02:33:05 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2010-12-19 10:50:58 +01:00
|
|
|
size_t buf_size = 0;
|
2010-11-29 02:33:05 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
for (typename Mutable_Buffers::const_iterator i = buffers.begin()
|
|
|
|
, end(buffers.end()); i != end; ++i)
|
|
|
|
{
|
2015-06-06 07:22:53 +02:00
|
|
|
using boost::asio::buffer_cast;
|
|
|
|
using boost::asio::buffer_size;
|
2010-11-29 02:33:05 +01:00
|
|
|
add_read_buffer(buffer_cast<void*>(*i), buffer_size(*i));
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2010-11-29 02:33:05 +01:00
|
|
|
buf_size += buffer_size(*i);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
std::size_t ret = read_some(true);
|
2010-12-24 02:32:26 +01:00
|
|
|
TORRENT_ASSERT(ret <= buf_size);
|
2010-11-29 02:33:05 +01:00
|
|
|
TORRENT_ASSERT(ret > 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Const_Buffers>
|
2014-07-07 08:28:48 +02:00
|
|
|
std::size_t write_some(Const_Buffers const& /* buffers */, error_code& /* ec */)
|
2010-11-29 02:33:05 +01:00
|
|
|
{
|
2011-09-10 07:52:07 +02:00
|
|
|
TORRENT_ASSERT(false && "not implemented!");
|
2014-01-20 11:11:07 +01:00
|
|
|
// TODO: implement blocking write. Low priority since it's not used (yet)
|
2010-11-29 02:33:05 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-10 07:52:07 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
template <class Mutable_Buffers>
|
|
|
|
std::size_t read_some(Mutable_Buffers const& buffers)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
std::size_t ret = read_some(buffers, ec);
|
|
|
|
if (ec)
|
|
|
|
boost::throw_exception(boost::system::system_error(ec));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Const_Buffers>
|
|
|
|
std::size_t write_some(Const_Buffers const& buffers)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
std::size_t ret = write_some(buffers, ec);
|
|
|
|
if (ec)
|
|
|
|
boost::throw_exception(boost::system::system_error(ec));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
template <class Const_Buffers, class Handler>
|
|
|
|
void async_write_some(Const_Buffers const& buffers, Handler const& handler)
|
|
|
|
{
|
|
|
|
if (m_impl == 0)
|
|
|
|
{
|
2016-05-25 06:31:52 +02:00
|
|
|
m_io_service.post(std::bind<void>(handler
|
2015-06-06 07:22:53 +02:00
|
|
|
, boost::asio::error::not_connected, 0));
|
2010-11-29 02:33:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_ASSERT(!m_write_handler);
|
|
|
|
if (m_write_handler)
|
|
|
|
{
|
2016-05-25 06:31:52 +02:00
|
|
|
m_io_service.post(std::bind<void>(handler
|
2015-06-06 07:22:53 +02:00
|
|
|
, boost::asio::error::operation_not_supported, 0));
|
2010-11-29 02:33:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-19 01:51:17 +01:00
|
|
|
std::size_t bytes_added = 0;
|
2010-11-29 02:33:05 +01:00
|
|
|
for (typename Const_Buffers::const_iterator i = buffers.begin()
|
|
|
|
, end(buffers.end()); i != end; ++i)
|
|
|
|
{
|
2013-08-31 22:19:03 +02:00
|
|
|
if (buffer_size(*i) == 0) continue;
|
2015-06-06 07:22:53 +02:00
|
|
|
using boost::asio::buffer_cast;
|
|
|
|
using boost::asio::buffer_size;
|
2015-07-18 04:01:45 +02:00
|
|
|
add_write_buffer(buffer_cast<void const*>(*i), buffer_size(*i));
|
2013-08-31 22:19:03 +02:00
|
|
|
bytes_added += buffer_size(*i);
|
|
|
|
}
|
|
|
|
if (bytes_added == 0)
|
|
|
|
{
|
2015-07-18 04:01:45 +02:00
|
|
|
// if we're writing 0 bytes, post handler immediately
|
2013-08-31 22:19:03 +02:00
|
|
|
// asio's SSL layer depends on this behavior
|
2016-05-25 06:31:52 +02:00
|
|
|
m_io_service.post(std::bind<void>(handler, error_code(), 0));
|
2013-08-31 22:19:03 +02:00
|
|
|
return;
|
2010-11-29 02:33:05 +01:00
|
|
|
}
|
|
|
|
m_write_handler = handler;
|
2015-02-15 01:25:56 +01:00
|
|
|
issue_write();
|
2010-11-29 02:33:05 +01:00
|
|
|
}
|
|
|
|
|
2015-02-28 20:51:15 +01:00
|
|
|
private:
|
2010-11-29 02:33:05 +01:00
|
|
|
|
|
|
|
void cancel_handlers(error_code const&);
|
|
|
|
|
2016-08-13 03:31:55 +02:00
|
|
|
std::function<void(error_code const&)> m_connect_handler;
|
|
|
|
std::function<void(error_code const&, std::size_t)> m_read_handler;
|
|
|
|
std::function<void(error_code const&, std::size_t)> m_write_handler;
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2015-06-06 07:22:53 +02:00
|
|
|
io_service& m_io_service;
|
2010-11-29 02:33:05 +01:00
|
|
|
utp_socket_impl* m_impl;
|
2013-11-26 08:47:48 +01:00
|
|
|
|
2017-02-04 02:27:31 +01:00
|
|
|
close_reason_t m_incoming_close_reason = close_reason_t::none;
|
2015-02-28 20:51:15 +01:00
|
|
|
|
2013-11-26 08:47:48 +01:00
|
|
|
// this field requires another 8 bytes (including padding)
|
2010-11-29 02:33:05 +01:00
|
|
|
bool m_open;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|