Skip to content

Commit

Permalink
Merge branch 'cleanup'
Browse files Browse the repository at this point in the history
Conflicts:
	src/protocol/handshake_manager.cc
	src/torrent/connection_manager.h
  • Loading branch information
rakshasa committed May 3, 2012
2 parents ac869ae + 1957566 commit 4ae336e
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 72 deletions.
5 changes: 2 additions & 3 deletions README
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ to the mailinglist.


LIBRARY DEPENDENCIES LIBRARY DEPENDENCIES


libsigc++ 2.0 (deb: libsigc++-2.0-dev) g++ >= 4.2.1
g++ >= 3.3


SIGC++ SIGC++


Expand All @@ -34,4 +33,4 @@ POLLING


"libtorrent/src/torrent/poll.h" provides an abstract class for "libtorrent/src/torrent/poll.h" provides an abstract class for
implementing any kind of polling the client wishes to use. Currently implementing any kind of polling the client wishes to use. Currently
epoll and select based polling is included. epoll and select based polling is included.
4 changes: 0 additions & 4 deletions configure.ac
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ AC_ARG_ENABLE(cyrus-rc4,
] ]
) )


PKG_CHECK_MODULES(STUFF, sigc++-2.0,
CXXFLAGS="$CXXFLAGS $STUFF_CFLAGS";
LIBS="$LIBS $STUFF_LIBS")

AC_CHECK_FUNCS(posix_memalign) AC_CHECK_FUNCS(posix_memalign)


TORRENT_CHECK_MADVISE() TORRENT_CHECK_MADVISE()
Expand Down
17 changes: 17 additions & 0 deletions rak/functional.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -661,6 +661,23 @@ slot_list_call(const Container& slot_list, Arg1 arg1) {
(*first)(arg1); (*first)(arg1);
} }


template <typename Container, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
inline void
slot_list_call(const Container& slot_list, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4) {
if (slot_list.empty())
return;

typename Container::const_iterator first = slot_list.begin();
typename Container::const_iterator next = slot_list.begin();

while (++next != slot_list.end()) {
(*first)(arg1, arg2, arg3, arg4);
first = next;
}

(*first)(arg1, arg2, arg3, arg4);
}

} }


