broadcast socket handler take a span

This commit is contained in:
arvidn 2018-01-14 20:54:01 +01:00 committed by Arvid Norberg
parent e829c1942a
commit a01274ce2c
7 changed files with 14 additions and 18 deletions

View File

@ -39,6 +39,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/address.hpp" #include "libtorrent/address.hpp"
#include "libtorrent/error_code.hpp" #include "libtorrent/error_code.hpp"
#include "libtorrent/string_view.hpp" #include "libtorrent/string_view.hpp"
#include "libtorrent/span.hpp"
#include <memory> #include <memory>
#include <list> #include <list>
@ -57,7 +58,7 @@ namespace libtorrent {
address ensure_v6(address const& a); address ensure_v6(address const& a);
typedef std::function<void(udp::endpoint const& from typedef std::function<void(udp::endpoint const& from
, char const* buffer, int size)> receive_handler_t; , span<char const> buffer)> receive_handler_t;
class TORRENT_EXTRA_EXPORT broadcast_socket class TORRENT_EXTRA_EXPORT broadcast_socket
{ {

View File

@ -60,8 +60,7 @@ private:
, bool broadcast, int retry_count); , bool broadcast, int retry_count);
void resend_announce(error_code const& e, sha1_hash const& ih void resend_announce(error_code const& e, sha1_hash const& ih
, int listen_port, int retry_count); , int listen_port, int retry_count);
void on_announce(udp::endpoint const& from, char const* buffer void on_announce(udp::endpoint const& from, span<char const> buffer);
, std::size_t bytes_transferred);
aux::lsd_callback& m_callback; aux::lsd_callback& m_callback;

View File

@ -203,8 +203,7 @@ private:
void discover_device_impl(); void discover_device_impl();
void resend_request(error_code const& e); void resend_request(error_code const& e);
void on_reply(udp::endpoint const& from, char const* buffer void on_reply(udp::endpoint const& from, span<char const> buffer);
, std::size_t bytes_transferred);
struct rootdevice; struct rootdevice;
void next(rootdevice& d, port_mapping_t i); void next(rootdevice& d, port_mapping_t i);

View File

@ -318,7 +318,7 @@ namespace libtorrent {
maybe_abort(); maybe_abort();
return; return;
} }
m_on_receive(s->remote, s->buffer, int(bytes_transferred)); m_on_receive(s->remote, {s->buffer, bytes_transferred});
if (maybe_abort()) return; if (maybe_abort()) return;
if (!s->socket) return; if (!s->socket) return;

View File

@ -103,12 +103,12 @@ void lsd::debug_log(char const* fmt, ...) const
void lsd::start(error_code& ec) void lsd::start(error_code& ec)
{ {
m_socket.open(std::bind(&lsd::on_announce, self(), _1, _2, _3) m_socket.open(std::bind(&lsd::on_announce, self(), _1, _2)
, m_broadcast_timer.get_io_service(), ec); , m_broadcast_timer.get_io_service(), ec);
if (ec) return; if (ec) return;
#if TORRENT_USE_IPV6 #if TORRENT_USE_IPV6
m_socket6.open(std::bind(&lsd::on_announce, self(), _1, _2, _3) m_socket6.open(std::bind(&lsd::on_announce, self(), _1, _2)
, m_broadcast_timer.get_io_service(), ec); , m_broadcast_timer.get_io_service(), ec);
#endif #endif
} }
@ -198,13 +198,12 @@ void lsd::resend_announce(error_code const& e, sha1_hash const& info_hash
announce_impl(info_hash, listen_port, false, retry_count); announce_impl(info_hash, listen_port, false, retry_count);
} }
void lsd::on_announce(udp::endpoint const& from, char const* buf void lsd::on_announce(udp::endpoint const& from, span<char const> buf)
, std::size_t const bytes_transferred)
{ {
http_parser p; http_parser p;
bool error = false; bool error = false;
p.incoming({buf, bytes_transferred}, error); p.incoming(buf, error);
if (!p.header_finished() || error) if (!p.header_finished() || error)
{ {

View File

@ -120,7 +120,7 @@ void upnp::start()
TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(is_single_thread());
error_code ec; error_code ec;
m_socket.open(std::bind(&upnp::on_reply, self(), _1, _2, _3) m_socket.open(std::bind(&upnp::on_reply, self(), _1, _2)
, m_refresh_timer.get_io_service(), ec); , m_refresh_timer.get_io_service(), ec);
m_mappings.reserve(10); m_mappings.reserve(10);
@ -370,8 +370,7 @@ void upnp::resend_request(error_code const& ec)
} }
} }
void upnp::on_reply(udp::endpoint const& from, char const* buffer void upnp::on_reply(udp::endpoint const& from, span<char const> buffer)
, std::size_t const bytes_transferred)
{ {
TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(is_single_thread());
std::shared_ptr<upnp> me(self()); std::shared_ptr<upnp> me(self());
@ -484,7 +483,7 @@ void upnp::on_reply(udp::endpoint const& from, char const* buffer
http_parser p; http_parser p;
bool error = false; bool error = false;
p.incoming({buffer, bytes_transferred}, error); p.incoming(buffer, error);
if (error) if (error)
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING

View File

@ -69,12 +69,11 @@ char const* soap_delete_response[] = {
"<s:Body><u:DeletePortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:2\">" "<s:Body><u:DeletePortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:2\">"
"</u:DeletePortMapping></s:Body></s:Envelope>"}; "</u:DeletePortMapping></s:Body></s:Envelope>"};
void incoming_msearch(udp::endpoint const& from, char const* buffer void incoming_msearch(udp::endpoint const& from, span<char const> buffer)
, int size)
{ {
http_parser p; http_parser p;
bool error = false; bool error = false;
p.incoming(span<char const>(buffer, size), error); p.incoming(buffer, error);
if (error || !p.header_finished()) if (error || !p.header_finished())
{ {
std::cout << "*** malformed HTTP from " std::cout << "*** malformed HTTP from "