|
| 1 | +#include "resolve_attempt_tcp.h" |
| 2 | +#include "common.h" |
| 3 | +#include "resolver_impl.h" |
| 4 | +#include "stream_info_impl.h" |
| 5 | +#include <algorithm> |
| 6 | +#include <asio/buffer.hpp> |
| 7 | +#include <asio/connect.hpp> |
| 8 | +#include <asio/read.hpp> |
| 9 | +#include <asio/write.hpp> |
| 10 | +#include <exception> |
| 11 | +#include <loguru.hpp> |
| 12 | +#include <mutex> |
| 13 | +#include <utility> |
| 14 | + |
| 15 | +using namespace lsl; |
| 16 | +using err_t = const asio::error_code &; |
| 17 | + |
| 18 | +/// Maximum number of TCP probes in flight at once (one socket each). |
| 19 | +static const std::size_t MAX_CONCURRENT_PROBES = 16; |
| 20 | + |
| 21 | +resolve_attempt_tcp::resolve_attempt_tcp(asio::io_context &io, |
| 22 | + const std::vector<tcp::endpoint> &targets, const std::string &query, resolver_impl &resolver, |
| 23 | + double cancel_after) |
| 24 | + : io_(io), resolver_(resolver), cancel_after_(cancel_after), cancelled_(false), |
| 25 | + targets_(targets), next_target_(0), active_workers_(0), cancel_timer_(io) { |
| 26 | + // Precompute the query message. Unlike the UDP query there is no return port or query id: |
| 27 | + // the reply comes back on this very connection and the TCP shortinfo responder replies with |
| 28 | + // the bare shortinfo message (no query-id prefix). |
| 29 | + query_msg_ = "LSL:shortinfo\r\n" + query + "\r\n"; |
| 30 | + // register ourselves as a candidate for cancellation |
| 31 | + register_at(&resolver); |
| 32 | +} |
| 33 | + |
| 34 | +resolve_attempt_tcp::~resolve_attempt_tcp() { unregister_from_all(); } |
| 35 | + |
| 36 | +void resolve_attempt_tcp::begin() { |
| 37 | + // arm the cancel timer |
| 38 | + if (cancel_after_ != FOREVER) { |
| 39 | + cancel_timer_.expires_after(timeout_sec(cancel_after_)); |
| 40 | + cancel_timer_.async_wait([shared_this = shared_from_this(), this](err_t err) { |
| 41 | + if (!err) do_cancel(); |
| 42 | + }); |
| 43 | + } |
| 44 | + // launch the worker pool |
| 45 | + active_workers_ = std::min(MAX_CONCURRENT_PROBES, targets_.size()); |
| 46 | + worker_sockets_.resize(active_workers_); |
| 47 | + std::size_t workers = active_workers_; |
| 48 | + for (std::size_t w = 0; w < workers; w++) probe_next(w); |
| 49 | +} |
| 50 | + |
| 51 | +void resolve_attempt_tcp::cancel() { |
| 52 | + post(io_, [shared_this = shared_from_this()]() { shared_this->do_cancel(); }); |
| 53 | +} |
| 54 | + |
| 55 | +void resolve_attempt_tcp::probe_next(std::size_t worker) { |
| 56 | + if (cancelled_) return; |
| 57 | + if (next_target_ >= targets_.size()) { |
| 58 | + // this worker is out of endpoints; once all workers are done, drop the cancel timer so |
| 59 | + // the attempt can finish (and the resolver can schedule a fresh burst) |
| 60 | + if (--active_workers_ == 0) cancel_timer_.cancel(); |
| 61 | + return; |
| 62 | + } |
| 63 | + const tcp::endpoint ep = targets_[next_target_++]; |
| 64 | + auto sock = std::make_shared<tcp_socket>(io_); |
| 65 | + worker_sockets_[worker] = sock; |
| 66 | + |
| 67 | + auto self = shared_from_this(); |
| 68 | + sock->async_connect(ep, [self, this, worker, ep, sock](err_t err) { |
| 69 | + if (cancelled_) return; |
| 70 | + if (err) { |
| 71 | + // closed / filtered / unreachable port: move on to the next target |
| 72 | + probe_next(worker); |
| 73 | + return; |
| 74 | + } |
| 75 | + // connected: send the shortinfo query |
| 76 | + asio::async_write(*sock, asio::buffer(query_msg_), |
| 77 | + [self, this, worker, ep, sock](err_t err, std::size_t /*unused*/) { |
| 78 | + if (cancelled_) return; |
| 79 | + if (err) { |
| 80 | + probe_next(worker); |
| 81 | + return; |
| 82 | + } |
| 83 | + // read the reply until the outlet closes the connection (EOF) |
| 84 | + auto reply = std::make_shared<std::string>(); |
| 85 | + asio::async_read(*sock, asio::dynamic_buffer(*reply), |
| 86 | + [self, this, worker, ep, sock, reply](err_t err, std::size_t /*unused*/) { |
| 87 | + if (cancelled_) return; |
| 88 | + if (!err || err == asio::error::eof) handle_reply(*reply, ep); |
| 89 | + probe_next(worker); |
| 90 | + }); |
| 91 | + }); |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | +void resolve_attempt_tcp::handle_reply(const std::string &reply, const tcp::endpoint &ep) { |
| 96 | + if (reply.empty()) return; // no match: the responder closed without replying |
| 97 | + try { |
| 98 | + stream_info_impl info; |
| 99 | + info.from_shortinfo_message(reply); |
| 100 | + // The shortinfo XML carries the ports but not the host address; fill it in from the |
| 101 | + // endpoint we connected to (mirrors the UDP resolver using the reply's source address). |
| 102 | + const std::string addr = ep.address().to_string(); |
| 103 | + std::string uid = info.uid(); |
| 104 | + { |
| 105 | + std::lock_guard<std::mutex> lock(resolver_.results_mut_); |
| 106 | + auto it = resolver_.results_.find(uid); |
| 107 | + if (it == resolver_.results_.end()) |
| 108 | + it = resolver_.results_.emplace(uid, std::make_pair(info, lsl_clock())).first; |
| 109 | + else |
| 110 | + it->second.second = lsl_clock(); |
| 111 | + auto &stored_info = it->second.first; |
| 112 | + if (ep.address().is_v4()) { |
| 113 | + if (stored_info.v4address().empty()) stored_info.v4address(addr); |
| 114 | + } else { |
| 115 | + if (stored_info.v6address().empty()) stored_info.v6address(addr); |
| 116 | + } |
| 117 | + } |
| 118 | + // prepone the next cancellation check so a hit cancels the remaining (slow) probes |
| 119 | + if (resolver_.check_cancellation_criteria()) resolver_.cancel_ongoing_resolve(); |
| 120 | + } catch (std::exception &e) { |
| 121 | + LOG_F(WARNING, "resolve_attempt_tcp: could not parse a reply from %s: %s", |
| 122 | + ep.address().to_string().c_str(), e.what()); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +void resolve_attempt_tcp::do_cancel() { |
| 127 | + try { |
| 128 | + cancelled_ = true; |
| 129 | + for (auto &sock : worker_sockets_) |
| 130 | + if (sock && sock->is_open()) sock->close(); |
| 131 | + cancel_timer_.cancel(); |
| 132 | + } catch (std::exception &e) { |
| 133 | + LOG_F(WARNING, "Unexpected error while trying to cancel a resolve_attempt_tcp: %s", |
| 134 | + e.what()); |
| 135 | + } |
| 136 | +} |
0 commit comments