Add missing #include to src/main/torrentclient.h.
[tairent.git] / src / main / connection.h
blob53c173b2a8cec56b971795141584bbc0bdd9bac3
1 /***************************************************************************
2 * *
3 * Copyright (C) 2006 David Brodsky *
4 * *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation and appearing *
8 * in the file LICENSE.GPL included in the packaging of this file. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13 * General Public License for more details. *
14 * *
15 ***************************************************************************/
17 #ifndef _main_connection_h
18 #define _main_connection_h
20 #include <list>
21 #include <map>
22 #include <set>
24 #include <tairon/core/signals.h>
26 using Tairon::Core::String;
28 namespace Tairon
31 namespace Net
34 class Limiter;
35 class Reader;
36 class Socket;
37 class Writer;
39 }; // namespace Net
41 }; // namespace Tairon
43 namespace Tairent
46 namespace Core
49 class BitField;
51 }; // namespace Core
53 namespace Main
56 class RateMeasurer;
57 class TorrentClient;
59 /** \brief Struct for informations about piece readers.
61 * Piece readers are used for reading a piece from the peer. This struct holds
62 * a list with all readers used to read the piece and piece's index.
64 struct PieceReadersStruct
66 /** Are these readers fake? That means that we want to read the piece buf
67 * we don't want the data.
69 bool fake;
71 /** Index of the piece.
73 uint32_t index;
75 /** Length of the requested piece.
77 uint32_t length;
79 /** List of piece's readers.
81 std::list<Tairon::Net::Reader *> readers;
84 /** \brief Struct with informations about one peer's request.
86 struct PieceRequestStruct
88 /** Index of the piece.
90 uint32_t index;
92 /** Length of the chunk.
94 uint32_t length;
96 /** Offset of the chunk within the piece.
98 uint32_t start;
101 /** \brief Struct for informations about piece writers.
103 * Piece writers are used for writing a piece to the peer. This struct holds a
104 * list with all writers used to write the piece and piece's index.
106 struct PieceWritersStruct
108 /** Index of the piece.
110 uint32_t index;
112 /** Length of the piece.
114 uint32_t length;
116 /** list of piece's writers.
118 std::list<Tairon::Net::Writer *> writers;
121 /** \brief Connection between two torrent clients.
123 * This class handles low level things around BitTorrent protocol.
125 class Connection
127 public:
128 /** Constructs a Connection object.
130 * \param fd Descriptor of the socket.
132 Connection(int fd);
134 /** Constructs a Connection object. Creates a new socket and connects
135 * it to the peer.
137 * \param ip IP address of the peer.
138 * \param port Port on which the peer is listening.
139 * \param pID Peer's ID.
140 * \param c Client of this connection.
142 Connection(const String &ip, uint16_t port, const String &pID, TorrentClient *c);
144 /** Destroys the obejct.
146 ~Connection();
148 /** Appends a writer to the command queue. It immediately starts
149 * writing if the queue is empty.
151 void addCommandWriter(Tairon::Net::Writer *writer);
153 /** Clears mapping with requested pieces.
155 void clearRequested();
157 /** Closes the connection.
159 void close();
161 /** Destroys piece readers that are in the queue.
163 void destroyPieceReaders();
165 /** Destroys piece writers that are in the queue.
167 void destroyPieceWriters();
169 /** Returns this peer's bitfield.
171 Tairent::Core::BitField *getBitField() {
172 return bitfield;
175 /** Returns current incoming rate.
177 double getIncomingRate();
179 /** Returns current outgoing rate.
181 double getOutgoingRate();
183 /** Returns length of the piece incoming right now.
185 uint32_t getPieceLength();
187 /** Returns current piece reader. If there is no piece reader then this
188 * method returns 0.
190 PieceReadersStruct *getPieceReader() {
191 return pieceReaders;
194 /** Returns current piece writer. If there is no piece writer then this
195 * method returs 0.
197 PieceWritersStruct *getPieceWriter() {
198 return pieceWriters;
201 /** Returns peer's ID.
203 const String &getPeerID() {
204 return peerID;
207 /** Returns pointer to the limiter that limits reading.
209 Tairon::Net::Limiter *getReadingLimiter();
211 /** Returns reference to the mapping with requested pieces.
213 const std::map<uint32_t, std::set<uint32_t> > &getRequested() {
214 return requests;
217 /** Returns number of currently requested pieces.
219 unsigned int getRequestsCount();
221 /** Returns pointer to the connection's socket.
223 Tairon::Net::Socket *getSocket() {
224 return socket;
227 /** Returns time when the peer was unchoked.
229 int getUnchokeTime() {
230 return unchokeTime;
233 /** Returns pointer to the limiter that limits writing.
235 Tairon::Net::Limiter *getWritingLimiter();
237 /** Returns true it we are being choked by the peer; otheriwse returns
238 * false.
240 bool isChoked() {
241 return choked;
244 /** Returns true if we are interested; otherwise returns false.
246 bool isInterested() {
247 return interested;
250 /** Returns true if we are choking the peer; otherise returns false.
252 bool isPeerChoked() {
253 return peerChoked;
256 /** Returns true if the peer is interested; otherwise returns false.
258 bool isPeerInterested() {
259 return peerInterested;
262 /** Returns true if this connection is snubbed; otherwise returns false.
264 * TODO: write what snubbed means
266 bool isSnubbed();
268 /** Sends choke message to the peer.
270 void sendChoke();
272 /** Sends handshake sequence to the peer.
274 * \param infoHash 20 byte length SHA1 hash of the info part of the torrent.
276 void sendHandshake(const String &infoHash);
278 /** Sends have message to the peer.
280 * \param index Number of the piece that we currently have.
282 void sendHave(uint32_t index);
284 /** Sends interested message to the peer.
286 void sendInterested();
288 /** Sends not interested message to the peer.
290 void sendNotInterested();
292 /** Sends request message to the peer.
294 void sendRequest(uint32_t index, uint32_t start, uint32_t length);
296 /** Sends unchoke message to the peer.
298 void sendUnchoke(int time);
300 /** Sets peer's ID of this connection. It should be called only when we
301 * are trying to connect to that peer.
303 void setPeerID(const String &pID) {
304 peerID = pID;
307 /** Sets list of readers that read a piece.
309 void setReaders(PieceReadersStruct *readers);
311 /** Sets list of writers that writes a piece.
313 void setWriters(PieceWritersStruct *writers);
315 public:
316 /** Signal emitted when the connection is closed.
318 Tairon::Core::Signal1<void, Connection *> closedSignal;
320 private:
321 /** Called when a socket error occured while connecting to the socket.
323 void connectingError(Tairon::Net::Socket *, int);
325 /** Called when a Writer has written data to the socket.
327 void dataWritten(Tairon::Net::Writer *writer);
329 /** Initializes socket's signals and creates the first reader.
331 void init();
333 /** Called when a piece Writer has written data to the socket.
335 void pieceDataWritten(Tairon::Net::Writer *writer);
337 /** Reads peer's bitfield.
339 void readBitField(Tairon::Net::Reader *);
341 /** Reads cancel message.
343 void readCancel(Tairon::Net::Reader *);
345 /** Reads one byte command (i.e. message type).
347 void readCommand(Tairon::Net::Reader *);
349 /** Called when a socket error occured while reading by a reader. Emits
350 * the closedSignal.
352 void readerError(Tairon::Net::Reader *, Tairon::Net::Socket *);
354 /** Reads first command.
356 void readFirstCommand(Tairon::Net::Reader *);
358 /** Reads length of the first command.
360 void readFirstLength(Tairon::Net::Reader *);
362 /** Reads handshake sequence.
364 void readHandshake(Tairon::Net::Reader *);
366 /** Reads have message.
368 void readHave(Tairon::Net::Reader *);
370 /** Reads length of the message.
372 void readLength(Tairon::Net::Reader *);
374 /** Reads peer ID.
376 void readPeerID(Tairon::Net::Reader *);
378 /** Reads index and start of the piece.
380 void readPiece(Tairon::Net::Reader *);
382 /** Reads length of the protocol name. The first byte that arrives
383 * should be 0x13.
385 void readProtocolLength(Tairon::Net::Reader *, size_t);
387 /** Reads index, start and length of a peer's request.
389 void readRequest(Tairon::Net::Reader *);
391 /** Called when the socket is ready to read.
393 void readyRead(Tairon::Net::Socket *);
395 /** Called when the socket is ready to write.
397 void readyWrite(Tairon::Net::Socket *);
399 /** Sets the next reader used for reading a piece.
401 void setNextReader(Tairon::Net::Reader *);
403 /** Called when the socket has been connected to the peer.
405 void socketConnected(Tairon::Net::Socket *);
407 /** Called when a socket error occured. Closes the connection and emits
408 * the closedSignal.
410 void socketError(Tairon::Net::Socket *, int);
412 /** Called when a socket error occured while writing to the socket by a
413 * writer. Emits the closedSignal.
415 void writerError(Tairon::Net::Writer *, Tairon::Net::Socket *);
417 private:
418 /** Peer's bitfield of pieces.
420 Tairent::Core::BitField *bitfield;
422 /** Whether we are choked by the peer.
424 bool choked;
426 /** Client for this connection.
428 TorrentClient *client;
430 /** Length of the current command.
432 uint32_t commandLength;
434 /** Queue with unsent commands. The first element may be writing to the
435 * socket.
437 std::list<Tairon::Net::Writer *> commandQueue;
439 /** Reader used for reading commands.
441 Tairon::Net::Reader *commandReader;
443 /** Whether the current reader should be deleted.
445 bool deleteReader;
447 /** Measurer for incoming rate.
449 RateMeasurer *incomingRate;
451 /** Are we interested?
453 bool interested;
455 /** Time (in miliseconds) of last piece arrival.
457 uint64_t lastPieceTime;
459 /** Reader used for reading length of messages.
461 Tairon::Net::Reader *lengthReader;
463 /** Measurer for outgoing rate.
465 RateMeasurer *outgoingRate;
467 /** Is the peer choked?
469 bool peerChoked;
471 /** Peer's ID.
473 String peerID;
475 /** Is the peer interested?
477 bool peerInterested;
479 /** List of the peer's requests.
481 std::list<PieceRequestStruct> peersRequests;
483 /** Index of the piece that is being downloaded now.
485 uint32_t pieceIndex;
487 /** List of readers used for reading a piece.
489 PieceReadersStruct *pieceReaders;
491 /** List of writers used for writing a piece to the peer.
493 PieceWritersStruct *pieceWriters;
495 /** Start of the piece that is being downloaded now.
497 uint32_t pieceStart;
499 /** Reader used for reading from the socket.
501 Tairon::Net::Reader *reader;
503 /** Method that will read data from the socket.
505 void (Connection::*readMethod)();
507 /** Lists of requests sent to the peer.
509 std::map<uint32_t, std::set<uint32_t> > requests;
511 /** Socket associated with this connection.
513 Tairon::Net::Socket *socket;
515 /** Time when we unchoked the peer.
517 int unchokeTime;
520 }; // namespace Main
522 }; // namespace Tairent
524 #endif
526 // vim: ai sw=4 ts=4 noet fdm=marker