Skip to content

Commit 098c57b

Browse files
committed
* Optimized AvailableList::erase.
* Moved PeerInfo and DownloadMain into Extensions. * Added a flag for if we've received the initial handshake and moved setting of flag_received_ext to Extension. git-svn-id: svn://rakshasa.no/libtorrent/trunk/libtorrent@980 e378c898-3ddf-0310-93e7-cc216c733640
1 parent 49b4d2f commit 098c57b

14 files changed

+338
-134
lines changed

extra/object.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
namespace torrent {
4848

4949
// Add support for the GCC move semantics.
50-
50+
//
51+
// Not today?
5152
class ObjectRefRef;
5253

5354
class LIBTORRENT_EXPORT Object {
@@ -93,6 +94,8 @@ class LIBTORRENT_EXPORT Object {
9394

9495
Object() : m_state(type_none) {}
9596
Object(const value_type v) : m_state(type_value), m_value(v) {}
97+
98+
// Don't inline these.
9699
Object(const char* s) : m_state(type_string), m_string(new string_type(s)) {}
97100
Object(const string_type& s) : m_state(type_string), m_string(new string_type(s)) {}
98101
Object(const Object& b);
@@ -179,6 +182,9 @@ class LIBTORRENT_EXPORT Object {
179182
};
180183
};
181184

185+
// Inline?
186+
//
187+
// Or just replace with specific ctors?
182188
inline
183189
Object::Object(type_type t) :
184190
m_state(t) {

src/download/available_list.cc

+6-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,12 @@ void
9090
AvailableList::erase(const rak::socket_address& sa) {
9191
iterator itr = std::find(begin(), end(), sa);
9292

93-
if (itr != end())
94-
erase(itr);
93+
if (itr != end()) {
94+
value_type tmp = *itr;
95+
*itr = back();
96+
97+
pop_back();
98+
}
9599
}
96100

97101
}

src/download/download_info.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ class DownloadInfo {
8787
m_skipRate(60),
8888

8989
m_uploadedBaseline(0),
90-
m_completedBaseline(0) {
90+
m_completedBaseline(0),
91+
m_sizePex(0),
92+
m_maxSizePex(5) {
9193
}
9294

9395
const std::string& name() const { return m_name; }
@@ -139,6 +141,12 @@ class DownloadInfo {
139141
uint64_t completed_baseline() const { return m_completedBaseline; }
140142
void set_completed_baseline(uint64_t b) { m_completedBaseline = b; }
141143

144+
uint32_t size_pex() const { return m_sizePex; }
145+
void set_size_pex(uint32_t b) { m_sizePex = b; }
146+
147+
uint32_t max_size_pex() const { return m_maxSizePex; }
148+
void set_max_size_pex(uint32_t b) { m_maxSizePex = b; }
149+
142150
uint32_t http_timeout() const { return 60; }
143151
uint32_t udp_timeout() const { return 30; }
144152
uint32_t udp_tries() const { return 2; }
@@ -173,6 +181,8 @@ class DownloadInfo {
173181

174182
uint64_t m_uploadedBaseline;
175183
uint64_t m_completedBaseline;
184+
uint32_t m_sizePex;
185+
uint32_t m_maxSizePex;
176186

177187
slot_stat_type m_slotStatCompleted;
178188
slot_stat_type m_slotStatLeft;

src/download/download_main.cc

+8-3
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ DownloadMain::~DownloadMain() {
115115

116116
m_ut_pex_delta.clear();
117117
m_ut_pex_initial.clear();
118+
119+
if (m_info->size_pex() != 0)
120+
throw internal_error("DownloadMain::~DownloadMain(): m_info->size_pex() != 0.");
118121
}
119122

120123
void
@@ -273,7 +276,8 @@ DownloadMain::do_peer_exchange() {
273276
if (!info()->is_active())
274277
throw internal_error("DownloadMain::do_peer_exchange called on inactive download.");
275278

276-
// Check whether we should tell the peers to stop/start sending PEX messages.
279+
// Check whether we should tell the peers to stop/start sending PEX
280+
// messages.
277281
int togglePex = 0;
278282

279283
if (!m_info->is_pex_active() && m_peerList.available_list()->size() < m_peerList.available_list()->max_size() / 4) {
@@ -289,10 +293,11 @@ DownloadMain::do_peer_exchange() {
289293

290294
for (ConnectionList::iterator itr = m_connectionList->begin(); itr != m_connectionList->end(); ++itr) {
291295
if (togglePex != 0)
292-
(*itr)->toggle_peer_exchange(togglePex);
296+
(*itr)->set_peer_exchange(togglePex == PeerConnectionBase::PEX_ENABLE);
293297

294298
// Still using the old buffer? Make a copy in this rare case.
295-
ProtocolExtension::Buffer* message = (*itr)->extension_message();
299+
DataBuffer* message = (*itr)->extension_message();
300+
296301
if (!message->empty() && (message->data() == m_ut_pex_initial.data() || message->data() == m_ut_pex_delta.data())) {
297302
char* buffer = new char[message->length()];
298303
memcpy(buffer, message->data(), message->length());

src/download/download_main.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class DownloadMain {
104104
ThrottleList* download_throttle() { return m_downloadThrottle; }
105105
void set_download_throttle(ThrottleList* t) { m_downloadThrottle = t; }
106106

107-
ProtocolExtension::Buffer get_ut_pex(bool initial) { return initial ? m_ut_pex_initial : m_ut_pex_delta; }
107+
DataBuffer get_ut_pex(bool initial) { return initial ? m_ut_pex_initial : m_ut_pex_delta; }
108108

109109
bool want_pex_msg() { return m_info->is_pex_active() && m_peerList.available_list()->want_more(); };
110110

@@ -163,8 +163,8 @@ class DownloadMain {
163163

164164
uint32_t m_lastConnectedSize;
165165

166-
ProtocolExtension::Buffer m_ut_pex_delta;
167-
ProtocolExtension::Buffer m_ut_pex_initial;
166+
DataBuffer m_ut_pex_delta;
167+
DataBuffer m_ut_pex_initial;
168168
ProtocolExtension::PEXList m_ut_pex_list;
169169

170170
ThrottleList* m_uploadThrottle;

src/download/download_wrapper.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ DownloadWrapper::receive_tick(uint32_t ticks) {
302302
} else if (info()->is_pex_active()) {
303303
info()->set_pex_active(false);
304304
for (ConnectionList::iterator itr = m_main.connection_list()->begin(); itr != m_main.connection_list()->end(); ++itr)
305-
(*itr)->toggle_peer_exchange(PeerConnectionBase::PEX_DISABLE);
305+
(*itr)->set_peer_exchange(false);
306306
}
307307
}
308308

src/net/Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ noinst_LTLIBRARIES = libsub_net.la
33
libsub_net_la_SOURCES = \
44
address_list.cc \
55
address_list.h \
6+
data_buffer.h \
67
listen.cc \
78
listen.h \
89
protocol_buffer.h \

src/net/data_buffer.h

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// libTorrent - BitTorrent library
2+
// Copyright (C) 2005-2007, Jari Sundell
3+
//
4+
// This program is free software; you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation; either version 2 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program; if not, write to the Free Software
16+
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17+
//
18+
// In addition, as a special exception, the copyright holders give
19+
// permission to link the code of portions of this program with the
20+
// OpenSSL library under certain conditions as described in each
21+
// individual source file, and distribute linked combinations
22+
// including the two.
23+
//
24+
// You must obey the GNU General Public License in all respects for
25+
// all of the code used other than OpenSSL. If you modify file(s)
26+
// with this exception, you may extend this exception to your version
27+
// of the file(s), but you are not obligated to do so. If you do not
28+
// wish to do so, delete this exception statement from your version.
29+
// If you delete this exception statement from all source files in the
30+
// program, then also delete it here.
31+
//
32+
// Contact: Jari Sundell <[email protected]>
33+
//
34+
// Skomakerveien 33
35+
// 3185 Skoppum, NORWAY
36+
37+
#ifndef LIBTORRENT_NET_DATA_BUFFER_H
38+
#define LIBTORRENT_NET_DATA_BUFFER_H
39+
40+
#include <memory>
41+
#include <inttypes.h>
42+
43+
namespace torrent {
44+
45+
// Recipient must call clear() when done with the buffer.
46+
struct DataBuffer {
47+
DataBuffer() : m_data(NULL), m_end(NULL), m_copied(false) {}
48+
DataBuffer(char* data, char* end) : m_data(data), m_end(end), m_copied(false) {}
49+
50+
char* data() const { return m_data; }
51+
char* end() const { return m_end; }
52+
53+
bool copied() const { return m_copied; }
54+
bool empty() const { return m_data == NULL; }
55+
size_t length() const { return m_end - m_data; }
56+
57+
void clear();
58+
void set(char* data, char* end, bool copied);
59+
60+
private:
61+
char* m_data;
62+
char* m_end;
63+
64+
// Used to indicate if buffer held by PCB is copied and needs to be
65+
// deleted after transmission.
66+
bool m_copied;
67+
};
68+
69+
inline void
70+
DataBuffer::clear() {
71+
if (!empty())
72+
delete[] m_data;
73+
74+
m_data = m_end = NULL;
75+
m_copied = false;
76+
}
77+
78+
inline void
79+
DataBuffer::set(char* data, char* end, bool copied) {
80+
m_data = data;
81+
m_end = end;
82+
m_copied = copied;
83+
}
84+
85+
}
86+
87+
#endif

0 commit comments

Comments
 (0)