move out alert_manager to its own compilation unit. remove TORRENT_DISABLE_EXTENSIONS from affecting the public API

This commit is contained in:
Arvid Norberg 2013-04-09 02:38:11 +00:00
parent 8e772e8baf
commit 520b8bfcd1
15 changed files with 330 additions and 248 deletions

View File

@ -4,6 +4,7 @@ project(libtorrent)
set(sources
web_connection_base
alert
alert_manager
allocator
asio
asio_ssl

View File

@ -331,6 +331,9 @@ feature.compose <windows-version>xp : <define>_WIN32_WINNT=0x0501 ;
feature.compose <windows-version>vista : <define>_WIN32_WINNT=0x0600 ;
feature.compose <windows-version>win7 : <define>_WIN32_WINNT=0x0601 ;
feature extensions : on off : composite propagated link-incompatible ;
feature.compose <extensions>off : <define>TORRENT_DISABLE_EXTENSION ;
feature asio-debugging : off on : composite propagated link-incompatible ;
feature.compose <asio-debugging>on : <define>TORRENT_ASIO_DEBUGGING ;
@ -463,6 +466,7 @@ lib iphlpapi : : <name>iphlpapi <link>shared ;
SOURCES =
alert
alert_manager
allocator
asio
assert

View File

@ -8,6 +8,7 @@ nobase_include_HEADERS = \
address.hpp \
add_torrent_params.hpp \
alert.hpp \
alert_manager.hpp \
alert_dispatcher.hpp \
alert_types.hpp \
alloca.hpp \

View File

@ -1,6 +1,6 @@
/*
Copyright (c) 2003-2012, Arvid Norberg, Daniel Wallin
Copyright (c) 2003-2013, Arvid Norberg, Daniel Wallin
All rights reserved.
Redistribution and use in source and binary forms, with or without
@ -41,28 +41,18 @@ POSSIBILITY OF SUCH DAMAGE.
#pragma warning(push, 1)
#endif
#include <boost/function/function1.hpp>
#include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
#include <boost/preprocessor/repetition/enum.hpp>
#include <boost/preprocessor/repetition/enum_params.hpp>
#include <boost/preprocessor/repetition/enum_shifted_params.hpp>
#include <boost/preprocessor/repetition/enum_shifted_binary_params.hpp>
#ifndef TORRENT_DISABLE_EXTENSIONS
#include <boost/shared_ptr.hpp>
#include <list>
#endif
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#include "libtorrent/ptime.hpp"
#include "libtorrent/config.hpp"
#include "libtorrent/assert.hpp"
#include "libtorrent/thread.hpp"
#include "libtorrent/io_service_fwd.hpp"
#ifndef TORRENT_MAX_ALERT_TYPES
#define TORRENT_MAX_ALERT_TYPES 15
@ -70,10 +60,6 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent {
#ifndef TORRENT_DISABLE_EXTENSIONS
struct plugin;
#endif
class TORRENT_EXPORT alert
{
public:
@ -129,68 +115,6 @@ namespace libtorrent {
ptime m_timestamp;
};
class TORRENT_EXTRA_EXPORT alert_manager
{
public:
alert_manager(io_service& ios, int queue_limit
, boost::uint32_t alert_mask = alert::error_notification);
~alert_manager();
void post_alert(const alert& alert_);
void post_alert_ptr(alert* alert_);
bool pending() const;
std::auto_ptr<alert> get();
void get_all(std::deque<alert*>* alerts);
template <class T>
bool should_post() const
{
mutex::scoped_lock lock(m_mutex);
if (m_alerts.size() >= m_queue_size_limit) return false;
return (m_alert_mask & T::static_category) != 0;
}
bool should_post(alert const* a) const
{
return m_alert_mask & a->category();
}
alert const* wait_for_alert(time_duration max_wait);
void set_alert_mask(boost::uint32_t m)
{
mutex::scoped_lock lock(m_mutex);
m_alert_mask = m;
}
int alert_mask() const { return m_alert_mask; }
size_t alert_queue_size_limit() const { return m_queue_size_limit; }
size_t set_alert_queue_size_limit(size_t queue_size_limit_);
void set_dispatch_function(boost::function<void(std::auto_ptr<alert>)> const&);
#ifndef TORRENT_DISABLE_EXTENSIONS
void add_extension(boost::shared_ptr<plugin> ext);
#endif
private:
void post_impl(std::auto_ptr<alert>& alert_, mutex::scoped_lock& l);
std::deque<alert*> m_alerts;
mutable mutex m_mutex;
condition_variable m_condition;
boost::uint32_t m_alert_mask;
size_t m_queue_size_limit;
boost::function<void(std::auto_ptr<alert>)> m_dispatch;
io_service& m_ios;
#ifndef TORRENT_DISABLE_EXTENSIONS
typedef std::list<boost::shared_ptr<plugin> > ses_extension_list_t;
ses_extension_list_t m_ses_extensions;
#endif
};
struct TORRENT_EXPORT unhandled_alert : std::exception
{
unhandled_alert() {}

View File

@ -0,0 +1,116 @@
/*
Copyright (c) 2003-2013, Arvid Norberg, Daniel Wallin
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_ALERT_MANAGER_HPP_INCLUDED
#define TORRENT_ALERT_MANAGER_HPP_INCLUDED
#include "libtorrent/config.hpp"
#include "libtorrent/alert.hpp"
#include "libtorrent/io_service_fwd.hpp"
#include "libtorrent/thread.hpp"
#include <boost/function/function1.hpp>
#include <boost/shared_ptr.hpp>
#include <list>
namespace libtorrent {
#ifndef TORRENT_DISABLE_EXTENSIONS
struct plugin;
#endif
class TORRENT_EXTRA_EXPORT alert_manager
{
public:
alert_manager(io_service& ios, int queue_limit
, boost::uint32_t alert_mask = alert::error_notification);
~alert_manager();
void post_alert(const alert& alert_);
void post_alert_ptr(alert* alert_);
bool pending() const;
std::auto_ptr<alert> get();
void get_all(std::deque<alert*>* alerts);
template <class T>
bool should_post() const
{
mutex::scoped_lock lock(m_mutex);
if (m_alerts.size() >= m_queue_size_limit) return false;
return (m_alert_mask & T::static_category) != 0;
}
bool should_post(alert const* a) const
{
return m_alert_mask & a->category();
}
alert const* wait_for_alert(time_duration max_wait);
void set_alert_mask(boost::uint32_t m)
{
mutex::scoped_lock lock(m_mutex);
m_alert_mask = m;
}
int alert_mask() const { return m_alert_mask; }
size_t alert_queue_size_limit() const { return m_queue_size_limit; }
size_t set_alert_queue_size_limit(size_t queue_size_limit_);
void set_dispatch_function(boost::function<void(std::auto_ptr<alert>)> const&);
#ifndef TORRENT_DISABLE_EXTENSIONS
void add_extension(boost::shared_ptr<plugin> ext);
#endif
private:
void post_impl(std::auto_ptr<alert>& alert_, mutex::scoped_lock& l);
std::deque<alert*> m_alerts;
mutable mutex m_mutex;
condition_variable m_condition;
boost::uint32_t m_alert_mask;
size_t m_queue_size_limit;
boost::function<void(std::auto_ptr<alert>)> m_dispatch;
io_service& m_ios;
#ifndef TORRENT_DISABLE_EXTENSIONS
typedef std::list<boost::shared_ptr<plugin> > ses_extension_list_t;
ses_extension_list_t m_ses_extensions;
#endif
};
}
#endif

View File

@ -80,7 +80,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/assert.hpp"
#include "libtorrent/thread.hpp"
#include "libtorrent/policy.hpp" // for policy::peer
#include "libtorrent/alert.hpp" // for alert_manager
#include "libtorrent/alert_manager.hpp" // for alert_manager
#include "libtorrent/deadline_timer.hpp"
#include "libtorrent/socket_io.hpp" // for print_address
#include "libtorrent/address.hpp"

View File

@ -81,18 +81,11 @@ POSSIBILITY OF SUCH DAMAGE.
#define TORRENT_CFG_STATS fullstats_
#endif
#ifdef TORRENT_DISABLE_EXTENSIONS
#define TORRENT_CFG_EXT noext_
#else
#define TORRENT_CFG_EXT ext_
#endif
#define TORRENT_CFG \
BOOST_PP_CAT(TORRENT_CFG_DEBUG, \
BOOST_PP_CAT(TORRENT_CFG_TIME, \
BOOST_PP_CAT(TORRENT_CFG_LOG, \
BOOST_PP_CAT(TORRENT_CFG_DEPR, \
TORRENT_CFG_EXT))))
TORRENT_CFG_DEPR)))
#define TORRENT_CFG_STRING BOOST_PP_STRINGIZE(TORRENT_CFG)

View File

@ -79,6 +79,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/socket_type_fwd.hpp"
#include "libtorrent/error_code.hpp"
#include "libtorrent/sliding_average.hpp"
#include "libtorrent/io_service_fwd.hpp"
#ifdef TORRENT_STATS
#include "libtorrent/aux_/session_impl.hpp"

View File

@ -284,10 +284,8 @@ namespace libtorrent
pe_settings get_pe_settings() const;
#endif
#ifndef TORRENT_DISABLE_EXTENSIONS
void add_extension(boost::function<boost::shared_ptr<torrent_plugin>(torrent*, void*)> ext);
void add_extension(boost::shared_ptr<plugin> ext);
#endif
#ifndef TORRENT_DISABLE_GEO_IP
int as_for_ip(address const& addr);

View File

@ -223,10 +223,8 @@ namespace libtorrent
void remove_http_seed(std::string const& url) const;
std::set<std::string> http_seeds() const;
#ifndef TORRENT_DISABLE_EXTENSIONS
void add_extension(boost::function<boost::shared_ptr<torrent_plugin>(torrent*, void*)> const& ext
, void* userdata = 0);
#endif
bool set_metadata(char const* metadata, int size) const;

View File

@ -23,6 +23,7 @@ endif
libtorrent_rasterbar_la_SOURCES = \
web_connection_base.cpp \
alert.cpp \
alert_manager.cpp \
allocator.cpp \
asio.cpp \
assert.cpp \

View File

@ -340,160 +340,6 @@ namespace libtorrent {
return msg;
}
alert_manager::alert_manager(io_service& ios, int queue_limit, boost::uint32_t alert_mask)
: m_alert_mask(alert_mask)
, m_queue_size_limit(queue_limit)
, m_ios(ios)
{}
alert_manager::~alert_manager()
{
while (!m_alerts.empty())
{
TORRENT_ASSERT(alert_cast<save_resume_data_alert>(m_alerts.front()) == 0
&& "shutting down session with remaining resume data alerts in the alert queue. "
"You proabably wany to make sure you always wait for all resume data "
"alerts before shutting down");
delete m_alerts.front();
m_alerts.pop_front();
}
}
alert const* alert_manager::wait_for_alert(time_duration max_wait)
{
mutex::scoped_lock lock(m_mutex);
if (!m_alerts.empty()) return m_alerts.front();
// this call can be interrupted prematurely by other signals
m_condition.wait_for(lock, max_wait);
if (!m_alerts.empty()) return m_alerts.front();
return NULL;
}
void alert_manager::set_dispatch_function(boost::function<void(std::auto_ptr<alert>)> const& fun)
{
mutex::scoped_lock lock(m_mutex);
m_dispatch = fun;
std::deque<alert*> alerts;
m_alerts.swap(alerts);
lock.unlock();
while (!alerts.empty())
{
TORRENT_TRY {
m_dispatch(std::auto_ptr<alert>(alerts.front()));
} TORRENT_CATCH(std::exception&) {}
alerts.pop_front();
}
}
void dispatch_alert(boost::function<void(alert const&)> dispatcher
, alert* alert_)
{
std::auto_ptr<alert> holder(alert_);
dispatcher(*alert_);
}
void alert_manager::post_alert_ptr(alert* alert_)
{
std::auto_ptr<alert> a(alert_);
#ifndef TORRENT_DISABLE_EXTENSIONS
for (ses_extension_list_t::iterator i = m_ses_extensions.begin()
, end(m_ses_extensions.end()); i != end; ++i)
{
TORRENT_TRY {
(*i)->on_alert(alert_);
} TORRENT_CATCH(std::exception&) {}
}
#endif
mutex::scoped_lock lock(m_mutex);
post_impl(a, lock);
}
void alert_manager::post_alert(const alert& alert_)
{
std::auto_ptr<alert> a(alert_.clone());
#ifndef TORRENT_DISABLE_EXTENSIONS
for (ses_extension_list_t::iterator i = m_ses_extensions.begin()
, end(m_ses_extensions.end()); i != end; ++i)
{
TORRENT_TRY {
(*i)->on_alert(&alert_);
} TORRENT_CATCH(std::exception&) {}
}
#endif
mutex::scoped_lock lock(m_mutex);
post_impl(a, lock);
}
void alert_manager::post_impl(std::auto_ptr<alert>& alert_, mutex::scoped_lock& l)
{
if (m_dispatch)
{
TORRENT_ASSERT(m_alerts.empty());
TORRENT_TRY {
m_dispatch(alert_);
} TORRENT_CATCH(std::exception&) {}
}
else if (m_alerts.size() < m_queue_size_limit || !alert_->discardable())
{
m_alerts.push_back(alert_.release());
if (m_alerts.size() == 1)
m_condition.notify_all();
}
}
#ifndef TORRENT_DISABLE_EXTENSIONS
void alert_manager::add_extension(boost::shared_ptr<plugin> ext)
{
m_ses_extensions.push_back(ext);
}
#endif
std::auto_ptr<alert> alert_manager::get()
{
mutex::scoped_lock lock(m_mutex);
if (m_alerts.empty())
return std::auto_ptr<alert>(0);
alert* result = m_alerts.front();
m_alerts.pop_front();
return std::auto_ptr<alert>(result);
}
void alert_manager::get_all(std::deque<alert*>* alerts)
{
mutex::scoped_lock lock(m_mutex);
if (m_alerts.empty()) return;
m_alerts.swap(*alerts);
}
bool alert_manager::pending() const
{
mutex::scoped_lock lock(m_mutex);
return !m_alerts.empty();
}
size_t alert_manager::set_alert_queue_size_limit(size_t queue_size_limit_)
{
mutex::scoped_lock lock(m_mutex);
std::swap(m_queue_size_limit, queue_size_limit_);
return queue_size_limit_;
}
stats_alert::stats_alert(torrent_handle const& h, int in
, stat const& s)
: torrent_alert(h)

197
src/alert_manager.cpp Normal file
View File

@ -0,0 +1,197 @@
/*
Copyright (c) 2003-2013, Arvid Norberg, Daniel Wallin
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.
*/
#include "libtorrent/config.hpp"
#include "libtorrent/alert_manager.hpp"
#include "libtorrent/alert_types.hpp"
#ifndef TORRENT_DISABLE_EXTENSIONS
#include "libtorrent/extensions.hpp"
#endif
namespace libtorrent
{
alert_manager::alert_manager(io_service& ios, int queue_limit, boost::uint32_t alert_mask)
: m_alert_mask(alert_mask)
, m_queue_size_limit(queue_limit)
, m_ios(ios)
{}
alert_manager::~alert_manager()
{
while (!m_alerts.empty())
{
TORRENT_ASSERT(alert_cast<save_resume_data_alert>(m_alerts.front()) == 0
&& "shutting down session with remaining resume data alerts in the alert queue. "
"You proabably wany to make sure you always wait for all resume data "
"alerts before shutting down");
delete m_alerts.front();
m_alerts.pop_front();
}
}
alert const* alert_manager::wait_for_alert(time_duration max_wait)
{
mutex::scoped_lock lock(m_mutex);
if (!m_alerts.empty()) return m_alerts.front();
// this call can be interrupted prematurely by other signals
m_condition.wait_for(lock, max_wait);
if (!m_alerts.empty()) return m_alerts.front();
return NULL;
}
void alert_manager::set_dispatch_function(boost::function<void(std::auto_ptr<alert>)> const& fun)
{
mutex::scoped_lock lock(m_mutex);
m_dispatch = fun;
std::deque<alert*> alerts;
m_alerts.swap(alerts);
lock.unlock();
while (!alerts.empty())
{
TORRENT_TRY {
m_dispatch(std::auto_ptr<alert>(alerts.front()));
} TORRENT_CATCH(std::exception&) {}
alerts.pop_front();
}
}
void dispatch_alert(boost::function<void(alert const&)> dispatcher
, alert* alert_)
{
std::auto_ptr<alert> holder(alert_);
dispatcher(*alert_);
}
void alert_manager::post_alert_ptr(alert* alert_)
{
std::auto_ptr<alert> a(alert_);
#ifndef TORRENT_DISABLE_EXTENSIONS
for (ses_extension_list_t::iterator i = m_ses_extensions.begin()
, end(m_ses_extensions.end()); i != end; ++i)
{
TORRENT_TRY {
(*i)->on_alert(alert_);
} TORRENT_CATCH(std::exception&) {}
}
#endif
mutex::scoped_lock lock(m_mutex);
post_impl(a, lock);
}
void alert_manager::post_alert(const alert& alert_)
{
std::auto_ptr<alert> a(alert_.clone());
#ifndef TORRENT_DISABLE_EXTENSIONS
for (ses_extension_list_t::iterator i = m_ses_extensions.begin()
, end(m_ses_extensions.end()); i != end; ++i)
{
TORRENT_TRY {
(*i)->on_alert(&alert_);
} TORRENT_CATCH(std::exception&) {}
}
#endif
mutex::scoped_lock lock(m_mutex);
post_impl(a, lock);
}
void alert_manager::post_impl(std::auto_ptr<alert>& alert_, mutex::scoped_lock& l)
{
if (m_dispatch)
{
TORRENT_ASSERT(m_alerts.empty());
TORRENT_TRY {
m_dispatch(alert_);
} TORRENT_CATCH(std::exception&) {}
}
else if (m_alerts.size() < m_queue_size_limit || !alert_->discardable())
{
m_alerts.push_back(alert_.release());
if (m_alerts.size() == 1)
m_condition.notify_all();
}
}
#ifndef TORRENT_DISABLE_EXTENSIONS
void alert_manager::add_extension(boost::shared_ptr<plugin> ext)
{
m_ses_extensions.push_back(ext);
}
#endif
std::auto_ptr<alert> alert_manager::get()
{
mutex::scoped_lock lock(m_mutex);
if (m_alerts.empty())
return std::auto_ptr<alert>(0);
alert* result = m_alerts.front();
m_alerts.pop_front();
return std::auto_ptr<alert>(result);
}
void alert_manager::get_all(std::deque<alert*>* alerts)
{
mutex::scoped_lock lock(m_mutex);
if (m_alerts.empty()) return;
m_alerts.swap(*alerts);
}
bool alert_manager::pending() const
{
mutex::scoped_lock lock(m_mutex);
return !m_alerts.empty();
}
size_t alert_manager::set_alert_queue_size_limit(size_t queue_size_limit_)
{
mutex::scoped_lock lock(m_mutex);
std::swap(m_queue_size_limit, queue_size_limit_);
return queue_size_limit_;
}
}

View File

@ -489,17 +489,19 @@ namespace libtorrent
TORRENT_SYNC_CALL1(get_feeds, &f);
}
#ifndef TORRENT_DISABLE_EXTENSIONS
void session::add_extension(boost::function<boost::shared_ptr<torrent_plugin>(torrent*, void*)> ext)
{
#ifndef TORRENT_DISABLE_EXTENSIONS
TORRENT_ASYNC_CALL1(add_extension, ext);
#endif
}
void session::add_extension(boost::shared_ptr<plugin> ext)
{
#ifndef TORRENT_DISABLE_EXTENSIONS
TORRENT_ASYNC_CALL1(add_ses_extension, ext);
}
#endif
}
#ifndef TORRENT_DISABLE_GEO_IP
void session::load_asnum_db(char const* file)

View File

@ -369,15 +369,15 @@ namespace libtorrent
TORRENT_ASYNC_CALL2(rename_file, index, new_name);
}
#ifndef TORRENT_DISABLE_EXTENSIONS
void torrent_handle::add_extension(
boost::function<boost::shared_ptr<torrent_plugin>(torrent*, void*)> const& ext
, void* userdata)
{
#ifndef TORRENT_DISABLE_EXTENSIONS
INVARIANT_CHECK;
TORRENT_ASYNC_CALL2(add_extension, ext, userdata);
}
#endif
}
bool torrent_handle::set_metadata(char const* metadata, int size) const
{