2006-04-25 23:04:48 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2003, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_WEB_PEER_CONNECTION_HPP_INCLUDED
|
|
|
|
#define TORRENT_WEB_PEER_CONNECTION_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include <ctime>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
#include <deque>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <boost/smart_ptr.hpp>
|
|
|
|
#include <boost/weak_ptr.hpp>
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
#include <boost/array.hpp>
|
|
|
|
#include <boost/optional.hpp>
|
|
|
|
#include <boost/cstdint.hpp>
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2009-11-23 09:38:50 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2010-10-10 20:43:58 +02:00
|
|
|
#include "libtorrent/web_connection_base.hpp"
|
2009-11-23 09:38:50 +01:00
|
|
|
#include "libtorrent/disk_buffer_holder.hpp"
|
2006-04-25 23:04:48 +02:00
|
|
|
#include "libtorrent/torrent.hpp"
|
|
|
|
#include "libtorrent/piece_block_progress.hpp"
|
2008-01-31 09:24:01 +01:00
|
|
|
#include "libtorrent/http_parser.hpp"
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
class torrent;
|
|
|
|
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
struct session_impl;
|
|
|
|
}
|
|
|
|
|
2012-03-19 00:31:04 +01:00
|
|
|
class TORRENT_EXTRA_EXPORT web_peer_connection
|
2010-10-10 20:43:58 +02:00
|
|
|
: public web_connection_base
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
|
|
|
friend class invariant_access;
|
|
|
|
public:
|
|
|
|
|
|
|
|
// this is the constructor where the we are the active part.
|
|
|
|
// The peer_conenction should handshake and verify that the
|
|
|
|
// other end has the correct id
|
|
|
|
web_peer_connection(
|
2006-10-11 16:02:21 +02:00
|
|
|
aux::session_impl& ses
|
2006-04-25 23:04:48 +02:00
|
|
|
, boost::weak_ptr<torrent> t
|
2007-04-23 23:36:21 +02:00
|
|
|
, boost::shared_ptr<socket_type> s
|
2006-04-25 23:04:48 +02:00
|
|
|
, tcp::endpoint const& remote
|
2007-04-10 23:23:13 +02:00
|
|
|
, std::string const& url
|
2010-10-10 20:43:58 +02:00
|
|
|
, policy::peer* peerinfo
|
|
|
|
, std::string const& ext_auth
|
|
|
|
, web_seed_entry::headers_t const& ext_headers);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2009-11-02 21:43:38 +01:00
|
|
|
virtual int type() const { return peer_connection::url_seed_connection; }
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// called from the main loop when this connection has any
|
|
|
|
// work to do.
|
2008-05-03 18:05:42 +02:00
|
|
|
void on_receive(error_code const& error
|
2006-04-25 23:04:48 +02:00
|
|
|
, std::size_t bytes_transferred);
|
|
|
|
|
2011-11-25 11:56:33 +01:00
|
|
|
std::string const& url() const { return m_url; }
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-05-25 21:42:10 +02:00
|
|
|
virtual void get_specific_peer_info(peer_info& p) const;
|
2010-04-09 07:51:31 +02:00
|
|
|
virtual void disconnect(error_code const& ec, int error = 0);
|
2007-01-29 08:39:33 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void write_request(peer_request const& r);
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2011-11-26 21:48:31 +01:00
|
|
|
bool maybe_harvest_block();
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// returns the block currently being
|
|
|
|
// downloaded. And the progress of that
|
|
|
|
// block. If the peer isn't downloading
|
|
|
|
// a piece for the moment, the boost::optional
|
|
|
|
// will be invalid.
|
|
|
|
boost::optional<piece_block_progress> downloading_piece_progress() const;
|
|
|
|
|
|
|
|
// this has one entry per http-request
|
|
|
|
// (might be more than the bt requests)
|
|
|
|
std::deque<int> m_file_requests;
|
|
|
|
|
|
|
|
std::string m_url;
|
|
|
|
|
|
|
|
// this is used for intermediate storage of pieces
|
2009-04-11 22:45:14 +02:00
|
|
|
// that are received in more than one HTTP response
|
2006-04-25 23:04:48 +02:00
|
|
|
std::vector<char> m_piece;
|
2007-05-11 20:40:22 +02:00
|
|
|
|
|
|
|
// the number of bytes received in the current HTTP
|
|
|
|
// response. used to know where in the buffer the
|
|
|
|
// next response starts
|
2011-02-21 06:24:41 +01:00
|
|
|
size_type m_received_body;
|
2008-06-03 17:17:09 +02:00
|
|
|
|
|
|
|
// position in the current range response
|
2011-02-21 06:24:41 +01:00
|
|
|
size_type m_range_pos;
|
2009-04-11 22:45:14 +02:00
|
|
|
|
|
|
|
// the position in the current block
|
|
|
|
int m_block_pos;
|
2010-10-27 08:39:18 +02:00
|
|
|
|
|
|
|
// this is the offset inside the current receive
|
|
|
|
// buffer where the next chunk header will be.
|
|
|
|
// this is updated for each chunk header that's
|
|
|
|
// parsed. It does not necessarily point to a valid
|
|
|
|
// offset in the receive buffer, if we haven't received
|
|
|
|
// it yet. This offset never includes the HTTP header
|
2011-02-21 06:24:41 +01:00
|
|
|
size_type m_chunk_pos;
|
2010-10-27 08:39:18 +02:00
|
|
|
|
|
|
|
// this is the number of bytes we've already received
|
|
|
|
// from the next chunk header we're waiting for
|
|
|
|
int m_partial_chunk_header;
|
2006-04-25 23:04:48 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_WEB_PEER_CONNECTION_HPP_INCLUDED
|
|
|
|
|