Add main page for Doxygen generated documentation.
[tairent.git] / src / main / connection.h
blob6e76786549c007521e57d3c871d6662c42c28278
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 what the data.
69 bool fake;
71 /** Index of the piece.
73 uint32_t index;
75 /** List of piece's readers.
77 std::list<Tairon::Net::Reader *> readers;
80 /** \brief Struct with informations about one peer's request.
82 struct PieceRequestStruct
84 /** Index of the piece.
86 uint32_t index;
88 /** Length of the chunk.
90 uint32_t length;
92 /** Offset of the chunk within the piece.
94 uint32_t start;
97 /** \brief Struct for informations about piece writers.
99 * Piece writers are used for writing a piece to the peer. This struct holds a
100 * list with all writers used to write the piece and piece's index.
102 struct PieceWritersStruct
104 /** Index of the piece.
106 uint32_t index;
108 /** Length of the piece.
110 uint32_t length;
112 /** list of piece's writers.
114 std::list<Tairon::Net::Writer *> writers;
117 /** \brief Connection between two torrent clients.
119 * This class handles low level things around BitTorrent protocol.
121 class Connection
123 public:
124 /** Constructs a Connection object.
126 * \param fd Descriptor of the socket.
128 Connection(int fd);
130 /** Constructs a Connection object. Creates a new socket and connects
131 * it to the peer.
133 * \param ip IP address of the peer.
134 * \param port Port on which the peer is listening.
135 * \param pID Peer's ID.
136 * \param c Client of this connection.
138 Connection(const String &ip, uint16_t port, const String &pID, TorrentClient *c);
140 /** Destroys the obejct.
142 ~Connection();
144 /** Appends a writer to the command queue. It immediately starts
145 * writing if the queue is empty.
147 void addCommandWriter(Tairon::Net::Writer *writer);
149 /** Clears mapping with requested pieces.
151 void clearRequested();
153 /** Closes the connection.
155 void close();
157 /** Destroys piece readers that are in the queue.
159 void destroyPieceReaders();
161 /** Destroys piece writers that are in the queue.
163 void destroyPieceWriters();
165 /** Returns this peer's bitfield.
167 Tairent::Core::BitField *getBitField() {
168 return bitfield;
171 /** Returns current incoming rate.
173 double getIncomingRate();
175 /** Returns length of the piece incoming right now.
177 uint32_t getPieceLength();
179 /** Returns current piece reader. If there is no piece reader then this
180 * method returns 0.
182 PieceReadersStruct *getPieceReader() {
183 return pieceReaders;
186 /** Returns current piece writer. If there is no piece writer then this
187 * method returs 0.
189 PieceWritersStruct *getPieceWriter() {
190 return pieceWriters;
193 /** Returns peer's ID.
195 const String &getPeerID() {
196 return peerID;
199 /** Returns pointer to the limiter that limits reading.
201 Tairon::Net::Limiter *getReadingLimiter();
203 /** Returns reference to the mapping with requested pieces.
205 const std::map<uint32_t, std::set<uint32_t> > &getRequested() {
206 return requests;
209 /** Returns number of currently requested pieces.
211 unsigned int getRequestsCount();
213 /** Returns pointer to the connection's socket.
215 Tairon::Net::Socket *getSocket() {
216 return socket;
219 /** Returns pointer to the limiter that limits writing.
221 Tairon::Net::Limiter *getWritingLimiter();
223 /** Returns true it we are being choked by the peer; otheriwse returns
224 * false.
226 bool isChoked() {
227 return choked;
230 /** Returns true if we are interested; otherwise returns false.
232 bool isInterested() {
233 return interested;
236 /** Returns true if we are choking the peer; otherise returns false.
238 bool isPeerChoked() {
239 return peerChoked;
242 /** Returns true if the peer is interested; otherwise returns false.
244 bool isPeerInterested() {
245 return peerInterested;
248 /** Returns true if this connection is snubbed; otherwise returns false.
250 * TODO: write what snubbed means
252 bool isSnubbed();
254 /** Sends choke message to the peer.
256 void sendChoke();
258 /** Sends handshake sequence to the peer.
260 * \param infoHash 20 byte length SHA1 hash of the info part of the torrent.
262 void sendHandshake(const String &infoHash);
264 /** Sends have message to the peer.
266 * \param index Number of the piece that we currently have.
268 void sendHave(uint32_t index);
270 /** Sends interested message to the peer.
272 void sendInterested();
274 /** Sends not interested message to the peer.
276 void sendNotInterested();
278 /** Sends request message to the peer.
280 void sendRequest(uint32_t index, uint32_t start, uint32_t length);
282 /** Sends unchoke message to the peer.
284 void sendUnchoke();
286 /** Sets peer's ID of this connection. It should be called only when we
287 * are trying to connect to that peer.
289 void setPeerID(const String &pID) {
290 peerID = pID;
293 /** Sets list of readers that read a piece.
295 void setReaders(PieceReadersStruct *readers);
297 /** Sets list of writers that writes a piece.
299 void setWriters(PieceWritersStruct *writers);
301 public:
302 /** Signal emitted when the connection is closed.
304 Tairon::Core::Signal1<void, Connection *> closedSignal;
306 private:
307 /** Called when a socket error occured while connecting to the socket.
309 void connectingError(Tairon::Net::Socket *, int);
311 /** Called when a Writer has written data to the socket.
313 void dataWritten(Tairon::Net::Writer *writer);
315 /** Initializes socket's signals and creates the first reader.
317 void init();
319 /** Called when a piece Writer has written data to the socket.
321 void pieceDataWritten(Tairon::Net::Writer *writer);
323 /** Reads peer's bitfield.
325 void readBitField(Tairon::Net::Reader *);
327 /** Reads cancel message.
329 void readCancel(Tairon::Net::Reader *);
331 /** Reads one byte command (i.e. message type).
333 void readCommand(Tairon::Net::Reader *);
335 /** Called when a socket error occured while reading by a reader. Emits
336 * the closedSignal.
338 void readerError(Tairon::Net::Reader *, Tairon::Net::Socket *);
340 /** Reads first command.
342 void readFirstCommand(Tairon::Net::Reader *);
344 /** Reads length of the first command.
346 void readFirstLength(Tairon::Net::Reader *);
348 /** Reads handshake sequence.
350 void readHandshake(Tairon::Net::Reader *);
352 /** Reads have message.
354 void readHave(Tairon::Net::Reader *);
356 /** Reads length of the message.
358 void readLength(Tairon::Net::Reader *);
360 /** Reads peer ID.
362 void readPeerID(Tairon::Net::Reader *);
364 /** Reads index and start of the piece.
366 void readPiece(Tairon::Net::Reader *);
368 /** Reads length of the protocol name. The first byte that arrives
369 * should be 0x13.
371 void readProtocolLength(Tairon::Net::Reader *, size_t);
373 /** Reads index, start and length of a peer's request.
375 void readRequest(Tairon::Net::Reader *);
377 /** Called when the socket is ready to read.
379 void readyRead(Tairon::Net::Socket *);
381 /** Called when the socket is ready to write.
383 void readyWrite(Tairon::Net::Socket *);
385 /** Sets the next reader used for reading a piece.
387 void setNextReader(Tairon::Net::Reader *);
389 /** Called when the socket has been connected to the peer.
391 void socketConnected(Tairon::Net::Socket *);
393 /** Called when a socket error occured. Closes the connection and emits
394 * the closedSignal.
396 void socketError(Tairon::Net::Socket *, int);
398 /** Called when a socket error occured while writing to the socket by a
399 * writer. Emits the closedSignal.
401 void writerError(Tairon::Net::Writer *, Tairon::Net::Socket *);
403 private:
404 /** Peer's bitfield of pieces.
406 Tairent::Core::BitField *bitfield;
408 /** Whether we are choked by the peer.
410 bool choked;
412 /** Client for this connection.
414 TorrentClient *client;
416 /** Length of the current command.
418 uint32_t commandLength;
420 /** Queue with unsent commands. The first element may be writing to the
421 * socket.
423 std::list<Tairon::Net::Writer *> commandQueue;
425 /** Reader used for reading commands.
427 Tairon::Net::Reader *commandReader;
429 /** Whether the current reader should be deleted.
431 bool deleteReader;
433 /** Measurer for incoming rate.
435 RateMeasurer *incomingRate;
437 /** Are we interested?
439 bool interested;
441 /** Time (in miliseconds) of last piece arrival.
443 uint64_t lastPieceTime;
445 /** Reader used for reading length of messages.
447 Tairon::Net::Reader *lengthReader;
449 /** Is the peer choked?
451 bool peerChoked;
453 /** Peer's ID.
455 String peerID;
457 /** Is the peer interested?
459 bool peerInterested;
461 /** List of the peer's requests.
463 std::list<PieceRequestStruct> peersRequests;
465 /** Index of the piece that is being downloaded now.
467 uint32_t pieceIndex;
469 /** List of readers used for reading a piece.
471 PieceReadersStruct *pieceReaders;
473 /** List of writers used for writing a piece to the peer.
475 PieceWritersStruct *pieceWriters;
477 /** Start of the piece that is being downloaded now.
479 uint32_t pieceStart;
481 /** Reader used for reading from the socket.
483 Tairon::Net::Reader *reader;
485 /** Method that will read data from the socket.
487 void (Connection::*readMethod)();
489 /** Lists of requests sent to the peer.
491 std::map<uint32_t, std::set<uint32_t> > requests;
493 /** Socket associated with this connection.
495 Tairon::Net::Socket *socket;
498 }; // namespace Main
500 }; // namespace Tairent
502 #endif
504 // vim: ai sw=4 ts=4 noet fdm=marker