libktorrent  2.1.1
poll.h
1 /***************************************************************************
2  * Copyright (C) 2010 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 NET_POLL_H
22 #define NET_POLL_H
23 
24 #include <map>
25 #include <vector>
26 #include <QSharedPointer>
27 #include <util/constants.h>
28 #include <ktorrent_export.h>
29 
30 #ifdef Q_WS_WIN
31 #include <util/win32.h>
32 #endif
33 
34 struct pollfd;
35 
36 namespace net
37 {
38 
42  class KTORRENT_EXPORT PollClient
43  {
44  public:
45  PollClient() {}
46  virtual ~PollClient() {}
47 
49  virtual int fd() const = 0;
50 
52  virtual void handleData() = 0;
53 
55  virtual void reset() = 0;
56 
57  typedef QSharedPointer<PollClient> Ptr;
58  };
59 
63  class KTORRENT_EXPORT Poll
64  {
65  public:
66  Poll();
67  virtual ~Poll();
68 
69  enum Mode
70  {
71  INPUT, OUTPUT
72  };
73 
75  int add(int fd,Mode mode);
76 
78  int add(PollClient::Ptr pc);
79 
81  int poll(int timeout = -1);
82 
84  bool ready(int index,Mode mode) const;
85 
87  void reset();
88 
89  private:
90  std::vector<struct pollfd> fd_vec;
91  bt::Uint32 num_sockets;
92  std::map<int,PollClient::Ptr> poll_clients;
93  };
94 
95 }
96 
97 #endif // NET_POLL_H
net::Poll::add
int add(int fd, Mode mode)
Add a file descriptor to the poll (returns the index of it)
net::Poll::add
int add(PollClient::Ptr pc)
Add a poll client.
net::Poll::ready
bool ready(int index, Mode mode) const
Check if a socket at an index is read.
net::PollClient
Definition: poll.h:43
pollfd
Definition: win32.h:81
net::PollClient::reset
virtual void reset()=0
Reset the client called after poll finishes.
net::PollClient::handleData
virtual void handleData()=0
Handle data.
net::Poll::reset
void reset()
Reset the poll.
net::PollClient::fd
virtual int fd() const =0
Get the filedescriptor to poll.
net::Poll
Definition: poll.h:64
net::Poll::poll
int poll(int timeout=-1)
Poll all sockets.