2016-01-31 03:33:47 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2016, 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 BITTORRENT_PEER_HPP
|
|
|
|
#define BITTORRENT_PEER_HPP
|
|
|
|
|
|
|
|
#include "libtorrent/socket.hpp"
|
|
|
|
#include "libtorrent/sha1_hash.hpp"
|
|
|
|
#include "libtorrent/io_service.hpp"
|
|
|
|
#include "libtorrent/time.hpp"
|
|
|
|
#include "libtorrent/address.hpp"
|
|
|
|
#include "libtorrent/torrent_info.hpp"
|
|
|
|
#include "test.hpp" // for EXPORT
|
2016-08-13 03:31:55 +02:00
|
|
|
#include <functional>
|
2017-04-02 20:22:17 +02:00
|
|
|
#include <array>
|
2016-01-31 03:33:47 +01:00
|
|
|
|
|
|
|
struct EXPORT peer_conn
|
|
|
|
{
|
2017-04-02 20:22:17 +02:00
|
|
|
enum class peer_mode_t
|
2016-01-31 03:33:47 +01:00
|
|
|
{ uploader, downloader, idle };
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
peer_conn(lt::io_service& ios
|
2016-08-13 03:31:55 +02:00
|
|
|
, std::function<void(int, char const*, int)> on_msg
|
2017-04-12 20:05:53 +02:00
|
|
|
, lt::torrent_info const& ti
|
|
|
|
, lt::tcp::endpoint const& ep
|
2016-01-31 03:33:47 +01:00
|
|
|
, peer_mode_t mode);
|
|
|
|
|
|
|
|
void start_conn();
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
void on_connect(lt::error_code const& ec);
|
|
|
|
void on_handshake(char* h, lt::error_code const& ec, size_t bytes_transferred);
|
|
|
|
void on_handshake2(lt::error_code const& ec, size_t bytes_transferred);
|
2016-01-31 03:33:47 +01:00
|
|
|
void write_have_all();
|
2017-04-12 20:05:53 +02:00
|
|
|
void on_have_all_sent(lt::error_code const& ec, size_t bytes_transferred);
|
2016-01-31 03:33:47 +01:00
|
|
|
bool write_request();
|
2017-04-12 20:05:53 +02:00
|
|
|
void on_req_sent(char* m, lt::error_code const& ec, size_t bytes_transferred);
|
|
|
|
void close(char const* fmt, lt::error_code const& ec);
|
2016-01-31 03:33:47 +01:00
|
|
|
void work_download();
|
2017-04-12 20:05:53 +02:00
|
|
|
void on_msg_length(lt::error_code const& ec, size_t bytes_transferred);
|
|
|
|
void on_message(lt::error_code const& ec, size_t bytes_transferred);
|
2016-01-31 03:33:47 +01:00
|
|
|
bool verify_piece(int piece, int start, char const* ptr, int size);
|
|
|
|
void write_piece(int piece, int start, int length);
|
|
|
|
void write_have(int piece);
|
|
|
|
|
|
|
|
void abort();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
lt::tcp::socket s;
|
2017-04-02 20:22:17 +02:00
|
|
|
std::array<char, 100> write_buf_proto;
|
|
|
|
std::array<std::uint32_t, 17 * 1024 / 4> write_buffer;
|
|
|
|
std::array<char, 17 * 1024> buffer;
|
2016-05-05 23:09:11 +02:00
|
|
|
|
2017-04-02 20:22:17 +02:00
|
|
|
peer_mode_t const m_mode;
|
2017-04-12 20:05:53 +02:00
|
|
|
lt::torrent_info const& m_ti;
|
2016-01-31 03:33:47 +01:00
|
|
|
|
2017-04-02 20:22:17 +02:00
|
|
|
int read_pos = 0;
|
2016-01-31 03:33:47 +01:00
|
|
|
|
2016-08-13 03:31:55 +02:00
|
|
|
std::function<void(int, char const*, int)> m_on_msg;
|
2016-01-31 03:33:47 +01:00
|
|
|
|
|
|
|
std::vector<int> pieces;
|
|
|
|
std::vector<int> suggested_pieces;
|
|
|
|
std::vector<int> allowed_fast;
|
2017-04-02 20:22:17 +02:00
|
|
|
bool choked = true;
|
|
|
|
int current_piece = -1; // the piece we're currently requesting blocks from
|
|
|
|
bool m_current_piece_is_allowed = false;
|
|
|
|
int block = 0;
|
2016-01-31 03:33:47 +01:00
|
|
|
int const m_blocks_per_piece;
|
2017-04-02 20:22:17 +02:00
|
|
|
int outstanding_requests = 0;
|
2016-01-31 03:33:47 +01:00
|
|
|
// if this is true, this connection is a seed
|
2017-04-02 20:22:17 +02:00
|
|
|
bool fast_extension = false;
|
|
|
|
int blocks_received = 0;
|
|
|
|
int blocks_sent = 0;
|
2017-04-12 20:05:53 +02:00
|
|
|
lt::time_point start_time = lt::clock_type::now();
|
|
|
|
lt::time_point end_time;
|
|
|
|
lt::tcp::endpoint endpoint;
|
2017-04-02 20:22:17 +02:00
|
|
|
bool restarting = false;
|
2016-01-31 03:33:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|