Skip to content

Commit

Permalink
Fixed websocket tracker to correctly route answers
Browse files Browse the repository at this point in the history
  • Loading branch information
paullouisageneau committed Nov 25, 2019
1 parent 01efd86 commit c0af193
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
3 changes: 3 additions & 0 deletions include/libtorrent/websocket_tracker_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE.

#include <boost/beast/core/flat_buffer.hpp>

#include <map>
#include <memory>
#include <queue>
#include <tuple>
Expand Down Expand Up @@ -104,6 +105,8 @@ 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;
};

}
Expand Down
58 changes: 37 additions & 21 deletions src/websocket_tracker_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,23 @@ void websocket_tracker_connection::queue_answer(tracker_answer ans)

void websocket_tracker_connection::send_pending()
{
if(!m_sending && !m_pending.empty())
{
m_sending = true;
if(m_sending || m_pending.empty()) return;

std::visit([this](auto const& message) {
do_send(message);
},
std::get<0>(m_pending.front()));
}
m_sending = true;

tracker_message msg;
std::weak_ptr<request_callback> cb;
std::tie(msg, cb) = m_pending.front();

// Update requester
if(cb.lock()) m_requester = cb;

std::visit([&](auto const& m)
{
do_send(m);
}
, msg
);
}

void websocket_tracker_connection::do_send(tracker_request const& req)
Expand Down Expand Up @@ -281,20 +289,16 @@ void websocket_tracker_connection::on_connect(error_code const& ec)
do_read();
}

void websocket_tracker_connection::on_timeout(error_code const& ec)
void websocket_tracker_connection::on_timeout(error_code const& /*ec*/)
{
if(ec)
{
// TODO
return;
}
// Dummy
}

void websocket_tracker_connection::on_read(error_code const& ec, std::size_t /* bytes_read */)
{
if(ec)
{
// TODO
// Ignore
return;
}

Expand All @@ -312,6 +316,10 @@ void websocket_tracker_connection::on_read(error_code const& ec, std::size_t /*

auto const info_hash = sha1_hash(to_latin1(payload.value<std::string>("info_hash", "")));

// Find the correct callback given the info_hash
if(auto it = m_callbacks.find(info_hash); it != m_callbacks.end()) cb = it->second.lock();
if (!cb) return;

if(auto it = payload.find("offer"); it != payload.end())
{
auto const &payload_offer = *it;
Expand Down Expand Up @@ -361,18 +369,26 @@ void websocket_tracker_connection::on_write(error_code const& ec, std::size_t /*

if(!m_pending.empty())
{
// Update requester
if(auto r = std::get<1>(m_pending.front()); r.lock())
tracker_message msg;
std::weak_ptr<request_callback> cb;
std::tie(msg, cb) = std::move(m_pending.front());
m_pending.pop();

// Store callback
if(cb.lock())
{
m_requester = r;
std::visit([&](auto const& m)
{
m_callbacks.emplace(m.info_hash, cb);
}
, msg
);
}

m_pending.pop();
}

if(ec)
{
// TODO
// Ignore
return;
}

Expand Down

0 comments on commit c0af193

Please sign in to comment.