1
// libTorrent - BitTorrent library
2
// Copyright (C) 2005-2007, Jari Sundell
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.
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.
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
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
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.
32
// Contact: Jari Sundell <jaris@ifi.uio.no>
35
// 3185 Skoppum, NORWAY
37
#ifndef LIBTORRENT_NET_HANDSHAKE_MANAGER_H
38
#define LIBTORRENT_NET_HANDSHAKE_MANAGER_H
42
#include <rak/functional.h>
43
#include <rak/unordered_vector.h>
44
#include <rak/socket_address.h>
45
#include <torrent/connection_manager.h>
47
#include "net/socket_fd.h"
52
class DownloadManager;
54
class PeerConnectionBase;
56
class HandshakeManager : private rak::unordered_vector<Handshake*> {
58
typedef rak::unordered_vector<Handshake*> base_type;
59
typedef uint32_t size_type;
61
typedef rak::mem_fun1<DownloadManager, DownloadMain*, const char*> SlotDownloadId;
63
// Do not connect to peers with this many or more failed chunks.
64
static const unsigned int max_failed = 3;
66
using base_type::empty;
68
HandshakeManager() { }
69
~HandshakeManager() { clear(); }
71
size_type size() const { return base_type::size(); }
72
size_type size_info(DownloadMain* info) const;
76
bool find(const rak::socket_address& sa);
78
void erase_download(DownloadMain* info);
81
void add_incoming(SocketFd fd, const rak::socket_address& sa);
82
void add_outgoing(const rak::socket_address& sa, DownloadMain* info);
84
void slot_download_id(SlotDownloadId s) { m_slotDownloadId = s; }
85
void slot_download_id_obfuscated(SlotDownloadId s) { m_slotDownloadIdObfuscated = s; }
87
void receive_succeeded(Handshake* h);
88
void receive_failed(Handshake* h, int message, int error);
89
void receive_timeout(Handshake* h);
91
// This needs to be filterable slot.
92
DownloadMain* download_info(const char* hash) { return m_slotDownloadId(hash); }
93
DownloadMain* download_info_obfuscated(const char* hash) { return m_slotDownloadIdObfuscated(hash); }
95
ProtocolExtension* default_extensions() const { return &DefaultExtensions; }
98
void create_outgoing(const rak::socket_address& sa, DownloadMain* info, int encryptionOptions);
99
void erase(Handshake* handshake);
101
bool setup_socket(SocketFd fd);
103
static ProtocolExtension DefaultExtensions;
105
SlotDownloadId m_slotDownloadId;
106
SlotDownloadId m_slotDownloadIdObfuscated;