Enable customizing the save statistics time interval
[qBittorrent.git] / src / base / bittorrent / tracker.h
blob8370d39587950622209c6585c06aae78e4ba60af
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2019 Mike Tzou (Chocobo1)
4 * Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
5 * Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * In addition, as a special exception, the copyright holders give permission to
22 * link this program with the OpenSSL project's "OpenSSL" library (or with
23 * modified versions of it that use the same license as the "OpenSSL" library),
24 * and distribute the linked executables. You must obey the GNU General Public
25 * License in all respects for all of the code used other than "OpenSSL". If you
26 * modify file(s), you may extend this exception to your version of the file(s),
27 * but you are not obligated to do so. If you do not wish to do so, delete this
28 * exception statement from your version.
31 #pragma once
33 #include <string>
35 #include <libtorrent/entry.hpp>
37 #include <QtTypes>
38 #include <QHash>
39 #include <QObject>
40 #include <QSet>
42 #include "base/bittorrent/infohash.h"
43 #include "base/http/irequesthandler.h"
44 #include "base/http/responsebuilder.h"
46 namespace Http
48 class Server;
51 namespace BitTorrent
53 struct Peer
55 QByteArray peerId;
56 ushort port = 0; // self-claimed by peer, might not be the same as socket port
57 bool isSeeder = false;
59 // caching precomputed values
60 lt::entry::string_type address;
61 lt::entry::string_type endpoint;
63 QByteArray uniqueID() const;
66 bool operator==(const Peer &left, const Peer &right);
67 std::size_t qHash(const Peer &key, std::size_t seed = 0);
69 // *Basic* Bittorrent tracker implementation
70 // [BEP-3] The BitTorrent Protocol Specification
71 // also see: https://wiki.theory.org/index.php/BitTorrentSpecification#Tracker_HTTP.2FHTTPS_Protocol
72 class Tracker final : public QObject, public Http::IRequestHandler, private Http::ResponseBuilder
74 Q_OBJECT
75 Q_DISABLE_COPY_MOVE(Tracker)
77 struct TrackerAnnounceRequest;
79 struct TorrentStats
81 qint64 seeders = 0;
82 QSet<Peer> peers;
84 void setPeer(const Peer &peer);
85 bool removePeer(const Peer &peer);
88 public:
89 explicit Tracker(QObject *parent = nullptr);
91 bool start();
93 private:
94 Http::Response processRequest(const Http::Request &request, const Http::Environment &env) override;
95 void processAnnounceRequest();
97 void registerPeer(const TrackerAnnounceRequest &announceReq);
98 void unregisterPeer(const TrackerAnnounceRequest &announceReq);
99 void prepareAnnounceResponse(const TrackerAnnounceRequest &announceReq);
101 Http::Server *m_server = nullptr;
102 Http::Request m_request;
103 Http::Environment m_env;
105 QHash<TorrentID, TorrentStats> m_torrents;