2010-11-29 02:33:05 +01:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2009-2018, 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_SOCKET_MANAGER_HPP_INCLUDED
|
|
|
|
#define TORRENT_UTP_SOCKET_MANAGER_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include <map>
|
2016-08-13 03:31:55 +02:00
|
|
|
#include <functional>
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2018-01-28 11:48:21 +01:00
|
|
|
#include "libtorrent/aux_/socket_type.hpp"
|
2010-11-29 02:33:05 +01:00
|
|
|
#include "libtorrent/session_status.hpp"
|
|
|
|
#include "libtorrent/enum_net.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/aux_/session_settings.hpp"
|
2016-07-22 18:31:42 +02:00
|
|
|
#include "libtorrent/span.hpp"
|
2017-02-10 14:25:03 +01:00
|
|
|
#include "libtorrent/packet_pool.hpp"
|
2016-04-24 21:26:28 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2017-02-04 02:27:31 +01:00
|
|
|
struct utp_stream;
|
2010-11-29 02:33:05 +01:00
|
|
|
struct utp_socket_impl;
|
2014-07-06 21:18:00 +02:00
|
|
|
struct counters;
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2017-04-26 05:00:41 +02:00
|
|
|
// interface/handle to the underlying udp socket
|
|
|
|
struct TORRENT_EXTRA_EXPORT utp_socket_interface
|
2010-11-29 02:33:05 +01:00
|
|
|
{
|
2017-04-26 05:00:41 +02:00
|
|
|
virtual udp::endpoint local_endpoint() = 0;
|
|
|
|
protected:
|
|
|
|
virtual ~utp_socket_interface() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct utp_socket_manager
|
|
|
|
{
|
2017-07-26 19:38:40 +02:00
|
|
|
using send_fun_t = std::function<void(std::weak_ptr<utp_socket_interface>
|
2017-04-26 05:00:41 +02:00
|
|
|
, udp::endpoint const&
|
2016-07-22 18:31:42 +02:00
|
|
|
, span<char const>
|
2017-07-26 19:38:40 +02:00
|
|
|
, error_code&, udp_send_flags_t)>;
|
2016-04-24 21:26:28 +02:00
|
|
|
|
2018-01-28 11:48:21 +01:00
|
|
|
using incoming_utp_callback_t = std::function<void(std::shared_ptr<aux::socket_type> const&)>;
|
2016-04-24 21:26:28 +02:00
|
|
|
|
|
|
|
utp_socket_manager(send_fun_t const& send_fun
|
|
|
|
, incoming_utp_callback_t const& cb
|
|
|
|
, io_service& ios
|
|
|
|
, aux::session_settings const& sett
|
2017-07-26 19:38:40 +02:00
|
|
|
, counters& cnt, void* ssl_context);
|
2010-11-29 02:33:05 +01:00
|
|
|
~utp_socket_manager();
|
|
|
|
|
|
|
|
// return false if this is not a uTP packet
|
2017-04-26 05:00:41 +02:00
|
|
|
bool incoming_packet(std::weak_ptr<utp_socket_interface> socket
|
|
|
|
, udp::endpoint const& ep, span<char const> p);
|
2012-06-22 06:21:20 +02:00
|
|
|
|
2016-04-24 21:26:28 +02:00
|
|
|
// if the UDP socket failed with an EAGAIN or EWOULDBLOCK, this will be
|
|
|
|
// called once the socket is writeable again
|
|
|
|
void writable();
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2016-04-24 21:26:28 +02:00
|
|
|
// when the upper layer has drained the underlying UDP socket, this is
|
|
|
|
// called, and uTP sockets will send their ACKs. This ensures ACKs at
|
2017-08-13 03:24:15 +02:00
|
|
|
// least coalesce packets returned during the same wakeup
|
2016-04-24 21:26:28 +02:00
|
|
|
void socket_drained();
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2016-04-24 21:26:28 +02:00
|
|
|
void tick(time_point now);
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2017-04-26 05:00:41 +02:00
|
|
|
void send_packet(std::weak_ptr<utp_socket_interface> sock, udp::endpoint const& ep
|
|
|
|
, char const* p, int len
|
2017-07-26 19:38:40 +02:00
|
|
|
, error_code& ec, udp_send_flags_t flags = {});
|
2012-07-01 20:44:46 +02:00
|
|
|
void subscribe_writable(utp_socket_impl* s);
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2017-04-26 05:00:41 +02:00
|
|
|
void remove_udp_socket(std::weak_ptr<utp_socket_interface> sock);
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
// internal, used by utp_stream
|
2016-06-18 20:01:38 +02:00
|
|
|
void remove_socket(std::uint16_t id);
|
2010-11-29 02:33:05 +01:00
|
|
|
|
|
|
|
utp_socket_impl* new_utp_socket(utp_stream* str);
|
2014-07-06 21:18:00 +02:00
|
|
|
int gain_factor() const { return m_sett.get_int(settings_pack::utp_gain_factor); }
|
|
|
|
int target_delay() const { return m_sett.get_int(settings_pack::utp_target_delay) * 1000; }
|
|
|
|
int syn_resends() const { return m_sett.get_int(settings_pack::utp_syn_resends); }
|
|
|
|
int fin_resends() const { return m_sett.get_int(settings_pack::utp_fin_resends); }
|
|
|
|
int num_resends() const { return m_sett.get_int(settings_pack::utp_num_resends); }
|
|
|
|
int connect_timeout() const { return m_sett.get_int(settings_pack::utp_connect_timeout); }
|
|
|
|
int min_timeout() const { return m_sett.get_int(settings_pack::utp_min_timeout); }
|
|
|
|
int loss_multiplier() const { return m_sett.get_int(settings_pack::utp_loss_multiplier); }
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2018-07-19 13:26:44 +02:00
|
|
|
std::pair<int, int> mtu_for_dest(address const& addr);
|
2016-05-02 18:36:21 +02:00
|
|
|
int num_sockets() const { return int(m_utp_sockets.size()); }
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2012-06-21 17:05:57 +02:00
|
|
|
void defer_ack(utp_socket_impl* s);
|
2013-02-06 05:38:30 +01:00
|
|
|
void subscribe_drained(utp_socket_impl* s);
|
2012-06-21 17:05:57 +02:00
|
|
|
|
2017-01-13 14:44:08 +01:00
|
|
|
void restrict_mtu(int const mtu)
|
2016-03-12 07:07:17 +01:00
|
|
|
{
|
2017-01-09 07:43:57 +01:00
|
|
|
m_restrict_mtu[std::size_t(m_mtu_idx)] = mtu;
|
|
|
|
m_mtu_idx = (m_mtu_idx + 1) % int(m_restrict_mtu.size());
|
2016-03-12 07:07:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int restrict_mtu() const
|
|
|
|
{
|
|
|
|
return *std::max_element(m_restrict_mtu.begin(), m_restrict_mtu.end());
|
|
|
|
}
|
|
|
|
|
2013-09-14 12:06:48 +02:00
|
|
|
// used to keep stats of uTP events
|
2014-07-06 21:18:00 +02:00
|
|
|
// the counter is the enum from ``counters``.
|
2015-01-04 22:31:02 +01:00
|
|
|
void inc_stats_counter(int counter, int delta = 1);
|
2013-09-14 12:06:48 +02:00
|
|
|
|
2017-02-11 20:21:48 +01:00
|
|
|
packet_ptr acquire_packet(int const allocate) { return m_packet_pool.acquire(allocate); }
|
|
|
|
void release_packet(packet_ptr p) { m_packet_pool.release(std::move(p)); }
|
2017-02-11 02:54:26 +01:00
|
|
|
void decay() { m_packet_pool.decay(); }
|
2017-02-10 14:25:03 +01:00
|
|
|
|
2015-04-27 04:21:12 +02:00
|
|
|
// explicitly disallow assignment, to silence msvc warning
|
2018-06-11 18:55:30 +02:00
|
|
|
utp_socket_manager& operator=(utp_socket_manager const&) = delete;
|
|
|
|
|
|
|
|
private:
|
2015-04-27 04:21:12 +02:00
|
|
|
|
2016-04-24 21:26:28 +02:00
|
|
|
send_fun_t m_send_fun;
|
2010-11-29 02:33:05 +01:00
|
|
|
incoming_utp_callback_t m_cb;
|
|
|
|
|
|
|
|
// replace with a hash-map
|
2018-03-22 17:01:38 +01:00
|
|
|
using socket_map_t = std::multimap<std::uint16_t, utp_socket_impl*>;
|
2010-11-29 02:33:05 +01:00
|
|
|
socket_map_t m_utp_sockets;
|
|
|
|
|
2017-01-09 01:18:26 +01:00
|
|
|
using socket_vector_t = std::vector<utp_socket_impl*>;
|
|
|
|
|
2012-06-21 17:05:57 +02:00
|
|
|
// this is a list of sockets that needs to send an ack.
|
|
|
|
// once the UDP socket is drained, all of these will
|
|
|
|
// have a chance to do that. This is to avoid sending
|
|
|
|
// an ack for every single packet
|
2017-01-09 01:18:26 +01:00
|
|
|
socket_vector_t m_deferred_acks;
|
|
|
|
|
|
|
|
// storage used for saving cpu time on "push_back"
|
|
|
|
// by using already pre-allocated vector
|
|
|
|
socket_vector_t m_temp_sockets;
|
2013-02-06 05:38:30 +01:00
|
|
|
|
|
|
|
// sockets that have received or sent packets this
|
|
|
|
// round, may subscribe to the event of draining the
|
|
|
|
// UDP socket. At that point they may call the
|
|
|
|
// user callback function to indicate bytes have been
|
|
|
|
// sent or received.
|
2017-01-09 01:18:26 +01:00
|
|
|
socket_vector_t m_drained_event;
|
2015-08-20 02:40:21 +02:00
|
|
|
|
2012-07-01 20:44:46 +02:00
|
|
|
// list of sockets that received EWOULDBLOCK from the
|
|
|
|
// underlying socket. They are notified when the socket
|
|
|
|
// becomes writable again
|
2017-01-09 01:18:26 +01:00
|
|
|
socket_vector_t m_stalled_sockets;
|
2012-06-21 17:05:57 +02:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
// the last socket we received a packet on
|
2016-10-27 02:41:57 +02:00
|
|
|
utp_socket_impl* m_last_socket = nullptr;
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2016-10-27 02:41:57 +02:00
|
|
|
int m_new_connection = -1;
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
aux::session_settings const& m_sett;
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2013-09-14 12:06:48 +02:00
|
|
|
// stats counters
|
2014-07-06 21:18:00 +02:00
|
|
|
counters& m_counters;
|
2014-10-06 05:03:01 +02:00
|
|
|
|
2016-04-24 21:26:28 +02:00
|
|
|
io_service& m_ios;
|
|
|
|
|
2016-05-01 05:10:47 +02:00
|
|
|
std::array<int, 3> m_restrict_mtu;
|
2016-10-27 02:41:57 +02:00
|
|
|
int m_mtu_idx = 0;
|
2016-03-12 07:07:17 +01:00
|
|
|
|
2014-10-06 05:03:01 +02:00
|
|
|
// this is passed on to the instantiate connection
|
2016-06-20 17:32:06 +02:00
|
|
|
// if this is non-nullptr it will create SSL connections over uTP
|
2014-10-06 05:03:01 +02:00
|
|
|
void* m_ssl_context;
|
2017-02-10 14:25:03 +01:00
|
|
|
|
|
|
|
packet_pool m_packet_pool;
|
2010-11-29 02:33:05 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|