mirror of
https://github.com/asamy/ctorrent
synced 2025-10-05 23:52:41 +02:00
Cleanup and fixes
This commit is contained in:
@@ -116,12 +116,15 @@ TrackerQuery Torrent::makeTrackerQuery(TrackerEvent event)
|
||||
return q;
|
||||
}
|
||||
|
||||
Torrent::DownloadState Torrent::prepare(uint16_t port)
|
||||
Torrent::DownloadState Torrent::prepare(uint16_t port, bool seeder)
|
||||
{
|
||||
if (isFinished())
|
||||
if (seeder) {
|
||||
if (!m_listener && !(m_listener = new(std::nothrow) Server(port)))
|
||||
return DownloadState::NetworkError;
|
||||
} else if (isFinished())
|
||||
return DownloadState::AlreadyDownloaded;
|
||||
|
||||
if (!queryTrackers(makeTrackerQuery(TrackerEvent::Started), port))
|
||||
if (!queryTrackers(makeTrackerQuery(TrackerEvent::Started), seeder ? port : 0))
|
||||
return DownloadState::TrackerQueryFailure;
|
||||
|
||||
m_startTime = clock();
|
||||
@@ -142,42 +145,29 @@ bool Torrent::finish()
|
||||
return !!(event == TrackerEvent::Completed);
|
||||
}
|
||||
|
||||
void Torrent::checkTrackers()
|
||||
bool Torrent::checkTrackers()
|
||||
{
|
||||
uint32_t failed = 0;
|
||||
for (Tracker *tracker : m_activeTrackers)
|
||||
if (tracker->timeUp())
|
||||
tracker->query(makeTrackerQuery(TrackerEvent::None));
|
||||
failed |= !tracker->query(makeTrackerQuery(TrackerEvent::None));
|
||||
return !!failed;
|
||||
}
|
||||
|
||||
bool Torrent::seed(uint16_t port)
|
||||
bool Torrent::nextConnection()
|
||||
{
|
||||
if (!m_listener) {
|
||||
m_listener = new(std::nothrow) Server(port);
|
||||
if (!m_listener)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!queryTrackers(makeTrackerQuery(TrackerEvent::Started), port))
|
||||
return false;
|
||||
|
||||
// Just loop and wait for new connections
|
||||
while (!m_listener->stopped()) {
|
||||
if (m_listener) {
|
||||
m_listener->accept(
|
||||
[this] (const ConnectionPtr &c) {
|
||||
auto peer = std::make_shared<Peer>(c, this);
|
||||
peer->verify();
|
||||
}
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
TrackerQuery q = makeTrackerQuery(TrackerEvent::Stopped);
|
||||
for (Tracker *tracker : m_activeTrackers) {
|
||||
tracker->query(q);
|
||||
delete tracker;
|
||||
}
|
||||
|
||||
m_activeTrackers.clear();
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Torrent::queryTrackers(const TrackerQuery &query, uint16_t port)
|
||||
|
@@ -55,10 +55,11 @@ public:
|
||||
Torrent();
|
||||
~Torrent();
|
||||
|
||||
DownloadState prepare(uint16_t port);
|
||||
void checkTrackers();
|
||||
DownloadState prepare(uint16_t port, bool seeder);
|
||||
bool checkTrackers();
|
||||
bool open(const std::string& fileName, const std::string &downloadDir);
|
||||
bool seed(uint16_t port);
|
||||
bool prepareforSeed(uint16_t port);
|
||||
bool nextConnection();
|
||||
bool finish();
|
||||
bool isFinished() const { return m_fileManager.totalPieces() == m_fileManager.completedPieces(); }
|
||||
bool hasTrackers() const { return !m_activeTrackers.empty(); }
|
||||
|
25
main.cpp
25
main.cpp
@@ -167,7 +167,6 @@ int main(int argc, char *argv[])
|
||||
attrset(A_BOLD); // boldy
|
||||
curs_set(0); // don't show cursor
|
||||
#endif
|
||||
|
||||
size_t total = files.size();
|
||||
size_t completed = 0;
|
||||
size_t errors = 0;
|
||||
@@ -175,7 +174,7 @@ int main(int argc, char *argv[])
|
||||
size_t started = 0;
|
||||
|
||||
Torrent torrents[total];
|
||||
for (size_t i = 0; i < files.size(); ++i) {
|
||||
for (size_t i = 0; i < total; ++i) {
|
||||
std::string file = files[i];
|
||||
Torrent *t = &torrents[i];
|
||||
|
||||
@@ -194,7 +193,7 @@ int main(int argc, char *argv[])
|
||||
continue;
|
||||
}
|
||||
|
||||
Torrent::DownloadState state = t->prepare(startport++);
|
||||
Torrent::DownloadState state = t->prepare(startport++, !noseed);
|
||||
switch (state) {
|
||||
case Torrent::DownloadState::None:
|
||||
++started;
|
||||
@@ -214,10 +213,14 @@ int main(int argc, char *argv[])
|
||||
break;
|
||||
|
||||
Torrent *t = &torrents[completed];
|
||||
if (t->hasTrackers() && t->isFinished())
|
||||
if (t->hasTrackers() && t->isFinished()) {
|
||||
t->finish();
|
||||
else
|
||||
++completed;
|
||||
} else {
|
||||
t->checkTrackers();
|
||||
if (!noseed)
|
||||
t->nextConnection();
|
||||
}
|
||||
|
||||
Connection::poll();
|
||||
print_all_stats(torrents, total);
|
||||
@@ -228,13 +231,19 @@ int main(int argc, char *argv[])
|
||||
if (!noseed && completed > 0) {
|
||||
for (int i = 0; i < total; ++i) {
|
||||
Torrent *t = &torrents[i];
|
||||
if (!t->hasTrackers() && t->prepare(startport - i) > Torrent::DownloadState::Completed)
|
||||
if (!t->hasTrackers())
|
||||
++eseed;
|
||||
}
|
||||
|
||||
while (eseed != total) {
|
||||
for (int i = 0; i < total; ++i)
|
||||
torrents[i].checkTrackers();
|
||||
for (int i = 0; i < total; ++i) {
|
||||
Torrent *t = &torrents[i];
|
||||
if (!t->nextConnection() || !t->checkTrackers())
|
||||
++eseed;
|
||||
}
|
||||
|
||||
if (eseed == total)
|
||||
break;
|
||||
|
||||
Connection::poll();
|
||||
print_all_stats(torrents, total);
|
||||
|
@@ -29,7 +29,8 @@ Server::Server(uint16_t port)
|
||||
|
||||
Server::~Server()
|
||||
{
|
||||
m_acceptor.cancel();
|
||||
boost::system::error_code ec;
|
||||
m_acceptor.cancel(ec);
|
||||
m_acceptor.close();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user