From b355541a7de2235a4333e11325e21d26783ba664 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 7 Oct 2008 05:46:42 +0000 Subject: [PATCH] applied patch from Sergei Vorobyov to allow changing the limit on outstanding alerts --- docs/manual.rst | 9 +++++++-- include/libtorrent/alert.hpp | 6 ++++++ include/libtorrent/aux_/session_impl.hpp | 1 + include/libtorrent/session.hpp | 1 + src/alert.cpp | 13 ++++++++++--- src/session.cpp | 5 +++++ src/session_impl.cpp | 6 ++++++ 7 files changed, 36 insertions(+), 5 deletions(-) diff --git a/docs/manual.rst b/docs/manual.rst index 0eae75d8a..811ebd6be 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -151,6 +151,7 @@ The ``session`` class has the following synopsis:: std::auto_ptr pop_alert(); alert const* wait_for_alert(time_duration max_wait); void set_alert_mask(int m); + size_t set_alert_queue_size_limit(size_t queue_size_limit_); void add_extension(boost::function< boost::shared_ptr(torrent*)> ext); @@ -742,14 +743,15 @@ with a DHT ping packet, and connect to those that responds first. On windows one can only connect to a few peers at a time because of a built in limitation (in XP Service pack 2). -pop_alert() set_alert_mask() wait_for_alert() ---------------------------------------------- +pop_alert() set_alert_mask() wait_for_alert() set_alert_queue_size_limit() +-------------------------------------------------------------------------- :: std::auto_ptr pop_alert(); alert const* wait_for_alert(time_duration max_wait); void set_alert_mask(int m); + size_t set_alert_queue_size_limit(size_t queue_size_limit_); ``pop_alert()`` is used to ask the session if any errors or events has occurred. With ``set_alert_mask()`` you can filter which alerts to receive through ``pop_alert()``. @@ -763,6 +765,9 @@ same pointer until the alert is popped by calling ``pop_alert``. This is useful leaving any alert dispatching mechanism independent of this blocking call, the dispatcher can be called and it can pop the alert independently. +``set_alert_queue_size_limit()`` you can specify how many alerts can be awaiting for dispatching. +If this limit is reached, new incoming alerts can not be received until alerts are popped +by calling ``pop_alert``. Default value is 1000. add_extension() --------------- diff --git a/include/libtorrent/alert.hpp b/include/libtorrent/alert.hpp index 4c5759b78..904028a39 100644 --- a/include/libtorrent/alert.hpp +++ b/include/libtorrent/alert.hpp @@ -111,6 +111,8 @@ namespace libtorrent { class TORRENT_EXPORT alert_manager { public: + enum { queue_size_limit_default = 1000 }; + alert_manager(); ~alert_manager(); @@ -125,11 +127,15 @@ namespace libtorrent { void set_alert_mask(int m) { m_alert_mask = m; } + size_t alert_queue_size_limit() const { return m_queue_size_limit; } + size_t set_alert_queue_size_limit(size_t queue_size_limit_); + private: std::queue m_alerts; mutable boost::mutex m_mutex; boost::condition m_condition; int m_alert_mask; + size_t m_queue_size_limit; }; struct TORRENT_EXPORT unhandled_alert : std::exception diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index cc24d6987..aa5d1bae1 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -212,6 +212,7 @@ namespace libtorrent void done_checking(boost::shared_ptr const& t); void set_alert_mask(int m); + size_t set_alert_queue_size_limit(size_t queue_size_limit_); std::auto_ptr pop_alert(); alert const* wait_for_alert(time_duration max_wait); diff --git a/include/libtorrent/session.hpp b/include/libtorrent/session.hpp index f112253ea..1f837e317 100644 --- a/include/libtorrent/session.hpp +++ b/include/libtorrent/session.hpp @@ -331,6 +331,7 @@ namespace libtorrent void set_severity_level(alert::severity_t s) TORRENT_DEPRECATED; #endif void set_alert_mask(int m); + size_t set_alert_queue_size_limit(size_t queue_size_limit_); alert const* wait_for_alert(time_duration max_wait); diff --git a/src/alert.cpp b/src/alert.cpp index 3e65d2b08..14c5c4e5c 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -35,8 +35,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/alert.hpp" #include -enum { queue_size_limit = 1000 }; - namespace libtorrent { alert::alert() : m_timestamp(time_now()) {} @@ -45,6 +43,7 @@ namespace libtorrent { alert_manager::alert_manager() : m_alert_mask(alert::error_notification) + , m_queue_size_limit(queue_size_limit_default) {} alert_manager::~alert_manager() @@ -85,7 +84,7 @@ namespace libtorrent { { boost::mutex::scoped_lock lock(m_mutex); - if (m_alerts.size() >= queue_size_limit) return; + if (m_alerts.size() >= m_queue_size_limit) return; m_alerts.push(alert_.clone().release()); m_condition.notify_all(); } @@ -108,5 +107,13 @@ namespace libtorrent { return !m_alerts.empty(); } + size_t alert_manager::set_alert_queue_size_limit(size_t queue_size_limit_) + { + boost::mutex::scoped_lock lock(m_mutex); + + std::swap(m_queue_size_limit, queue_size_limit_); + return queue_size_limit_; + } + } // namespace libtorrent diff --git a/src/session.cpp b/src/session.cpp index b0db1d9ef..2bd5f2bae 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -553,6 +553,11 @@ namespace libtorrent m_impl->set_alert_mask(m); } + size_t set_alert_queue_size_limit(size_t queue_size_limit_); + { + return m_impl->set_alert_queue_size_limit(queue_size_limit_); + } + #ifndef TORRENT_NO_DEPRECATE void session::set_severity_level(alert::severity_t s) { diff --git a/src/session_impl.cpp b/src/session_impl.cpp index c7409a8fc..844a006e7 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -2312,6 +2312,12 @@ namespace aux { m_alerts.set_alert_mask(m); } + size_t session_impl::set_alert_queue_size_limit(size_t queue_size_limit_) + { + mutex_t::scoped_lock l(m_mutex); + return m_alerts.set_alert_queue_size_limit(queue_size_limit_); + } + int session_impl::upload_rate_limit() const { mutex_t::scoped_lock l(m_mutex);