| 1 |
/* |
|---|
| 2 |
* Copyright (C) 2005-2006 Alo Sarv <madcat_@users.sourceforge.net> |
|---|
| 3 |
* |
|---|
| 4 |
* This program is free software; you can redistribute it and/or modify |
|---|
| 5 |
* it under the terms of the GNU General Public License as published by |
|---|
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
|---|
| 7 |
* (at your option) any later version. |
|---|
| 8 |
* |
|---|
| 9 |
* This program is distributed in the hope that it will be useful, |
|---|
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 |
* GNU General Public License for more details. |
|---|
| 13 |
* |
|---|
| 14 |
* You should have received a copy of the GNU General Public License |
|---|
| 15 |
* along with this program; if not, write to the Free Software |
|---|
| 16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 17 |
*/ |
|---|
| 18 |
|
|---|
| 19 |
#ifndef __BT_CLIENT_H__ |
|---|
| 20 |
#define __BT_CLIENT_H__ |
|---|
| 21 |
|
|---|
| 22 |
#include <hncore/bt/types.h> |
|---|
| 23 |
#include <hncore/fwd.h> |
|---|
| 24 |
#include <hncore/baseclient.h> |
|---|
| 25 |
#include <hnbase/hash.h> |
|---|
| 26 |
#include <queue> |
|---|
| 27 |
|
|---|
| 28 |
namespace Bt { |
|---|
| 29 |
|
|---|
| 30 |
class Client : public BaseClient, public Trackable { |
|---|
| 31 |
public: |
|---|
| 32 |
Client(TcpSocket *sock); |
|---|
| 33 |
Client(IPV4Address addr); |
|---|
| 34 |
~Client(); |
|---|
| 35 |
|
|---|
| 36 |
boost::signal<void (Client*)> handshakeReceived; |
|---|
| 37 |
boost::signal<void (Client*)> connectionLost; |
|---|
| 38 |
|
|---|
| 39 |
// outgoing packets |
|---|
| 40 |
void sendPing(); |
|---|
| 41 |
void sendChoke(); |
|---|
| 42 |
void sendInterested(); |
|---|
| 43 |
void sendUnchoke(); |
|---|
| 44 |
void sendUninterested(); |
|---|
| 45 |
void sendHave(uint32_t index); |
|---|
| 46 |
void sendRequest(uint32_t index, uint32_t offset, uint32_t length); |
|---|
| 47 |
void sendCancel(uint32_t index, uint32_t offset, uint32_t length); |
|---|
| 48 |
void sendPiece(uint32_t index, uint32_t offset,const std::string &data); |
|---|
| 49 |
void sendBitfield(); |
|---|
| 50 |
|
|---|
| 51 |
bool isChoking() const { return m_isChoking; } |
|---|
| 52 |
bool isInterested() const { return m_isInterested; } |
|---|
| 53 |
bool amChoking() const { return m_amChoking; } |
|---|
| 54 |
bool amInterested() const { return m_amInterested; } |
|---|
| 55 |
|
|---|
| 56 |
Hash<SHA1Hash> getInfoHash() const { return m_infoHash; } |
|---|
| 57 |
std::string getPeerId() const { return m_peerId; } |
|---|
| 58 |
IPV4Address getAddr() const { return m_addr; } |
|---|
| 59 |
bool isFullSource() const { return m_bitField.size(); } |
|---|
| 60 |
|
|---|
| 61 |
void setFile(SharedFile *file); |
|---|
| 62 |
void setFile(PartData *file); |
|---|
| 63 |
void setTorrent(Torrent *t); |
|---|
| 64 |
|
|---|
| 65 |
struct Request { |
|---|
| 66 |
Request(); |
|---|
| 67 |
Request(uint64_t index, uint64_t offset, uint64_t length); |
|---|
| 68 |
Request(::Detail::LockedRangePtr r, Torrent *t); |
|---|
| 69 |
|
|---|
| 70 |
friend bool operator<(const Request &x, const Request &y) { |
|---|
| 71 |
if (x.m_index == y.m_index) { |
|---|
| 72 |
return x.m_offset < y.m_offset; |
|---|
| 73 |
} |
|---|
| 74 |
return x.m_index < y.m_index; |
|---|
| 75 |
} |
|---|
| 76 |
friend bool operator==(const Request &x, const Request &y) { |
|---|
| 77 |
return x.m_index == y.m_index && |
|---|
| 78 |
x.m_offset == y.m_offset && |
|---|
| 79 |
x.m_length == y.m_length; |
|---|
| 80 |
} |
|---|
| 81 |
friend bool operator!=(const Request &x, const Request &y) { |
|---|
| 82 |
return !(x == y); |
|---|
| 83 |
} |
|---|
| 84 |
uint64_t m_index; |
|---|
| 85 |
uint64_t m_offset; |
|---|
| 86 |
uint64_t m_length; |
|---|
| 87 |
|
|---|
| 88 |
::Detail::LockedRangePtr m_locked; |
|---|
| 89 |
}; |
|---|
| 90 |
|
|---|
| 91 |
// implement BaseClient interface |
|---|
| 92 |
virtual uint32_t getUploadSpeed() const; |
|---|
| 93 |
virtual uint32_t getDownloadSpeed() const; |
|---|
| 94 |
virtual uint64_t getSessionUploaded() const; |
|---|
| 95 |
virtual uint64_t getSessionDownloaded() const; |
|---|
| 96 |
virtual uint64_t getTotalUploaded() const; |
|---|
| 97 |
virtual uint64_t getTotalDownloaded() const; |
|---|
| 98 |
virtual std::string getSoft() const { return m_clientSoft; } |
|---|
| 99 |
virtual std::string getSoftVersion() const { return m_clientVersion;} |
|---|
| 100 |
private: |
|---|
| 101 |
Client(const Client&); |
|---|
| 102 |
Client& operator=(const Client&); |
|---|
| 103 |
|
|---|
| 104 |
void onSocketEvent(TcpSocket *sock, SocketEvent evt); |
|---|
| 105 |
void parseBuffer(); |
|---|
| 106 |
void parsePacket(BEIStream &i, uint32_t len); |
|---|
| 107 |
void sendHandshake(); |
|---|
| 108 |
void sendNextChunk(); |
|---|
| 109 |
void checkNeedParts(); |
|---|
| 110 |
void sendRequests(); |
|---|
| 111 |
|
|---|
| 112 |
// incoming packets |
|---|
| 113 |
void onPing(); |
|---|
| 114 |
void onChoke(); |
|---|
| 115 |
void onUnchoke(); |
|---|
| 116 |
void onInterested(); |
|---|
| 117 |
void onUninterested(); |
|---|
| 118 |
void onHave(uint32_t index); |
|---|
| 119 |
void onBitfield(const std::string &bits); |
|---|
| 120 |
void onRequest(uint32_t index, uint32_t offset, uint32_t length); |
|---|
| 121 |
void onPiece(uint32_t index, uint32_t offset, const std::string &data); |
|---|
| 122 |
void onCancel(uint32_t index, uint32_t offset, uint32_t length); |
|---|
| 123 |
|
|---|
| 124 |
void onVerified(PartData *file, uint32_t chunkSize, uint32_t chunk); |
|---|
| 125 |
void onDestroyed(PartData *file); |
|---|
| 126 |
|
|---|
| 127 |
void updateSignals(); |
|---|
| 128 |
void parsePeerId(const std::string &peerId); |
|---|
| 129 |
|
|---|
| 130 |
boost::scoped_ptr<TcpSocket> m_socket; |
|---|
| 131 |
|
|---|
| 132 |
std::string m_peerId; //!< Remote peer ID |
|---|
| 133 |
Hash<SHA1Hash> m_infoHash; //!< Torrent info hash |
|---|
| 134 |
IPV4Address m_addr; //!< IP of the client |
|---|
| 135 |
std::string m_clientSoft; //!< Client's software / program |
|---|
| 136 |
std::string m_clientVersion; //!< Version of client's software |
|---|
| 137 |
|
|---|
| 138 |
/** |
|---|
| 139 |
* @name State variables |
|---|
| 140 |
*/ |
|---|
| 141 |
//!@{ |
|---|
| 142 |
bool m_isChoking; //!< The client is choking us |
|---|
| 143 |
bool m_isInterested; //!< The client is interested |
|---|
| 144 |
bool m_amChoking; //!< We are choking the client |
|---|
| 145 |
bool m_amInterested; //!< We are interested |
|---|
| 146 |
bool m_handshakeSent; //!< We sent handshake |
|---|
| 147 |
bool m_needParts; //!< We need parts from client |
|---|
| 148 |
std::vector<bool> m_bitField; //!< Parts the client has |
|---|
| 149 |
|
|---|
| 150 |
std::string m_inBuffer; //!< incoming data buffer |
|---|
| 151 |
std::list<Request> m_requests; //!< incoming requests |
|---|
| 152 |
std::deque<Request> m_outRequests; //!< outgoing chunk requests |
|---|
| 153 |
|
|---|
| 154 |
::Detail::UsedRangePtr m_usedRange; //!< currently used range |
|---|
| 155 |
//!@} |
|---|
| 156 |
|
|---|
| 157 |
SharedFile *m_file; //!< seeded file |
|---|
| 158 |
PartData *m_partData; //!< downloaded file |
|---|
| 159 |
Torrent *m_torrent; //!< torrent the client belongs to |
|---|
| 160 |
|
|---|
| 161 |
bool m_sourceMaskAdded; //!< If we added srcmask to file |
|---|
| 162 |
|
|---|
| 163 |
/** |
|---|
| 164 |
* Client's speedmeter is connected to whichever (physical) file it's |
|---|
| 165 |
* currently downloading; since that can change at any point during |
|---|
| 166 |
* transfer, this connection is disconnected and re-attached to |
|---|
| 167 |
* different file when crossing file boundaries. |
|---|
| 168 |
*/ |
|---|
| 169 |
boost::signals::connection m_currentSpeedMeter; |
|---|
| 170 |
|
|---|
| 171 |
/** |
|---|
| 172 |
* Client's upload speedmeter connected to whatever (physical) file |
|---|
| 173 |
* it's currently uploading; since that can change at any point during |
|---|
| 174 |
* transfer, this connection is disconnected and re-attached to |
|---|
| 175 |
* different file when crossing file boundaries. |
|---|
| 176 |
*/ |
|---|
| 177 |
boost::signals::connection m_currentUploadMeter; |
|---|
| 178 |
}; |
|---|
| 179 |
|
|---|
| 180 |
} |
|---|
| 181 |
|
|---|
| 182 |
#endif |
|---|