1 /***************************************************************************
3 * Copyright (C) 2006 David Brodsky *
5 * This library is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU Library General Public *
7 * License as published by the Free Software Foundation and appearing *
8 * in the file LICENSE.LGPL included in the packaging of this file. *
10 * This library 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 GNU *
13 * Library General Public License for more details. *
15 ***************************************************************************/
17 #ifndef _tairon_net_core_poll_h
18 #define _tairon_net_core_poll_h
20 #include <sys/epoll.h>
23 #include <tairon/core/exceptions.h>
24 #include <tairon/core/signals.h>
26 using Tairon::Core::String
;
34 /** \brief I/O event notification facility.
36 * This class provides interface to epoll(7) functionality.
41 /** Creates Poll object.
43 * \param maxSockets Size of the internal table of descriptors.
45 Poll(size_t maxSockets
);
47 /** Destroys the object.
51 /** Sets error handler of the socket.
53 * \param fd Descriptor of the socket.
54 * \param functor Functor that will be called when an socket error
57 void addSocketErrorHandler(int fd
, Tairon::Core::Functor0
<void> *functor
);
59 /** Adds socket to be available to read operation.
61 * \param fd Descriptor of the socket.
62 * \param functor Functor that will be called when the event occurs.
64 void addSocketToRead(int fd
, Tairon::Core::Functor0
<void> *functor
);
66 /** Adds socket to be available to write operation.
68 * \param fd Descriptor of the socket.
69 * \param functor Functor that will be called when the event occurs.
71 void addSocketToWrite(int fd
, Tairon::Core::Functor0
<void> *functor
);
73 /** Deletes error handler associated with a socket.
75 * \param fd Descriptor of the socket.
77 void delSocketErrorHandler(int fd
);
79 /** Deletes socket from read event notifications.
81 * \param fd Descriptor of the socket to delete.
83 void delSocketToRead(int fd
);
85 /** Deletes socket from write event notifications.
87 * \param fd Descriptor of the socket to delete.
89 void delSocketToWrite(int fd
);
91 /** Waits for events to occur.
93 * \param timeout Specifies how many miliseconds should this method
94 * wait for an event. Specifying a timeout of -1 makes this method wait
95 * indefinitely, while specifying a timeout equal to zero makes this
96 * method return immediately even if no events are available.
98 void poll(int timeout
);
100 /** Returns pointer to the instance of this class.
102 static Poll
*self() {
107 /** Adds a socket to the epoll.
109 * \param fd Descriptor of the socket.
110 * \param events Events for polling.
112 void addSocket(int fd
, uint32_t events
);
114 /** Changes a socket in the epoll.
116 * \param fd Descriptor of the socket.
117 * \param events Events for polling.
119 void changeSocket(int fd
, uint32_t events
);
121 /** Deletes a socket from the epoll.
123 * \param fd Descriptor of the socket.
125 void delSocket(int fd
);
128 /** Epoll descriptor.
132 /** Table of descriptors and their associated error handlers.
134 std::map
<int, Tairon::Core::Functor0
<void> *> errorFunctors
;
136 /** Buffer for the catched events.
138 struct epoll_event
*events
;
140 /** Size of the events array.
144 /** Holds pointer to the instance of this class.
146 static Poll
*pollInstance
;
148 /** Table of descriptors waiting for read.
150 std::map
<int, Tairon::Core::Functor0
<void> *> readFunctors
;
152 /** Table of descriptors waiting for write.
154 std::map
<int, Tairon::Core::Functor0
<void> *> writeFunctors
;
158 /** \brief Exception for poll errors.
160 class PollException
: public Tairon::Core::Exception
163 /** Constructor that takes as parameters description of the exception
164 * and its error number.
166 PollException(const String
&desc
, int err
) : Tairon::Core::Exception(desc
), errorNumber(err
) {};
168 /** Standard destructor.
170 virtual ~PollException() {};
172 /** Returns error number of the exception.
174 int getErrorNumber() const {
179 /** Error number of this exception.
186 }; // namespace Tairon
190 // vim: ai sw=4 ts=4 noet fdm=marker