started work on udp-tracker support.

This commit is contained in:
Arvid Norberg 2004-01-20 22:59:21 +00:00
parent 6d22d4144c
commit a9ef075d7d
5 changed files with 47 additions and 34 deletions

View File

@ -44,6 +44,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/array.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/optional.hpp>
#include <boost/cstdint.hpp>
#include "libtorrent/socket.hpp"
#include "libtorrent/peer_id.hpp"
@ -119,6 +120,28 @@ namespace libtorrent
++start;
}
template <class OutIt>
inline void write_int64(boost::int64_t val, OutIt& start)
{
for (int i = 7; i <= 0; --i)
{
*start = static_cast<unsigned char>((val >> (i*8)) & 0xff);
++start;
}
}
template <class InIt>
inline boost::int64_t read_int64(InIt& start)
{
boost::int64_t ret = 0;
for (int i = 7; i <= 0; --i)
{
ret |= static_cast<unsigned char>(*start) << (i*8);
++start;
}
return ret;
}
}
struct protocol_error: std::runtime_error
@ -244,6 +267,9 @@ namespace libtorrent
// mainloop to disconnect peers.
void disconnect();
bool is_disconnecting() const
{ return m_disconnecting; }
// sets the number of bytes this peer
// is allowed to send until it should
// stop sending. When it stops sending
@ -551,6 +577,8 @@ namespace libtorrent
// the time it took for the peer to send the piece
// message
boost::posix_time::time_duration m_last_piece_time;
bool m_disconnecting;
};
// this is called each time this peer generates some

View File

@ -178,7 +178,7 @@ namespace libtorrent
// generates a request string for sending
// to the tracker
std::string generate_tracker_request(int port);
tracker_request generate_tracker_request(int port);
boost::posix_time::ptime next_announce() const
{ return m_next_request; }
@ -209,7 +209,7 @@ namespace libtorrent
// so it wont pick it for download
void announce_piece(int index);
void close_all_connections();
void disconnect_all();
void disconnect_seeds();
piece_picker& picker() { return m_picker; }

View File

@ -104,6 +104,7 @@ namespace libtorrent
, m_num_invalid_requests(0)
, m_last_piece(boost::gregorian::date(std::time(0)))
, m_last_piece_time(boost::posix_time::seconds(0))
, m_disconnecting(false)
{
assert(!m_socket->is_blocking());
assert(m_torrent != 0);
@ -163,6 +164,7 @@ namespace libtorrent
, m_num_invalid_requests(0)
, m_last_piece(boost::gregorian::date(std::time(0)))
, m_last_piece_time(boost::posix_time::seconds(0))
, m_disconnecting(false)
{
assert(!m_socket->is_blocking());
@ -868,7 +870,9 @@ namespace libtorrent
void peer_connection::disconnect()
{
assert(m_disconnecting == false);
detail::session_impl::connection_map::iterator i = m_ses.m_connections.find(m_socket);
m_disconnecting = true;
assert(i != m_ses.m_connections.end());
assert(std::find(m_ses.m_disconnect_peer.begin(), m_ses.m_disconnect_peer.end(), i) == m_ses.m_disconnect_peer.end());
m_ses.m_disconnect_peer.push_back(i);

View File

@ -647,7 +647,7 @@ namespace libtorrent
{
m_tracker_manager.queue_request(
i->second->generate_tracker_request(m_listen_port));
i->second->close_all_connections();
i->second->disconnect_all();
#ifndef NDEBUG
sha1_hash i_hash = i->second->torrent_file().info_hash();
#endif

View File

@ -482,45 +482,26 @@ namespace libtorrent
i->second->announce_piece(index);
}
std::string torrent::generate_tracker_request(int port)
tracker_request torrent::generate_tracker_request(int port)
{
m_duration = 1800;
m_next_request = boost::posix_time::second_clock::local_time() + boost::posix_time::seconds(m_duration);
std::vector<char> buffer;
std::string request = m_torrent_file.trackers()[m_currently_trying_tracker].url;
request += "?info_hash=";
request += escape_string(reinterpret_cast<const char*>(m_torrent_file.info_hash().begin()), 20);
request += "&peer_id=";
request += escape_string(reinterpret_cast<const char*>(m_ses.get_peer_id().begin()), 20);
request += "&port=";
request += boost::lexical_cast<std::string>(port);
request += "&uploaded=";
request += boost::lexical_cast<std::string>(m_stat.total_payload_upload());
request += "&downloaded=";
request += boost::lexical_cast<std::string>(m_stat.total_payload_download());
request += "&left=";
request += boost::lexical_cast<std::string>(bytes_left());
tracker_request req;
req.info_hash = m_torrent_file.info_hash();
req.id = m_ses.get_peer_id();
req.downloaded = m_stat.total_payload_download();
req.uploaded = m_stat.total_payload_upload();
req.left = bytes_left();
req.listen_port = port;
if (m_event != event_none)
{
const char* event_string[] = {"started", "stopped", "completed"};
request += "&event=";
request += event_string[m_event];
req.event += event_string[m_event];
m_event = event_none;
}
// extension that tells the tracker that
// we don't need any peer_id's in the response
request += "&no_peer_id=1";
return request;
req.url = m_torrent_file.trackers()[m_currently_trying_tracker].url;
return req;
}
void torrent::parse_response(const entry& e, std::vector<peer_entry>& peer_list)
@ -634,7 +615,7 @@ namespace libtorrent
m_policy->new_connection(*i->second);
}
void torrent::close_all_connections()
void torrent::disconnect_all()
{
for (peer_iterator i = m_connections.begin();
i != m_connections.end();