Exemple #1
0
	void torrent_handle::async_call(Fun f, Args&&... a) const
	{
		std::shared_ptr<torrent> t = m_torrent.lock();
		if (!t)
		{
#ifndef BOOST_NO_EXCEPTIONS
			throw_invalid_handle();
#else
			std::terminate();
#endif
		}
		session_impl& ses = static_cast<session_impl&>(t->session());
		ses.get_io_service().dispatch([=,&ses] ()
		{
#ifndef BOOST_NO_EXCEPTIONS
			try {
#endif
				(t.get()->*f)(a...);
#ifndef BOOST_NO_EXCEPTIONS
			} catch (system_error const& e) {
				ses.alerts().emplace_alert<torrent_error_alert>(torrent_handle(m_torrent)
					, e.code(), e.what());
			} catch (std::exception const& e) {
				ses.alerts().emplace_alert<torrent_error_alert>(torrent_handle(m_torrent)
					, error_code(), e.what());
			} catch (...) {
				ses.alerts().emplace_alert<torrent_error_alert>(torrent_handle(m_torrent)
					, error_code(), "unknown error");
			}
#endif
		} );
	}
Exemple #2
0
	void session::remove_torrent(const torrent_handle& h, int options)
	{
		if (!h.is_valid())
#ifdef BOOST_NO_EXCEPTIONS
			return;
#else
			throw_invalid_handle();
#endif
		TORRENT_ASYNC_CALL2(remove_torrent, h, options);
	}
Exemple #3
0
	torrent_info const& torrent_handle::get_torrent_info() const
	{
		INVARIANT_CHECK;
#ifdef BOOST_NO_EXCEPTIONS
		const static torrent_info empty(sha1_hash(0));
#endif
		boost::shared_ptr<torrent> t = m_torrent.lock();
		if (!t)
#ifdef BOOST_NO_EXCEPTIONS
			return empty;
#else
			throw_invalid_handle();
#endif
		session_impl::mutex_t::scoped_lock l(t->session().m_mutex);
		if (!t->valid_metadata())
#ifdef BOOST_NO_EXCEPTIONS
			return empty;
#else
			throw_invalid_handle();
#endif
		return t->torrent_file();
	}
Exemple #4
0
	void torrent_handle::connect_peer(tcp::endpoint const& adr, int source) const
	{
		INVARIANT_CHECK;

		boost::shared_ptr<torrent> t = m_torrent.lock();
		if (!t)
#ifdef BOOST_NO_EXCEPTIONS
			return;
#else
			throw_invalid_handle();
#endif
		session_impl::mutex_t::scoped_lock l(t->session().m_mutex);
		
		peer_id id;
		std::fill(id.begin(), id.end(), 0);
		t->get_policy().add_peer(adr, id, source, 0);
	}
Exemple #5
0
	Ret torrent_handle::sync_call_ret(Ret def, Fun f, Args&&... a) const
	{
		std::shared_ptr<torrent> t = m_torrent.lock();
		Ret r = def;
#ifndef BOOST_NO_EXCEPTIONS
		if (!t) throw_invalid_handle();
#else
		if (!t) return r;
#endif
		session_impl& ses = static_cast<session_impl&>(t->session());

		// this is the flag to indicate the call has completed
		bool done = false;

		std::exception_ptr ex;
		ses.get_io_service().dispatch([=,&r,&done,&ses,&ex] ()
		{
#ifndef BOOST_NO_EXCEPTIONS
			try {
#endif
				r = (t.get()->*f)(a...);
#ifndef BOOST_NO_EXCEPTIONS
			} catch (...) {
				ex = std::current_exception();
			}
#endif
			std::unique_lock<std::mutex> l(ses.mut);
			done = true;
			ses.cond.notify_all();
		} );

		aux::torrent_wait(done, ses);

		if (ex) std::rethrow_exception(ex);
		return r;
	}