arvidn / libtorrent

an efficient feature complete C++ bittorrent implementation

Home Page:http://libtorrent.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question about on_alert Method in Libtorrent Extensions: Queue Placement and Alert Handling

joriscarrier opened this issue · comments

Why is the on_alert method in Libtorrent extensions only called if alerts can be placed in the queue? Is it possible to call on_alert directly, then decide whether the alert should be placed in the queue?

something like that (this dirty code is juste for example)

		template <class T, typename... Args>
		void emplace_alert(Args&&... args) try
		{
			std::unique_lock<std::recursive_mutex> lock(m_mutex);


//  ------------------------ here call on_alert -------------------------------------
			T a(m_allocations[m_generation], std::forward<Args>(args)...);
	#ifndef TORRENT_DISABLE_EXTENSIONS
			for (auto& e : m_ses_extensions)
				e->on_alert(&a);
	#else
			TORRENT_UNUSED(a);
	#endif
// ---------------------------------------------------------------------------------

			heterogeneous_queue<alert>& queue = m_alerts[m_generation];

			// don't add more than this number of alerts, unless it's a
			// high priority alert, in which case we try harder to deliver it
			// for high priority alerts, double the upper limit
			if (queue.size() / (1 + static_cast<int>(T::priority)) >= m_queue_size_limit)
			{
				// record that we dropped an alert of this type
				m_dropped.set(T::alert_type);
				return;
			}

			T& alert = queue.emplace_back<T>(
				m_allocations[m_generation], std::forward<Args>(args)...);

			maybe_notify(&alert); 
		}
		catch (std::bad_alloc const&)
		{
			// record that we dropped an alert of this type
			std::unique_lock<std::recursive_mutex> lock(m_mutex);
			m_dropped.set(T::alert_type);
		}