Skip to content

Commit

Permalink
Fixes after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
paullouisageneau committed Nov 25, 2019
1 parent 850fd91 commit 56b3964
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 32 deletions.
1 change: 1 addition & 0 deletions include/libtorrent/alert_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down
8 changes: 5 additions & 3 deletions include/libtorrent/aux_/rtc_signaling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -58,6 +56,10 @@ namespace rtc {
}

namespace libtorrent {

class alert_manager;
class torrent;

namespace aux {

struct rtc_stream_init;
Expand Down
2 changes: 1 addition & 1 deletion include/libtorrent/aux_/rtc_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
1 change: 1 addition & 0 deletions include/libtorrent/socket_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ enum class socket_type_t : std::uint8_t {
http,
utp,
i2p,
rtc,
tcp_ssl,
socks5_ssl,
http_ssl,
Expand Down
2 changes: 0 additions & 2 deletions include/libtorrent/tracker_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::uint8_t, struct tracker_request_flags_tag>;
Expand Down
15 changes: 6 additions & 9 deletions include/libtorrent/websocket_tracker_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -104,11 +100,12 @@ class TORRENT_EXTRA_EXPORT websocket_tracker_connection

using tracker_message = std::variant<tracker_request, tracker_answer>;
std::queue<std::tuple<tracker_message, std::weak_ptr<request_callback>>> m_pending;
bool m_sending;

std::map<sha1_hash, std::weak_ptr<request_callback>> m_callbacks;

bool m_sending = false;
};

}

#endif // TORRENT_WEBSOCKET_TRACKER_CONNECTION_HPP_INCLUDED

3 changes: 3 additions & 0 deletions src/rtc_signaling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
19 changes: 16 additions & 3 deletions src/rtc_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,23 @@ rtc_stream::rtc_stream(io_context& ioc, rtc_stream_init const& init)
char const *raw = reinterpret_cast<char const*>(data.data());
post(m_io_context, std::bind(&rtc_stream::on_message
, this
, boost::system::error_code{}
, error_code{}
, std::vector<char>(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();
Expand Down Expand Up @@ -86,8 +96,11 @@ close_reason_t rtc_stream::get_close_reason()

void rtc_stream::close()
{
m_data_channel->onMessage([](std::variant<rtc::binary, rtc::string> const&) {});
m_data_channel->close();
if(m_data_channel) // it can be null after a move constructor
{
m_data_channel->onMessage([](std::variant<rtc::binary, rtc::string> const&) {});
m_data_channel->close();
}

cancel_handlers(boost::asio::error::operation_aborted);
}
Expand Down
2 changes: 1 addition & 1 deletion src/socket_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace aux {
#if TORRENT_USE_RTC
bool is_rtc(socket_type const& s)
{
return s.get<rtc_stream>() != nullptr;
return boost::get<rtc_stream>(&s);
}
#endif

Expand Down
9 changes: 4 additions & 5 deletions src/torrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<aux::socket_type> s = std::make_shared<aux::socket_type>(m_ses.get_context());
s->instantiate<aux::rtc_stream>(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{
Expand All @@ -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<bt_peer_connection>(std::move(pack));
auto c = std::make_shared<bt_peer_connection>(pack);

#if TORRENT_USE_ASSERTS
c->m_in_constructor = false;
Expand Down
1 change: 1 addition & 0 deletions src/tracker_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,4 @@ constexpr tracker_request_flags_t tracker_request::i2p;
return int(m_http_conns.size() + m_udp_conns.size());
}
}

13 changes: 5 additions & 8 deletions src/websocket_tracker_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -125,8 +123,7 @@ websocket_tracker_connection::websocket_tracker_connection(io_context& ios
, std::weak_ptr<request_callback> cb)
: tracker_connection(man, req, ios, cb)
, m_io_context(ios)
, m_websocket(std::make_shared<websocket_stream>(m_io_context, m_man.host_resolver(), req.ssl_ctx))
, m_sending(false)
, m_websocket(std::make_shared<aux::websocket_stream>(m_io_context, m_man.host_resolver(), req.ssl_ctx))
{
aux::session_settings const& settings = m_man.settings();

Expand Down Expand Up @@ -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<request_callback>());
m_pending.emplace(tracker_message{std::move(ans)}, std::weak_ptr<request_callback>{});
if(m_websocket->is_open()) send_pending();
}

Expand Down Expand Up @@ -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<int>(req.event) - 1];

payload["peer_id"] = from_latin1({req.pid.data(), req.pid.size()});
Expand Down

0 comments on commit 56b3964

Please sign in to comment.