3 Copyright (c) 2003, Arvid Norberg
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #ifndef TORRENT_TRACKER_MANAGER_HPP_INCLUDED
34 #define TORRENT_TRACKER_MANAGER_HPP_INCLUDED
42 #pragma warning(push, 1)
45 #include <boost/shared_ptr.hpp>
46 #include <boost/cstdint.hpp>
47 #include <boost/weak_ptr.hpp>
48 #include <boost/intrusive_ptr.hpp>
49 #include <boost/thread/mutex.hpp>
50 #include <boost/thread/recursive_mutex.hpp>
51 #include <boost/tuple/tuple.hpp>
57 #include "libtorrent/socket.hpp"
58 #include "libtorrent/entry.hpp"
59 #include "libtorrent/session_settings.hpp"
60 #include "libtorrent/peer_id.hpp"
61 #include "libtorrent/peer.hpp"
62 #include "libtorrent/config.hpp"
63 #include "libtorrent/time.hpp"
64 #include "libtorrent/connection_queue.hpp"
65 #include "libtorrent/intrusive_ptr_base.hpp"
69 struct request_callback
;
70 class tracker_manager
;
71 struct timeout_handler
;
72 struct tracker_connection
;
74 // returns -1 if gzip header is invalid or the header size in bytes
75 TORRENT_EXPORT
int gzip_header(const char* buf
, int size
);
77 struct TORRENT_EXPORT tracker_request
80 : kind(announce_request
)
102 size_type downloaded
;
105 unsigned short listen_port
;
113 struct TORRENT_EXPORT request_callback
115 friend class tracker_manager
;
116 request_callback(): m_manager(0) {}
117 virtual ~request_callback() {}
118 virtual void tracker_warning(tracker_request
const& req
119 , std::string
const& msg
) = 0;
120 virtual void tracker_scrape_response(tracker_request
const& req
121 , int complete
, int incomplete
, int downloads
) {}
122 virtual void tracker_response(
123 tracker_request
const& req
124 , std::vector
<peer_entry
>& peers
128 , address
const& external_ip
) = 0;
129 virtual void tracker_request_timed_out(
130 tracker_request
const& req
) = 0;
131 virtual void tracker_request_error(
132 tracker_request
const& req
134 , const std::string
& description
) = 0;
136 tcp::endpoint m_tracker_address
;
138 #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
139 virtual void debug_log(const std::string
& line
) = 0;
142 tracker_manager
* m_manager
;
145 struct TORRENT_EXPORT timeout_handler
146 : intrusive_ptr_base
<timeout_handler
>
149 timeout_handler(io_service
& str
);
151 void set_timeout(int completion_timeout
, int read_timeout
);
152 void restart_read_timeout();
155 virtual void on_timeout() = 0;
156 virtual ~timeout_handler() {}
160 void timeout_callback(error_code
const&);
162 boost::intrusive_ptr
<timeout_handler
> self()
163 { return boost::intrusive_ptr
<timeout_handler
>(this); }
166 // this is set when the request has been sent
168 // this is set every time something is received
170 // the asio async operation
171 deadline_timer m_timeout
;
173 int m_completion_timeout
;
176 typedef boost::mutex mutex_t
;
177 mutable mutex_t m_mutex
;
181 struct TORRENT_EXPORT tracker_connection
184 tracker_connection(tracker_manager
& man
185 , tracker_request
const& req
187 , address bind_interface
188 , boost::weak_ptr
<request_callback
> r
);
190 boost::shared_ptr
<request_callback
> requester();
191 virtual ~tracker_connection() {}
193 tracker_request
const& tracker_req() const { return m_req
; }
195 void fail(int code
, char const* msg
);
197 virtual void close();
198 address
const& bind_interface() const { return m_bind_interface
; }
201 boost::weak_ptr
<request_callback
> m_requester
;
203 address m_bind_interface
;
204 tracker_manager
& m_man
;
205 const tracker_request m_req
;
208 class TORRENT_EXPORT tracker_manager
: boost::noncopyable
212 tracker_manager(session_settings
const& s
, proxy_settings
const& ps
)
219 , connection_queue
& cc
221 , std::string
const& auth
223 , boost::weak_ptr
<request_callback
> c
224 = boost::weak_ptr
<request_callback
>());
225 void abort_all_requests();
227 void remove_request(tracker_connection
const*);
229 int num_requests() const;
233 typedef boost::recursive_mutex mutex_t
;
234 mutable mutex_t m_mutex
;
236 typedef std::list
<boost::intrusive_ptr
<tracker_connection
> >
237 tracker_connections_t
;
238 tracker_connections_t m_connections
;
239 session_settings
const& m_settings
;
240 proxy_settings
const& m_proxy
;
245 #endif // TORRENT_TRACKER_MANAGER_HPP_INCLUDED