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 
60  class TransmissionError
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 
106  void startConnecting();
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 
139  bool waitUntilConnected();
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
net::Address
Definition: address.h:59
utp::TimeValue
Definition: timevalue.h:51
utp::Transmitter
Definition: connection.h:228
utp::RemoteWindow
Definition: remotewindow.h:88
utp::Connection
Definition: connection.h:68
utp::UTPServer
Definition: utpserver.h:56
utp::LocalWindow
Definition: localwindow.h:76
utp::DelayWindow
Definition: delaywindow.h:53
utp::PacketBuffer
Definition: packetbuffer.h:56
utp::Connection::Stats
Definition: connection.h:87
utp::Header
Definition: utpprotocol.h:68
bt::CircularBuffer
Definition: circularbuffer.h:53