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/array.hpp>
#include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <boost/cstdint.hpp>
#include "libtorrent/socket.hpp" #include "libtorrent/socket.hpp"
#include "libtorrent/peer_id.hpp" #include "libtorrent/peer_id.hpp"
@ -119,6 +120,28 @@ namespace libtorrent
++start; ++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 struct protocol_error: std::runtime_error
@ -244,6 +267,9 @@ namespace libtorrent
// mainloop to disconnect peers. // mainloop to disconnect peers.
void disconnect(); void disconnect();
bool is_disconnecting() const
{ return m_disconnecting; }
// sets the number of bytes this peer // sets the number of bytes this peer
// is allowed to send until it should // is allowed to send until it should
// stop sending. When it stops sending // stop sending. When it stops sending
@ -551,6 +577,8 @@ namespace libtorrent
// the time it took for the peer to send the piece // the time it took for the peer to send the piece
// message // message
boost::posix_time::time_duration m_last_piece_time; boost::posix_time::time_duration m_last_piece_time;
bool m_disconnecting;
}; };
// this is called each time this peer generates some // this is called each time this peer generates some

View File

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

View File

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

View File

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

View File

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