libktorrent  2.2.0
remotewindow.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_REMOTEWINDOW_H
22 #define UTP_REMOTEWINDOW_H
23 
24 #include <QList>
25 #include <QSharedPointer>
26 #include <QMutex>
27 #include <ktorrent_export.h>
28 #include <util/constants.h>
29 #include <utp/timevalue.h>
30 #include <utp/packetbuffer.h>
31 
32 namespace utp
33 {
34  struct SelectiveAck;
35  struct Header;
36 
37  struct UnackedPacket
38  {
39  UnackedPacket(const PacketBuffer & packet, bt::Uint16 seq_nr, bt::TimeStamp send_time);
40  ~UnackedPacket();
41 
42  PacketBuffer packet;
43  bt::Uint16 seq_nr;
44  bt::TimeStamp send_time;
45  bool retransmitted;
46  };
47 
51  class KTORRENT_EXPORT Retransmitter
52  {
53  public:
54  virtual ~Retransmitter() {}
55 
57  virtual void updateRTT(const Header* hdr, bt::Uint32 packet_rtt, bt::Uint32 packet_size) = 0;
58 
60  virtual void retransmit(PacketBuffer & packet, bt::Uint16 p_seq_nr) = 0;
61 
63  virtual bt::Uint32 currentTimeout() const = 0;
64  };
65 
69  class KTORRENT_EXPORT RemoteWindow
70  {
71  public:
72  RemoteWindow();
73  virtual ~RemoteWindow();
74 
76  void packetReceived(const Header* hdr, const SelectiveAck* sack, Retransmitter* conn);
77 
79  void addPacket(const PacketBuffer & packet, bt::Uint16 seq_nr, bt::TimeStamp send_time);
80 
82  bool allowedToSend(bt::Uint32 packet_size) const
83  {
84  return cur_window + packet_size <= qMin(wnd_size, max_window);
85  }
86 
88  bt::Uint32 availableSpace() const
89  {
90  bt::Uint32 m = qMin(wnd_size, max_window);
91  if (cur_window > m)
92  return 0;
93  else
94  return m - cur_window;
95  }
96 
98  bool allPacketsAcked() const {return unacked_packets.isEmpty();}
99 
101  bt::Uint32 numUnackedPackets() const {return unacked_packets.count();}
102 
104  void timeout(Retransmitter* conn);
105 
107  double windowUsageFactor() const {return qMax((double)cur_window / max_window, 1.0);}
108 
110  void updateWindowSize(double scaled_gain);
111 
112  bt::Uint32 currentWindow() const {return cur_window;}
113  bt::Uint32 maxWindow() const {return max_window;}
114  bt::Uint32 windowSize() const {return wnd_size;}
115 
117  void clear();
118 
119  private:
120  void checkLostPackets(const Header* hdr, const SelectiveAck* sack, Retransmitter* conn);
121  bt::Uint16 lost(const SelectiveAck* sack);
122 
123  private:
124  bt::Uint32 cur_window;
125  bt::Uint32 max_window;
126  bt::Uint32 wnd_size; // advertised window size from the other side
127  QList<UnackedPacket> unacked_packets;
128  bt::Uint16 last_ack_nr;
129  bt::Uint32 last_ack_receive_count;
130  };
131 
132 }
133 
134 #endif // UTP_REMOTEWINDOW_H
utp::RemoteWindow
Definition: remotewindow.h:88
utp::SelectiveAck
Definition: utpprotocol.h:85
utp::PacketBuffer
Definition: packetbuffer.h:56
utp::Retransmitter
Definition: remotewindow.h:70
utp::Header
Definition: utpprotocol.h:68