switched extension handshake parsing over to use lazy_bdecode for improved performance
This commit is contained in:
parent
9c84908bb9
commit
10f5418ce7
|
@ -188,7 +188,7 @@ peer_plugin
|
||||||
|
|
||||||
virtual void add_handshake(entry&);
|
virtual void add_handshake(entry&);
|
||||||
virtual bool on_handshake(char const* reserved_bits);
|
virtual bool on_handshake(char const* reserved_bits);
|
||||||
virtual bool on_extension_handshake(entry const& h);
|
virtual bool on_extension_handshake(lazy_entry const& h);
|
||||||
|
|
||||||
virtual bool on_choke();
|
virtual bool on_choke();
|
||||||
virtual bool on_unchoke();
|
virtual bool on_unchoke();
|
||||||
|
|
|
@ -56,6 +56,7 @@ namespace libtorrent
|
||||||
struct peer_request;
|
struct peer_request;
|
||||||
class peer_connection;
|
class peer_connection;
|
||||||
class entry;
|
class entry;
|
||||||
|
struct lazy_entry;
|
||||||
struct disk_buffer_holder;
|
struct disk_buffer_holder;
|
||||||
struct bitfield;
|
struct bitfield;
|
||||||
|
|
||||||
|
@ -107,7 +108,7 @@ namespace libtorrent
|
||||||
// supported by this peer. It will result in this peer_plugin
|
// supported by this peer. It will result in this peer_plugin
|
||||||
// being removed from the peer_connection and destructed.
|
// being removed from the peer_connection and destructed.
|
||||||
// this is not called for web seeds
|
// this is not called for web seeds
|
||||||
virtual bool on_extension_handshake(entry const& h) { return true; }
|
virtual bool on_extension_handshake(lazy_entry const& h) { return true; }
|
||||||
|
|
||||||
// returning true from any of the message handlers
|
// returning true from any of the message handlers
|
||||||
// indicates that the plugin has handeled the message.
|
// indicates that the plugin has handeled the message.
|
||||||
|
|
|
@ -1246,9 +1246,9 @@ namespace libtorrent
|
||||||
|
|
||||||
buffer::const_interval recv_buffer = receive_buffer();
|
buffer::const_interval recv_buffer = receive_buffer();
|
||||||
|
|
||||||
entry root;
|
lazy_entry root;
|
||||||
root = bdecode(recv_buffer.begin + 2, recv_buffer.end);
|
lazy_bdecode(recv_buffer.begin + 2, recv_buffer.end, root);
|
||||||
if (root.type() == entry::undefined_t)
|
if (root.type() != lazy_entry::dict_t)
|
||||||
{
|
{
|
||||||
#ifdef TORRENT_VERBOSE_LOGGING
|
#ifdef TORRENT_VERBOSE_LOGGING
|
||||||
(*m_logger) << "invalid extended handshake\n";
|
(*m_logger) << "invalid extended handshake\n";
|
||||||
|
@ -1257,9 +1257,7 @@ namespace libtorrent
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef TORRENT_VERBOSE_LOGGING
|
#ifdef TORRENT_VERBOSE_LOGGING
|
||||||
std::stringstream ext;
|
(*m_logger) << "<== EXTENDED HANDSHAKE: \n" << root;
|
||||||
root.print(ext);
|
|
||||||
(*m_logger) << "<== EXTENDED HANDSHAKE: \n" << ext.str();
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef TORRENT_DISABLE_EXTENSIONS
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
||||||
|
@ -1276,56 +1274,39 @@ namespace libtorrent
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// there is supposed to be a remote listen port
|
// there is supposed to be a remote listen port
|
||||||
if (entry* listen_port = root.find_key("p"))
|
int listen_port = root.dict_find_int_value("p");
|
||||||
|
if (listen_port > 0 && peer_info_struct() != 0)
|
||||||
{
|
{
|
||||||
if (listen_port->type() == entry::int_t
|
t->get_policy().update_peer_port(listen_port
|
||||||
&& peer_info_struct() != 0)
|
, peer_info_struct(), peer_info::incoming);
|
||||||
{
|
|
||||||
t->get_policy().update_peer_port(int(listen_port->integer())
|
|
||||||
, peer_info_struct(), peer_info::incoming);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// there should be a version too
|
// there should be a version too
|
||||||
// but where do we put that info?
|
// but where do we put that info?
|
||||||
|
|
||||||
if (entry* client_info = root.find_key("v"))
|
std::string client_info = root.dict_find_string_value("v");
|
||||||
{
|
if (!client_info.empty()) m_client_version = client_info;
|
||||||
if (client_info->type() == entry::string_t)
|
|
||||||
m_client_version = client_info->string();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (entry* reqq = root.find_key("reqq"))
|
int reqq = root.dict_find_int_value("reqq");
|
||||||
{
|
if (reqq > 0) m_max_out_request_queue = reqq;
|
||||||
if (reqq->type() == entry::int_t)
|
|
||||||
m_max_out_request_queue = int(reqq->integer());
|
|
||||||
if (m_max_out_request_queue < 1)
|
|
||||||
m_max_out_request_queue = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (entry* upload_only = root.find_key("upload_only"))
|
if (root.dict_find_int_value("upload_only"))
|
||||||
{
|
set_upload_only(true);
|
||||||
if (upload_only->type() == entry::int_t && upload_only->integer() != 0)
|
|
||||||
set_upload_only(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (entry* myip = root.find_key("yourip"))
|
std::string myip = root.dict_find_string_value("yourip");
|
||||||
|
if (!myip.empty())
|
||||||
{
|
{
|
||||||
// TODO: don't trust this blindly
|
// TODO: don't trust this blindly
|
||||||
if (myip->type() == entry::string_t)
|
if (myip.size() == address_v4::bytes_type::static_size)
|
||||||
{
|
{
|
||||||
std::string const& my_ip = myip->string().c_str();
|
address_v4::bytes_type bytes;
|
||||||
if (my_ip.size() == address_v4::bytes_type::static_size)
|
std::copy(myip.begin(), myip.end(), bytes.begin());
|
||||||
{
|
m_ses.set_external_address(address_v4(bytes));
|
||||||
address_v4::bytes_type bytes;
|
}
|
||||||
std::copy(my_ip.begin(), my_ip.end(), bytes.begin());
|
else if (myip.size() == address_v6::bytes_type::static_size)
|
||||||
m_ses.set_external_address(address_v4(bytes));
|
{
|
||||||
}
|
address_v6::bytes_type bytes;
|
||||||
else if (my_ip.size() == address_v6::bytes_type::static_size)
|
std::copy(myip.begin(), myip.end(), bytes.begin());
|
||||||
{
|
m_ses.set_external_address(address_v6(bytes));
|
||||||
address_v6::bytes_type bytes;
|
|
||||||
std::copy(my_ip.begin(), my_ip.end(), bytes.begin());
|
|
||||||
m_ses.set_external_address(address_v6(bytes));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -81,11 +81,10 @@ namespace
|
||||||
virtual void add_handshake(entry&) {}
|
virtual void add_handshake(entry&) {}
|
||||||
|
|
||||||
// called when the extension handshake from the other end is received
|
// called when the extension handshake from the other end is received
|
||||||
virtual bool on_extension_handshake(entry const& h)
|
virtual bool on_extension_handshake(lazy_entry const& h)
|
||||||
{
|
{
|
||||||
log_timestamp();
|
log_timestamp();
|
||||||
m_file << "<== EXTENSION_HANDSHAKE\n";
|
m_file << "<== EXTENSION_HANDSHAKE\n" << h;
|
||||||
h.print(m_file);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -278,15 +278,16 @@ namespace libtorrent { namespace
|
||||||
}
|
}
|
||||||
|
|
||||||
// called when the extension handshake from the other end is received
|
// called when the extension handshake from the other end is received
|
||||||
virtual bool on_extension_handshake(entry const& h)
|
virtual bool on_extension_handshake(lazy_entry const& h)
|
||||||
{
|
{
|
||||||
m_message_index = 0;
|
m_message_index = 0;
|
||||||
entry const* messages = h.find_key("m");
|
if (h.type() != lazy_entry::dict_t) return false;
|
||||||
if (!messages || messages->type() != entry::dictionary_t) return false;
|
lazy_entry const* messages = h.dict_find("m");
|
||||||
|
if (!messages || messages->type() != lazy_entry::dict_t) return false;
|
||||||
|
|
||||||
entry const* index = messages->find_key("LT_metadata");
|
int index = messages->dict_find_int_value("LT_metadata", -1);
|
||||||
if (!index || index->type() != entry::int_t) return false;
|
if (index == -1) return false;
|
||||||
m_message_index = int(index->integer());
|
m_message_index = index;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -235,18 +235,20 @@ namespace libtorrent { namespace
|
||||||
}
|
}
|
||||||
|
|
||||||
// called when the extension handshake from the other end is received
|
// called when the extension handshake from the other end is received
|
||||||
virtual bool on_extension_handshake(entry const& h)
|
virtual bool on_extension_handshake(lazy_entry const& h)
|
||||||
{
|
{
|
||||||
entry const* metadata_size = h.find_key("metadata_size");
|
m_message_index = 0;
|
||||||
if (metadata_size && metadata_size->type() == entry::int_t)
|
if (h.type() != lazy_entry::dict_t) return false;
|
||||||
m_tp.metadata_size(metadata_size->integer());
|
lazy_entry const* messages = h.dict_find("m");
|
||||||
|
if (!messages || messages->type() != lazy_entry::dict_t) return false;
|
||||||
|
|
||||||
entry const* messages = h.find_key("m");
|
int index = messages->dict_find_int_value("ut_metadata", -1);
|
||||||
if (!messages || messages->type() != entry::dictionary_t) return false;
|
if (index == -1) return false;
|
||||||
|
m_message_index = index;
|
||||||
|
|
||||||
entry const* index = messages->find_key("ut_metadata");
|
int metadata_size = h.dict_find_int_value("metadata_size");
|
||||||
if (!index || index->type() != entry::int_t) return false;
|
if (metadata_size > 0)
|
||||||
m_message_index = int(index->integer());
|
m_tp.metadata_size(metadata_size);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -196,16 +196,16 @@ namespace libtorrent { namespace
|
||||||
messages[extension_name] = extension_index;
|
messages[extension_name] = extension_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual bool on_extension_handshake(entry const& h)
|
virtual bool on_extension_handshake(lazy_entry const& h)
|
||||||
{
|
{
|
||||||
m_message_index = 0;
|
m_message_index = 0;
|
||||||
entry const* messages = h.find_key("m");
|
if (h.type() != lazy_entry::dict_t) return false;
|
||||||
if (!messages || messages->type() != entry::dictionary_t) return false;
|
lazy_entry const* messages = h.dict_find("m");
|
||||||
|
if (!messages || messages->type() != lazy_entry::dict_t) return false;
|
||||||
|
|
||||||
entry const* index = messages->find_key(extension_name);
|
int index = messages->dict_find_int_value(extension_name, -1);
|
||||||
if (!index || index->type() != entry::int_t) return false;
|
if (index == -1) return false;
|
||||||
|
m_message_index = index;
|
||||||
m_message_index = int(index->integer());
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue