00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef TORRENT_PEER_CONNECTION_HPP_INCLUDED
00034 #define TORRENT_PEER_CONNECTION_HPP_INCLUDED
00035
00036 #include <ctime>
00037 #include <algorithm>
00038 #include <vector>
00039 #include <deque>
00040 #include <string>
00041
00042 #include "libtorrent/debug.hpp"
00043
00044 #ifdef _MSC_VER
00045 #pragma warning(push, 1)
00046 #endif
00047
00048 #include <boost/smart_ptr.hpp>
00049 #include <boost/weak_ptr.hpp>
00050 #include <boost/noncopyable.hpp>
00051 #include <boost/array.hpp>
00052 #include <boost/date_time/posix_time/posix_time.hpp>
00053 #include <boost/optional.hpp>
00054 #include <boost/cstdint.hpp>
00055 #include <boost/detail/atomic_count.hpp>
00056
00057 #ifdef _MSC_VER
00058 #pragma warning(pop)
00059 #endif
00060
00061 #include "libtorrent/buffer.hpp"
00062 #include "libtorrent/socket.hpp"
00063 #include "libtorrent/peer_id.hpp"
00064 #include "libtorrent/storage.hpp"
00065 #include "libtorrent/stat.hpp"
00066 #include "libtorrent/alert.hpp"
00067 #include "libtorrent/torrent_handle.hpp"
00068 #include "libtorrent/torrent.hpp"
00069 #include "libtorrent/allocate_resources.hpp"
00070 #include "libtorrent/peer_request.hpp"
00071 #include "libtorrent/piece_block_progress.hpp"
00072 #include "libtorrent/config.hpp"
00073 #include "libtorrent/session.hpp"
00074 #include "libtorrent/bandwidth_manager.hpp"
00075
00076
00077
00078
00079
00080 namespace libtorrent
00081 {
00082 class torrent;
00083 struct peer_plugin;
00084
00085 namespace detail
00086 {
00087 struct session_impl;
00088 }
00089
00090 TORRENT_EXPORT void intrusive_ptr_add_ref(peer_connection const*);
00091 TORRENT_EXPORT void intrusive_ptr_release(peer_connection const*);
00092
00093 struct TORRENT_EXPORT protocol_error: std::runtime_error
00094 {
00095 protocol_error(const std::string& msg): std::runtime_error(msg) {};
00096 };
00097
00098 class TORRENT_EXPORT peer_connection
00099 : public boost::noncopyable
00100 {
00101 friend class invariant_access;
00102 friend void intrusive_ptr_add_ref(peer_connection const*);
00103 friend void intrusive_ptr_release(peer_connection const*);
00104 public:
00105
00106 enum channels
00107 {
00108 upload_channel,
00109 download_channel,
00110 num_channels
00111 };
00112
00113
00114
00115
00116 peer_connection(
00117 aux::session_impl& ses
00118 , boost::weak_ptr<torrent> t
00119 , boost::shared_ptr<stream_socket> s
00120 , tcp::endpoint const& remote
00121 , tcp::endpoint const& proxy);
00122
00123
00124
00125 peer_connection(
00126 aux::session_impl& ses
00127 , boost::shared_ptr<stream_socket> s);
00128
00129 virtual ~peer_connection();
00130
00131 #ifndef TORRENT_DISABLE_EXTENSIONS
00132 void add_extension(boost::shared_ptr<peer_plugin>);
00133 #endif
00134
00135
00136
00137
00138
00139 void init();
00140
00141
00142
00143 virtual void on_metadata() {}
00144
00145 void set_upload_limit(int limit);
00146 void set_download_limit(int limit);
00147
00148 bool prefer_whole_pieces() const
00149 { return m_prefer_whole_pieces; }
00150
00151 void prefer_whole_pieces(bool b)
00152 { m_prefer_whole_pieces = b; }
00153
00154 bool request_large_blocks() const
00155 { return m_request_large_blocks; }
00156
00157 void request_large_blocks(bool b)
00158 { m_request_large_blocks = b; }
00159
00160 void set_non_prioritized(bool b)
00161 { m_non_prioritized = b; }
00162
00163
00164
00165 void announce_piece(int index);
00166
00167
00168
00169 bool can_write() const;
00170 bool can_read() const;
00171
00172 bool is_seed() const;
00173
00174 bool has_timed_out() const;
00175
00176
00177 void keep_alive();
00178
00179 peer_id const& pid() const { return m_peer_id; }
00180 void set_pid(const peer_id& pid) { m_peer_id = pid; }
00181 bool has_piece(int i) const;
00182
00183 const std::deque<piece_block>& download_queue() const;
00184 const std::deque<piece_block>& request_queue() const;
00185 const std::deque<peer_request>& upload_queue() const;
00186
00187 bool is_interesting() const { return m_interesting; }
00188 bool is_choked() const { return m_choked; }
00189
00190 bool is_peer_interested() const { return m_peer_interested; }
00191 bool has_peer_choked() const { return m_peer_choked; }
00192
00193
00194
00195
00196
00197 boost::weak_ptr<torrent> associated_torrent() const
00198 { return m_torrent; }
00199
00200 const stat& statistics() const { return m_statistics; }
00201 void add_stat(size_type downloaded, size_type uploaded);
00202
00203
00204 void second_tick(float tick_interval);
00205
00206 boost::shared_ptr<stream_socket> get_socket() const { return m_socket; }
00207 tcp::endpoint const& remote() const { return m_remote; }
00208 tcp::endpoint const& proxy() const { return m_remote_proxy; }
00209
00210 std::vector<bool> const& get_bitfield() const;
00211
00212
00213 void disconnect();
00214 bool is_disconnecting() const { return m_disconnecting; }
00215
00216
00217
00218
00219 void on_connection_complete(asio::error_code const& e);
00220
00221
00222
00223 bool is_connecting() const { return m_connecting; }
00224
00225
00226
00227
00228 bool is_queued() const { return m_queued; }
00229
00230
00231
00232
00233
00234 void connect();
00235
00236
00237
00238
00239 void reset_upload_quota();
00240
00241
00242 size_type total_free_upload() const;
00243 void add_free_upload(size_type free_upload);
00244
00245
00246 void received_valid_data(int index);
00247 void received_invalid_data(int index);
00248 int trust_points() const;
00249
00250 size_type share_diff() const;
00251
00252
00253
00254 bool is_local() const { return m_active; }
00255
00256 void set_failed() { m_failed = true; }
00257 bool failed() const { return m_failed; }
00258
00259 int desired_queue_size() const { return m_desired_queue_size; }
00260
00261 #ifdef TORRENT_VERBOSE_LOGGING
00262 boost::shared_ptr<logger> m_logger;
00263 #endif
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 void incoming_keepalive();
00274 void incoming_choke();
00275 void incoming_unchoke();
00276 void incoming_interested();
00277 void incoming_not_interested();
00278 void incoming_have(int piece_index);
00279 void incoming_bitfield(std::vector<bool> const& bitfield);
00280 void incoming_request(peer_request const& r);
00281 void incoming_piece(peer_request const& p, char const* data);
00282 void incoming_piece_fragment();
00283 void incoming_cancel(peer_request const& r);
00284 void incoming_dht_port(int listen_port);
00285
00286
00287
00288 void send_choke();
00289 void send_unchoke();
00290 void send_interested();
00291 void send_not_interested();
00292
00293
00294 void add_request(piece_block const& b);
00295
00296
00297
00298
00299 void cancel_request(piece_block const& b);
00300 void send_block_requests();
00301
00302 int max_assignable_bandwidth(int channel) const
00303 {
00304 return m_bandwidth_limit[channel].max_assignable();
00305 }
00306
00307 void assign_bandwidth(int channel, int amount);
00308 void expire_bandwidth(int channel, int amount);
00309
00310 #ifndef NDEBUG
00311 void check_invariant() const;
00312 boost::posix_time::ptime m_last_choke;
00313 #endif
00314
00315 virtual void get_peer_info(peer_info& p) const = 0;
00316
00317
00318
00319 virtual bool in_handshake() const = 0;
00320
00321
00322
00323
00324
00325
00326 virtual boost::optional<piece_block_progress>
00327 downloading_piece_progress() const
00328 {
00329 #ifdef TORRENT_VERBOSE_LOGGING
00330 (*m_logger) << "downloading_piece_progress() dispatched to the base class!\n";
00331 #endif
00332 return boost::optional<piece_block_progress>();
00333 }
00334
00335 void send_buffer(char const* begin, char const* end);
00336 buffer::interval allocate_send_buffer(int size);
00337 void setup_send();
00338
00339 void set_country(char const* c)
00340 {
00341 assert(strlen(c) == 2);
00342 m_country[0] = c[0];
00343 m_country[1] = c[1];
00344 }
00345 bool has_country() const { return m_country[0] != 0; }
00346
00347 protected:
00348
00349 virtual void write_choke() = 0;
00350 virtual void write_unchoke() = 0;
00351 virtual void write_interested() = 0;
00352 virtual void write_not_interested() = 0;
00353 virtual void write_request(peer_request const& r) = 0;
00354 virtual void write_cancel(peer_request const& r) = 0;
00355 virtual void write_have(int index) = 0;
00356 virtual void write_keepalive() = 0;
00357 virtual void write_piece(peer_request const& r) = 0;
00358
00359 virtual void on_connected() = 0;
00360 virtual void on_tick() {}
00361
00362 virtual void on_receive(asio::error_code const& error
00363 , std::size_t bytes_transferred) = 0;
00364 virtual void on_sent(asio::error_code const& error
00365 , std::size_t bytes_transferred) = 0;
00366
00367 int send_buffer_size() const
00368 {
00369 return (int)m_send_buffer[0].size()
00370 + (int)m_send_buffer[1].size()
00371 - m_write_pos;
00372 }
00373
00374 buffer::const_interval receive_buffer() const
00375 {
00376 return buffer::const_interval(&m_recv_buffer[0]
00377 , &m_recv_buffer[0] + m_recv_pos);
00378 }
00379
00380 void cut_receive_buffer(int size, int packet_size);
00381
00382 void reset_recv_buffer(int packet_size);
00383 int packet_size() const { return m_packet_size; }
00384
00385 bool packet_finished() const
00386 {
00387 assert(m_recv_pos <= m_packet_size);
00388 return m_packet_size <= m_recv_pos;
00389 }
00390
00391 void setup_receive();
00392
00393 void attach_to_torrent(sha1_hash const& ih);
00394
00395 bool verify_piece(peer_request const& p) const;
00396
00397
00398
00399 bandwidth_limit m_bandwidth_limit[num_channels];
00400
00401
00402
00403
00404 stat m_statistics;
00405
00406
00407
00408 aux::session_impl& m_ses;
00409
00410 boost::intrusive_ptr<peer_connection> self()
00411 { return boost::intrusive_ptr<peer_connection>(this); }
00412
00413
00414
00415 void on_send_data(asio::error_code const& error
00416 , std::size_t bytes_transferred);
00417 void on_receive_data(asio::error_code const& error
00418 , std::size_t bytes_transferred);
00419
00420
00421
00422
00423
00424
00425
00426 int m_max_out_request_queue;
00427
00428 void set_timeout(int s) { m_timeout = s; }
00429
00430 #ifndef TORRENT_DISABLE_EXTENSIONS
00431 typedef std::list<boost::shared_ptr<peer_plugin> > extension_list_t;
00432 extension_list_t m_extensions;
00433 #endif
00434
00435
00436
00437
00438
00439 char m_country[2];
00440
00441 private:
00442
00443 void fill_send_buffer();
00444
00445
00446 int m_timeout;
00447
00448
00449
00450 boost::posix_time::ptime m_last_piece;
00451
00452 int m_packet_size;
00453 int m_recv_pos;
00454 std::vector<char> m_recv_buffer;
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465 std::vector<char> m_send_buffer[2];
00466
00467
00468
00469 int m_current_send_buffer;
00470
00471
00472
00473
00474 int m_write_pos;
00475
00476
00477 boost::posix_time::ptime m_last_receive;
00478 boost::posix_time::ptime m_last_sent;
00479
00480 boost::shared_ptr<stream_socket> m_socket;
00481
00482
00483
00484 tcp::endpoint m_remote;
00485
00486
00487 tcp::endpoint m_remote_proxy;
00488
00489
00490
00491
00492
00493
00494 boost::weak_ptr<torrent> m_torrent;
00495
00496
00497
00498 bool m_active;
00499
00500
00501 peer_id m_peer_id;
00502
00503
00504
00505 bool m_peer_interested;
00506
00507
00508
00509 bool m_peer_choked;
00510
00511
00512 bool m_interesting;
00513
00514
00515 bool m_choked;
00516
00517
00518
00519
00520
00521 bool m_failed;
00522
00523
00524 std::vector<bool> m_have_piece;
00525
00526
00527
00528
00529
00530 int m_num_pieces;
00531
00532
00533
00534 std::deque<peer_request> m_requests;
00535
00536
00537
00538 std::deque<piece_block> m_request_queue;
00539
00540
00541
00542 std::deque<piece_block> m_download_queue;
00543
00544
00545
00546 int m_desired_queue_size;
00547
00548
00549
00550
00551
00552
00553
00554 size_type m_free_upload;
00555
00556
00557
00558
00559
00560
00561
00562 int m_trust_points;
00563
00564
00565
00566
00567
00568
00569 bool m_assume_fifo;
00570
00571
00572
00573
00574
00575
00576
00577
00578 int m_num_invalid_requests;
00579
00580
00581
00582 bool m_disconnecting;
00583
00584
00585
00586 boost::posix_time::ptime m_became_uninterested;
00587
00588
00589
00590 boost::posix_time::ptime m_became_uninteresting;
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600 bool m_connecting;
00601
00602
00603
00604
00605 bool m_queued;
00606
00607
00608
00609 bool m_writing;
00610 bool m_reading;
00611
00612
00613
00614
00615
00616
00617 bool m_prefer_whole_pieces;
00618
00619
00620
00621
00622
00623
00624
00625 bool m_request_large_blocks;
00626
00627
00628
00629
00630
00631 bool m_non_prioritized;
00632
00633
00634 mutable boost::detail::atomic_count m_refs;
00635
00636 int m_upload_limit;
00637 int m_download_limit;
00638
00639 #ifndef NDEBUG
00640 public:
00641 bool m_in_constructor;
00642 #endif
00643 };
00644 }
00645
00646 #endif // TORRENT_PEER_CONNECTION_HPP_INCLUDED
00647