00001 /* 00002 00003 Copyright (c) 2003, Arvid Norberg 00004 All rights reserved. 00005 00006 Redistribution and use in source and binary forms, with or without 00007 modification, are permitted provided that the following conditions 00008 are met: 00009 00010 * Redistributions of source code must retain the above copyright 00011 notice, this list of conditions and the following disclaimer. 00012 * Redistributions in binary form must reproduce the above copyright 00013 notice, this list of conditions and the following disclaimer in 00014 the documentation and/or other materials provided with the distribution. 00015 * Neither the name of the author nor the names of its 00016 contributors may be used to endorse or promote products derived 00017 from this software without specific prior written permission. 00018 00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00020 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00021 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00022 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00023 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00024 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00025 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00026 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00027 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00028 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00029 POSSIBILITY OF SUCH DAMAGE. 00030 00031 */ 00032 00033 #ifndef TORRENT_WEB_PEER_CONNECTION_HPP_INCLUDED 00034 #define TORRENT_WEB_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 00056 #ifdef _MSC_VER 00057 #pragma warning(pop) 00058 #endif 00059 00060 #include "libtorrent/buffer.hpp" 00061 #include "libtorrent/peer_connection.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 // parse_url 00074 #include "libtorrent/tracker_manager.hpp" 00075 // http_parser 00076 #include "libtorrent/http_tracker_connection.hpp" 00077 00078 namespace libtorrent 00079 { 00080 class torrent; 00081 00082 namespace detail 00083 { 00084 struct session_impl; 00085 } 00086 00087 class TORRENT_EXPORT web_peer_connection 00088 : public peer_connection 00089 { 00090 friend class invariant_access; 00091 public: 00092 00093 // this is the constructor where the we are the active part. 00094 // The peer_conenction should handshake and verify that the 00095 // other end has the correct id 00096 web_peer_connection( 00097 aux::session_impl& ses 00098 , boost::weak_ptr<torrent> t 00099 , boost::shared_ptr<stream_socket> s 00100 , tcp::endpoint const& remote 00101 , tcp::endpoint const& proxy 00102 , std::string const& url); 00103 00104 ~web_peer_connection(); 00105 00106 // called from the main loop when this connection has any 00107 // work to do. 00108 void on_sent(asio::error_code const& error 00109 , std::size_t bytes_transferred); 00110 void on_receive(asio::error_code const& error 00111 , std::size_t bytes_transferred); 00112 00113 std::string const& url() const { return m_url; } 00114 00115 virtual void get_peer_info(peer_info& p) const; 00116 virtual bool in_handshake() const; 00117 00118 // the following functions appends messages 00119 // to the send buffer 00120 void write_choke() {} 00121 void write_unchoke() {} 00122 void write_interested() {} 00123 void write_not_interested() {} 00124 void write_request(peer_request const& r); 00125 void write_cancel(peer_request const& r) {} 00126 void write_have(int index) {} 00127 void write_piece(peer_request const& r) {} 00128 void write_keepalive() {} 00129 void on_connected(); 00130 00131 #ifndef NDEBUG 00132 void check_invariant() const; 00133 #endif 00134 00135 private: 00136 00137 // returns the block currently being 00138 // downloaded. And the progress of that 00139 // block. If the peer isn't downloading 00140 // a piece for the moment, the boost::optional 00141 // will be invalid. 00142 boost::optional<piece_block_progress> downloading_piece_progress() const; 00143 00144 // this has one entry per bittorrent request 00145 std::deque<peer_request> m_requests; 00146 // this has one entry per http-request 00147 // (might be more than the bt requests) 00148 std::deque<int> m_file_requests; 00149 00150 std::string m_server_string; 00151 http_parser m_parser; 00152 std::string m_host; 00153 int m_port; 00154 std::string m_path; 00155 std::string m_url; 00156 00157 // the first request will contain a little bit more data 00158 // than subsequent ones, things that aren't critical are left 00159 // out to save bandwidth. 00160 bool m_first_request; 00161 00162 // this is used for intermediate storage of pieces 00163 // that is received in more than on HTTP responses 00164 std::vector<char> m_piece; 00165 // the mapping of the data in the m_piece buffer 00166 peer_request m_intermediate_piece; 00167 }; 00168 } 00169 00170 #endif // TORRENT_WEB_PEER_CONNECTION_HPP_INCLUDED 00171
1.5.6