libktorrent  2.1.1
packetreader.h
1 /***************************************************************************
2  * Copyright (C) 2005 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 #ifndef BTPACKETREADER_H
21 #define BTPACKETREADER_H
22 
23 #include <QList>
24 #include <QMutex>
25 #include <deque>
26 #include <ktorrent_export.h>
27 #include <net/trafficshapedsocket.h>
28 
29 namespace bt
30 {
31  class PeerInterface;
32 
33  struct IncomingPacket
34  {
35  QScopedArrayPointer<Uint8> data;
36  Uint32 size;
37  Uint32 read;
38 
39  IncomingPacket(Uint32 size);
40 
41  typedef QSharedPointer<IncomingPacket> Ptr;
42  };
43 
48  class KTORRENT_EXPORT PacketReader : public net::SocketReader
49  {
50  public:
51  PacketReader(Uint32 max_packet_size);
52  ~PacketReader() override;
53 
58  void update(PeerInterface & peer);
59 
61  bool ok() const {return !error;}
62 
63  void onDataReady(Uint8* buf, Uint32 size) override;
64 
65  private:
66  Uint32 newPacket(Uint8* buf, Uint32 size);
67  Uint32 readPacket(Uint8* buf, Uint32 size);
68  IncomingPacket::Ptr dequeuePacket();
69 
70  private:
71  bool error;
72 #ifndef DO_NOT_USE_DEQUE
73  std::deque<IncomingPacket::Ptr> packet_queue;
74 #else
75  QList<IncomingPacket::Ptr> packet_queue;
76 #endif
77  QMutex mutex;
78  Uint8 len[4];
79  int len_received;
80  Uint32 max_packet_size;
81  };
82 
83 }
84 
85 #endif
bt::PeerInterface
Interface for a Peer.
Definition: peerinterface.h:58
bt::PacketReader
Definition: packetreader.h:67
net::SocketReader
Definition: trafficshapedsocket.h:52