1 /***************************************************************************
3 * Copyright (C) 2006 David Brodsky *
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. *
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. *
15 ***************************************************************************/
17 #ifndef _main_connection_h
18 #define _main_connection_h
24 #include <tairon/core/signals.h>
26 using Tairon::Core::String
;
41 }; // namespace Tairon
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 /** Index of the piece.
70 /** List of piece's readers.
72 std::list
<Tairon::Net::Reader
*> readers
;
75 /** \brief Struct for informations about piece writers.
77 * Piece writers are used for writing a piece to the peer. This struct holds a
78 * list with all writers used to write the piece and piece's index.
80 struct PieceWritersStruct
82 /** Index of the piece.
86 /** Length of the piece.
90 /** list of piece's writers.
92 std::list
<Tairon::Net::Writer
*> writers
;
95 /** \brief Connection between two torrent clients.
97 * This class handles low level things around BitTorrent protocol.
102 /** Constructs a Connection object.
104 * \param fd Descriptor of the socket.
105 * \param c Client for this connection. If there is no client
106 * specified, then the connection is incoming and the client will wait
107 * for a peer id and info hash before handshaking.
109 Connection(int fd
, TorrentClient
*c
= 0);
111 /** Destroys the obejct.
115 /** Appends a writer to the command queue. It immediately starts
116 * writing if the queue is empty.
118 void addCommandWriter(Tairon::Net::Writer
*writer
);
120 /** Adds a list of writers that will send peer some piece.
122 void addWriters(PieceWritersStruct
*writers
);
124 /** Clears mapping with requested pieces.
126 void clearRequested();
128 /** Closes the connection.
132 /** Destroys piece readers that are in the queue.
134 void destroyPieceReaders();
136 /** Destroys piece writers that are in the queue.
138 void destroyPieceWriters();
140 /** Returns this peer's bitfield.
142 Tairent::Core::BitField
*getBitField() {
146 /** Returns current incoming rate.
148 double getIncomingRate();
150 /** Returns length of the piece incoming right now.
152 uint32_t getPieceLength();
154 /** Returns current piece reader. If there is no piece reader then this method returns 0.
156 PieceReadersStruct
*getPieceReader() {
160 /** Returns list with queued piece writers.
162 const std::list
<PieceWritersStruct
*> &getPieceWriters() {
166 /** Returns pointer to the limiter that limits reading.
168 Tairon::Net::Limiter
*getReadingLimiter();
170 /** Returns reference to the mapping with requested pieces.
172 const std::map
<uint32_t, std::set
<uint32_t> > &getRequested() {
176 /** Returns pointer to the connection's socket.
178 Tairon::Net::Socket
*getSocket() {
182 /** Returns pointer to the limiter that limits writing.
184 Tairon::Net::Limiter
*getWritingLimiter();
186 /** Returns true it we are being choked by the peer; otheriwse returns
193 /** Returns true if we are interested; otherwise returns false.
195 bool isInterested() {
199 /** Returns true if we are choking the peer; otherise returns false.
201 bool isPeerChoked() {
205 /** Returns true if the peer is interested; otherwise returns false.
207 bool isPeerInterested() {
208 return peerInterested
;
211 /** Returns true if this connection is snubbed; otherwise returns false.
213 * TODO: write what snubbed means
217 /** Sends choke message to the peer.
221 /** Sends handshake sequence to the peer.
223 * \param infoHash 20 byte length SHA1 hash of the info part of the torrent.
225 void sendHandshake(const String
&infoHash
);
227 /** Sends have message to the peer.
229 * \param index Number of the piece that we currently have.
231 void sendHave(uint32_t index
);
233 /** Sends interested message to the peer.
235 void sendInterested();
237 /** Sends not interested message to the peer.
239 void sendNotInterested();
241 /** Sends request message to the peer.
243 void sendRequest(uint32_t index
, uint32_t start
, uint32_t length
);
245 /** Sends unchoke message to the peer.
249 /** Sets list of readers that read a piece.
251 void setReaders(PieceReadersStruct
*readers
);
254 /** Signal emitted when the connection is closed.
256 Tairon::Core::Signal1
<void, Connection
*> closedSignal
;
259 /** Called when a Writer has written data to the socket.
261 void dataWritten(Tairon::Net::Writer
*writer
);
263 /** Called when a piece Writer has written data to the socket.
265 void pieceDataWritten(Tairon::Net::Writer
*writer
);
267 /** Called by a piece reader. Frees all remaining piece readers, closes
268 * the connection and emits the closedSignal.
270 void pieceReaderError(Tairon::Net::Reader
*, Tairon::Net::Socket
*);
272 /** Reads peer's bitfield.
274 void readBitField(Tairon::Net::Reader
*);
276 /** Reads one byte command (i.e. message type).
278 void readCommand(Tairon::Net::Reader
*);
280 /** Called when a socket error occured while reading by a reader. Emits
283 void readerError(Tairon::Net::Reader
*, Tairon::Net::Socket
*);
285 /** Reads first command.
287 void readFirstCommand(Tairon::Net::Reader
*);
289 /** Reads length of the first command.
291 void readFirstLength(Tairon::Net::Reader
*);
293 /** Reads handshake sequence.
295 void readHandshake(Tairon::Net::Reader
*);
297 /** Reads have message.
299 void readHave(Tairon::Net::Reader
*);
301 /** Reads length of the message.
303 void readLength(Tairon::Net::Reader
*);
307 void readPeerID(Tairon::Net::Reader
*);
309 /** Reads index and start of the piece.
311 void readPiece(Tairon::Net::Reader
*);
313 /** Reads index, start and length of a peer's request.
315 void readRequest(Tairon::Net::Reader
*);
317 /** Called when the socket is ready to read.
319 void readyRead(Tairon::Net::Socket
*);
321 /** Called when the socket is ready to write.
323 void readyWrite(Tairon::Net::Socket
*);
325 /** Sets the next reader used for reading a piece.
327 void setNextReader(Tairon::Net::Reader
*);
329 /** Called when a socket error occured. Closes the connection and emits
332 void socketError(Tairon::Net::Socket
*, int);
334 /** Called when a socket error occured while writing to the socket by a
335 * writer. Emits the closedSignal.
337 void writerError(Tairon::Net::Writer
*, Tairon::Net::Socket
*);
340 /** Peer's bitfield of pieces.
342 Tairent::Core::BitField
*bitfield
;
344 /** Whether we are choked by the peer.
348 /** Client for this connection.
350 TorrentClient
*client
;
352 /** Length of the current command.
354 uint32_t commandLength
;
356 /** Queue with unsent commands. The first element may be writing to the
359 std::list
<Tairon::Net::Writer
*> commandQueue
;
361 /** Reader used for reading commands.
363 Tairon::Net::Reader
*commandReader
;
365 /** Whether the current reader should be deleted.
369 /** Measurer for incoming rate.
371 RateMeasurer
*incomingRate
;
373 /** Are we interested?
377 /** Time (in miliseconds) of last piece arrival.
379 uint64_t lastPieceTime
;
381 /** Reader used for reading length of messages.
383 Tairon::Net::Reader
*lengthReader
;
387 /** Is the peer interested?
391 /** Index of the piece that is being downloaded now.
395 /** List of readers used for reading a piece.
397 PieceReadersStruct
*pieceReaders
;
399 /** List of lists with piece writers.
401 std::list
<PieceWritersStruct
*> pieceWriters
;
403 /** Start of the piece that is being downloaded now.
407 /** Reader used for reading from the socket.
409 Tairon::Net::Reader
*reader
;
411 /** Method that will read data from the socket.
413 void (Connection::*readMethod
)();
415 /** Lists of requests sent to the peer.
417 std::map
<uint32_t, std::set
<uint32_t> > requests
;
419 /** Whether we are sending a piece to the peer.
423 /** Socket associated with this connection.
425 Tairon::Net::Socket
*socket
;
430 }; // namespace Tairent
434 // vim: ai sw=4 ts=4 noet fdm=marker