libktorrent  2.2.0
packetbuffer.h
1 /***************************************************************************
2  * Copyright (C) 2011 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_PACKETBUFFER_H_
22 #define UTP_PACKETBUFFER_H_
23 
24 #include <QSharedPointer>
25 #include <ktorrent_export.h>
26 #include <util/constants.h>
27 #include <util/circularbuffer.h>
28 #include <util/bufferpool.h>
29 
30 namespace utp
31 {
32  class Header;
33 
37  class KTORRENT_EXPORT PacketBuffer
38  {
39  public:
40  PacketBuffer();
41  ~PacketBuffer();
42 
43  PacketBuffer(const PacketBuffer & buf) = default;
44  PacketBuffer &operator=(const PacketBuffer & buf) = default;
45 
49  bool isEmpty() const {return size == 0;}
50 
57  bool setHeader(const Header & hdr, bt::Uint32 extension_length);
58 
60  bt::Uint8* extensionData() {return extension;}
61 
68  bt::Uint32 fillData(bt::CircularBuffer & cbuf, bt::Uint32 to_read);
69 
76  bt::Uint32 fillData(const bt::Uint8* data, bt::Uint32 data_size);
77 
82  void fillDummyData(bt::Uint32 amount);
83 
87  static void clearPool();
88 
90  const bt::Uint8* data() const {return header;}
91 
93  bt::Uint32 bufferSize() const {return size;}
94 
96  bt::Uint32 payloadSize() const {return payload ? (buffer->get() + MAX_SIZE) - payload: 0;}
97 
99  bt::Uint32 headRoom() const {return payload ? payload - buffer->get() : MAX_SIZE;}
100 
101  static const bt::Uint32 MAX_SIZE = 1500;
102 
103  private:
104  bt::Buffer::Ptr buffer;
105  bt::Uint8* header;
106  bt::Uint8* extension;
107  bt::Uint8* payload;
108  bt::Uint32 size;
109 
110  static bt::BufferPool::Ptr pool;
111  };
112 
113 }
114 
115 #endif /* PACKETBUFFER_H_ */
bt::CircularBuffer
Definition: circularbuffer.h:53