Beispiel #1
0
	///按指定大小输出为位图块.
	// @param bitfield以int为单位的位图数组, 每个元素表示1个piece, 为0表示空, 为1表示满.
	// @param piece_size指定的piece大小.
	inline void range_to_bitfield(bitfield& bf, int piece_size)
	{
		// 先整理连续的空间, 将连续的区间合并为一个区间.
		if (m_need_merge)
			merge();

#ifndef AVHTTP_DISABLE_THREAD
		boost::mutex::scoped_lock lock(*m_mutex);
#endif

		// 计算出分片总数.
		int piece_num = (m_size / piece_size) + (m_size % piece_size == 0 ? 0 : 1);

		// 分配位图空间, 默认为0.
		bf.resize(piece_num, 0);

		boost::int64_t l = 0;
		boost::int64_t r = 0;
		range_map::iterator it = m_ranges.begin();
		range_map::iterator end = m_ranges.end();

		// 从0位置开始, 每次检查piece_size大小的空间是否数据完整, 数据
		// 完整则设置到位图为1, 表示有数据.
		for (int i = 0; i < piece_num; i++)
		{
			l = i * piece_size;
			r = (i + 1) * piece_size;
			r = r > m_size ? m_size : r;

			bool is_complete = false;
			if (it != end)
			{
				// 在已经下载的区间中.
				if (l >= it->first && r <= it->second)
				{
					is_complete = true;
				}
				else
				{
					// 除非右边界大于等于当前区间的右边界, 否则一直使用当前区间判断.
					// 这样就避免了每次从头循环遍历.
					if (r >= it->second)
					{
						if (++it != end)
						{
							if (l >= it->first && r <= it->second)
							{
								is_complete = true;
							}
						}
					}
				}
			}

			if (is_complete)
			{
				bf.set_bit(i);
			}
		}
	}
Beispiel #2
0
void print_bitfield(bitfield const& b)
{
	std::string out;
	for (int i = 0; i < b.size(); ++i)
	{
		out += b.get_bit(i) ? "1" : "0";
	}
	std::printf("%s\n", out.c_str());
}
Beispiel #3
0
	///按指定的分片大小bitfield更新rangefield.
	// @param bf为指定的bitfield.
	// @param piece_size是指定的分片大小.
	inline void bitfield_to_range(const bitfield& bf, int piece_size)
	{
#ifndef AVHTTP_DISABLE_THREAD
		boost::mutex::scoped_lock lock(*m_mutex);
#endif
		m_ranges.clear();

		boost::int64_t left = 0;
		boost::int64_t right = 0;
		bool left_record = false;
		int index = 0;

		for (bitfield::const_iterator i = bf.begin(); i != bf.end(); i++, index++)
		{
			BOOST_ASSERT(index * piece_size < m_size);

			if (*i && !left_record)
			{
				left = index * piece_size;
				right = left + piece_size;

				left_record = true;
				continue;
			}

			if (*i && left_record)
			{
				right += piece_size;
			}

			if (left_record && !(*i))
			{
				// 得到区间.
				right = (std::min)(right, m_size);
				m_ranges[left] = right;
				left_record = false;
			}
		}

		if (left_record)
		{
			right = (std::min)(right, m_size);
			m_ranges[left] = right;
			left_record = false;
		}

		m_need_merge = true;
	}
Beispiel #4
0
		void peer_lost(bitfield const& bits, peer_connection const* peer)
		{
			if (has_picker())
			{
				if (bits.all_set() && bits.size() > 0)
					m_picker->dec_refcount_all(peer);
				else
					m_picker->dec_refcount(bits, peer);
			}
#ifdef TORRENT_DEBUG
			else
			{
				TORRENT_ASSERT(is_seed());
			}
#endif
		}
Beispiel #5
0
void QTorrentHandle::downloading_pieces(bitfield &bf) const {
  std::vector<partial_piece_info> queue;
  torrent_handle::get_download_queue(queue);
  for (std::vector<partial_piece_info>::const_iterator it=queue.begin(); it!= queue.end(); it++) {
    bf.set_bit(it->piece_index);
  }
  return;
}
void print_bitfield(bitfield const& b)
{
	std::string out;
	out.reserve(b.size());
	for (bool bit : b)
		out += bit ? '1' : '0';
	std::printf("%s\n", out.c_str());
}
Beispiel #7
0
		void peer_lost(bitfield const& bits)
		{
			if (has_picker())
			{
				if (bits.all_set())
					m_picker->dec_refcount_all();
				else
					m_picker->dec_refcount(bits);
			}
#ifdef TORRENT_DEBUG
			else
			{
				TORRENT_ASSERT(is_seed());
			}
#endif
		}
void test_iterators(bitfield& test1)
{
	test1.set_all();
	int num = 0;

	std::printf("expecting %d ones\n", test1.size());
	for (bitfield::const_iterator i = test1.begin(); i != test1.end(); ++i)
	{
		std::printf("%d", *i);
		TEST_EQUAL(*i, true);
		num += *i;
	}
	std::printf("\n");
	TEST_EQUAL(num, test1.size());
	TEST_EQUAL(num, test1.count());
}
Beispiel #9
0
		bitfield(bitfield const& rhs) { assign(rhs.data(), rhs.size()); }