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 /** Are these readers fake? That means that we want to read the piece buf
67 * we don't what the data.
71 /** Index of the piece.
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.
88 /** Length of the chunk.
92 /** Offset of the chunk within the piece.
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.
108 /** Length of the piece.
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.
124 /** Constructs a Connection object.
126 * \param fd Descriptor of the socket.
130 /** Constructs a Connection object. Creates a new socket and connects
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.
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.
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() {
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
182 PieceReadersStruct
*getPieceReader() {
186 /** Returns current piece writer. If there is no piece writer then this
189 PieceWritersStruct
*getPieceWriter() {
193 /** Returns peer's ID.
195 const String
&getPeerID() {
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() {
209 /** Returns number of currently requested pieces.
211 unsigned int getRequestsCount();
213 /** Returns pointer to the connection's socket.
215 Tairon::Net::Socket
*getSocket() {
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
230 /** Returns true if we are interested; otherwise returns false.
232 bool isInterested() {
236 /** Returns true if we are choking the peer; otherise returns false.
238 bool isPeerChoked() {
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
254 /** Sends choke message to the peer.
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.
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
) {
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
);
302 /** Signal emitted when the connection is closed.
304 Tairon::Core::Signal1
<void, Connection
*> closedSignal
;
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.
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
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
*);
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
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
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
*);
404 /** Peer's bitfield of pieces.
406 Tairent::Core::BitField
*bitfield
;
408 /** Whether we are choked by the peer.
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
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.
433 /** Measurer for incoming rate.
435 RateMeasurer
*incomingRate
;
437 /** Are we 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?
457 /** Is the peer interested?
461 /** List of the peer's requests.
463 std::list
<PieceRequestStruct
> peersRequests
;
465 /** Index of the piece that is being downloaded now.
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.
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
;
500 }; // namespace Tairent
504 // vim: ai sw=4 ts=4 noet fdm=marker