2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
2017-03-26 20:20:40 +02:00
|
|
|
Copyright (c) 2003-2017, Arvid Norberg
|
2003-10-23 01:00:57 +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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <cstdio> // for snprintf
|
|
|
|
#include <cstdlib> // for atoi
|
|
|
|
#include <cstring>
|
2016-07-10 13:34:45 +02:00
|
|
|
#include <utility>
|
2016-09-07 05:37:36 +02:00
|
|
|
#include <deque>
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2007-03-02 02:16:59 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
|
|
|
|
2014-11-19 10:23:08 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2015-01-07 17:47:37 +01:00
|
|
|
#include <direct.h> // for _mkdir and _getcwd
|
2014-11-29 02:53:22 +01:00
|
|
|
#include <sys/types.h> // for _stat
|
|
|
|
#include <sys/stat.h>
|
2014-11-19 10:23:08 +01:00
|
|
|
#endif
|
|
|
|
|
2015-09-18 06:23:45 +02:00
|
|
|
#include "libtorrent/torrent_info.hpp"
|
|
|
|
#include "libtorrent/announce_entry.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
#include "libtorrent/entry.hpp"
|
|
|
|
#include "libtorrent/bencode.hpp"
|
|
|
|
#include "libtorrent/session.hpp"
|
2003-12-22 08:14:35 +01:00
|
|
|
#include "libtorrent/identify_client.hpp"
|
2004-02-01 01:55:28 +01:00
|
|
|
#include "libtorrent/alert_types.hpp"
|
2006-10-11 16:02:21 +02:00
|
|
|
#include "libtorrent/ip_filter.hpp"
|
2007-12-03 07:03:16 +01:00
|
|
|
#include "libtorrent/magnet_uri.hpp"
|
2009-11-26 06:45:43 +01:00
|
|
|
#include "libtorrent/peer_info.hpp"
|
2015-03-12 06:20:12 +01:00
|
|
|
#include "libtorrent/bdecode.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/add_torrent_params.hpp"
|
2010-08-23 10:48:02 +02:00
|
|
|
#include "libtorrent/time.hpp"
|
2016-02-18 06:52:54 +01:00
|
|
|
#include "libtorrent/read_resume_data.hpp"
|
2017-03-05 15:31:28 +01:00
|
|
|
#include "libtorrent/write_resume_data.hpp"
|
2017-03-18 17:19:49 +01:00
|
|
|
#include "libtorrent/string_view.hpp"
|
2017-05-03 05:45:43 +02:00
|
|
|
#include "libtorrent/disk_interface.hpp" // for open_file_state
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
#include "torrent_view.hpp"
|
2014-07-09 00:37:13 +02:00
|
|
|
#include "session_view.hpp"
|
2014-07-08 17:35:14 +02:00
|
|
|
#include "print.hpp"
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using lt::total_milliseconds;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2003-11-09 19:17:09 +01:00
|
|
|
|
2003-11-02 22:06:50 +01:00
|
|
|
#include <windows.h>
|
2003-10-30 00:28:09 +01:00
|
|
|
#include <conio.h>
|
|
|
|
|
2017-05-03 05:45:43 +02:00
|
|
|
bool sleep_and_input(int* c, lt::time_duration const sleep)
|
2003-10-30 00:28:09 +01:00
|
|
|
{
|
2011-06-06 06:24:12 +02:00
|
|
|
for (int i = 0; i < 2; ++i)
|
2003-10-30 00:28:09 +01:00
|
|
|
{
|
2009-05-13 18:02:43 +02:00
|
|
|
if (_kbhit())
|
|
|
|
{
|
|
|
|
*c = _getch();
|
|
|
|
return true;
|
|
|
|
}
|
2017-05-03 05:45:43 +02:00
|
|
|
std::this_thread::sleep_for(sleep / 2);
|
2003-10-30 00:28:09 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2003-10-31 12:17:45 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
#include <termios.h>
|
2007-07-23 01:24:27 +02:00
|
|
|
#include <sys/ioctl.h>
|
2016-07-10 05:17:55 +02:00
|
|
|
#include <csignal>
|
2016-07-10 16:03:49 +02:00
|
|
|
#include <utility>
|
2017-05-06 05:09:36 +02:00
|
|
|
#include <dirent.h>
|
2005-08-15 00:04:58 +02:00
|
|
|
|
2003-10-31 12:17:45 +01:00
|
|
|
struct set_keypress
|
|
|
|
{
|
2017-04-04 00:47:08 +02:00
|
|
|
enum terminal_mode {
|
|
|
|
echo = 1,
|
|
|
|
canonical = 2
|
|
|
|
};
|
|
|
|
|
|
|
|
explicit set_keypress(std::uint8_t const mode = 0)
|
2003-10-31 12:17:45 +01:00
|
|
|
{
|
2005-08-25 15:11:39 +02:00
|
|
|
termios new_settings;
|
2017-04-04 00:47:08 +02:00
|
|
|
tcgetattr(0, &stored_settings);
|
2005-08-25 15:11:39 +02:00
|
|
|
new_settings = stored_settings;
|
2003-10-31 12:17:45 +01:00
|
|
|
// Disable canonical mode, and set buffer size to 1 byte
|
2015-05-26 21:10:49 +02:00
|
|
|
// and disable echo
|
2017-04-04 00:47:08 +02:00
|
|
|
if (mode & echo) new_settings.c_lflag |= ECHO;
|
|
|
|
else new_settings.c_lflag &= ~ECHO;
|
|
|
|
|
|
|
|
if (mode & canonical) new_settings.c_lflag |= ICANON;
|
|
|
|
else new_settings.c_lflag &= ~ICANON;
|
|
|
|
|
2005-08-25 15:11:39 +02:00
|
|
|
new_settings.c_cc[VTIME] = 0;
|
|
|
|
new_settings.c_cc[VMIN] = 1;
|
|
|
|
tcsetattr(0,TCSANOW,&new_settings);
|
2003-10-31 12:17:45 +01:00
|
|
|
}
|
2017-04-04 00:47:08 +02:00
|
|
|
~set_keypress() { tcsetattr(0, TCSANOW, &stored_settings); }
|
|
|
|
private:
|
2005-08-25 15:11:39 +02:00
|
|
|
termios stored_settings;
|
2003-10-31 12:17:45 +01:00
|
|
|
};
|
|
|
|
|
2017-05-03 05:45:43 +02:00
|
|
|
bool sleep_and_input(int* c, lt::time_duration const sleep)
|
2003-10-31 12:17:45 +01:00
|
|
|
{
|
2017-05-03 05:45:43 +02:00
|
|
|
lt::time_point const done = lt::clock_type::now() + sleep;
|
2010-08-23 10:48:02 +02:00
|
|
|
int ret = 0;
|
|
|
|
retry:
|
2003-10-31 12:17:45 +01:00
|
|
|
fd_set set;
|
|
|
|
FD_ZERO(&set);
|
|
|
|
FD_SET(0, &set);
|
2017-05-03 05:45:43 +02:00
|
|
|
int const delay = total_milliseconds(done - lt::clock_type::now());
|
|
|
|
timeval tv = {delay / 1000, (delay % 1000) * 1000 };
|
2016-07-09 22:26:26 +02:00
|
|
|
ret = select(1, &set, nullptr, nullptr, &tv);
|
2010-08-23 10:48:02 +02:00
|
|
|
if (ret > 0)
|
2010-01-05 14:00:04 +01:00
|
|
|
{
|
2011-03-31 05:19:05 +02:00
|
|
|
*c = getc(stdin);
|
2010-01-05 14:00:04 +01:00
|
|
|
return true;
|
|
|
|
}
|
2010-08-23 10:48:02 +02:00
|
|
|
if (errno == EINTR)
|
|
|
|
{
|
2017-05-03 05:45:43 +02:00
|
|
|
if (lt::clock_type::now() < done)
|
2010-08-23 10:48:02 +02:00
|
|
|
goto retry;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0 && errno != 0 && errno != ETIMEDOUT)
|
2011-06-06 06:24:12 +02:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "select failed: %s\n", strerror(errno));
|
2017-03-26 20:20:40 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(500));
|
2011-06-06 06:24:12 +02:00
|
|
|
}
|
2010-08-23 10:48:02 +02:00
|
|
|
|
2003-10-31 12:17:45 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-10-30 00:28:09 +01:00
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-11-29 09:38:40 +01:00
|
|
|
bool print_trackers = false;
|
2008-04-05 06:53:22 +02:00
|
|
|
bool print_peers = false;
|
|
|
|
bool print_log = false;
|
|
|
|
bool print_downloads = false;
|
2016-03-12 19:01:34 +01:00
|
|
|
bool print_matrix = false;
|
2008-04-05 06:53:22 +02:00
|
|
|
bool print_file_progress = false;
|
2009-01-13 18:49:14 +01:00
|
|
|
bool show_pad_files = false;
|
2008-09-20 19:42:25 +02:00
|
|
|
bool show_dht_status = false;
|
2008-04-05 06:53:22 +02:00
|
|
|
bool sequential_download = false;
|
|
|
|
|
|
|
|
bool print_ip = true;
|
|
|
|
bool print_timers = false;
|
|
|
|
bool print_block = false;
|
|
|
|
bool print_peer_rate = false;
|
|
|
|
bool print_fails = false;
|
|
|
|
bool print_send_bufs = true;
|
2014-07-06 21:18:00 +02:00
|
|
|
bool print_disk_stats = false;
|
2009-05-13 03:02:06 +02:00
|
|
|
|
2011-07-04 07:33:06 +02:00
|
|
|
// the number of times we've asked to save resume data
|
|
|
|
// without having received a response (successful or failure)
|
|
|
|
int num_outstanding_resume_data = 0;
|
|
|
|
|
2015-01-17 18:02:58 +01:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2017-04-12 20:05:53 +02:00
|
|
|
std::vector<lt::dht_lookup> dht_active_requests;
|
|
|
|
std::vector<lt::dht_routing_bucket> dht_routing_table;
|
2015-01-17 18:02:58 +01:00
|
|
|
#endif
|
|
|
|
|
2016-06-04 16:01:43 +02:00
|
|
|
std::string to_hex(lt::sha1_hash const& s)
|
|
|
|
{
|
|
|
|
std::stringstream ret;
|
|
|
|
ret << s;
|
|
|
|
return ret.str();
|
|
|
|
}
|
|
|
|
|
2014-07-09 00:37:13 +02:00
|
|
|
int load_file(std::string const& filename, std::vector<char>& v
|
2017-04-12 20:05:53 +02:00
|
|
|
, lt::error_code& ec, int limit = 8000000)
|
2013-07-19 23:41:33 +02:00
|
|
|
{
|
|
|
|
ec.clear();
|
2016-05-17 15:24:06 +02:00
|
|
|
FILE* f = std::fopen(filename.c_str(), "rb");
|
2016-06-20 17:32:06 +02:00
|
|
|
if (f == nullptr)
|
2013-07-19 23:41:33 +02:00
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, boost::system::system_category());
|
2013-07-19 23:41:33 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int r = fseek(f, 0, SEEK_END);
|
|
|
|
if (r != 0)
|
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, boost::system::system_category());
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fclose(f);
|
2013-07-19 23:41:33 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
long s = ftell(f);
|
|
|
|
if (s < 0)
|
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, boost::system::system_category());
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fclose(f);
|
2013-07-19 23:41:33 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s > limit)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fclose(f);
|
2013-07-19 23:41:33 +02:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = fseek(f, 0, SEEK_SET);
|
|
|
|
if (r != 0)
|
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, boost::system::system_category());
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fclose(f);
|
2013-07-19 23:41:33 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
v.resize(s);
|
|
|
|
if (s == 0)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fclose(f);
|
2013-07-19 23:41:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
r = int(std::fread(&v[0], 1, v.size(), f));
|
2013-07-19 23:41:33 +02:00
|
|
|
if (r < 0)
|
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, boost::system::system_category());
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fclose(f);
|
2013-07-19 23:41:33 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fclose(f);
|
2013-07-19 23:41:33 +02:00
|
|
|
|
|
|
|
if (r != s) return -3;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-17 04:41:01 +01:00
|
|
|
bool is_absolute_path(std::string const& f)
|
|
|
|
{
|
|
|
|
if (f.empty()) return false;
|
|
|
|
#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2)
|
|
|
|
int i = 0;
|
|
|
|
// match the xx:\ or xx:/ form
|
|
|
|
while (f[i] && strchr("abcdefghijklmnopqrstuvxyz", f[i])) ++i;
|
|
|
|
if (i < int(f.size()-1) && f[i] == ':' && (f[i+1] == '\\' || f[i+1] == '/'))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// match the \\ form
|
|
|
|
if (int(f.size()) >= 2 && f[0] == '\\' && f[1] == '\\')
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
if (f[0] == '/') return true;
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string path_append(std::string const& lhs, std::string const& rhs)
|
|
|
|
{
|
|
|
|
if (lhs.empty() || lhs == ".") return rhs;
|
|
|
|
if (rhs.empty() || rhs == ".") return lhs;
|
|
|
|
|
|
|
|
#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2)
|
|
|
|
#define TORRENT_SEPARATOR "\\"
|
|
|
|
bool need_sep = lhs[lhs.size()-1] != '\\' && lhs[lhs.size()-1] != '/';
|
|
|
|
#else
|
|
|
|
#define TORRENT_SEPARATOR "/"
|
|
|
|
bool need_sep = lhs[lhs.size()-1] != '/';
|
|
|
|
#endif
|
|
|
|
return lhs + (need_sep?TORRENT_SEPARATOR:"") + rhs;
|
|
|
|
}
|
|
|
|
|
2017-03-26 15:16:42 +02:00
|
|
|
std::string make_absolute_path(std::string const& p)
|
|
|
|
{
|
|
|
|
if (is_absolute_path(p)) return p;
|
|
|
|
std::string ret;
|
|
|
|
#if defined TORRENT_WINDOWS
|
|
|
|
char* cwd = ::_getcwd(nullptr, 0);
|
|
|
|
ret = path_append(cwd, p);
|
|
|
|
std::free(cwd);
|
|
|
|
#else
|
|
|
|
char* cwd = ::getcwd(nullptr, 0);
|
|
|
|
ret = path_append(cwd, p);
|
|
|
|
std::free(cwd);
|
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
std::string print_endpoint(lt::tcp::endpoint const& ep)
|
2013-07-20 03:02:15 +02:00
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2013-07-20 03:02:15 +02:00
|
|
|
error_code ec;
|
|
|
|
char buf[200];
|
|
|
|
address const& addr = ep.address();
|
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
if (addr.is_v6())
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(buf, sizeof(buf), "[%s]:%d", addr.to_string(ec).c_str(), ep.port());
|
2013-07-20 03:02:15 +02:00
|
|
|
else
|
|
|
|
#endif
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(buf, sizeof(buf), "%s:%d", addr.to_string(ec).c_str(), ep.port());
|
2013-07-20 03:02:15 +02:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using lt::torrent_status;
|
2010-11-06 17:57:15 +01:00
|
|
|
|
2016-07-09 22:26:26 +02:00
|
|
|
FILE* g_log_file = nullptr;
|
2011-12-23 06:36:13 +01:00
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
int peer_index(lt::tcp::endpoint addr, std::vector<lt::peer_info> const& peers)
|
2005-08-15 00:04:58 +02:00
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2016-05-25 06:31:52 +02:00
|
|
|
std::vector<peer_info>::const_iterator i = std::find_if(peers.begin(), peers.end()
|
|
|
|
, [&addr](peer_info const& pi) { return pi.ip == addr; });
|
2007-01-10 16:02:25 +01:00
|
|
|
if (i == peers.end()) return -1;
|
2005-08-15 00:04:58 +02:00
|
|
|
|
2016-04-25 23:22:09 +02:00
|
|
|
return int(i - peers.begin());
|
2005-08-15 00:04:58 +02:00
|
|
|
}
|
|
|
|
|
2015-02-08 04:24:56 +01:00
|
|
|
// returns the number of lines printed
|
|
|
|
int print_peer_info(std::string& out
|
2017-04-12 20:05:53 +02:00
|
|
|
, std::vector<lt::peer_info> const& peers, int max_lines)
|
2004-07-24 13:54:17 +02:00
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2015-02-08 04:24:56 +01:00
|
|
|
int pos = 0;
|
2014-07-06 21:18:00 +02:00
|
|
|
if (print_ip) out += "IP ";
|
|
|
|
out += "progress down (total | peak ) up (total | peak ) sent-req tmo bsy rcv flags dn up source ";
|
2009-05-13 03:02:06 +02:00
|
|
|
if (print_fails) out += "fail hshf ";
|
2016-06-27 02:41:03 +02:00
|
|
|
if (print_send_bufs) out += "rq sndb (recvb |alloc | wmrk ) q-bytes ";
|
2009-05-13 03:02:06 +02:00
|
|
|
if (print_timers) out += "inactive wait timeout q-time ";
|
2014-11-24 05:35:52 +01:00
|
|
|
out += " v disk ^ rtt ";
|
2009-05-13 03:02:06 +02:00
|
|
|
if (print_block) out += "block-progress ";
|
2016-09-01 15:43:08 +02:00
|
|
|
if (print_peer_rate) out += "est.rec.rate ";
|
2015-02-08 04:24:56 +01:00
|
|
|
out += "client \x1b[K\n";
|
|
|
|
++pos;
|
2004-07-24 13:54:17 +02:00
|
|
|
|
2009-05-15 18:21:36 +02:00
|
|
|
char str[500];
|
2004-07-24 13:54:17 +02:00
|
|
|
for (std::vector<peer_info>::const_iterator i = peers.begin();
|
2005-05-25 12:01:01 +02:00
|
|
|
i != peers.end(); ++i)
|
2004-07-24 13:54:17 +02:00
|
|
|
{
|
2014-10-05 21:32:29 +02:00
|
|
|
if (i->flags & (peer_info::handshake | peer_info::connecting))
|
2007-03-15 23:03:56 +01:00
|
|
|
continue;
|
|
|
|
|
2008-04-05 06:53:22 +02:00
|
|
|
if (print_ip)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(str, sizeof(str), "%-30s ", (::print_endpoint(i->ip) +
|
2013-10-27 20:56:37 +01:00
|
|
|
(i->flags & peer_info::utp_socket ? " [uTP]" : "") +
|
|
|
|
(i->flags & peer_info::i2p_socket ? " [i2p]" : "")
|
2014-07-06 21:18:00 +02:00
|
|
|
).c_str());
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
|
|
|
|
2011-06-06 06:24:12 +02:00
|
|
|
char temp[10];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(temp, sizeof(temp), "%d/%d"
|
2011-06-06 06:24:12 +02:00
|
|
|
, i->download_queue_length
|
|
|
|
, i->target_dl_queue_length);
|
|
|
|
temp[7] = 0;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
char peer_progress[10];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(peer_progress, sizeof(peer_progress), "%.1f%%", i->progress_ppm / 10000.f);
|
|
|
|
std::snprintf(str, sizeof(str)
|
2014-07-06 21:18:00 +02:00
|
|
|
, "%s %s%s (%s|%s) %s%s (%s|%s) %s%7s %4d%4d%4d %s%s%s%s%s%s%s%s%s%s%s%s%s %s%s%s %s%s%s %s%s%s%s%s%s "
|
|
|
|
, progress_bar(i->progress_ppm / 1000, 15, col_green, '#', '-', peer_progress).c_str()
|
2009-05-13 03:02:06 +02:00
|
|
|
, esc("32"), add_suffix(i->down_speed, "/s").c_str()
|
|
|
|
, add_suffix(i->total_download).c_str(), add_suffix(i->download_rate_peak, "/s").c_str()
|
|
|
|
, esc("31"), add_suffix(i->up_speed, "/s").c_str(), add_suffix(i->total_upload).c_str()
|
2009-05-15 18:21:36 +02:00
|
|
|
, add_suffix(i->upload_rate_peak, "/s").c_str(), esc("0")
|
|
|
|
|
2011-06-06 06:24:12 +02:00
|
|
|
, temp // sent requests and target number of outstanding reqs.
|
|
|
|
, i->timed_out_requests
|
|
|
|
, i->busy_requests
|
2009-05-15 18:21:36 +02:00
|
|
|
, i->upload_queue_length
|
2009-05-13 03:02:06 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
, color("I", (i->flags & peer_info::interesting)?col_white:col_blue).c_str()
|
|
|
|
, color("C", (i->flags & peer_info::choked)?col_white:col_blue).c_str()
|
|
|
|
, color("i", (i->flags & peer_info::remote_interested)?col_white:col_blue).c_str()
|
|
|
|
, color("c", (i->flags & peer_info::remote_choked)?col_white:col_blue).c_str()
|
|
|
|
, color("x", (i->flags & peer_info::supports_extensions)?col_white:col_blue).c_str()
|
|
|
|
, color("o", (i->flags & peer_info::local_connection)?col_white:col_blue).c_str()
|
|
|
|
, color("p", (i->flags & peer_info::on_parole)?col_white:col_blue).c_str()
|
|
|
|
, color("O", (i->flags & peer_info::optimistic_unchoke)?col_white:col_blue).c_str()
|
|
|
|
, color("S", (i->flags & peer_info::snubbed)?col_white:col_blue).c_str()
|
|
|
|
, color("U", (i->flags & peer_info::upload_only)?col_white:col_blue).c_str()
|
|
|
|
, color("e", (i->flags & peer_info::endgame_mode)?col_white:col_blue).c_str()
|
|
|
|
, color("E", (i->flags & peer_info::rc4_encrypted)?col_white:(i->flags & peer_info::plaintext_encrypted)?col_cyan:col_blue).c_str()
|
|
|
|
, color("h", (i->flags & peer_info::holepunched)?col_white:col_blue).c_str()
|
|
|
|
|
|
|
|
, color("d", (i->read_state & peer_info::bw_disk)?col_white:col_blue).c_str()
|
|
|
|
, color("l", (i->read_state & peer_info::bw_limit)?col_white:col_blue).c_str()
|
|
|
|
, color("n", (i->read_state & peer_info::bw_network)?col_white:col_blue).c_str()
|
|
|
|
, color("d", (i->write_state & peer_info::bw_disk)?col_white:col_blue).c_str()
|
|
|
|
, color("l", (i->write_state & peer_info::bw_limit)?col_white:col_blue).c_str()
|
|
|
|
, color("n", (i->write_state & peer_info::bw_network)?col_white:col_blue).c_str()
|
|
|
|
|
|
|
|
, color("t", (i->source & peer_info::tracker)?col_white:col_blue).c_str()
|
|
|
|
, color("p", (i->source & peer_info::pex)?col_white:col_blue).c_str()
|
|
|
|
, color("d", (i->source & peer_info::dht)?col_white:col_blue).c_str()
|
|
|
|
, color("l", (i->source & peer_info::lsd)?col_white:col_blue).c_str()
|
|
|
|
, color("r", (i->source & peer_info::resume_data)?col_white:col_blue).c_str()
|
|
|
|
, color("i", (i->source & peer_info::incoming)?col_white:col_blue).c_str());
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
|
|
|
|
2008-04-05 06:53:22 +02:00
|
|
|
if (print_fails)
|
2004-07-24 13:54:17 +02:00
|
|
|
{
|
2017-05-07 22:18:14 +02:00
|
|
|
std::snprintf(str, sizeof(str), "%4d %4d "
|
2009-05-13 03:02:06 +02:00
|
|
|
, i->failcount, i->num_hashfails);
|
|
|
|
out += str;
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
2008-04-05 06:53:22 +02:00
|
|
|
if (print_send_bufs)
|
|
|
|
{
|
2016-06-27 02:41:03 +02:00
|
|
|
std::snprintf(str, sizeof(str), "%2d %6d %6d|%6d|%6d%5dkB "
|
2014-07-06 21:18:00 +02:00
|
|
|
, i->requests_in_buffer, i->used_send_buffer
|
|
|
|
, i->used_receive_buffer
|
2016-06-27 02:41:03 +02:00
|
|
|
, i->receive_buffer_size
|
|
|
|
, i->receive_buffer_watermark
|
2014-07-06 21:18:00 +02:00
|
|
|
, i->queue_bytes / 1000);
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
|
|
|
if (print_timers)
|
2005-05-30 19:43:03 +02:00
|
|
|
{
|
2014-12-08 18:06:04 +01:00
|
|
|
char req_timeout[20] = "-";
|
|
|
|
// timeout is only meaningful if there is at least one outstanding
|
|
|
|
// request to the peer
|
|
|
|
if (i->download_queue_length > 0)
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(req_timeout, sizeof(req_timeout), "%d", i->request_timeout);
|
2014-12-08 18:06:04 +01:00
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(str, sizeof(str), "%8d %4d %7s %6d "
|
2014-07-06 21:18:00 +02:00
|
|
|
, int(total_seconds(i->last_active))
|
|
|
|
, int(total_seconds(i->last_request))
|
2014-12-08 18:06:04 +01:00
|
|
|
, req_timeout
|
2014-07-06 21:18:00 +02:00
|
|
|
, int(total_seconds(i->download_queue_time)));
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(str, sizeof(str), "%s|%s %5d "
|
2009-05-13 03:02:06 +02:00
|
|
|
, add_suffix(i->pending_disk_bytes).c_str()
|
2014-07-06 21:18:00 +02:00
|
|
|
, add_suffix(i->pending_disk_read_bytes).c_str()
|
2009-05-13 03:02:06 +02:00
|
|
|
, i->rtt);
|
|
|
|
out += str;
|
2008-04-05 06:53:22 +02:00
|
|
|
|
|
|
|
if (print_block)
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
if (i->downloading_piece_index >= piece_index_t(0))
|
2008-04-05 06:53:22 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
char buf[50];
|
2016-12-22 16:42:33 +01:00
|
|
|
std::snprintf(buf, sizeof(buf), "%d:%d"
|
|
|
|
, static_cast<int>(i->downloading_piece_index), i->downloading_block_index);
|
2009-05-13 03:02:06 +02:00
|
|
|
out += progress_bar(
|
2014-07-06 21:18:00 +02:00
|
|
|
i->downloading_progress * 1000 / i->downloading_total, 14, col_green, '-', '#', buf);
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-19 06:59:27 +02:00
|
|
|
out += progress_bar(0, 14);
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
2005-05-30 19:43:03 +02:00
|
|
|
}
|
2004-07-24 13:54:17 +02:00
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
if (print_peer_rate)
|
|
|
|
{
|
2010-02-09 04:04:41 +01:00
|
|
|
bool unchoked = (i->flags & peer_info::choked) == 0;
|
|
|
|
|
2016-09-01 15:43:08 +02:00
|
|
|
std::snprintf(str, sizeof(str), " %s"
|
2010-02-09 04:04:41 +01:00
|
|
|
, unchoked ? add_suffix(i->estimated_reciprocation_rate, "/s").c_str() : " ");
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
|
|
|
}
|
|
|
|
out += " ";
|
2007-06-07 02:05:18 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (i->flags & peer_info::handshake)
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
out += esc("31");
|
|
|
|
out += " waiting for handshake";
|
|
|
|
out += esc("0");
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
else if (i->flags & peer_info::connecting)
|
2005-11-02 17:28:39 +01:00
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
out += esc("31");
|
|
|
|
out += " connecting to peer";
|
|
|
|
out += esc("0");
|
2005-11-02 17:28:39 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
out += " ";
|
|
|
|
out += i->client;
|
2005-11-02 17:28:39 +01:00
|
|
|
}
|
2015-02-08 04:24:56 +01:00
|
|
|
out += "\x1b[K\n";
|
|
|
|
++pos;
|
|
|
|
if (pos >= max_lines) break;
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
2015-02-08 04:24:56 +01:00
|
|
|
return pos;
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
int allocation_mode = lt::storage_mode_sparse;
|
2010-01-12 02:56:48 +01:00
|
|
|
std::string save_path(".");
|
|
|
|
int torrent_upload_limit = 0;
|
|
|
|
int torrent_download_limit = 0;
|
|
|
|
std::string monitor_dir;
|
|
|
|
int poll_interval = 5;
|
|
|
|
int max_connections_per_torrent = 50;
|
2011-08-07 08:34:27 +02:00
|
|
|
bool seed_mode = false;
|
2016-03-06 05:16:33 +01:00
|
|
|
int cache_size = 1024;
|
2010-01-12 02:56:48 +01:00
|
|
|
|
2010-09-05 18:01:36 +02:00
|
|
|
bool share_mode = false;
|
2011-07-10 22:17:32 +02:00
|
|
|
bool disable_storage = false;
|
2010-09-05 18:01:36 +02:00
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
bool quit = false;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-05-02 18:36:21 +02:00
|
|
|
void signal_handler(int)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
|
|
|
// make the main loop terminate
|
2014-07-08 17:35:14 +02:00
|
|
|
quit = true;
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
|
2012-01-14 17:04:25 +01:00
|
|
|
// if non-empty, a peer that will be added to all torrents
|
|
|
|
std::string peer;
|
|
|
|
|
2016-03-17 05:24:57 +01:00
|
|
|
void print_settings(int const start, int const num
|
|
|
|
, char const* const fmt)
|
|
|
|
{
|
|
|
|
for (int i = start; i < start + num; ++i)
|
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
char const* name = lt::name_for_setting(i);
|
2016-03-17 05:24:57 +01:00
|
|
|
if (!name || name[0] == '\0') continue;
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf(fmt, name);
|
2016-03-17 05:24:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 15:16:42 +02:00
|
|
|
std::string resume_file(lt::sha1_hash const& info_hash)
|
|
|
|
{
|
|
|
|
return path_append(save_path, path_append(".resume"
|
|
|
|
, to_hex(info_hash) + ".resume"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_magnet(lt::session& ses, lt::string_view uri)
|
|
|
|
{
|
|
|
|
lt::add_torrent_params p;
|
|
|
|
lt::error_code ec;
|
|
|
|
lt::parse_magnet_uri(uri.to_string(), p, ec);
|
|
|
|
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
std::printf("invalid magnet link \"%s\": %s\n"
|
|
|
|
, uri.to_string().c_str(), ec.message().c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<char> resume_data;
|
|
|
|
load_file(resume_file(p.info_hash), resume_data, ec);
|
|
|
|
if (!ec)
|
|
|
|
{
|
2017-03-29 22:10:32 +02:00
|
|
|
p = lt::read_resume_data(resume_data, ec);
|
2017-03-26 15:16:42 +02:00
|
|
|
if (ec) std::printf(" failed to load resume data: %s\n", ec.message().c_str());
|
|
|
|
parse_magnet_uri(uri.to_string(), p, ec);
|
|
|
|
}
|
|
|
|
ec.clear();
|
|
|
|
|
2017-03-26 23:57:59 +02:00
|
|
|
p.max_connections = max_connections_per_torrent;
|
|
|
|
p.max_uploads = -1;
|
|
|
|
p.upload_limit = torrent_upload_limit;
|
|
|
|
p.download_limit = torrent_download_limit;
|
|
|
|
|
2017-03-26 15:16:42 +02:00
|
|
|
if (seed_mode) p.flags |= lt::add_torrent_params::flag_seed_mode;
|
|
|
|
if (disable_storage) p.storage = lt::disabled_storage_constructor;
|
|
|
|
if (share_mode) p.flags |= lt::add_torrent_params::flag_share_mode;
|
|
|
|
p.save_path = save_path;
|
|
|
|
p.storage_mode = static_cast<lt::storage_mode_t>(allocation_mode);
|
|
|
|
|
|
|
|
std::printf("adding magnet: %s\n", uri.to_string().c_str());
|
2017-03-26 20:54:16 +02:00
|
|
|
ses.async_add_torrent(std::move(p));
|
2017-03-26 15:16:42 +02:00
|
|
|
}
|
|
|
|
|
2017-03-26 20:20:40 +02:00
|
|
|
// return false on failure
|
2017-04-12 20:05:53 +02:00
|
|
|
bool add_torrent(lt::session& ses, std::string torrent)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2014-07-06 21:18:00 +02:00
|
|
|
static int counter = 0;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("[%d] %s\n", counter++, torrent.c_str());
|
2005-10-16 11:15:46 +02:00
|
|
|
|
2016-02-18 06:52:54 +01:00
|
|
|
error_code ec;
|
2017-03-26 15:16:42 +02:00
|
|
|
auto ti = std::make_shared<torrent_info>(torrent, ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
std::printf("failed to load torrent \"%s\": %s\n"
|
|
|
|
, torrent.c_str(), ec.message().c_str());
|
2017-03-26 20:20:40 +02:00
|
|
|
return false;
|
2017-03-26 15:16:42 +02:00
|
|
|
}
|
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
add_torrent_params p;
|
2008-07-01 01:14:31 +02:00
|
|
|
|
2016-02-18 06:52:54 +01:00
|
|
|
std::vector<char> resume_data;
|
2017-03-26 15:16:42 +02:00
|
|
|
load_file(resume_file(ti->info_hash()), resume_data, ec);
|
2016-02-18 06:52:54 +01:00
|
|
|
if (!ec)
|
|
|
|
{
|
2017-03-29 22:10:32 +02:00
|
|
|
p = lt::read_resume_data(resume_data, ec);
|
2016-05-17 15:24:06 +02:00
|
|
|
if (ec) std::printf(" failed to load resume data: %s\n", ec.message().c_str());
|
2016-02-18 06:52:54 +01:00
|
|
|
}
|
|
|
|
ec.clear();
|
2008-07-01 01:14:31 +02:00
|
|
|
|
2016-02-18 06:52:54 +01:00
|
|
|
if (seed_mode) p.flags |= add_torrent_params::flag_seed_mode;
|
|
|
|
if (disable_storage) p.storage = disabled_storage_constructor;
|
|
|
|
if (share_mode) p.flags |= add_torrent_params::flag_share_mode;
|
2008-07-01 01:14:31 +02:00
|
|
|
|
2017-03-26 23:57:59 +02:00
|
|
|
p.max_connections = max_connections_per_torrent;
|
|
|
|
p.max_uploads = -1;
|
|
|
|
p.upload_limit = torrent_upload_limit;
|
|
|
|
p.download_limit = torrent_download_limit;
|
2017-03-26 15:16:42 +02:00
|
|
|
p.ti = ti;
|
2008-04-24 05:28:48 +02:00
|
|
|
p.save_path = save_path;
|
2010-01-18 06:29:35 +01:00
|
|
|
p.storage_mode = (storage_mode_t)allocation_mode;
|
2011-11-08 06:36:22 +01:00
|
|
|
p.flags &= ~add_torrent_params::flag_duplicate_is_error;
|
2016-05-17 15:24:06 +02:00
|
|
|
p.userdata = static_cast<void*>(new std::string(torrent));
|
2017-03-26 20:54:16 +02:00
|
|
|
ses.async_add_torrent(std::move(p));
|
2017-03-26 20:20:40 +02:00
|
|
|
return true;
|
2005-10-19 02:50:38 +02:00
|
|
|
}
|
|
|
|
|
2014-11-17 04:41:01 +01:00
|
|
|
std::vector<std::string> list_dir(std::string path
|
2017-03-25 05:01:47 +01:00
|
|
|
, bool (*filter_fun)(lt::string_view)
|
2017-04-12 20:05:53 +02:00
|
|
|
, lt::error_code& ec)
|
2014-11-17 04:41:01 +01:00
|
|
|
{
|
|
|
|
std::vector<std::string> ret;
|
|
|
|
#ifdef TORRENT_WINDOWS
|
2014-11-19 10:23:08 +01:00
|
|
|
if (!path.empty() && path[path.size()-1] != '\\') path += "\\*";
|
|
|
|
else path += "*";
|
2014-11-17 04:41:01 +01:00
|
|
|
|
2015-01-08 19:19:35 +01:00
|
|
|
WIN32_FIND_DATAA fd;
|
|
|
|
HANDLE handle = FindFirstFileA(path.c_str(), &fd);
|
2014-11-17 04:41:01 +01:00
|
|
|
if (handle == INVALID_HANDLE_VALUE)
|
2014-11-24 05:25:05 +01:00
|
|
|
{
|
2014-11-28 05:53:21 +01:00
|
|
|
ec.assign(GetLastError(), boost::system::system_category());
|
2014-11-17 04:41:01 +01:00
|
|
|
return ret;
|
2014-11-24 05:25:05 +01:00
|
|
|
}
|
2014-11-17 04:41:01 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2017-03-25 05:01:47 +01:00
|
|
|
lt::string_view p = fd.cFileName;
|
2014-11-17 04:41:01 +01:00
|
|
|
if (filter_fun(p))
|
2017-03-25 05:01:47 +01:00
|
|
|
ret.push_back(p.to_string());
|
2015-07-11 09:39:00 +02:00
|
|
|
|
2015-01-08 19:19:35 +01:00
|
|
|
} while (FindNextFileA(handle, &fd));
|
2014-11-17 04:41:01 +01:00
|
|
|
FindClose(handle);
|
|
|
|
#else
|
|
|
|
|
|
|
|
if (!path.empty() && path[path.size()-1] == '/')
|
|
|
|
path.resize(path.size()-1);
|
|
|
|
|
|
|
|
DIR* handle = opendir(path.c_str());
|
2016-07-09 22:26:26 +02:00
|
|
|
if (handle == nullptr)
|
2014-11-24 05:25:05 +01:00
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, boost::system::system_category());
|
2014-11-24 05:25:05 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2014-11-17 04:41:01 +01:00
|
|
|
|
|
|
|
struct dirent de;
|
|
|
|
dirent* dummy;
|
|
|
|
while (readdir_r(handle, &de, &dummy) == 0)
|
|
|
|
{
|
2016-07-09 22:26:26 +02:00
|
|
|
if (dummy == nullptr) break;
|
2014-11-17 04:41:01 +01:00
|
|
|
|
2017-03-25 05:01:47 +01:00
|
|
|
lt::string_view p(de.d_name);
|
2014-11-17 04:41:01 +01:00
|
|
|
if (filter_fun(p))
|
2017-03-25 05:01:47 +01:00
|
|
|
ret.push_back(p.to_string());
|
2014-11-17 04:41:01 +01:00
|
|
|
}
|
2014-11-24 05:25:05 +01:00
|
|
|
closedir(handle);
|
2014-11-17 04:41:01 +01:00
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
void scan_dir(std::string const& dir_path, lt::session& ses)
|
2014-11-17 04:41:01 +01:00
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2005-10-19 02:50:38 +02:00
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
error_code ec;
|
2017-03-26 20:20:40 +02:00
|
|
|
std::vector<std::string> ents = list_dir(dir_path
|
|
|
|
, [](lt::string_view p) { return p.size() > 8 && p.substr(p.size() - 8) == ".torrent"; }, ec);
|
2014-11-24 05:25:05 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "failed to list directory: (%s : %d) %s\n"
|
2014-11-24 05:25:05 +01:00
|
|
|
, ec.category().name(), ec.value(), ec.message().c_str());
|
|
|
|
return;
|
|
|
|
}
|
2014-01-19 05:11:49 +01:00
|
|
|
|
2017-03-25 05:01:47 +01:00
|
|
|
for (auto const& e : ents)
|
2014-01-19 05:11:49 +01:00
|
|
|
{
|
2017-03-25 05:01:47 +01:00
|
|
|
std::string const file = path_append(dir_path, e);
|
2005-10-16 18:58:41 +02:00
|
|
|
|
2017-03-26 20:20:40 +02:00
|
|
|
// there's a new file in the monitor directory, load it up
|
|
|
|
if (add_torrent(ses, file))
|
2005-10-19 02:50:38 +02:00
|
|
|
{
|
2017-03-26 20:20:40 +02:00
|
|
|
if (::remove(file.c_str()) < 0)
|
|
|
|
{
|
|
|
|
std::fprintf(stderr, "failed to remove torrent file: \"%s\"\n"
|
|
|
|
, file.c_str());
|
|
|
|
}
|
2005-10-19 02:50:38 +02:00
|
|
|
}
|
|
|
|
}
|
2005-10-16 11:15:46 +02:00
|
|
|
}
|
|
|
|
|
2014-05-10 22:20:57 +02:00
|
|
|
char const* timestamp()
|
|
|
|
{
|
2016-07-09 22:26:26 +02:00
|
|
|
time_t t = std::time(nullptr);
|
2014-05-10 22:20:57 +02:00
|
|
|
tm* timeinfo = std::localtime(&t);
|
|
|
|
static char str[200];
|
|
|
|
std::strftime(str, 200, "%b %d %X", timeinfo);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
void print_alert(lt::alert const* a, std::string& str)
|
2008-07-12 10:19:17 +02:00
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2008-07-12 10:19:17 +02:00
|
|
|
|
|
|
|
if (a->category() & alert::error_notification)
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
str += esc("31");
|
2008-07-12 10:19:17 +02:00
|
|
|
}
|
|
|
|
else if (a->category() & (alert::peer_notification | alert::storage_notification))
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
str += esc("33");
|
2008-07-12 10:19:17 +02:00
|
|
|
}
|
2009-05-13 03:02:06 +02:00
|
|
|
str += "[";
|
2014-05-10 22:20:57 +02:00
|
|
|
str += timestamp();
|
2009-05-13 03:02:06 +02:00
|
|
|
str += "] ";
|
|
|
|
str += a->message();
|
|
|
|
str += esc("0");
|
2008-07-12 10:19:17 +02:00
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
if (g_log_file)
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(g_log_file, "[%s] %s\n", timestamp(), a->message().c_str());
|
2009-05-13 03:02:06 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:31:28 +01:00
|
|
|
int save_file(std::string const& filename, std::vector<char> const& v)
|
2009-05-13 03:02:06 +02:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
FILE* f = std::fopen(filename.c_str(), "wb");
|
2016-06-20 17:32:06 +02:00
|
|
|
if (f == nullptr)
|
2014-10-15 07:07:26 +02:00
|
|
|
return -1;
|
2009-05-13 03:02:06 +02:00
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
int w = int(std::fwrite(&v[0], 1, v.size(), f));
|
|
|
|
std::fclose(f);
|
2014-10-15 07:07:26 +02:00
|
|
|
|
2015-04-29 06:05:29 +02:00
|
|
|
if (w < 0) return -1;
|
2014-10-15 07:07:26 +02:00
|
|
|
if (w != int(v.size())) return -3;
|
2009-05-13 03:02:06 +02:00
|
|
|
return 0;
|
2008-07-12 10:19:17 +02:00
|
|
|
}
|
|
|
|
|
2011-12-23 06:36:13 +01:00
|
|
|
// returns true if the alert was handled (and should not be printed to the log)
|
|
|
|
// returns false if the alert was not handled
|
2017-03-19 06:35:33 +01:00
|
|
|
bool handle_alert(torrent_view& view, session_view& ses_view
|
2017-04-12 20:05:53 +02:00
|
|
|
, lt::session& ses, lt::alert* a)
|
2008-07-06 14:22:56 +02:00
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2008-07-06 14:22:56 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (session_stats_alert* s = alert_cast<session_stats_alert>(a))
|
|
|
|
{
|
2017-05-28 02:33:31 +02:00
|
|
|
ses_view.update_counters(s->counters()
|
2015-04-03 22:15:48 +02:00
|
|
|
, duration_cast<microseconds>(s->timestamp().time_since_epoch()).count());
|
2014-07-06 21:18:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-06-07 19:52:19 +02:00
|
|
|
|
2015-01-17 18:02:58 +01:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
if (dht_stats_alert* p = alert_cast<dht_stats_alert>(a))
|
|
|
|
{
|
2015-04-03 22:15:48 +02:00
|
|
|
dht_active_requests = p->active_requests;
|
|
|
|
dht_routing_table = p->routing_table;
|
2015-02-14 07:14:15 +01:00
|
|
|
return true;
|
2015-01-17 18:02:58 +01:00
|
|
|
}
|
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2011-09-12 05:51:49 +02:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
if (torrent_need_cert_alert* p = alert_cast<torrent_need_cert_alert>(a))
|
|
|
|
{
|
|
|
|
torrent_handle h = p->handle;
|
2016-06-04 16:01:43 +02:00
|
|
|
std::string base_name = path_append("certificates", to_hex(h.info_hash()));
|
2014-07-06 21:18:00 +02:00
|
|
|
std::string cert = base_name + ".pem";
|
|
|
|
std::string priv = base_name + "_key.pem";
|
2014-11-19 10:23:08 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
struct ::_stat st;
|
|
|
|
int ret = ::_stat(cert.c_str(), &st);
|
|
|
|
if (ret < 0 || (st.st_mode & _S_IFREG) == 0)
|
|
|
|
#else
|
|
|
|
struct ::stat st;
|
|
|
|
int ret = ::stat(cert.c_str(), &st);
|
|
|
|
if (ret < 0 || (st.st_mode & S_IFREG) == 0)
|
|
|
|
#endif
|
2011-09-12 05:51:49 +02:00
|
|
|
{
|
|
|
|
char msg[256];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(msg, sizeof(msg), "ERROR. could not load certificate %s: %s\n"
|
|
|
|
, cert.c_str(), std::strerror(errno));
|
|
|
|
if (g_log_file) std::fprintf(g_log_file, "[%s] %s\n", timestamp(), msg);
|
2011-12-23 06:36:13 +01:00
|
|
|
return true;
|
2011-09-12 05:51:49 +02:00
|
|
|
}
|
2014-11-19 10:23:08 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
ret = ::_stat(priv.c_str(), &st);
|
|
|
|
if (ret < 0 || (st.st_mode & _S_IFREG) == 0)
|
|
|
|
#else
|
|
|
|
ret = ::stat(priv.c_str(), &st);
|
|
|
|
if (ret < 0 || (st.st_mode & S_IFREG) == 0)
|
|
|
|
#endif
|
2011-09-12 05:51:49 +02:00
|
|
|
{
|
|
|
|
char msg[256];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(msg, sizeof(msg), "ERROR. could not load private key %s: %s\n"
|
|
|
|
, priv.c_str(), std::strerror(errno));
|
|
|
|
if (g_log_file) std::fprintf(g_log_file, "[%s] %s\n", timestamp(), msg);
|
2011-12-23 06:36:13 +01:00
|
|
|
return true;
|
2011-09-12 05:51:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char msg[256];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(msg, sizeof(msg), "loaded certificate %s and key %s\n", cert.c_str(), priv.c_str());
|
|
|
|
if (g_log_file) std::fprintf(g_log_file, "[%s] %s\n", timestamp(), msg);
|
2011-09-12 05:51:49 +02:00
|
|
|
|
2012-01-14 17:04:25 +01:00
|
|
|
h.set_ssl_certificate(cert, priv, "certificates/dhparams.pem", "1234");
|
2011-09-12 05:51:49 +02:00
|
|
|
h.resume();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-02-15 06:17:09 +01:00
|
|
|
// don't log every peer we try to connect to
|
|
|
|
if (alert_cast<peer_connect_alert>(a)) return true;
|
|
|
|
|
|
|
|
if (peer_disconnected_alert* pd = alert_cast<peer_disconnected_alert>(a))
|
|
|
|
{
|
|
|
|
// ignore failures to connect and peers not responding with a
|
|
|
|
// handshake. The peers that we successfully connect to and then
|
|
|
|
// disconnect is more interesting.
|
2017-06-15 00:46:11 +02:00
|
|
|
if (pd->op == operation_t::connect
|
2016-10-02 21:27:50 +02:00
|
|
|
|| pd->error == errors::timed_out_no_handshake)
|
2015-02-15 06:17:09 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-01-17 18:02:58 +01:00
|
|
|
|
2016-05-02 18:36:21 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
// it seems msvc makes the definitions of 'p' escape the if-statement here
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable: 4456)
|
|
|
|
#endif
|
|
|
|
|
2012-02-21 06:56:22 +01:00
|
|
|
if (metadata_received_alert* p = alert_cast<metadata_received_alert>(a))
|
|
|
|
{
|
|
|
|
torrent_handle h = p->handle;
|
2017-03-26 20:20:40 +02:00
|
|
|
h.save_resume_data(torrent_handle::save_info_dict);
|
|
|
|
++num_outstanding_resume_data;
|
2012-02-21 06:56:22 +01:00
|
|
|
}
|
|
|
|
else if (add_torrent_alert* p = alert_cast<add_torrent_alert>(a))
|
2011-10-12 12:27:17 +02:00
|
|
|
{
|
|
|
|
if (p->error)
|
|
|
|
{
|
2017-03-26 20:20:40 +02:00
|
|
|
std::fprintf(stderr, "failed to add torrent: %s %s\n"
|
|
|
|
, p->params.ti ? p->params.ti->name().c_str() : p->params.name.c_str()
|
2015-01-01 19:03:56 +01:00
|
|
|
, p->error.message().c_str());
|
2011-10-12 12:27:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
torrent_handle h = p->handle;
|
|
|
|
|
2017-03-26 23:57:59 +02:00
|
|
|
h.save_resume_data(torrent_handle::save_info_dict | torrent_handle::only_if_modified);
|
2017-03-26 20:20:40 +02:00
|
|
|
++num_outstanding_resume_data;
|
|
|
|
|
2012-01-14 17:04:25 +01:00
|
|
|
// if we have a peer specified, connect to it
|
|
|
|
if (!peer.empty())
|
|
|
|
{
|
|
|
|
char* port = (char*) strrchr((char*)peer.c_str(), ':');
|
2016-06-20 17:32:06 +02:00
|
|
|
if (port != nullptr)
|
2012-01-14 17:04:25 +01:00
|
|
|
{
|
|
|
|
*port++ = 0;
|
|
|
|
char const* ip = peer.c_str();
|
|
|
|
int peer_port = atoi(port);
|
2012-02-20 05:13:39 +01:00
|
|
|
error_code ec;
|
2012-01-14 17:04:25 +01:00
|
|
|
if (peer_port > 0)
|
2016-06-18 20:01:38 +02:00
|
|
|
h.connect_peer(tcp::endpoint(address::from_string(ip, ec), std::uint16_t(peer_port)));
|
2012-01-14 17:04:25 +01:00
|
|
|
}
|
|
|
|
}
|
2011-10-12 12:27:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (torrent_finished_alert* p = alert_cast<torrent_finished_alert>(a))
|
2008-07-06 14:22:56 +02:00
|
|
|
{
|
2010-01-12 02:56:48 +01:00
|
|
|
p->handle.set_max_connections(max_connections_per_torrent / 2);
|
2008-07-06 14:22:56 +02:00
|
|
|
|
|
|
|
// write resume data for the finished torrent
|
2008-10-19 06:04:12 +02:00
|
|
|
// the alert handler for save_resume_data_alert
|
|
|
|
// will save it to disk
|
2008-07-06 14:22:56 +02:00
|
|
|
torrent_handle h = p->handle;
|
2017-04-09 15:37:06 +02:00
|
|
|
h.save_resume_data(torrent_handle::save_info_dict);
|
2011-07-04 07:33:06 +02:00
|
|
|
++num_outstanding_resume_data;
|
2008-07-06 14:22:56 +02:00
|
|
|
}
|
2009-11-02 04:34:16 +01:00
|
|
|
else if (save_resume_data_alert* p = alert_cast<save_resume_data_alert>(a))
|
2008-07-06 14:22:56 +02:00
|
|
|
{
|
2011-07-04 07:33:06 +02:00
|
|
|
--num_outstanding_resume_data;
|
2008-07-06 14:22:56 +02:00
|
|
|
torrent_handle h = p->handle;
|
2017-03-05 15:31:28 +01:00
|
|
|
auto const buf = write_resume_data_buf(p->params);
|
|
|
|
torrent_status st = h.status(torrent_handle::query_save_path);
|
2017-03-26 15:16:42 +02:00
|
|
|
save_file(resume_file(st.info_hash), buf);
|
2008-07-06 14:22:56 +02:00
|
|
|
}
|
2009-11-02 04:34:16 +01:00
|
|
|
else if (save_resume_data_failed_alert* p = alert_cast<save_resume_data_failed_alert>(a))
|
2008-11-17 02:19:46 +01:00
|
|
|
{
|
2011-07-04 07:33:06 +02:00
|
|
|
--num_outstanding_resume_data;
|
2017-03-26 23:57:59 +02:00
|
|
|
// don't print the error if it was just that we didn't need to save resume
|
|
|
|
// data. Returning true means "handled" and not printed to the log
|
|
|
|
return p->error == lt::errors::resume_data_not_modified;
|
2008-11-17 02:19:46 +01:00
|
|
|
}
|
2011-06-05 22:48:00 +02:00
|
|
|
else if (torrent_paused_alert* p = alert_cast<torrent_paused_alert>(a))
|
|
|
|
{
|
|
|
|
// write resume data for the finished torrent
|
|
|
|
// the alert handler for save_resume_data_alert
|
|
|
|
// will save it to disk
|
|
|
|
torrent_handle h = p->handle;
|
2017-04-09 15:37:06 +02:00
|
|
|
h.save_resume_data(torrent_handle::save_info_dict);
|
2011-07-04 07:33:06 +02:00
|
|
|
++num_outstanding_resume_data;
|
2011-06-05 22:48:00 +02:00
|
|
|
}
|
2011-12-23 06:36:13 +01:00
|
|
|
else if (state_update_alert* p = alert_cast<state_update_alert>(a))
|
|
|
|
{
|
2017-03-26 20:54:16 +02:00
|
|
|
view.update_torrents(std::move(p->status));
|
2011-12-23 06:36:13 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2016-05-02 18:36:21 +02:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
}
|
|
|
|
|
2017-03-27 01:07:15 +02:00
|
|
|
void pop_alerts(torrent_view& view, session_view& ses_view
|
2017-04-12 20:05:53 +02:00
|
|
|
, lt::session& ses, std::deque<std::string>& events)
|
2017-03-27 01:07:15 +02:00
|
|
|
{
|
|
|
|
std::vector<lt::alert*> alerts;
|
|
|
|
ses.pop_alerts(&alerts);
|
|
|
|
for (auto a : alerts)
|
|
|
|
{
|
|
|
|
if (::handle_alert(view, ses_view, ses, a)) continue;
|
|
|
|
|
|
|
|
// if we didn't handle the alert, print it to the log
|
|
|
|
std::string event_string;
|
|
|
|
print_alert(a, event_string);
|
|
|
|
events.push_back(event_string);
|
|
|
|
if (events.size() >= 20) events.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
void print_piece(lt::partial_piece_info const* pp
|
|
|
|
, lt::cached_piece_info const* cs
|
|
|
|
, std::vector<lt::peer_info> const& peers
|
2011-07-10 10:57:27 +02:00
|
|
|
, std::string& out)
|
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2011-07-10 10:57:27 +02:00
|
|
|
|
|
|
|
char str[1024];
|
2016-07-09 22:26:26 +02:00
|
|
|
assert(pp == nullptr || cs == nullptr || cs->piece == pp->piece_index);
|
2016-12-22 16:42:33 +01:00
|
|
|
int piece = static_cast<int>(pp ? pp->piece_index : cs->piece);
|
2016-04-25 23:22:09 +02:00
|
|
|
int num_blocks = pp ? pp->blocks_in_piece : int(cs->blocks.size());
|
2011-07-10 10:57:27 +02:00
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(str, sizeof(str), "%5d:[", piece);
|
2011-07-10 10:57:27 +02:00
|
|
|
out += str;
|
2017-05-07 22:18:14 +02:00
|
|
|
string_view last_color;
|
2011-07-10 10:57:27 +02:00
|
|
|
for (int j = 0; j < num_blocks; ++j)
|
|
|
|
{
|
2017-05-07 22:18:14 +02:00
|
|
|
int const index = pp ? peer_index(pp->blocks[j].peer(), peers) % 36 : -1;
|
|
|
|
char const* chr = " ";
|
|
|
|
bool const snubbed = index >= 0 ? ((peers[index].flags & peer_info::snubbed) != 0) : false;
|
2011-07-10 10:57:27 +02:00
|
|
|
|
|
|
|
char const* color = "";
|
|
|
|
|
2016-07-09 22:26:26 +02:00
|
|
|
if (pp == nullptr)
|
2011-07-10 10:57:27 +02:00
|
|
|
{
|
|
|
|
color = cs->blocks[j] ? esc("34;7") : esc("0");
|
2017-05-07 22:18:14 +02:00
|
|
|
chr = " ";
|
2011-07-10 10:57:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
if (cs && cs->blocks[j] && pp->blocks[j].state != block_info::finished)
|
|
|
|
color = esc("36;7");
|
2011-07-10 10:57:27 +02:00
|
|
|
else if (pp->blocks[j].bytes_progress > 0
|
|
|
|
&& pp->blocks[j].state == block_info::requested)
|
|
|
|
{
|
2017-05-07 22:18:14 +02:00
|
|
|
if (pp->blocks[j].num_peers > 1) color = esc("0;1");
|
|
|
|
else color = snubbed ? esc("0;35") : esc("0;33");
|
|
|
|
|
|
|
|
#ifndef TORRENT_WINDOWS
|
|
|
|
static char const* const progress[] = {
|
|
|
|
"\u2581", "\u2582", "\u2583", "\u2584",
|
|
|
|
"\u2585", "\u2586", "\u2587", "\u2588"
|
|
|
|
};
|
|
|
|
chr = progress[pp->blocks[j].bytes_progress * 8 / pp->blocks[j].block_size];
|
|
|
|
#else
|
|
|
|
static char const* const progress[] = { "\xb0", "\xb1", "\xb2" };
|
|
|
|
chr = progress[pp->blocks[j].bytes_progress * 3 / pp->blocks[j].block_size];
|
|
|
|
#endif
|
2011-07-10 10:57:27 +02:00
|
|
|
}
|
|
|
|
else if (pp->blocks[j].state == block_info::finished) color = esc("32;7");
|
|
|
|
else if (pp->blocks[j].state == block_info::writing) color = esc("36;7");
|
2017-05-07 22:18:14 +02:00
|
|
|
else if (pp->blocks[j].state == block_info::requested)
|
|
|
|
{
|
|
|
|
color = snubbed ? esc("0;35") : esc("0");
|
|
|
|
chr = "=";
|
|
|
|
}
|
|
|
|
else { color = esc("0"); chr = " "; }
|
2011-07-10 10:57:27 +02:00
|
|
|
}
|
2017-05-07 22:18:14 +02:00
|
|
|
if (last_color != color)
|
2015-05-30 08:11:41 +02:00
|
|
|
{
|
2017-05-07 22:18:14 +02:00
|
|
|
out += color;
|
|
|
|
last_color = color;
|
2015-05-30 08:11:41 +02:00
|
|
|
}
|
2017-05-07 22:18:14 +02:00
|
|
|
out += chr;
|
2011-07-10 10:57:27 +02:00
|
|
|
}
|
|
|
|
out += esc("0");
|
2014-10-07 05:09:32 +02:00
|
|
|
out += "]";
|
2011-07-10 10:57:27 +02:00
|
|
|
}
|
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
int main(int argc, char* argv[])
|
2005-10-16 11:15:46 +02:00
|
|
|
{
|
2015-05-26 20:39:49 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
// sets the terminal to single-character mode
|
|
|
|
// and resets when destructed
|
|
|
|
set_keypress s;
|
|
|
|
#endif
|
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (argc == 1)
|
2005-10-19 02:50:38 +02:00
|
|
|
{
|
2017-03-26 20:20:40 +02:00
|
|
|
std::fprintf(stderr, R"(usage: client_test [OPTIONS] [TORRENT|MAGNETURL]
|
|
|
|
OPTIONS:
|
|
|
|
|
|
|
|
CLIENT OPTIONS
|
|
|
|
-f <log file> logs all events to the given file
|
|
|
|
-s <path> sets the save path for downloads. This also determines
|
|
|
|
the resume data save directory. Torrents from the resume
|
|
|
|
directory are automatically added to the session on
|
|
|
|
startup.
|
|
|
|
-m <path> sets the .torrent monitor directory. torrent files
|
|
|
|
dropped in the directory are added the session and the
|
|
|
|
resume data directory, and removed from the monitor dir.
|
|
|
|
-t <seconds> sets the scan interval of the monitor dir
|
|
|
|
-F <milliseconds> sets the UI refresh rate. This is the number of
|
|
|
|
milliseconds between screen refreshes.
|
|
|
|
-k enable high performance settings. This overwrites any other
|
|
|
|
previous command line options, so be sure to specify this first
|
|
|
|
-G Add torrents in seed-mode (i.e. assume all pieces
|
|
|
|
are present and check hashes on-demand)
|
|
|
|
|
|
|
|
LIBTORRENT SETTINGS
|
|
|
|
--<name-of-setting>=<value>
|
|
|
|
set the libtorrent setting <name> to <value>
|
|
|
|
--list-settings print all libtorrent settings and exit
|
|
|
|
|
|
|
|
BITTORRENT OPTIONS
|
|
|
|
-T <limit> sets the max number of connections per torrent
|
|
|
|
-U <rate> sets per-torrent upload rate
|
|
|
|
-D <rate> sets per-torrent download rate
|
|
|
|
-Q enables share mode. Share mode attempts to maximize
|
|
|
|
share ratio rather than downloading
|
|
|
|
-r <IP:port> connect to specified peer
|
|
|
|
|
|
|
|
NETWORK OPTIONS
|
|
|
|
-x <file> loads an emule IP-filter file
|
|
|
|
-Y Rate limit local peers)"
|
2011-04-27 10:07:24 +02:00
|
|
|
#if TORRENT_USE_I2P
|
2017-03-26 20:20:40 +02:00
|
|
|
R"( -i <i2p-host> the hostname to an I2P SAM bridge to use
|
|
|
|
)"
|
2011-04-27 10:07:24 +02:00
|
|
|
#endif
|
2017-03-26 20:20:40 +02:00
|
|
|
R"(
|
|
|
|
DISK OPTIONS
|
|
|
|
-a <mode> sets the allocation mode. [sparse|allocate]
|
|
|
|
-0 disable disk I/O, read garbage and don't flush to disk
|
|
|
|
|
|
|
|
TORRENT is a path to a .torrent file
|
|
|
|
MAGNETURL is a magnet link
|
|
|
|
)") ;
|
2009-02-26 08:09:56 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2005-10-19 02:50:38 +02:00
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2017-03-19 06:35:33 +01:00
|
|
|
torrent_view view;
|
|
|
|
session_view ses_view;
|
|
|
|
|
2017-05-25 06:33:59 +02:00
|
|
|
session_params params;
|
|
|
|
|
|
|
|
error_code ec;
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
params.dht_settings.privacy_lookups = true;
|
|
|
|
|
|
|
|
std::vector<char> in;
|
|
|
|
load_file(".ses_state", in, ec);
|
|
|
|
if (!ec)
|
|
|
|
{
|
|
|
|
bdecode_node e;
|
|
|
|
if (bdecode(&in[0], &in[0] + in.size(), e, ec) == 0)
|
|
|
|
params = read_session_params(e, session_handle::save_dht_state);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
auto& settings = params.settings;
|
2016-03-06 05:16:33 +01:00
|
|
|
settings.set_int(settings_pack::cache_size, cache_size);
|
2014-11-30 05:57:54 +01:00
|
|
|
settings.set_int(settings_pack::choking_algorithm, settings_pack::rate_based_choker);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2017-05-03 05:45:43 +02:00
|
|
|
lt::time_duration refresh_delay = lt::milliseconds(500);
|
2015-06-07 19:52:19 +02:00
|
|
|
bool rate_limit_locals = false;
|
2009-05-13 18:02:43 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
std::deque<std::string> events;
|
2008-02-28 08:34:07 +01:00
|
|
|
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point next_dir_scan = clock_type::now();
|
2003-12-22 08:14:35 +01:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
// load the torrents given on the commandline
|
2017-03-26 15:16:42 +02:00
|
|
|
std::vector<lt::string_view> torrents;
|
2015-06-07 19:52:19 +02:00
|
|
|
ip_filter loaded_ip_filter;
|
2010-06-05 23:03:01 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
for (int i = 1; i < argc; ++i)
|
|
|
|
{
|
2016-06-13 06:06:15 +02:00
|
|
|
if (argv[i][0] != '-')
|
|
|
|
{
|
|
|
|
torrents.push_back(argv[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-18 17:19:49 +01:00
|
|
|
if (argv[i] == "--list-settings"_sv)
|
2016-03-17 05:24:57 +01:00
|
|
|
{
|
|
|
|
// print all libtorrent settings and exit
|
|
|
|
print_settings(settings_pack::string_type_base
|
|
|
|
, settings_pack::num_string_settings
|
|
|
|
, "%s=<string>\n");
|
|
|
|
print_settings(settings_pack::bool_type_base
|
|
|
|
, settings_pack::num_bool_settings
|
|
|
|
, "%s=<bool>\n");
|
|
|
|
print_settings(settings_pack::int_type_base
|
|
|
|
, settings_pack::num_int_settings
|
|
|
|
, "%s=<int>\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// maybe this is an assignment of a libtorrent setting
|
2016-06-20 17:32:06 +02:00
|
|
|
if (argv[i][1] == '-' && strchr(argv[i], '=') != nullptr)
|
2016-03-17 05:24:57 +01:00
|
|
|
{
|
|
|
|
char const* equal = strchr(argv[i], '=');
|
|
|
|
char const* start = argv[i]+2;
|
|
|
|
// +2 is to skip the --
|
|
|
|
std::string const key(start, equal - start);
|
|
|
|
char const* value = equal + 1;
|
|
|
|
|
|
|
|
int const sett_name = setting_by_name(key);
|
|
|
|
if (sett_name < 0)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "unknown setting: \"%s\"\n", key.c_str());
|
2016-03-17 05:24:57 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (sett_name & settings_pack::type_mask)
|
|
|
|
{
|
|
|
|
case settings_pack::string_type_base:
|
|
|
|
settings.set_str(sett_name, value);
|
|
|
|
break;
|
|
|
|
case settings_pack::bool_type_base:
|
2017-03-18 17:19:49 +01:00
|
|
|
if (value == "0"_sv || value == "1"_sv)
|
2016-03-17 05:24:57 +01:00
|
|
|
{
|
2016-04-20 06:45:32 +02:00
|
|
|
settings.set_bool(sett_name, atoi(value) != 0);
|
2016-03-17 05:24:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "invalid value for \"%s\". expected 0 or 1\n"
|
2016-03-17 05:24:57 +01:00
|
|
|
, key.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case settings_pack::int_type_base:
|
|
|
|
settings.set_int(sett_name, atoi(value));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
// if there's a flag but no argument following, ignore it
|
|
|
|
if (argc == i) continue;
|
|
|
|
char const* arg = argv[i+1];
|
2016-06-20 17:32:06 +02:00
|
|
|
if (arg == nullptr) arg = "";
|
2014-08-22 09:56:10 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
switch (argv[i][1])
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
case 'f': g_log_file = std::fopen(arg, "w+"); break;
|
2016-09-28 19:28:43 +02:00
|
|
|
case 'k': settings = high_performance_seed(); --i; break;
|
2011-08-07 08:34:27 +02:00
|
|
|
case 'G': seed_mode = true; --i; break;
|
2017-03-25 05:01:47 +01:00
|
|
|
case 's': save_path = make_absolute_path(arg); break;
|
2009-02-26 08:09:56 +01:00
|
|
|
case 'U': torrent_upload_limit = atoi(arg) * 1000; break;
|
|
|
|
case 'D': torrent_download_limit = atoi(arg) * 1000; break;
|
2017-03-25 05:01:47 +01:00
|
|
|
case 'm': monitor_dir = make_absolute_path(arg); break;
|
2010-11-29 02:33:05 +01:00
|
|
|
case 'Q': share_mode = true; --i; break;
|
2009-02-26 08:09:56 +01:00
|
|
|
case 't': poll_interval = atoi(arg); break;
|
2017-05-03 05:45:43 +02:00
|
|
|
case 'F': refresh_delay = lt::milliseconds(atoi(arg)); break;
|
2016-05-02 18:36:21 +02:00
|
|
|
case 'a': allocation_mode = (arg == std::string("sparse"))
|
2017-04-12 20:05:53 +02:00
|
|
|
? lt::storage_mode_sparse
|
|
|
|
: lt::storage_mode_allocate;
|
2016-05-02 18:36:21 +02:00
|
|
|
break;
|
2009-02-26 08:09:56 +01:00
|
|
|
case 'x':
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
FILE* filter = std::fopen(arg, "r");
|
2010-06-06 04:31:20 +02:00
|
|
|
if (filter)
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2010-06-06 04:31:20 +02:00
|
|
|
unsigned int a,b,c,d,e,f,g,h, flags;
|
2016-05-17 15:24:06 +02:00
|
|
|
while (std::fscanf(filter, "%u.%u.%u.%u - %u.%u.%u.%u %u\n"
|
|
|
|
, &a, &b, &c, &d, &e, &f, &g, &h, &flags) == 9)
|
2010-06-06 04:31:20 +02:00
|
|
|
{
|
|
|
|
address_v4 start((a << 24) + (b << 16) + (c << 8) + d);
|
|
|
|
address_v4 last((e << 24) + (f << 16) + (g << 8) + h);
|
|
|
|
if (flags <= 127) flags = ip_filter::blocked;
|
|
|
|
else flags = 0;
|
2015-06-07 19:52:19 +02:00
|
|
|
loaded_ip_filter.add_rule(start, last, flags);
|
2010-06-06 04:31:20 +02:00
|
|
|
}
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fclose(filter);
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2010-01-12 02:56:48 +01:00
|
|
|
case 'T': max_connections_per_torrent = atoi(arg); break;
|
2012-01-14 17:04:25 +01:00
|
|
|
case 'r': peer = arg; break;
|
2014-07-06 21:18:00 +02:00
|
|
|
case 'Y':
|
|
|
|
{
|
|
|
|
--i;
|
2015-06-07 19:52:19 +02:00
|
|
|
rate_limit_locals = true;
|
2014-07-06 21:18:00 +02:00
|
|
|
break;
|
|
|
|
}
|
2011-07-10 22:17:32 +02:00
|
|
|
case '0': disable_storage = true; --i;
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2009-02-28 08:02:30 +01:00
|
|
|
++i; // skip the argument
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
|
|
|
|
2011-01-31 04:13:22 +01:00
|
|
|
// create directory for resume files
|
2014-11-19 10:23:08 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
int ret = _mkdir(path_append(save_path, ".resume").c_str());
|
|
|
|
#else
|
2014-11-17 04:41:01 +01:00
|
|
|
int ret = mkdir(path_append(save_path, ".resume").c_str(), 0777);
|
2014-11-19 10:23:08 +01:00
|
|
|
#endif
|
2017-03-26 23:57:59 +02:00
|
|
|
if (ret < 0 && errno != EEXIST)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "failed to create resume file directory: (%d) %s\n"
|
2014-11-17 04:41:01 +01:00
|
|
|
, errno, strerror(errno));
|
2017-03-26 23:57:59 +02:00
|
|
|
}
|
2011-01-31 04:13:22 +01:00
|
|
|
|
2015-06-07 19:52:19 +02:00
|
|
|
settings.set_str(settings_pack::user_agent, "client_test/" LIBTORRENT_VERSION);
|
|
|
|
settings.set_int(settings_pack::alert_mask, alert::all_categories
|
|
|
|
& ~(alert::dht_notification
|
|
|
|
+ alert::progress_notification
|
|
|
|
+ alert::stats_notification
|
|
|
|
+ alert::session_log_notification
|
|
|
|
+ alert::torrent_log_notification
|
|
|
|
+ alert::peer_log_notification
|
|
|
|
+ alert::dht_log_notification
|
2015-11-29 06:58:46 +01:00
|
|
|
+ alert::picker_log_notification
|
2015-06-07 19:52:19 +02:00
|
|
|
));
|
|
|
|
|
2017-05-25 06:33:59 +02:00
|
|
|
lt::session ses(std::move(params));
|
2015-06-07 19:52:19 +02:00
|
|
|
|
|
|
|
if (rate_limit_locals)
|
|
|
|
{
|
|
|
|
ip_filter pcf;
|
|
|
|
pcf.add_rule(address_v4::from_string("0.0.0.0")
|
2017-03-31 04:51:39 +02:00
|
|
|
, address_v4::from_string("255.255.255.255")
|
|
|
|
, 1 << static_cast<std::uint32_t>(lt::session::global_peer_class_id));
|
2015-06-07 19:52:19 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
pcf.add_rule(address_v6::from_string("::")
|
|
|
|
, address_v6::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), 1);
|
|
|
|
#endif
|
|
|
|
ses.set_peer_class_filter(pcf);
|
|
|
|
}
|
|
|
|
|
|
|
|
ses.set_ip_filter(loaded_ip_filter);
|
2016-03-05 21:15:22 +01:00
|
|
|
|
2016-06-13 06:06:15 +02:00
|
|
|
for (auto const& i : torrents)
|
2010-06-05 23:03:01 +02:00
|
|
|
{
|
2017-03-26 20:20:40 +02:00
|
|
|
if (i.substr(0, 7) == "magnet:") add_magnet(ses, i);
|
|
|
|
else add_torrent(ses, i.to_string());
|
|
|
|
}
|
|
|
|
|
2017-05-05 04:46:30 +02:00
|
|
|
std::thread resume_data_loader([&ses]
|
2017-03-26 20:20:40 +02:00
|
|
|
{
|
2017-05-05 04:46:30 +02:00
|
|
|
// load resume files
|
|
|
|
error_code ec;
|
|
|
|
std::string const resume_dir = path_append(save_path, ".resume");
|
|
|
|
std::vector<std::string> ents = list_dir(resume_dir
|
|
|
|
, [](lt::string_view p) { return p.size() > 7 && p.substr(p.size() - 7) == ".resume"; }, ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
2017-03-26 20:20:40 +02:00
|
|
|
std::fprintf(stderr, "failed to list resume directory \"%s\": (%s : %d) %s\n"
|
|
|
|
, resume_dir.c_str(), ec.category().name(), ec.value(), ec.message().c_str());
|
2017-05-05 04:46:30 +02:00
|
|
|
}
|
|
|
|
else
|
2017-03-26 15:16:42 +02:00
|
|
|
{
|
2017-05-05 04:46:30 +02:00
|
|
|
for (auto const& e : ents)
|
2017-03-26 20:20:40 +02:00
|
|
|
{
|
2017-05-05 04:46:30 +02:00
|
|
|
std::string const file = path_append(resume_dir, e);
|
2017-03-26 20:20:40 +02:00
|
|
|
|
2017-05-05 04:46:30 +02:00
|
|
|
std::vector<char> resume_data;
|
|
|
|
load_file(file, resume_data, ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
std::printf(" failed to load resume file \"%s\": %s\n"
|
|
|
|
, file.c_str(), ec.message().c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
add_torrent_params p = lt::read_resume_data(resume_data, ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
std::printf(" failed to parse resume data \"%s\": %s\n"
|
|
|
|
, file.c_str(), ec.message().c_str());
|
|
|
|
continue;
|
|
|
|
}
|
2017-03-26 23:57:59 +02:00
|
|
|
|
2017-05-05 04:46:30 +02:00
|
|
|
// we're loading this torrent from resume data. There's no need to
|
|
|
|
// re-save the resume data immediately.
|
|
|
|
p.flags &= ~add_torrent_params::flag_need_save_resume;
|
2017-03-27 01:07:15 +02:00
|
|
|
|
2017-05-05 04:46:30 +02:00
|
|
|
ses.async_add_torrent(std::move(p));
|
2017-03-27 01:07:15 +02:00
|
|
|
}
|
2010-06-05 23:03:01 +02:00
|
|
|
}
|
2017-05-05 04:46:30 +02:00
|
|
|
});
|
2010-06-05 23:03:01 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
// main loop
|
|
|
|
std::vector<peer_info> peers;
|
|
|
|
std::vector<partial_piece_info> queue;
|
|
|
|
|
2016-05-24 05:02:52 +02:00
|
|
|
#ifndef _WIN32
|
2014-07-06 21:18:00 +02:00
|
|
|
signal(SIGTERM, signal_handler);
|
|
|
|
signal(SIGINT, signal_handler);
|
|
|
|
#endif
|
2014-07-09 00:37:13 +02:00
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
while (!quit)
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2011-12-23 06:36:13 +01:00
|
|
|
ses.post_torrent_updates();
|
2014-07-06 21:18:00 +02:00
|
|
|
ses.post_session_stats();
|
2015-01-17 18:02:58 +01:00
|
|
|
ses.post_dht_stats();
|
2011-03-23 03:46:22 +01:00
|
|
|
|
2014-07-09 00:37:13 +02:00
|
|
|
int terminal_width = 80;
|
|
|
|
int terminal_height = 50;
|
|
|
|
terminal_size(&terminal_width, &terminal_height);
|
2015-02-08 04:24:56 +01:00
|
|
|
view.set_size(terminal_width, terminal_height / 3);
|
|
|
|
ses_view.set_pos(terminal_height / 3);
|
2013-12-05 08:43:54 +01:00
|
|
|
|
2014-07-09 00:37:13 +02:00
|
|
|
int c = 0;
|
2013-12-05 08:43:54 +01:00
|
|
|
if (sleep_and_input(&c, refresh_delay))
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2014-07-09 00:37:13 +02:00
|
|
|
#ifdef _WIN32
|
2017-05-07 22:18:14 +02:00
|
|
|
constexpr int escape_seq = 224;
|
|
|
|
constexpr int left_arrow = 75;
|
|
|
|
constexpr int right_arrow = 77;
|
|
|
|
constexpr int up_arrow = 72;
|
|
|
|
constexpr int down_arrow = 80;
|
2014-07-06 21:18:00 +02:00
|
|
|
#else
|
2017-05-07 22:18:14 +02:00
|
|
|
constexpr int escape_seq = 27;
|
|
|
|
constexpr int left_arrow = 68;
|
|
|
|
constexpr int right_arrow = 67;
|
|
|
|
constexpr int up_arrow = 65;
|
|
|
|
constexpr int down_arrow = 66;
|
2014-07-06 21:18:00 +02:00
|
|
|
#endif
|
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
torrent_handle h = view.get_active_handle();
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (c == EOF) { break; }
|
2013-12-05 08:43:54 +01:00
|
|
|
do
|
2003-10-30 00:28:09 +01:00
|
|
|
{
|
2017-05-07 22:18:14 +02:00
|
|
|
if (c == escape_seq)
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
|
|
|
// escape code, read another character
|
2016-05-24 05:02:52 +02:00
|
|
|
#ifdef _WIN32
|
2016-06-05 03:33:10 +02:00
|
|
|
int c2 = _getch();
|
2008-05-07 11:28:49 +02:00
|
|
|
#else
|
2016-06-05 03:33:10 +02:00
|
|
|
int c2 = getc(stdin);
|
|
|
|
if (c2 == EOF) { break; }
|
|
|
|
if (c2 != '[') continue;
|
|
|
|
c2 = getc(stdin);
|
2008-05-07 11:28:49 +02:00
|
|
|
#endif
|
2016-06-05 03:33:10 +02:00
|
|
|
if (c2 == EOF) break;
|
2017-05-07 22:18:14 +02:00
|
|
|
if (c2 == left_arrow)
|
2011-02-01 10:48:28 +01:00
|
|
|
{
|
2017-05-07 22:18:14 +02:00
|
|
|
int const filter = view.filter();
|
2014-07-08 17:35:14 +02:00
|
|
|
if (filter > 0)
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2017-05-07 22:18:14 +02:00
|
|
|
view.set_filter(filter - 1);
|
2014-07-08 17:35:14 +02:00
|
|
|
h = view.get_active_handle();
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
2011-02-01 10:48:28 +01:00
|
|
|
}
|
2017-05-07 22:18:14 +02:00
|
|
|
else if (c2 == right_arrow)
|
2011-02-01 10:48:28 +01:00
|
|
|
{
|
2017-05-07 22:18:14 +02:00
|
|
|
int const filter = view.filter();
|
2014-07-08 17:35:14 +02:00
|
|
|
if (filter < torrent_view::torrents_max - 1)
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2017-05-07 22:18:14 +02:00
|
|
|
view.set_filter(filter + 1);
|
2014-07-08 17:35:14 +02:00
|
|
|
h = view.get_active_handle();
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
|
|
|
}
|
2017-05-07 22:18:14 +02:00
|
|
|
else if (c2 == up_arrow)
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
view.arrow_up();
|
|
|
|
h = view.get_active_handle();
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
2017-05-07 22:18:14 +02:00
|
|
|
else if (c2 == down_arrow)
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
view.arrow_down();
|
|
|
|
h = view.get_active_handle();
|
2011-02-01 10:48:28 +01:00
|
|
|
}
|
2010-11-06 17:57:15 +01:00
|
|
|
}
|
2013-12-05 08:43:54 +01:00
|
|
|
|
|
|
|
if (c == ' ')
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2013-12-05 08:43:54 +01:00
|
|
|
if (ses.is_paused()) ses.resume();
|
|
|
|
else ses.pause();
|
2008-05-07 11:28:49 +02:00
|
|
|
}
|
2013-12-05 08:43:54 +01:00
|
|
|
|
|
|
|
// add magnet link
|
|
|
|
if (c == 'm')
|
2008-06-29 21:08:30 +02:00
|
|
|
{
|
2013-12-05 08:43:54 +01:00
|
|
|
char url[4096];
|
2017-03-26 15:16:42 +02:00
|
|
|
url[0] = '\0';
|
2013-12-05 08:43:54 +01:00
|
|
|
puts("Enter magnet link:\n");
|
2017-04-04 00:47:08 +02:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
// enable terminal echo temporarily
|
|
|
|
set_keypress s(set_keypress::echo | set_keypress::canonical);
|
|
|
|
#endif
|
2017-03-26 15:16:42 +02:00
|
|
|
if (std::scanf("%4095s", url) == 1) add_magnet(ses, url);
|
|
|
|
else std::printf("failed to read magnet link\n");
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
2012-09-23 03:52:46 +02:00
|
|
|
|
2017-04-04 00:47:08 +02:00
|
|
|
if (c == 'q')
|
|
|
|
{
|
|
|
|
quit = true;
|
|
|
|
break;
|
|
|
|
}
|
2012-09-23 03:52:46 +02:00
|
|
|
|
2014-12-30 17:59:37 +01:00
|
|
|
if (c == 'W' && h.is_valid())
|
|
|
|
{
|
|
|
|
std::set<std::string> seeds = h.url_seeds();
|
|
|
|
for (std::set<std::string>::iterator i = seeds.begin()
|
|
|
|
, end(seeds.end()); i != end; ++i)
|
|
|
|
{
|
2014-12-30 18:05:34 +01:00
|
|
|
h.remove_url_seed(*i);
|
2014-12-30 17:59:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
seeds = h.http_seeds();
|
|
|
|
for (std::set<std::string>::iterator i = seeds.begin()
|
|
|
|
, end(seeds.end()); i != end; ++i)
|
|
|
|
{
|
2014-12-30 18:05:34 +01:00
|
|
|
h.remove_http_seed(*i);
|
2014-12-30 17:59:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
if (c == 'D' && h.is_valid())
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
torrent_status const& st = view.get_active_torrent();
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("\n\nARE YOU SURE YOU WANT TO DELETE THE FILES FOR '%s'. THIS OPERATION CANNOT BE UNDONE. (y/N)"
|
2014-07-08 17:35:14 +02:00
|
|
|
, st.name.c_str());
|
2017-04-04 00:47:08 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
// enable terminal echo temporarily
|
|
|
|
set_keypress s(set_keypress::echo | set_keypress::canonical);
|
|
|
|
#endif
|
2014-07-08 17:35:14 +02:00
|
|
|
char response = 'n';
|
2016-08-19 07:05:43 +02:00
|
|
|
int ret = std::scanf("%c", &response);
|
|
|
|
if (ret == 1 && response == 'y')
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2017-03-26 20:20:40 +02:00
|
|
|
// also delete the resume file
|
|
|
|
std::string const rpath = resume_file(st.info_hash);
|
|
|
|
if (::remove(rpath.c_str()) < 0)
|
|
|
|
std::printf("failed to delete resume file (\"%s\")\n"
|
|
|
|
, rpath.c_str());
|
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
if (st.handle.is_valid())
|
2017-03-26 20:20:40 +02:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
ses.remove_torrent(st.handle, lt::session::delete_files);
|
2017-03-26 20:20:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::printf("failed to delete torrent, invalid handle: %s\n"
|
|
|
|
, st.name.c_str());
|
|
|
|
}
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
|
|
|
}
|
2012-09-23 03:52:46 +02:00
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
if (c == 'j' && h.is_valid())
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
h.force_recheck();
|
2012-09-23 03:52:46 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
if (c == 'r' && h.is_valid())
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
h.force_reannounce();
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
2012-09-23 03:52:46 +02:00
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
if (c == 's' && h.is_valid())
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
torrent_status const& ts = view.get_active_torrent();
|
|
|
|
h.set_sequential_download(!ts.sequential_download);
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2013-12-05 08:43:54 +01:00
|
|
|
if (c == 'R')
|
2011-04-10 22:35:45 +02:00
|
|
|
{
|
2013-12-05 08:43:54 +01:00
|
|
|
// save resume data for all torrents
|
2016-05-02 18:36:21 +02:00
|
|
|
std::vector<torrent_status> torr;
|
2017-03-26 20:20:40 +02:00
|
|
|
ses.get_torrent_status(&torr, [](torrent_status const& st)
|
|
|
|
{ return st.need_save_resume; }, 0);
|
2016-05-02 18:36:21 +02:00
|
|
|
for (torrent_status const& st : torr)
|
2011-04-10 22:35:45 +02:00
|
|
|
{
|
2017-03-26 20:20:40 +02:00
|
|
|
st.handle.save_resume_data(torrent_handle::save_info_dict);
|
|
|
|
++num_outstanding_resume_data;
|
2011-04-10 22:35:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
if (c == 'o' && h.is_valid())
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
torrent_status const& ts = view.get_active_torrent();
|
2013-12-05 08:43:54 +01:00
|
|
|
int num_pieces = ts.num_pieces;
|
|
|
|
if (num_pieces > 300) num_pieces = 300;
|
2016-12-22 16:42:33 +01:00
|
|
|
for (piece_index_t i(0); i < piece_index_t(num_pieces); ++i)
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
h.set_piece_deadline(i, (static_cast<int>(i)+5) * 1000
|
|
|
|
, torrent_handle::alert_when_available);
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
|
|
|
}
|
2008-09-07 11:59:54 +02:00
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
if (c == 'v' && h.is_valid())
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
h.scrape_tracker();
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
if (c == 'p' && h.is_valid())
|
2010-03-20 23:00:13 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
torrent_status const& ts = view.get_active_torrent();
|
2013-12-05 08:43:54 +01:00
|
|
|
if (!ts.auto_managed && ts.paused)
|
2011-07-04 07:33:06 +02:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
h.auto_managed(true);
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
h.auto_managed(false);
|
|
|
|
h.pause(torrent_handle::graceful_pause);
|
2011-07-04 07:33:06 +02:00
|
|
|
}
|
2010-03-20 23:00:13 +01:00
|
|
|
}
|
|
|
|
|
2013-12-05 08:43:54 +01:00
|
|
|
// toggle force-start
|
2014-07-08 17:35:14 +02:00
|
|
|
if (c == 'k' && h.is_valid())
|
2009-11-02 02:01:07 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
torrent_status const& ts = view.get_active_torrent();
|
|
|
|
h.auto_managed(!ts.auto_managed);
|
|
|
|
if (ts.auto_managed && ts.paused) h.resume();
|
2009-11-02 02:01:07 +01:00
|
|
|
}
|
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
if (c == 'c' && h.is_valid())
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
h.clear_error();
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2013-12-05 08:43:54 +01:00
|
|
|
// toggle displays
|
|
|
|
if (c == 't') print_trackers = !print_trackers;
|
|
|
|
if (c == 'i') print_peers = !print_peers;
|
|
|
|
if (c == 'l') print_log = !print_log;
|
|
|
|
if (c == 'd') print_downloads = !print_downloads;
|
2016-03-12 19:01:34 +01:00
|
|
|
if (c == 'y') print_matrix = !print_matrix;
|
2013-12-05 08:43:54 +01:00
|
|
|
if (c == 'f') print_file_progress = !print_file_progress;
|
|
|
|
if (c == 'P') show_pad_files = !show_pad_files;
|
|
|
|
if (c == 'g') show_dht_status = !show_dht_status;
|
2015-01-04 22:31:02 +01:00
|
|
|
if (c == 'u') ses_view.print_utp_stats(!ses_view.print_utp_stats());
|
2014-07-09 00:37:13 +02:00
|
|
|
if (c == 'x') print_disk_stats = !print_disk_stats;
|
2013-12-05 08:43:54 +01:00
|
|
|
// toggle columns
|
|
|
|
if (c == '1') print_ip = !print_ip;
|
|
|
|
if (c == '3') print_timers = !print_timers;
|
|
|
|
if (c == '4') print_block = !print_block;
|
|
|
|
if (c == '5') print_peer_rate = !print_peer_rate;
|
|
|
|
if (c == '6') print_fails = !print_fails;
|
|
|
|
if (c == '7') print_send_bufs = !print_send_bufs;
|
2016-03-06 05:16:33 +01:00
|
|
|
if (c == 'C')
|
|
|
|
{
|
|
|
|
cache_size = (cache_size == 0) ? -1 : 0;
|
|
|
|
settings_pack p;
|
|
|
|
p.set_int(settings_pack::cache_size, cache_size);
|
2016-08-05 06:28:03 +02:00
|
|
|
ses.apply_settings(std::move(p));
|
2016-03-06 05:16:33 +01:00
|
|
|
}
|
2013-12-05 08:43:54 +01:00
|
|
|
if (c == 'h')
|
2010-11-06 19:04:07 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
clear_screen();
|
|
|
|
set_cursor_pos(0,0);
|
|
|
|
print(
|
2017-05-07 22:18:14 +02:00
|
|
|
R"(HELP SCREEN (press any key to dismiss)
|
|
|
|
|
|
|
|
CLIENT OPTIONS
|
|
|
|
|
|
|
|
[q] quit client [m] add magnet link
|
|
|
|
|
|
|
|
TORRENT ACTIONS
|
2017-06-21 06:50:14 +02:00
|
|
|
[p] pause/resume selected torrent [C] toggle disk cache
|
2017-05-07 22:18:14 +02:00
|
|
|
[s] toggle sequential download [j] force recheck
|
|
|
|
[space] toggle session pause [c] clear error
|
|
|
|
[v] scrape [D] delete torrent and data
|
|
|
|
[r] force reannounce [R] save resume data for all torrents
|
|
|
|
[o] set piece deadlines (sequential dl) [P] toggle auto-managed
|
|
|
|
[k] toggle force-started [W] remove all web seeds
|
|
|
|
|
|
|
|
DISPLAY OPTIONS
|
|
|
|
left/right arrow keys: select torrent filter
|
|
|
|
up/down arrow keys: select torrent
|
|
|
|
[i] toggle show peers [d] toggle show downloading pieces
|
|
|
|
[u] show uTP stats [f] toggle show files
|
|
|
|
[g] show DHT [x] toggle disk cache stats
|
|
|
|
[t] show trackers [l] toggle show log
|
|
|
|
[P] show pad files (in file list) [y] toggle show piece matrix
|
|
|
|
|
|
|
|
COLUMN OPTIONS
|
|
|
|
[1] toggle IP column [2]
|
|
|
|
[3] toggle timers column [4] toggle block progress column
|
|
|
|
[5] toggle peer rate column [6] toggle failures column
|
|
|
|
[7] toggle send buffers column
|
|
|
|
)");
|
2013-12-05 08:43:54 +01:00
|
|
|
int tmp;
|
2017-05-03 05:45:43 +02:00
|
|
|
while (sleep_and_input(&tmp, lt::milliseconds(500)) == false);
|
2010-11-06 19:04:07 +01:00
|
|
|
}
|
2014-07-09 00:37:13 +02:00
|
|
|
|
2017-05-03 05:45:43 +02:00
|
|
|
} while (sleep_and_input(&c, lt::milliseconds(0)));
|
2013-12-05 08:43:54 +01:00
|
|
|
if (c == 'q') break;
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 01:07:15 +02:00
|
|
|
pop_alerts(view, ses_view, ses, events);
|
2004-07-18 02:39:58 +02:00
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
std::string out;
|
2008-07-06 14:22:56 +02:00
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
char str[500];
|
2008-05-07 20:02:11 +02:00
|
|
|
|
2014-07-09 00:37:13 +02:00
|
|
|
int pos = view.height() + ses_view.height();
|
|
|
|
set_cursor_pos(0, pos);
|
2008-09-20 19:42:25 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
int cache_flags = print_downloads ? 0 : lt::session::disk_cache_no_pieces;
|
2014-07-08 17:35:14 +02:00
|
|
|
torrent_handle h = view.get_active_handle();
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
cache_status cs;
|
|
|
|
ses.get_cache_info(&cs, h, cache_flags);
|
|
|
|
|
2009-08-30 08:25:17 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2009-02-26 08:09:56 +01:00
|
|
|
if (show_dht_status)
|
|
|
|
{
|
2015-01-17 18:02:58 +01:00
|
|
|
// TODO: 3 expose these counters as performance counters
|
|
|
|
/*
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(str, sizeof(str), "DHT nodes: %d DHT cached nodes: %d "
|
2013-09-25 21:44:29 +02:00
|
|
|
"total DHT size: %" PRId64 " total observers: %d\n"
|
2010-11-06 08:12:57 +01:00
|
|
|
, sess_stat.dht_nodes, sess_stat.dht_node_cache, sess_stat.dht_global_nodes
|
|
|
|
, sess_stat.dht_total_allocations);
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
2015-01-17 18:02:58 +01:00
|
|
|
*/
|
2015-02-08 04:24:56 +01:00
|
|
|
|
2010-12-12 04:17:08 +01:00
|
|
|
int bucket = 0;
|
2016-10-13 05:39:00 +02:00
|
|
|
for (dht_routing_bucket const& n : dht_routing_table)
|
2010-12-12 04:17:08 +01:00
|
|
|
{
|
2014-11-02 10:41:29 +01:00
|
|
|
char const* progress_bar =
|
|
|
|
"################################"
|
|
|
|
"################################"
|
|
|
|
"################################"
|
|
|
|
"################################";
|
2014-12-09 10:49:14 +01:00
|
|
|
char const* short_progress_bar = "--------";
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(str, sizeof(str)
|
2015-02-08 04:24:56 +01:00
|
|
|
, "%3d [%3d, %d] %s%s\x1b[K\n"
|
2016-10-13 05:39:00 +02:00
|
|
|
, bucket, n.num_nodes, n.num_replacements
|
|
|
|
, progress_bar + (128 - n.num_nodes)
|
|
|
|
, short_progress_bar + (8 - (std::min)(8, n.num_replacements)));
|
2010-12-12 04:17:08 +01:00
|
|
|
out += str;
|
2015-02-08 04:24:56 +01:00
|
|
|
pos += 1;
|
2010-12-12 04:17:08 +01:00
|
|
|
}
|
|
|
|
|
2016-10-13 05:39:00 +02:00
|
|
|
for (dht_lookup const& l : dht_active_requests)
|
2008-05-07 11:28:49 +02:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(str, sizeof(str)
|
2016-10-13 05:39:00 +02:00
|
|
|
, " %10s target: %s "
|
|
|
|
"[limit: %2d] "
|
2010-12-12 04:28:06 +01:00
|
|
|
"in-flight: %-2d "
|
|
|
|
"left: %-3d "
|
|
|
|
"1st-timeout: %-2d "
|
|
|
|
"timeouts: %-2d "
|
|
|
|
"responses: %-2d "
|
2016-10-13 05:39:00 +02:00
|
|
|
"last_sent: %-2d "
|
|
|
|
"\x1b[K\n"
|
|
|
|
, l.type
|
|
|
|
, to_hex(l.target).c_str()
|
|
|
|
, l.branch_factor
|
|
|
|
, l.outstanding_requests
|
|
|
|
, l.nodes_left
|
|
|
|
, l.first_timeout
|
|
|
|
, l.timeouts
|
|
|
|
, l.responses
|
|
|
|
, l.last_sent);
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
2015-02-08 04:24:56 +01:00
|
|
|
pos += 1;
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
|
|
|
}
|
2009-08-30 08:25:17 +02:00
|
|
|
#endif
|
2017-03-26 20:20:40 +02:00
|
|
|
time_point const now = clock_type::now();
|
2014-07-06 21:18:00 +02:00
|
|
|
if (h.is_valid())
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
torrent_status const& s = view.get_active_torrent();
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2015-02-08 04:24:56 +01:00
|
|
|
print((piece_bar(s.pieces, 126) + "\x1b[K\n").c_str());
|
|
|
|
pos += 1;
|
2014-07-10 10:32:32 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if ((print_downloads && s.state != torrent_status::seeding)
|
|
|
|
|| print_peers)
|
|
|
|
h.get_peer_info(peers);
|
2008-05-07 11:28:49 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (print_peers && !peers.empty())
|
2015-02-08 04:24:56 +01:00
|
|
|
pos += print_peer_info(out, peers, terminal_height - pos - 2);
|
2004-02-25 00:55:42 +01:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (print_trackers)
|
|
|
|
{
|
2016-10-13 05:39:00 +02:00
|
|
|
for (announce_entry const& ae : h.trackers())
|
2008-11-29 09:38:40 +01:00
|
|
|
{
|
2017-04-28 05:34:39 +02:00
|
|
|
auto best_ae = std::min_element(ae.endpoints.begin(), ae.endpoints.end()
|
|
|
|
, [](announce_endpoint const& l, announce_endpoint const& r) { return l.fails < r.fails; } );
|
|
|
|
|
2015-02-08 04:24:56 +01:00
|
|
|
if (pos + 1 >= terminal_height) break;
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(str, sizeof(str), "%2d %-55s fails: %-3d (%-3d) %s %s %5d \"%s\" %s\x1b[K\n"
|
2017-04-28 05:34:39 +02:00
|
|
|
, ae.tier, ae.url.c_str()
|
|
|
|
, best_ae != ae.endpoints.end() ? best_ae->fails : 0, ae.fail_limit, ae.verified?"OK ":"- "
|
2017-05-15 02:09:45 +02:00
|
|
|
, to_string(best_ae != ae.endpoints.end() ? int(total_seconds(best_ae->next_announce - now)) : 0, 8).c_str()
|
|
|
|
, best_ae != ae.endpoints.end() && best_ae->min_announce > now ? int(total_seconds(best_ae->min_announce - now)) : 0
|
2017-04-28 05:34:39 +02:00
|
|
|
, best_ae != ae.endpoints.end() && best_ae->last_error ? best_ae->last_error.message().c_str() : ""
|
|
|
|
, best_ae != ae.endpoints.end() ? best_ae->message.c_str() : "");
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
2015-02-08 04:24:56 +01:00
|
|
|
pos += 1;
|
2008-11-29 09:38:40 +01:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2008-11-29 09:38:40 +01:00
|
|
|
|
2016-03-12 19:01:34 +01:00
|
|
|
if (print_matrix)
|
|
|
|
{
|
|
|
|
int height = 0;
|
2017-05-07 22:18:14 +02:00
|
|
|
print(piece_matrix(s.pieces, std::min(terminal_width, 160), &height).c_str());
|
2016-03-12 19:01:34 +01:00
|
|
|
pos += height;
|
|
|
|
}
|
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (print_downloads)
|
|
|
|
{
|
|
|
|
h.get_download_queue(queue);
|
2011-07-10 10:57:27 +02:00
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
std::sort(queue.begin(), queue.end()
|
|
|
|
, [] (partial_piece_info const& lhs, partial_piece_info const& rhs)
|
|
|
|
{ return lhs.piece_index < rhs.piece_index; });
|
2008-02-08 11:22:05 +01:00
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
std::sort(cs.pieces.begin(), cs.pieces.end()
|
|
|
|
, [](cached_piece_info const& lhs, cached_piece_info const& rhs)
|
|
|
|
{ return lhs.piece < rhs.piece; });
|
2008-02-08 11:22:05 +01:00
|
|
|
|
2015-02-08 22:59:52 +01:00
|
|
|
int p = 0; // this is horizontal position
|
2017-03-26 20:20:40 +02:00
|
|
|
for (cached_piece_info const& i : cs.pieces)
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2015-02-08 04:24:56 +01:00
|
|
|
if (pos + 3 >= terminal_height) break;
|
|
|
|
|
2016-07-09 22:26:26 +02:00
|
|
|
partial_piece_info* pp = nullptr;
|
2011-07-10 10:57:27 +02:00
|
|
|
partial_piece_info tmp;
|
2017-03-26 20:20:40 +02:00
|
|
|
tmp.piece_index = i.piece;
|
2011-07-10 10:57:27 +02:00
|
|
|
std::vector<partial_piece_info>::iterator ppi
|
|
|
|
= std::lower_bound(queue.begin(), queue.end(), tmp
|
2016-05-25 06:31:52 +02:00
|
|
|
, [](partial_piece_info const& lhs, partial_piece_info const& rhs)
|
|
|
|
{ return lhs.piece_index < rhs.piece_index; });
|
|
|
|
|
2017-03-26 20:20:40 +02:00
|
|
|
if (ppi != queue.end() && ppi->piece_index == i.piece) pp = &*ppi;
|
2011-07-10 10:57:27 +02:00
|
|
|
|
2017-03-26 20:20:40 +02:00
|
|
|
print_piece(pp, &i, peers, out);
|
2011-07-10 10:57:27 +02:00
|
|
|
|
2017-03-26 20:20:40 +02:00
|
|
|
int num_blocks = pp ? pp->blocks_in_piece : int(i.blocks.size());
|
2015-02-08 04:24:56 +01:00
|
|
|
p += num_blocks + 8;
|
|
|
|
bool continuous_mode = 8 + num_blocks > terminal_width;
|
|
|
|
if (continuous_mode)
|
|
|
|
{
|
|
|
|
while (p > terminal_width)
|
|
|
|
{
|
|
|
|
p -= terminal_width;
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (p + num_blocks + 8 > terminal_width)
|
2014-10-07 05:09:32 +02:00
|
|
|
{
|
2015-02-08 04:24:56 +01:00
|
|
|
out += "\x1b[K\n";
|
|
|
|
pos += 1;
|
|
|
|
p = 0;
|
2014-10-07 05:09:32 +02:00
|
|
|
}
|
|
|
|
|
2011-07-10 10:57:27 +02:00
|
|
|
if (pp) queue.erase(ppi);
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
2006-06-11 15:48:39 +02:00
|
|
|
|
2017-03-26 20:20:40 +02:00
|
|
|
for (partial_piece_info const& i : queue)
|
2006-06-11 15:48:39 +02:00
|
|
|
{
|
2015-02-08 04:24:56 +01:00
|
|
|
if (pos + 3 >= terminal_height) break;
|
|
|
|
|
2017-03-26 20:20:40 +02:00
|
|
|
print_piece(&i, nullptr, peers, out);
|
2014-10-07 05:09:32 +02:00
|
|
|
|
2017-03-26 20:20:40 +02:00
|
|
|
int num_blocks = i.blocks_in_piece;
|
2015-02-08 04:24:56 +01:00
|
|
|
p += num_blocks + 8;
|
|
|
|
bool continuous_mode = 8 + num_blocks > terminal_width;
|
|
|
|
if (continuous_mode)
|
|
|
|
{
|
|
|
|
while (p > terminal_width)
|
|
|
|
{
|
|
|
|
p -= terminal_width;
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (p + num_blocks + 8 > terminal_width)
|
2014-10-07 05:09:32 +02:00
|
|
|
{
|
2015-02-08 04:24:56 +01:00
|
|
|
out += "\x1b[K\n";
|
|
|
|
pos += 1;
|
|
|
|
p = 0;
|
2014-10-07 05:09:32 +02:00
|
|
|
}
|
2006-06-11 15:48:39 +02:00
|
|
|
}
|
2015-02-08 04:24:56 +01:00
|
|
|
if (p != 0)
|
|
|
|
{
|
|
|
|
out += "\x1b[K\n";
|
|
|
|
pos += 1;
|
|
|
|
}
|
2014-10-07 05:09:32 +02:00
|
|
|
|
2017-05-07 22:18:14 +02:00
|
|
|
std::snprintf(str, sizeof(str), "%s %s read cache | %s %s downloading | %s %s cached | %s %s flushed | %s %s snubbed | = requested\x1b[K\n"
|
2011-07-10 10:57:27 +02:00
|
|
|
, esc("34;7"), esc("0") // read cache
|
|
|
|
, esc("33;7"), esc("0") // downloading
|
|
|
|
, esc("36;7"), esc("0") // cached
|
2014-12-07 23:22:38 +01:00
|
|
|
, esc("32;7"), esc("0") // flushed
|
|
|
|
, esc("35;7"), esc("0") // snubbed
|
|
|
|
);
|
2011-07-10 11:07:40 +02:00
|
|
|
out += str;
|
2015-02-08 04:24:56 +01:00
|
|
|
pos += 1;
|
2003-10-30 00:28:09 +01:00
|
|
|
}
|
2009-05-14 21:49:33 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (print_file_progress && s.has_metadata)
|
2003-12-22 08:14:35 +01:00
|
|
|
{
|
2016-06-18 20:01:38 +02:00
|
|
|
std::vector<std::int64_t> file_progress;
|
2009-02-26 08:09:56 +01:00
|
|
|
h.file_progress(file_progress);
|
2017-04-09 00:24:50 +02:00
|
|
|
std::vector<open_file_state> file_status = h.file_status();
|
2014-07-06 21:18:00 +02:00
|
|
|
std::vector<int> file_prio = h.file_priorities();
|
2017-04-09 00:24:50 +02:00
|
|
|
auto f = file_status.begin();
|
2016-08-17 23:26:35 +02:00
|
|
|
std::shared_ptr<const torrent_info> ti = h.torrent_file();
|
2015-02-08 22:59:52 +01:00
|
|
|
|
|
|
|
int p = 0; // this is horizontal position
|
2016-12-22 16:42:33 +01:00
|
|
|
for (file_index_t i(0); i < file_index_t(ti->num_files()); ++i)
|
2004-07-24 13:54:17 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
int const idx = static_cast<int>(i);
|
2015-02-08 04:24:56 +01:00
|
|
|
if (pos + 1 >= terminal_height) break;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
bool pad_file = ti->files().pad_file_at(i);
|
|
|
|
if (pad_file)
|
|
|
|
{
|
|
|
|
if (show_pad_files)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(str, sizeof(str), "\x1b[34m%-70s %s\x1b[0m\x1b[K\n"
|
2016-08-13 13:04:53 +02:00
|
|
|
, ti->files().file_name(i).to_string().c_str()
|
2014-07-06 21:18:00 +02:00
|
|
|
, add_suffix(ti->files().file_size(i)).c_str());
|
|
|
|
out += str;
|
2015-02-08 04:24:56 +01:00
|
|
|
pos += 1;
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-04-09 15:37:06 +02:00
|
|
|
int const progress = ti->files().file_size(i) > 0
|
2016-12-22 16:42:33 +01:00
|
|
|
? int(file_progress[idx] * 1000 / ti->files().file_size(i)) : 1000;
|
2017-04-09 15:37:06 +02:00
|
|
|
assert(file_progress[idx] <= ti->files().file_size(i));
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2017-04-09 15:37:06 +02:00
|
|
|
bool const complete = file_progress[idx] == ti->files().file_size(i);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-08-13 13:04:53 +02:00
|
|
|
std::string title = ti->files().file_name(i).to_string();
|
2014-07-06 21:18:00 +02:00
|
|
|
if (!complete)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(str, sizeof(str), " (%.1f%%)", progress / 10.f);
|
2014-07-06 21:18:00 +02:00
|
|
|
title += str;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f != file_status.end() && f->file_index == i)
|
|
|
|
{
|
|
|
|
title += " [ ";
|
2017-05-06 05:09:36 +02:00
|
|
|
if ((f->open_mode & file_open_mode::rw_mask) == file_open_mode::read_write) title += "read/write ";
|
|
|
|
else if ((f->open_mode & file_open_mode::rw_mask) == file_open_mode::read_only) title += "read ";
|
|
|
|
else if ((f->open_mode & file_open_mode::rw_mask) == file_open_mode::write_only) title += "write ";
|
2017-04-09 00:24:50 +02:00
|
|
|
if (f->open_mode & file_open_mode::random_access) title += "random_access ";
|
|
|
|
if (f->open_mode & file_open_mode::locked) title += "locked ";
|
|
|
|
if (f->open_mode & file_open_mode::sparse) title += "sparse ";
|
2014-07-06 21:18:00 +02:00
|
|
|
title += "]";
|
|
|
|
++f;
|
|
|
|
}
|
|
|
|
|
2015-02-08 22:59:52 +01:00
|
|
|
const int file_progress_width = 65;
|
|
|
|
|
|
|
|
// do we need to line-break?
|
|
|
|
if (p + file_progress_width + 13 > terminal_width)
|
|
|
|
{
|
|
|
|
out += "\x1b[K\n";
|
|
|
|
pos += 1;
|
|
|
|
p = 0;
|
|
|
|
}
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(str, sizeof(str), "%s %7s p: %d ",
|
2015-02-08 22:59:52 +01:00
|
|
|
progress_bar(progress, file_progress_width, complete ? col_green : col_yellow, '-', '#'
|
2014-07-06 21:18:00 +02:00
|
|
|
, title.c_str()).c_str()
|
2016-12-22 16:42:33 +01:00
|
|
|
, add_suffix(file_progress[idx]).c_str()
|
|
|
|
, file_prio[idx]);
|
2015-02-08 22:59:52 +01:00
|
|
|
|
|
|
|
p += file_progress_width + 13;
|
2009-05-14 21:49:33 +02:00
|
|
|
out += str;
|
2015-02-08 22:59:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p != 0)
|
|
|
|
{
|
|
|
|
out += "\x1b[K\n";
|
2015-02-08 04:24:56 +01:00
|
|
|
pos += 1;
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
2003-12-22 08:14:35 +01:00
|
|
|
}
|
2014-07-10 10:32:32 +02:00
|
|
|
}
|
2005-10-19 02:50:38 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (print_log)
|
|
|
|
{
|
2017-03-26 20:20:40 +02:00
|
|
|
for (auto const& e : events)
|
2005-10-19 02:50:38 +02:00
|
|
|
{
|
2015-02-08 04:24:56 +01:00
|
|
|
if (pos + 1 >= terminal_height) break;
|
2017-03-26 20:20:40 +02:00
|
|
|
out += e;
|
2015-02-08 04:24:56 +01:00
|
|
|
out += "\x1b[K\n";
|
|
|
|
pos += 1;
|
2005-10-19 02:50:38 +02:00
|
|
|
}
|
2003-10-30 00:28:09 +01:00
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2015-02-08 04:24:56 +01:00
|
|
|
// clear rest of screen
|
|
|
|
out += "\x1b[J";
|
2014-07-08 17:35:14 +02:00
|
|
|
print(out.c_str());
|
2015-02-08 04:24:56 +01:00
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
std::fflush(stdout);
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2017-03-26 20:20:40 +02:00
|
|
|
if (!monitor_dir.empty() && next_dir_scan < now)
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2017-03-26 20:20:40 +02:00
|
|
|
scan_dir(monitor_dir, ses);
|
|
|
|
next_dir_scan = now + seconds(poll_interval);
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2008-04-05 06:53:22 +02:00
|
|
|
|
2017-05-05 04:46:30 +02:00
|
|
|
resume_data_loader.join();
|
|
|
|
|
2010-01-31 22:30:43 +01:00
|
|
|
ses.pause();
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("saving resume data\n");
|
2017-03-25 23:00:23 +01:00
|
|
|
|
|
|
|
// get all the torrent handles that we need to save resume data for
|
2011-02-01 10:48:28 +01:00
|
|
|
std::vector<torrent_status> temp;
|
2017-03-25 23:00:23 +01:00
|
|
|
ses.get_torrent_status(&temp, [](torrent_status const& st)
|
2010-01-31 22:30:43 +01:00
|
|
|
{
|
2017-03-25 23:00:23 +01:00
|
|
|
if (!st.handle.is_valid()) return false;
|
|
|
|
if (!st.has_metadata) return false;
|
|
|
|
if (!st.need_save_resume) return false;
|
|
|
|
return true;
|
|
|
|
}, 0);
|
2010-01-31 22:30:43 +01:00
|
|
|
|
2017-03-27 01:07:15 +02:00
|
|
|
int idx = 0;
|
2017-03-25 23:00:23 +01:00
|
|
|
for (auto const& st : temp)
|
|
|
|
{
|
2010-01-31 22:30:43 +01:00
|
|
|
// save_resume_data will generate an alert when it's done
|
2017-03-26 20:20:40 +02:00
|
|
|
st.handle.save_resume_data(torrent_handle::save_info_dict);
|
2011-07-04 07:33:06 +02:00
|
|
|
++num_outstanding_resume_data;
|
2017-03-27 01:07:15 +02:00
|
|
|
++idx;
|
|
|
|
if ((idx % 32) == 0)
|
|
|
|
{
|
|
|
|
std::printf("\r%d ", num_outstanding_resume_data);
|
|
|
|
pop_alerts(view, ses_view, ses, events);
|
|
|
|
}
|
2010-01-31 22:30:43 +01:00
|
|
|
}
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("\nwaiting for resume data [%d]\n", num_outstanding_resume_data);
|
2010-01-31 22:30:43 +01:00
|
|
|
|
2011-07-04 07:33:06 +02:00
|
|
|
while (num_outstanding_resume_data > 0)
|
2010-01-31 22:30:43 +01:00
|
|
|
{
|
2011-01-24 01:11:13 +01:00
|
|
|
alert const* a = ses.wait_for_alert(seconds(10));
|
2016-07-09 22:26:26 +02:00
|
|
|
if (a == nullptr) continue;
|
2017-03-27 01:07:15 +02:00
|
|
|
pop_alerts(view, ses_view, ses, events);
|
2010-01-31 22:30:43 +01:00
|
|
|
}
|
2011-03-14 03:59:46 +01:00
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
if (g_log_file) std::fclose(g_log_file);
|
2016-03-05 21:15:22 +01:00
|
|
|
|
|
|
|
// we're just saving the DHT state
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("\nsaving session state\n");
|
2010-03-04 17:42:39 +01:00
|
|
|
{
|
|
|
|
entry session_state;
|
2016-03-05 21:15:22 +01:00
|
|
|
ses.save_state(session_state, session::save_dht_state);
|
2009-05-13 03:02:06 +02:00
|
|
|
|
|
|
|
std::vector<char> out;
|
|
|
|
bencode(std::back_inserter(out), session_state);
|
|
|
|
save_file(".ses_state", out);
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2016-03-05 21:15:22 +01:00
|
|
|
#endif
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2017-03-26 20:20:40 +02:00
|
|
|
std::printf("closing session\n");
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-03-26 20:20:40 +02:00
|
|
|
|