2006-04-25 23:04:48 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2003-2018, Arvid Norberg
|
2006-04-25 23:04:48 +02:00
|
|
|
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>
|
2016-06-20 17:32:06 +02:00
|
|
|
#include <cstdint>
|
|
|
|
|
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"
|
2006-04-25 23:04:48 +02:00
|
|
|
#include "libtorrent/piece_block_progress.hpp"
|
2015-02-15 06:17:09 +01:00
|
|
|
#include "libtorrent/operations.hpp" // for operation_t enum
|
2017-01-17 03:51:49 +01:00
|
|
|
#include "libtorrent/aux_/vector.hpp"
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
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.
|
2016-06-19 08:32:12 +02:00
|
|
|
// The peer_connection should handshake and verify that the
|
2006-04-25 23:04:48 +02:00
|
|
|
// other end has the correct id
|
2014-07-14 06:32:41 +02:00
|
|
|
web_peer_connection(peer_connection_args const& pack
|
2014-12-31 23:05:34 +01:00
|
|
|
, web_seed_t& web);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2017-09-12 23:10:11 +02:00
|
|
|
void on_connected() override;
|
2014-06-15 20:02:59 +02:00
|
|
|
|
2017-09-12 23:10:11 +02:00
|
|
|
connection_type type() const override
|
2016-09-24 19:47:17 +02:00
|
|
|
{ return connection_type::url_seed; }
|
2009-11-02 21:43:38 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// called from the main loop when this connection has any
|
|
|
|
// work to do.
|
2017-09-12 23:10:11 +02:00
|
|
|
void on_receive(error_code const& error
|
2016-04-30 17:05:54 +02:00
|
|
|
, std::size_t bytes_transferred) override;
|
2015-12-14 01:44:42 +01:00
|
|
|
|
2016-04-30 17:05:54 +02:00
|
|
|
std::string const& url() const override { return m_url; }
|
2015-12-14 01:44:42 +01:00
|
|
|
|
2017-09-12 23:10:11 +02:00
|
|
|
void get_specific_peer_info(peer_info& p) const override;
|
|
|
|
void disconnect(error_code const& ec
|
2016-04-30 17:05:54 +02:00
|
|
|
, operation_t op, int error = 0) override;
|
2007-01-29 08:39:33 +01:00
|
|
|
|
2017-09-12 23:10:11 +02:00
|
|
|
void write_request(peer_request const& r) override;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2017-09-12 23:10:11 +02:00
|
|
|
bool received_invalid_data(piece_index_t index, bool single_peer) override;
|
2012-09-29 19:46:41 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
private:
|
|
|
|
|
2016-01-22 05:32:20 +01:00
|
|
|
void on_receive_padfile();
|
|
|
|
void incoming_payload(char const* buf, int len);
|
|
|
|
void incoming_zeroes(int len);
|
|
|
|
void handle_redirect(int bytes_left);
|
|
|
|
void handle_error(int bytes_left);
|
|
|
|
void maybe_harvest_piece();
|
2011-11-26 21:48:31 +01:00
|
|
|
|
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.
|
2016-09-04 18:31:02 +02:00
|
|
|
piece_block_progress downloading_piece_progress() const override;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2016-01-22 05:32:20 +01:00
|
|
|
void handle_padfile();
|
2014-06-15 20:02:59 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// this has one entry per http-request
|
|
|
|
// (might be more than the bt requests)
|
2016-01-22 05:32:20 +01:00
|
|
|
struct file_request_t
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
file_index_t file_index;
|
2016-01-22 05:32:20 +01:00
|
|
|
int length;
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t start;
|
2016-01-22 05:32:20 +01:00
|
|
|
};
|
|
|
|
std::deque<file_request_t> m_file_requests;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
std::string m_url;
|
2015-12-14 01:44:42 +01:00
|
|
|
|
2014-12-31 23:05:34 +01:00
|
|
|
web_seed_t* m_web;
|
2015-12-14 01:44:42 +01:00
|
|
|
|
2016-01-22 05:32:20 +01:00
|
|
|
// this is used for intermediate storage of pieces to be delivered to the
|
|
|
|
// bittorrent engine
|
|
|
|
// TODO: 3 if we make this be a disk_buffer_holder instead
|
|
|
|
// we would save a copy
|
2012-07-15 00:49:29 +02:00
|
|
|
// use allocate_disk_receive_buffer and release_disk_receive_buffer
|
2017-01-17 03:51:49 +01:00
|
|
|
aux::vector<char> m_piece;
|
2015-12-14 01:44:42 +01:00
|
|
|
|
2016-01-22 05:32:20 +01:00
|
|
|
// the number of bytes we've forwarded to the incoming_payload() function
|
|
|
|
// in the current HTTP response. used to know where in the buffer the
|
2007-05-11 20:40:22 +02:00
|
|
|
// next response starts
|
2016-01-22 05:32:20 +01:00
|
|
|
int m_received_body;
|
2009-04-11 22:45:14 +02:00
|
|
|
|
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
|
2016-01-22 05:32:20 +01:00
|
|
|
int m_chunk_pos;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
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;
|
2013-10-20 04:40:43 +02:00
|
|
|
|
|
|
|
// the number of responses we've received so far on
|
|
|
|
// this connection
|
|
|
|
int m_num_responses;
|
2006-04-25 23:04:48 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_WEB_PEER_CONNECTION_HPP_INCLUDED
|