#endif #endif
2 changes: 1 addition & 1 deletion src/download/download_main.cc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ std::pair<ThrottleList*, ThrottleList*>
DownloadMain::throttles(const sockaddr* sa) { DownloadMain::throttles(const sockaddr* sa) {
ThrottlePair pair = ThrottlePair(NULL, NULL); ThrottlePair pair = ThrottlePair(NULL, NULL);


if (!manager->connection_manager()->address_throttle().empty()) if (manager->connection_manager()->address_throttle())
pair = manager->connection_manager()->address_throttle()(sa); pair = manager->connection_manager()->address_throttle()(sa);


return std::make_pair(pair.first == NULL ? upload_throttle() : pair.first->throttle_list(), return std::make_pair(pair.first == NULL ? upload_throttle() : pair.first->throttle_list(),
Expand Down
1 change: 0 additions & 1 deletion src/download/download_main.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@


#include <deque> #include <deque>
#include <rak/functional.h> #include <rak/functional.h>
#include <sigc++/signal.h>


#include "globals.h" #include "globals.h"


Expand Down
5 changes: 2 additions & 3 deletions src/download/download_wrapper.cc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include <iterator> #include <iterator>
#include <stdlib.h> #include <stdlib.h>
#include <rak/file_stat.h> #include <rak/file_stat.h>
#include <sigc++/adaptors/bind.h>


#include "data/chunk_list.h" #include "data/chunk_list.h"
#include "data/hash_queue.h" #include "data/hash_queue.h"
Expand Down Expand Up @@ -108,8 +107,8 @@ DownloadWrapper::initialize(const std::string& hash, const std::string& id) {


info()->mutable_local_id().assign(id.c_str()); info()->mutable_local_id().assign(id.c_str());


info()->slot_left() = sigc::mem_fun(m_main->file_list(), &FileList::left_bytes); info()->slot_left() = tr1::bind(&FileList::left_bytes, m_main->file_list());
info()->slot_completed() = sigc::mem_fun(m_main->file_list(), &FileList::completed_bytes); info()->slot_completed() = tr1::bind(&FileList::completed_bytes, m_main->file_list());


file_list()->mutable_data()->mutable_hash().assign(hash.c_str()); file_list()->mutable_data()->mutable_hash().assign(hash.c_str());


Expand Down
11 changes: 0 additions & 11 deletions src/download/download_wrapper.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@
#ifndef LIBTORRENT_DOWNLOAD_WRAPPER_H #ifndef LIBTORRENT_DOWNLOAD_WRAPPER_H
#define LIBTORRENT_DOWNLOAD_WRAPPER_H #define LIBTORRENT_DOWNLOAD_WRAPPER_H


#include <sigc++/connection.h>
#include <sigc++/signal.h>

#include "data/chunk_handle.h" #include "data/chunk_handle.h"

#include "download_main.h" #include "download_main.h"


namespace torrent { namespace torrent {
Expand All @@ -59,10 +55,6 @@ class Peer;


class DownloadWrapper { class DownloadWrapper {
public: public:
typedef sigc::signal0<void> Signal;
typedef sigc::signal1<void, uint32_t> SignalChunk;
typedef sigc::signal1<void, const std::string&> SignalString;

DownloadWrapper(); DownloadWrapper();
~DownloadWrapper(); ~DownloadWrapper();


Expand Down Expand Up @@ -126,9 +118,6 @@ class DownloadWrapper {
std::string m_hash; std::string m_hash;


int m_connectionType; int m_connectionType;

sigc::connection m_connectionChunkPassed;
sigc::connection m_connectionChunkFailed;
}; };


} }
Expand Down
16 changes: 12 additions & 4 deletions src/torrent/connection_manager.cc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@
#include "error.h" #include "error.h"
#include "exceptions.h" #include "exceptions.h"


namespace tr1 { using namespace std::tr1; }

namespace torrent { namespace torrent {


// Fix TrackerUdp, etc, if this is made async.
static ConnectionManager::slot_resolver_result_type* static ConnectionManager::slot_resolver_result_type*
resolve_host(const char* host, int family, int socktype, ConnectionManager::slot_resolver_result_type slot) { resolve_host(const char* host, int family, int socktype, ConnectionManager::slot_resolver_result_type slot) {
rak::address_info* ai; rak::address_info* ai;
Expand Down Expand Up @@ -76,8 +79,7 @@ ConnectionManager::ConnectionManager() :
m_encryptionOptions(encryption_none), m_encryptionOptions(encryption_none),


m_listen(new Listen), m_listen(new Listen),
m_listenPort(0), m_listenPort(0) {
m_slotResolver(&resolve_host) {


m_bindAddress = (new rak::socket_address())->c_sockaddr(); m_bindAddress = (new rak::socket_address())->c_sockaddr();
rak::socket_address::cast_from(m_bindAddress)->sa_inet()->clear(); rak::socket_address::cast_from(m_bindAddress)->sa_inet()->clear();
Expand All @@ -87,6 +89,12 @@ ConnectionManager::ConnectionManager() :


m_proxyAddress = (new rak::socket_address())->c_sockaddr(); m_proxyAddress = (new rak::socket_address())->c_sockaddr();
rak::socket_address::cast_from(m_proxyAddress)->sa_inet()->clear(); rak::socket_address::cast_from(m_proxyAddress)->sa_inet()->clear();

m_slot_resolver = tr1::bind(&resolve_host,
tr1::placeholders::_1,
tr1::placeholders::_2,
tr1::placeholders::_3,
tr1::placeholders::_4);
} }


ConnectionManager::~ConnectionManager() { ConnectionManager::~ConnectionManager() {
Expand Down Expand Up @@ -153,10 +161,10 @@ ConnectionManager::set_proxy_address(const sockaddr* sa) {


uint32_t uint32_t
ConnectionManager::filter(const sockaddr* sa) { ConnectionManager::filter(const sockaddr* sa) {
if (m_slotFilter.empty()) if (!m_slot_filter)
return 1; return 1;
else else
return m_slotFilter(sa); return m_slot_filter(sa);
} }


bool bool
Expand Down
59 changes: 28 additions & 31 deletions src/torrent/connection_manager.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -34,20 +34,19 @@
// Skomakerveien 33 // Skomakerveien 33
// 3185 Skoppum, NORWAY // 3185 Skoppum, NORWAY


// Add some helpfull words here. // Add some helpfull words here. (These are some words, hope they are
// helpful)


#ifndef LIBTORRENT_CONNECTION_MANAGER_H #ifndef LIBTORRENT_CONNECTION_MANAGER_H
#define LIBTORRENT_CONNECTION_MANAGER_H #define LIBTORRENT_CONNECTION_MANAGER_H


#include <sys/socket.h> #include <list>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/in_systm.h> #include <netinet/in_systm.h>
#include <netinet/ip.h> #include <netinet/ip.h>
#include <sigc++/connection.h> #include <sys/socket.h>
#include <sigc++/signal.h> #include <tr1/functional>
#include <sigc++/functors/slot.h>

#include <torrent/common.h> #include <torrent/common.h>


namespace torrent { namespace torrent {
Expand All @@ -58,10 +57,9 @@ typedef std::pair<Throttle*, Throttle*> ThrottlePair;


class LIBTORRENT_EXPORT ConnectionManager { class LIBTORRENT_EXPORT ConnectionManager {
public: public:
typedef uint32_t size_type; typedef uint32_t size_type;
typedef uint16_t port_type; typedef uint16_t port_type;
typedef uint8_t priority_type; typedef uint8_t priority_type;
typedef sigc::slot<uint32_t, const sockaddr*> slot_filter_type;


static const priority_type iptos_default = 0; static const priority_type iptos_default = 0;
static const priority_type iptos_lowdelay = IPTOS_LOWDELAY; static const priority_type iptos_lowdelay = IPTOS_LOWDELAY;
Expand Down Expand Up @@ -100,11 +98,12 @@ class LIBTORRENT_EXPORT ConnectionManager {
handshake_retry_encrypted = 9 handshake_retry_encrypted = 9
}; };


// The sockaddr argument in the result slot call is NULL if the resolve failed, and the int holds the errno. typedef std::tr1::function<uint32_t (const sockaddr*)> slot_filter_type;
typedef sigc::slot2<void, const sockaddr*, int> slot_resolver_result_type; typedef std::tr1::function<ThrottlePair (const sockaddr*)> slot_throttle_type;
typedef sigc::slot4<slot_resolver_result_type*, const char*, int, int, slot_resolver_result_type> slot_resolver_type;


typedef sigc::slot1<ThrottlePair, const sockaddr*> slot_address_throttle_type; // The sockaddr argument in the result slot call is NULL if the resolve failed, and the int holds the errno.
typedef std::tr1::function<void (const sockaddr*, int)> slot_resolver_result_type;
typedef std::tr1::function<slot_resolver_result_type* (const char*, int, int, slot_resolver_result_type)> slot_resolver_type;


ConnectionManager(); ConnectionManager();
~ConnectionManager(); ~ConnectionManager();
Expand Down Expand Up @@ -143,32 +142,30 @@ class LIBTORRENT_EXPORT ConnectionManager {
void set_proxy_address(const sockaddr* sa); void set_proxy_address(const sockaddr* sa);


uint32_t filter(const sockaddr* sa); uint32_t filter(const sockaddr* sa);
void set_filter(const slot_filter_type& s) { m_slotFilter = s; } void set_filter(const slot_filter_type& s) { m_slot_filter = s; }


bool listen_open(port_type begin, port_type end); bool listen_open(port_type begin, port_type end);
void listen_close(); void listen_close();


// The resolver returns a pointer to its copy of the result slot
// which the caller may set blocked to prevent the slot from being
// called. The pointer must be NULL if the result slot was already
// called because the resolve was synchronous.
const slot_resolver_type& resolver() const { return m_slotResolver; }
void set_resolver(const slot_resolver_type& s) { m_slotResolver = s; }

// Since trackers need our port number, it doesn't get cleared after // Since trackers need our port number, it doesn't get cleared after
// 'listen_close()'. The client may change the reported port number, // 'listen_close()'. The client may change the reported port number,
// but do note that it gets overwritten after 'listen_open(...)'. // but do note that it gets overwritten after 'listen_open(...)'.
port_type listen_port() const { return m_listenPort; } port_type listen_port() const { return m_listenPort; }
void set_listen_port(port_type p) { m_listenPort = p; } void set_listen_port(port_type p) { m_listenPort = p; }


// The slot returns a ThrottlePair to use for the given address, or NULLs to use the default throttle.
const slot_address_throttle_type& // The resolver returns a pointer to its copy of the result slot
address_throttle() const { return m_slotAddressThrottle; } // which the caller may set blocked to prevent the slot from being
void set_address_throttle(const slot_address_throttle_type& s) // called. The pointer must be NULL if the result slot was already
{ m_slotAddressThrottle = s; } // called because the resolve was synchronous.
slot_resolver_type& resolver() { return m_slot_resolver; }

// The slot returns a ThrottlePair to use for the given address, or
// NULLs to use the default throttle.
slot_throttle_type& address_throttle() { return m_slot_address_throttle; }


// For internal usage. // For internal usage.
Listen* listen() { return m_listen; } Listen* listen() { return m_listen; }


private: private:
ConnectionManager(const ConnectionManager&); ConnectionManager(const ConnectionManager&);
Expand All @@ -189,9 +186,9 @@ class LIBTORRENT_EXPORT ConnectionManager {
Listen* m_listen; Listen* m_listen;
port_type m_listenPort; port_type m_listenPort;


slot_filter_type m_slotFilter; slot_filter_type m_slot_filter;
slot_resolver_type m_slotResolver; slot_resolver_type m_slot_resolver;
slot_address_throttle_type m_slotAddressThrottle; slot_throttle_type m_slot_address_throttle;
}; };


} }
Expand Down
2 changes: 0 additions & 2 deletions src/torrent/download.cc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
#define __STDC_FORMAT_MACROS #define __STDC_FORMAT_MACROS


#include <inttypes.h> #include <inttypes.h>
#include <sigc++/adaptors/bind.h>
#include <sigc++/adaptors/hide.h>


#include "data/block.h" #include "data/block.h"
#include "data/block_list.h" #include "data/block_list.h"
Expand Down
2 changes: 0 additions & 2 deletions src/torrent/download.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
#include <list> #include <list>
#include <vector> #include <vector>
#include <string> #include <string>
#include <sigc++/connection.h>
#include <sigc++/functors/slot.h>


#include <torrent/common.h> #include <torrent/common.h>
#include <torrent/peer/peer.h> #include <torrent/peer/peer.h>
Expand Down
29 changes: 20 additions & 9 deletions src/tracker/tracker_udp.cc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@


#include <sys/types.h> #include <sys/types.h>


#include <sigc++/adaptors/bind.h>
#include <torrent/connection_manager.h> #include <torrent/connection_manager.h>
#include <cstdio> #include <cstdio>


Expand All @@ -56,6 +55,8 @@
#include "tracker_udp.h" #include "tracker_udp.h"
#include "manager.h" #include "manager.h"


namespace tr1 { using namespace std::tr1; }

#define LT_LOG_TRACKER(log_level, log_fmt, ...) \ #define LT_LOG_TRACKER(log_level, log_fmt, ...) \
lt_log_print_info(LOG_TRACKER_##log_level, m_parent->info(), "tracker", "[%u] " log_fmt, group(), __VA_ARGS__); lt_log_print_info(LOG_TRACKER_##log_level, m_parent->info(), "tracker", "[%u] " log_fmt, group(), __VA_ARGS__);


Expand All @@ -67,16 +68,18 @@ namespace torrent {
TrackerUdp::TrackerUdp(TrackerList* parent, const std::string& url, int flags) : TrackerUdp::TrackerUdp(TrackerList* parent, const std::string& url, int flags) :
Tracker(parent, url, flags), Tracker(parent, url, flags),


m_slotResolver(NULL), m_slot_resolver(NULL),
m_readBuffer(NULL), m_readBuffer(NULL),
m_writeBuffer(NULL) { m_writeBuffer(NULL) {


m_taskTimeout.slot() = std::tr1::bind(&TrackerUdp::receive_timeout, this); m_taskTimeout.slot() = std::tr1::bind(&TrackerUdp::receive_timeout, this);
} }


TrackerUdp::~TrackerUdp() { TrackerUdp::~TrackerUdp() {
if (m_slotResolver != NULL) if (m_slot_resolver != NULL) {
static_cast<ConnectionManager::slot_resolver_result_type*>(m_slotResolver)->blocked(); *m_slot_resolver = resolver_type();
m_slot_resolver = NULL;
}


close_directly(); close_directly();
} }
Expand All @@ -100,17 +103,25 @@ TrackerUdp::send_state(int state) {


// Because we can only remember one slot, set any pending resolves blocked // Because we can only remember one slot, set any pending resolves blocked
// so that if this tracker is deleted, the member function won't be called. // so that if this tracker is deleted, the member function won't be called.
if (m_slotResolver != NULL) if (m_slot_resolver != NULL) {
static_cast<ConnectionManager::slot_resolver_result_type*>(m_slotResolver)->blocked(); *m_slot_resolver = resolver_type();
m_slot_resolver = NULL;
}


m_sendState = state; m_sendState = state;
m_slotResolver = manager->connection_manager()->resolver()(hostname, PF_INET, SOCK_DGRAM, m_slot_resolver = manager->connection_manager()->resolver()(hostname, PF_INET, SOCK_DGRAM,
sigc::mem_fun(this, &TrackerUdp::start_announce)); tr1::bind(&TrackerUdp::start_announce,
this,
tr1::placeholders::_1,
tr1::placeholders::_2));
} }


void void
TrackerUdp::start_announce(const sockaddr* sa, int err) { TrackerUdp::start_announce(const sockaddr* sa, int err) {
m_slotResolver = NULL; if (m_slot_resolver != NULL) {
*m_slot_resolver = resolver_type();
m_slot_resolver = NULL;
}


if (sa == NULL) if (sa == NULL)
return receive_failed("Could not resolve hostname."); return receive_failed("Could not resolve hostname.");
Expand Down
5 changes: 4 additions & 1 deletion src/tracker/tracker_udp.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@


#include "net/protocol_buffer.h" #include "net/protocol_buffer.h"
#include "net/socket_datagram.h" #include "net/socket_datagram.h"
#include "torrent/connection_manager.h"
#include "torrent/tracker.h" #include "torrent/tracker.h"


#include "globals.h" #include "globals.h"
Expand All @@ -52,6 +53,8 @@ class TrackerUdp : public SocketDatagram, public Tracker {
typedef ProtocolBuffer<512> ReadBuffer; typedef ProtocolBuffer<512> ReadBuffer;
typedef ProtocolBuffer<512> WriteBuffer; typedef ProtocolBuffer<512> WriteBuffer;


typedef ConnectionManager::slot_resolver_result_type resolver_type;

static const uint64_t magic_connection_id = 0x0000041727101980ll; static const uint64_t magic_connection_id = 0x0000041727101980ll;


TrackerUdp(TrackerList* parent, const std::string& url, int flags); TrackerUdp(TrackerList* parent, const std::string& url, int flags);
Expand Down Expand Up @@ -92,7 +95,7 @@ class TrackerUdp : public SocketDatagram, public Tracker {


int m_sendState; int m_sendState;


void* m_slotResolver; resolver_type* m_slot_resolver;


uint32_t m_action; uint32_t m_action;
uint64_t m_connectionId; uint64_t m_connectionId;
Expand Down

0 comments on commit 4ae336e

Please sign in to comment.