libktorrent  2.2.0
connection.h
1 /***************************************************************************
2  * Copyright (C) 2009 by Joris Guisson *
3  * joris.guisson@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
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 *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 
21 #ifndef UTP_CONNECTION_H
22 #define UTP_CONNECTION_H
23 
24 #include <QPair>
25 #include <QMutex>
26 #include <QWaitCondition>
27 #include <QBasicTimer>
28 #include <QSharedPointer>
29 #include <ktorrent_export.h>
30 #include <net/address.h>
31 #include <utp/utpprotocol.h>
32 #include <util/circularbuffer.h>
33 #include <util/timer.h>
34 #include <utp/remotewindow.h>
35 #include <boost/concept_check.hpp>
36 
37 
38 
39 
40 namespace utp
41 {
42  class DelayWindow;
43  class LocalWindow;
44  class Transmitter;
45 
49  class KTORRENT_EXPORT Connection : public QObject, public Retransmitter
50  {
51  Q_OBJECT
52  public:
53  enum Type
54  {
55  INCOMING,
56  OUTGOING
57  };
58 
61  {
62  public:
63  TransmissionError(const char* file, int line);
64 
65  QString location;
66  };
67 
68  struct Stats
69  {
70  Type type;
71  net::Address remote;
72  ConnectionState state;
73  bt::Uint16 send_connection_id;
74  bt::Uint32 reply_micro;
75  bt::Uint16 recv_connection_id;
76  bt::Uint16 seq_nr;
77  int eof_seq_nr;
78  bt::Uint32 timeout;
79  TimeValue absolute_timeout;
80  int rtt;
81  int rtt_var;
82  bt::Uint32 packet_size;
83  bt::Uint32 last_window_size_transmitted;
84 
85  bt::Uint64 bytes_received;
86  bt::Uint64 bytes_sent;
87  bt::Uint32 packets_received;
88  bt::Uint32 packets_sent;
89  bt::Uint64 bytes_lost;
90  bt::Uint32 packets_lost;
91 
92  bool readable;
93  bool writeable;
94  };
95 
96  Connection(bt::Uint16 recv_connection_id, Type type, const net::Address & remote, Transmitter* transmitter);
97  ~Connection() override;
98 
100  void setBlocking(bool on) {blocking = on;}
101 
103  void dumpStats();
104 
107 
109  const Stats & connectionStats() const {return stats;}
110 
112  ConnectionState handlePacket(const PacketParser & parser, bt::Buffer::Ptr packet);
113 
115  const net::Address & remoteAddress() const {return stats.remote;}
116 
118  bt::Uint16 receiveConnectionID() const {return stats.recv_connection_id;}
119 
121  int send(const bt::Uint8* data, bt::Uint32 len);
122 
124  int recv(bt::Uint8* buf, bt::Uint32 max_len);
125 
127  ConnectionState connectionState() const {return stats.state;}
128 
130  Type connectionType() const {return stats.type;}
131 
133  bt::Uint32 bytesAvailable() const;
134 
136  bool isWriteable() const;
137 
140 
142  bool waitForData(bt::Uint32 timeout = 0);
143 
145  void close();
146 
148  void reset();
149 
151  void updateRTT(const Header* hdr, bt::Uint32 packet_rtt, bt::Uint32 packet_size) override;
152 
154  void retransmit(PacketBuffer & packet, bt::Uint16 p_seq_nr) override;
155 
157  bool allDataSent() const;
158 
160  bt::Uint32 currentTimeout() const override {return stats.timeout;}
161 
162  typedef QSharedPointer<Connection> Ptr;
163  typedef QWeakPointer<Connection> WPtr;
164 
166  void setWeakPointer(WPtr ptr) {self = ptr;}
167 
169  void checkTimeout(const TimeValue & now);
170 
171  private:
172  void sendSYN();
173  void sendState();
174  void sendFIN();
175  void sendReset();
176  void updateDelayMeasurement(const Header* hdr);
177  void sendStateOrData();
178  void sendPackets();
179  void sendPacket(bt::Uint32 type, bt::Uint16 p_ack_nr);
180  void checkIfClosed();
181  void sendDataPacket(PacketBuffer & packet, bt::Uint16 seq_nr, const TimeValue & now);
182  void startTimer();
183  void checkState();
184  bt::Uint32 extensionLength() const;
185  void handleTimeout();
186 
187  private:
188  Transmitter* transmitter;
189  LocalWindow* local_wnd;
190  RemoteWindow* remote_wnd;
191  bt::CircularBuffer output_buffer;
192  //bt::Timer timer;
193  mutable QMutex mutex;
194  QWaitCondition connected;
195  QWaitCondition data_ready;
196  Stats stats;
197  bool fin_sent;
198  TimeValue last_packet_sent;
199  DelayWindow* delay_window;
200  Connection::WPtr self;
201  bool blocking;
202 
203  friend class UTPServer;
204  };
205 
209  class KTORRENT_EXPORT Transmitter
210  {
211  public:
212  virtual ~Transmitter();
213 
215  virtual bool sendTo(Connection::Ptr conn, const PacketBuffer & packet) = 0;
216 
218  virtual void stateChanged(Connection::Ptr conn, bool readable, bool writeable) = 0;
219 
221  virtual void closed(Connection::Ptr conn) = 0;
222  };
223 
224 }
225 
226 #endif // UTP_CONNECTION_H
utp::Connection::checkTimeout
void checkTimeout(const TimeValue &now)
Check if we haven't hit a timeout yet.
utp::Connection::reset
void reset()
Reset the connection.
utp::Connection::updateRTT
void updateRTT(const Header *hdr, bt::Uint32 packet_rtt, bt::Uint32 packet_size) override
Update the RTT time.
utp::Transmitter::closed
virtual void closed(Connection::Ptr conn)=0
Called when the connection is closed.
net::Address
Definition: address.h:41
utp::Connection::remoteAddress
const net::Address & remoteAddress() const
Get the remote address.
Definition: connection.h:115
utp::Connection::waitUntilConnected
bool waitUntilConnected()
Wait until the connectTo call fails or succeeds.
utp::Connection::receiveConnectionID
bt::Uint16 receiveConnectionID() const
Get the receive connection id.
Definition: connection.h:118
utp::Connection::connectionStats
const Stats & connectionStats() const
Get the connection stats.
Definition: connection.h:109
utp::TimeValue
Definition: timevalue.h:33
utp::Connection::isWriteable
bool isWriteable() const
Can we write to this socket.
utp::Connection::currentTimeout
bt::Uint32 currentTimeout() const override
Get the current timeout.
Definition: connection.h:160
utp::Transmitter::stateChanged
virtual void stateChanged(Connection::Ptr conn, bool readable, bool writeable)=0
Connection has become readable, writeable or both.
utp::Connection::send
int send(const bt::Uint8 *data, bt::Uint32 len)
Send some data, returns the amount of bytes sent (or -1 on error)
utp::Transmitter
Definition: connection.h:210
utp::Connection::connectionState
ConnectionState connectionState() const
Get the connection state.
Definition: connection.h:127
utp::RemoteWindow
Definition: remotewindow.h:70
utp::Connection
Definition: connection.h:50
utp::UTPServer
Definition: utpserver.h:38
utp::LocalWindow
Definition: localwindow.h:58
utp::PacketParser
Definition: utpprotocol.h:164
utp::DelayWindow
Definition: delaywindow.h:35
utp::Transmitter::sendTo
virtual bool sendTo(Connection::Ptr conn, const PacketBuffer &packet)=0
Send a packet of a connection.
utp::Connection::allDataSent
bool allDataSent() const
Is all data sent.
utp::Connection::startConnecting
void startConnecting()
Start connecting (OUTGOING only)
utp::Connection::dumpStats
void dumpStats()
Dump connection stats.
utp::Connection::connectionType
Type connectionType() const
Get the type of connection.
Definition: connection.h:130
utp::Connection::handlePacket
ConnectionState handlePacket(const PacketParser &parser, bt::Buffer::Ptr packet)
Handle a single packet.
utp::Connection::setBlocking
void setBlocking(bool on)
Turn on or off blocking mode.
Definition: connection.h:100
utp::PacketBuffer
Definition: packetbuffer.h:38
utp::Retransmitter
Definition: remotewindow.h:52
utp::Connection::retransmit
void retransmit(PacketBuffer &packet, bt::Uint16 p_seq_nr) override
Retransmit a packet.
utp::Connection::recv
int recv(bt::Uint8 *buf, bt::Uint32 max_len)
Read available data from local window, returns the amount of bytes read.
utp::Connection::close
void close()
Close the socket.
utp::Connection::setWeakPointer
void setWeakPointer(WPtr ptr)
Set a weak pointer to self.
Definition: connection.h:166
utp::Connection::Stats
Definition: connection.h:69
utp::Header
Definition: utpprotocol.h:50
utp::Connection::bytesAvailable
bt::Uint32 bytesAvailable() const
Get the number of bytes available.
utp::Connection::TransmissionError
Thrown when a transmission error occurs, server should kill the connection if it happens.
Definition: connection.h:61
bt::CircularBuffer
Definition: circularbuffer.h:35
utp::Connection::waitForData
bool waitForData(bt::Uint32 timeout=0)
Wait until there is data ready or the socket is closed or a timeout occurs.