libktorrent  2.1.1
localwindow.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 
22 #ifndef UTP_LOCALWINDOW_H
23 #define UTP_LOCALWINDOW_H
24 
25 #include <vector>
26 #include <ktorrent_export.h>
27 #include <util/constants.h>
28 #include <QSharedPointer>
29 #include "packetbuffer.h"
30 
31 namespace utp
32 {
33  struct SelectiveAck;
34  struct Header;
35 
36  const bt::Uint32 DEFAULT_CAPACITY = 64*1024;
37 
38  struct WindowPacket
39  {
40  WindowPacket(bt::Uint16 seq_nr);
41  WindowPacket(bt::Uint16 seq_nr, bt::Buffer::Ptr packet, bt::Uint32 data_off);
42  ~WindowPacket();
43 
44  bt::Uint32 read(bt::Uint8* dst, bt::Uint32 max_len);
45  bool fullyRead() const;
46  void set(bt::Buffer::Ptr packet, bt::Uint32 data_off);
47 
48  bt::Uint16 seq_nr;
49  bt::Buffer::Ptr packet;
50  bt::Uint32 bytes_read;
51  };
52 
53 
57  class KTORRENT_EXPORT LocalWindow
58  {
59  public:
60  LocalWindow(bt::Uint32 cap = DEFAULT_CAPACITY);
61  virtual ~LocalWindow();
62 
64  bt::Uint32 availableSpace() const {return window_space;}
65 
67  bt::Uint32 currentWindow() const {return capacity - window_space;}
68 
70  bt::Uint32 windowCapacity() const {return capacity;}
71 
73  bool packetReceived(const Header* hdr, bt::Buffer::Ptr packet, bt::Uint32 data_off);
74 
76  void setLastSeqNr(bt::Uint16 lsn);
77 
79  bt::Uint16 lastSeqNr() const {return last_seq_nr;}
80 
82  bool isEmpty() const {return incoming_packets.empty();}
83 
85  bt::Uint32 read(bt::Uint8* data, bt::Uint32 max_len);
86 
88  bool isReadable() const {return bytes_available > 0;}
89 
91  bt::Uint32 bytesAvailable() const {return bytes_available;}
92 
94  bt::Uint32 selectiveAckBits() const;
95 
97  void fillSelectiveAck(SelectiveAck* sack);
98 
99  private:
100  typedef std::vector<WindowPacket> WindowPacketList;
101 
102  bt::Uint16 last_seq_nr;
103  bt::Uint16 first_seq_nr;
104  bt::Uint32 capacity;
105  WindowPacketList incoming_packets;
106  bt::Uint32 window_space;
107  bt::Uint32 bytes_available;
108  };
109 
110 }
111 
112 #endif // UTP_LOCALWINDOW_H
utp::SelectiveAck
Definition: utpprotocol.h:85