diff --git a/include/libtorrent/Makefile.am b/include/libtorrent/Makefile.am index f6c1aa07d..465ba1d0e 100644 --- a/include/libtorrent/Makefile.am +++ b/include/libtorrent/Makefile.am @@ -187,6 +187,7 @@ nobase_include_HEADERS = \ aux_/session_impl.hpp \ aux_/session_settings.hpp \ aux_/session_udp_sockets.hpp \ + aux_/set_socket_buffer.hpp \ aux_/proxy_settings.hpp \ aux_/session_interface.hpp \ aux_/suggest_piece.hpp \ diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index efc901a15..f8080309a 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -781,14 +781,12 @@ namespace aux { peer_class_pool m_classes; void init(); -// void init_dht(); void submit_disk_jobs(); void on_trigger_auto_manage(); void on_lsd_peer(tcp::endpoint const& peer, sha1_hash const& ih) override; - void setup_socket_buffers(socket_type& s) override; void set_external_address(std::shared_ptr const& sock, address const& ip , ip_source_t const source_type, address const& source); diff --git a/include/libtorrent/aux_/session_interface.hpp b/include/libtorrent/aux_/session_interface.hpp index 247cba12c..d78e4dc45 100644 --- a/include/libtorrent/aux_/session_interface.hpp +++ b/include/libtorrent/aux_/session_interface.hpp @@ -291,7 +291,6 @@ namespace libtorrent { namespace aux { virtual void announce_lsd(sha1_hash const& ih, int port, bool broadcast = false) = 0; virtual libtorrent::utp_socket_manager* utp_socket_manager() = 0; virtual void inc_boost_connections() = 0; - virtual void setup_socket_buffers(socket_type& s) = 0; virtual std::vector& block_info_storage() = 0; #ifdef TORRENT_USE_OPENSSL diff --git a/include/libtorrent/aux_/set_socket_buffer.hpp b/include/libtorrent/aux_/set_socket_buffer.hpp new file mode 100644 index 000000000..e5cc71a07 --- /dev/null +++ b/include/libtorrent/aux_/set_socket_buffer.hpp @@ -0,0 +1,83 @@ +/* + +Copyright (c) 2018, Arvid Norberg, Magnus Jonsson +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the distribution. + * Neither the name of the author nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +*/ + +#ifndef TORRENT_SET_SOCKET_BUFFER_HPP +#define TORRENT_SET_SOCKET_BUFFER_HPP + +#include "libtorrent/aux_/session_settings.hpp" +#include "libtorrent/error_code.hpp" + +namespace libtorrent { +namespace aux { + + template + void set_socket_buffer_size(Socket& s, session_settings const& sett, error_code& ec) + { + int const snd_size = sett.get_int(settings_pack::send_socket_buffer_size); + if (snd_size) + { + typename Socket::send_buffer_size prev_option; + s.get_option(prev_option, ec); + if (!ec && prev_option.value() != snd_size) + { + typename Socket::send_buffer_size option(snd_size); + s.set_option(option, ec); + if (ec) + { + // restore previous value + s.set_option(prev_option, ec); + return; + } + } + } + int const recv_size = sett.get_int(settings_pack::recv_socket_buffer_size); + if (recv_size) + { + typename Socket::receive_buffer_size prev_option; + s.get_option(prev_option, ec); + if (!ec && prev_option.value() != recv_size) + { + typename Socket::receive_buffer_size option(recv_size); + s.set_option(option, ec); + if (ec) + { + // restore previous value + s.set_option(prev_option, ec); + return; + } + } + } + } + +}} + +#endif diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 017272e13..9cb86f74a 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -88,6 +88,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/bind_to_device.hpp" #include "libtorrent/hex.hpp" // to_hex, from_hex #include "libtorrent/aux_/scope_end.hpp" +#include "libtorrent/aux_/set_socket_buffer.hpp" #ifndef TORRENT_DISABLE_LOGGING @@ -1007,50 +1008,6 @@ namespace aux { return m_port_filter; } -namespace { - - - template - void set_socket_buffer_size(Socket& s, session_settings const& sett, error_code& ec) - { - int const snd_size = sett.get_int(settings_pack::send_socket_buffer_size); - if (snd_size) - { - typename Socket::send_buffer_size prev_option; - s.get_option(prev_option, ec); - if (!ec && prev_option.value() != snd_size) - { - typename Socket::send_buffer_size option(snd_size); - s.set_option(option, ec); - if (ec) - { - // restore previous value - s.set_option(prev_option, ec); - return; - } - } - } - int const recv_size = sett.get_int(settings_pack::recv_socket_buffer_size); - if (recv_size) - { - typename Socket::receive_buffer_size prev_option; - s.get_option(prev_option, ec); - if (!ec && prev_option.value() != recv_size) - { - typename Socket::receive_buffer_size option(recv_size); - s.set_option(option, ec); - if (ec) - { - // restore previous value - s.set_option(prev_option, ec); - return; - } - } - } - } - - } // anonymous namespace - peer_class_t session_impl::create_peer_class(char const* name) { TORRENT_ASSERT(is_single_thread()); @@ -3031,7 +2988,19 @@ namespace { if (m_alerts.should_post()) m_alerts.emplace_alert(s->type(), endp); - setup_socket_buffers(*s); + { + error_code err; + set_socket_buffer_size(*s, m_settings, err); +#ifndef TORRENT_DISABLE_LOGGING + if (err && should_log()) + { + error_code ignore; + session_log("socket buffer size [ %s %d]: (%d) %s" + , s->local_endpoint().address().to_string(ignore).c_str() + , s->local_endpoint().port(), err.value(), err.message().c_str()); + } +#endif + } peer_connection_args pack; pack.ses = this; @@ -3064,12 +3033,6 @@ namespace { } } - void session_impl::setup_socket_buffers(socket_type& s) - { - error_code ec; - set_socket_buffer_size(s, m_settings, ec); - } - void session_impl::close_connection(peer_connection* p) noexcept { TORRENT_ASSERT(is_single_thread()); diff --git a/src/torrent.cpp b/src/torrent.cpp index 81aebabc2..b16f6d26d 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -98,6 +98,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/disk_io_thread.hpp" // for cache_status #include "libtorrent/aux_/numeric_cast.hpp" #include "libtorrent/aux_/path.hpp" +#include "libtorrent/aux_/set_socket_buffer.hpp" #ifndef TORRENT_DISABLE_LOGGING #include "libtorrent/aux_/session_impl.hpp" // for tracker_logger @@ -6577,7 +6578,19 @@ namespace libtorrent { #endif } - m_ses.setup_socket_buffers(*s); + { + error_code err; + aux::set_socket_buffer_size(*s, settings(), err); +#ifndef TORRENT_DISABLE_LOGGING + if (err && should_log()) + { + error_code ignore; + debug_log("socket buffer size [ %s %d]: (%d) %s" + , s->local_endpoint().address().to_string(ignore).c_str() + , s->local_endpoint().port(), ignore.value(), ignore.message().c_str()); + } +#endif + } peer_connection_args pack; pack.ses = &m_ses;