From 56b39647de8af5946034110747c160da62065cf1 Mon Sep 17 00:00:00 2001 From: Paul-Louis Ageneau Date: Mon, 25 Nov 2019 19:18:04 +0100 Subject: [PATCH] Fixes after rebase --- include/libtorrent/alert_types.hpp | 1 + include/libtorrent/aux_/rtc_signaling.hpp | 8 +++++--- include/libtorrent/aux_/rtc_stream.hpp | 2 +- include/libtorrent/socket_type.hpp | 1 + include/libtorrent/tracker_manager.hpp | 2 -- .../websocket_tracker_connection.hpp | 15 ++++++--------- src/rtc_signaling.cpp | 3 +++ src/rtc_stream.cpp | 19 ++++++++++++++++--- src/socket_type.cpp | 2 +- src/torrent.cpp | 9 ++++----- src/tracker_manager.cpp | 1 + src/websocket_tracker_connection.cpp | 13 +++++-------- 12 files changed, 44 insertions(+), 32 deletions(-) diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index b293ef7a916..c368b6c2021 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -1500,6 +1500,7 @@ TORRENT_VERSION_NAMESPACE_2 tcp_ssl TORRENT_DEPRECATED_ENUM, udp TORRENT_DEPRECATED_ENUM, i2p TORRENT_DEPRECATED_ENUM, + rtc TORRENT_DEPRECATED_ENUM, socks5 TORRENT_DEPRECATED_ENUM, utp_ssl TORRENT_DEPRECATED_ENUM }; diff --git a/include/libtorrent/aux_/rtc_signaling.hpp b/include/libtorrent/aux_/rtc_signaling.hpp index 50017edb9bc..1fb733d2640 100644 --- a/include/libtorrent/aux_/rtc_signaling.hpp +++ b/include/libtorrent/aux_/rtc_signaling.hpp @@ -33,9 +33,7 @@ POSSIBILITY OF SUCH DAMAGE. #ifndef TORRENT_RTC_SIGNALING_HPP_INCLUDED #define TORRENT_RTC_SIGNALING_HPP_INCLUDED -#include "libtorrent/alert.hpp" -#include "libtorrent/alert_manager.hpp" -#include "libtorrent/alert_types.hpp" +#include "libtorrent/config.hpp" #include "libtorrent/error_code.hpp" #include "libtorrent/io.hpp" #include "libtorrent/io_context.hpp" @@ -58,6 +56,10 @@ namespace rtc { } namespace libtorrent { + +class alert_manager; +class torrent; + namespace aux { struct rtc_stream_init; diff --git a/include/libtorrent/aux_/rtc_stream.hpp b/include/libtorrent/aux_/rtc_stream.hpp index f29e52308dd..a9f972f257f 100644 --- a/include/libtorrent/aux_/rtc_stream.hpp +++ b/include/libtorrent/aux_/rtc_stream.hpp @@ -82,7 +82,7 @@ struct TORRENT_EXTRA_EXPORT rtc_stream rtc_stream& operator=(rtc_stream const&) = delete; rtc_stream(rtc_stream const&) = delete; rtc_stream& operator=(rtc_stream&&) noexcept = delete; - rtc_stream(rtc_stream&&) noexcept = delete; + rtc_stream(rtc_stream&&) noexcept; lowest_layer_type& lowest_layer() { return *this; } diff --git a/include/libtorrent/socket_type.hpp b/include/libtorrent/socket_type.hpp index 06881729d26..a923568a1d2 100644 --- a/include/libtorrent/socket_type.hpp +++ b/include/libtorrent/socket_type.hpp @@ -45,6 +45,7 @@ enum class socket_type_t : std::uint8_t { http, utp, i2p, + rtc, tcp_ssl, socks5_ssl, http_ssl, diff --git a/include/libtorrent/tracker_manager.hpp b/include/libtorrent/tracker_manager.hpp index 612518ee971..9e343e5016b 100644 --- a/include/libtorrent/tracker_manager.hpp +++ b/include/libtorrent/tracker_manager.hpp @@ -96,8 +96,6 @@ namespace libtorrent { namespace aux { struct session_logger; struct session_settings; - struct rtc_offer; - struct rtc_answer; } using tracker_request_flags_t = flags::bitfield_flag; diff --git a/include/libtorrent/websocket_tracker_connection.hpp b/include/libtorrent/websocket_tracker_connection.hpp index a010f69fa37..1aba6b92c68 100644 --- a/include/libtorrent/websocket_tracker_connection.hpp +++ b/include/libtorrent/websocket_tracker_connection.hpp @@ -33,8 +33,11 @@ POSSIBILITY OF SUCH DAMAGE. #ifndef TORRENT_WEBSOCKET_TRACKER_CONNECTION_HPP_INCLUDED #define TORRENT_WEBSOCKET_TRACKER_CONNECTION_HPP_INCLUDED +#include "libtorrent/aux_/rtc_signaling.hpp" // for rtc_offer and rtc_answer +#include "libtorrent/aux_/websocket_stream.hpp" #include "libtorrent/config.hpp" #include "libtorrent/error_code.hpp" +#include "libtorrent/io_context.hpp" #include "libtorrent/peer_id.hpp" #include "libtorrent/resolver_interface.hpp" #include "libtorrent/tracker_manager.hpp" // for tracker_connection @@ -49,12 +52,6 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { -namespace aux { - class websocket_stream; - class rtc_offer; - class rtc_answer; -} - struct tracker_answer { sha1_hash info_hash; @@ -91,7 +88,6 @@ class TORRENT_EXTRA_EXPORT websocket_tracker_connection void send_pending(); void do_send(tracker_request const& req); void do_send(tracker_answer const& ans); - void do_read(); void on_connect(error_code const& ec); void on_timeout(error_code const& ec); @@ -104,11 +100,12 @@ class TORRENT_EXTRA_EXPORT websocket_tracker_connection using tracker_message = std::variant; std::queue>> m_pending; - bool m_sending; - std::map> m_callbacks; + + bool m_sending = false; }; } #endif // TORRENT_WEBSOCKET_TRACKER_CONNECTION_HPP_INCLUDED + diff --git a/src/rtc_signaling.cpp b/src/rtc_signaling.cpp index 8e827c4b4f9..d960606d8ec 100644 --- a/src/rtc_signaling.cpp +++ b/src/rtc_signaling.cpp @@ -31,6 +31,9 @@ POSSIBILITY OF SUCH DAMAGE. */ #include "libtorrent/aux_/rtc_signaling.hpp" +#include "libtorrent/alert.hpp" +#include "libtorrent/alert_manager.hpp" +#include "libtorrent/alert_types.hpp" #include "libtorrent/aux_/generate_peer_id.hpp" #include "libtorrent/aux_/rtc_stream.hpp" #include "libtorrent/random.hpp" diff --git a/src/rtc_stream.cpp b/src/rtc_stream.cpp index 6ce901b83fc..757c7966831 100644 --- a/src/rtc_stream.cpp +++ b/src/rtc_stream.cpp @@ -52,13 +52,23 @@ rtc_stream::rtc_stream(io_context& ioc, rtc_stream_init const& init) char const *raw = reinterpret_cast(data.data()); post(m_io_context, std::bind(&rtc_stream::on_message , this - , boost::system::error_code{} + , error_code{} , std::vector(raw, raw + data.size()) )); }, message); }); } +rtc_stream::rtc_stream(rtc_stream&& rhs) noexcept + : rtc_stream(rhs.m_io_context, { rhs.m_peer_connection, rhs.m_data_channel }) +{ + rhs.m_peer_connection.reset(); + rhs.m_data_channel.reset(); + + std::swap(m_incoming, rhs.m_incoming); + std::swap(m_incoming_size, rhs.m_incoming_size); +} + rtc_stream::~rtc_stream() { close(); @@ -86,8 +96,11 @@ close_reason_t rtc_stream::get_close_reason() void rtc_stream::close() { - m_data_channel->onMessage([](std::variant const&) {}); - m_data_channel->close(); + if(m_data_channel) // it can be null after a move constructor + { + m_data_channel->onMessage([](std::variant const&) {}); + m_data_channel->close(); + } cancel_handlers(boost::asio::error::operation_aborted); } diff --git a/src/socket_type.cpp b/src/socket_type.cpp index a8c8be53cda..74109b1c276 100644 --- a/src/socket_type.cpp +++ b/src/socket_type.cpp @@ -112,7 +112,7 @@ namespace aux { #if TORRENT_USE_RTC bool is_rtc(socket_type const& s) { - return s.get() != nullptr; + return boost::get(&s); } #endif diff --git a/src/torrent.cpp b/src/torrent.cpp index d4cb55c0acc..5e313d4a3af 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -5602,14 +5602,13 @@ bool is_downloading_state(int const st) void torrent::on_rtc_stream(peer_id const& pid, aux::rtc_stream_init& stream_init) { - std::shared_ptr s = std::make_shared(m_ses.get_context()); - s->instantiate(m_ses.get_context(), &stream_init); + aux::socket_type s(aux::rtc_stream(m_ses.get_context(), stream_init)); torrent_state st = get_peer_list_state(); torrent_peer* peerinfo = m_peer_list->add_rtc_peer(pid.to_string(), peer_source_flags_t{}, {}, &st); error_code ec; - auto remote_endpoint = s->remote_endpoint(ec); + auto remote_endpoint = s.remote_endpoint(ec); peer_id const our_pid = aux::generate_peer_id(settings()); peer_connection_args pack{ @@ -5619,13 +5618,13 @@ bool is_downloading_state(int const st) , &m_ses.disk_thread() , &m_ses.get_context() , shared_from_this() - , s + , std::move(s) , remote_endpoint , peerinfo , our_pid }; - auto c = std::make_shared(std::move(pack)); + auto c = std::make_shared(pack); #if TORRENT_USE_ASSERTS c->m_in_constructor = false; diff --git a/src/tracker_manager.cpp b/src/tracker_manager.cpp index 3cbe859ecef..72328138116 100644 --- a/src/tracker_manager.cpp +++ b/src/tracker_manager.cpp @@ -489,3 +489,4 @@ constexpr tracker_request_flags_t tracker_request::i2p; return int(m_http_conns.size() + m_udp_conns.size()); } } + diff --git a/src/websocket_tracker_connection.cpp b/src/websocket_tracker_connection.cpp index dc97fbb9218..300d2574883 100644 --- a/src/websocket_tracker_connection.cpp +++ b/src/websocket_tracker_connection.cpp @@ -30,14 +30,13 @@ POSSIBILITY OF SUCH DAMAGE. */ +#include "libtorrent/config.hpp" // for TORRENT_USE_RTC + #if TORRENT_USE_RTC #include "libtorrent/websocket_tracker_connection.hpp" #include "libtorrent/aux_/escape_string.hpp" -#include "libtorrent/aux_/rtc_signaling.hpp" #include "libtorrent/aux_/session_settings.hpp" -#include "libtorrent/aux_/websocket_stream.hpp" -#include "libtorrent/config.hpp" #include "libtorrent/io.hpp" #include "libtorrent/ip_filter.hpp" #include "libtorrent/socket.hpp" @@ -59,7 +58,6 @@ namespace libtorrent { using namespace std::placeholders; -using websocket_stream = aux::websocket_stream; using json = nlohmann::json; std::string from_latin1(std::string const& s) { @@ -125,8 +123,7 @@ websocket_tracker_connection::websocket_tracker_connection(io_context& ios , std::weak_ptr cb) : tracker_connection(man, req, ios, cb) , m_io_context(ios) - , m_websocket(std::make_shared(m_io_context, m_man.host_resolver(), req.ssl_ctx)) - , m_sending(false) + , m_websocket(std::make_shared(m_io_context, m_man.host_resolver(), req.ssl_ctx)) { aux::session_settings const& settings = m_man.settings(); @@ -173,7 +170,7 @@ void websocket_tracker_connection::queue_request(tracker_request req, std::weak_ void websocket_tracker_connection::queue_answer(tracker_answer ans) { - m_pending.emplace(tracker_message{std::move(ans)}, std::shared_ptr()); + m_pending.emplace(tracker_message{std::move(ans)}, std::weak_ptr{}); if(m_websocket->is_open()) send_pending(); } @@ -214,7 +211,7 @@ void websocket_tracker_connection::do_send(tracker_request const& req) payload["key"] = str_key; static const char* event_string[] = {"completed", "started", "stopped", "paused"}; - if(req.event != tracker_request::none) + if(req.event != event_t::none) payload["event"] = event_string[static_cast(req.event) - 1]; payload["peer_id"] = from_latin1({req.pid.data(), req.pid.size()});