Search:

SENF Extensible Network Framework

  • Home
  • Download
  • Wiki
  • BerliOS
  • ChangeLog
  • Browse SVN
  • Bug Tracker
  • Overview
  • Examples
  • HowTos
  • Glossary
  • PPI
  • Packets
  • Scheduler
  • Socket
  • Utils
  • Console
  • Daemon
  • Logger
  • Termlib
  • Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

Poller.ct

Go to the documentation of this file.
00001 // $Id: Poller.ct 1742 2010-11-04 14:51:56Z g0dil $
00002 //
00003 // Copyright (C) 2008
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00026 //#include "Poller.ih"
00027 
00028 // Custom includes
00029 #include <errno.h>
00030 #include <senf/Utils/Exception.hh>
00031 
00032 #define prefix_
00033 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00034 
00035 template <class Value>
00036 prefix_ bool senf::scheduler::detail::Poller<Value>::set(int fd, int events, Value * data)
00037 {
00038     struct epoll_event ev = { events, { data } };
00039     if (epoll_ctl(epollFd_, EPOLL_CTL_ADD, fd, &ev) != -1)
00040         return true;
00041     if (errno == EEXIST)
00042         if (epoll_ctl(epollFd_, EPOLL_CTL_MOD, fd, &ev) != -1)
00043             return true;
00044     if (errno == EPERM)
00045         return false;
00046     SENF_THROW_SYSTEM_EXCEPTION("epolll_ctl()");
00047 }
00048 
00049 template <class Value>
00050 prefix_ void senf::scheduler::detail::Poller<Value>::remove(int fd)
00051 {
00052     if (epoll_ctl(epollFd_, EPOLL_CTL_DEL, fd, 0) == -1)
00053         if (errno != ENOENT && errno != EBADF && errno != EPERM)
00054             // Calling remove() on a file descriptor which is not registered
00055             // is no error, it shall be ignored:
00056             // ENOENT: Not part of the poller but a valid (open) fd
00057             // EBADF: The fd has been closed already. The kernel automatically removes such fds
00058             //     from epoll structures
00059             // EPERM: The fd does not support epoll and thus can never have been added
00060             SENF_THROW_SYSTEM_EXCEPTION("epoll_ctl()");
00061 }
00062 
00063 template <class Value>
00064 prefix_ typename senf::scheduler::detail::Poller<Value>::range senf::scheduler::detail::Poller<Value>::wait()
00065 {
00066     static epoll_event events[NumEvents];
00067     int rv (0);
00068     rv = epoll_wait(epollFd_, events, NumEvents, timeout_);
00069     if (rv == -1) {
00070         if (errno == EINTR)
00071             rv = 0;
00072         else
00073             SENF_THROW_SYSTEM_EXCEPTION("epoll_wait()");
00074     }
00075     return boost::make_iterator_range(
00076         boost::make_transform_iterator(events, GetPollResult()),
00077         boost::make_transform_iterator(events+rv, GetPollResult()) );
00078 }
00079 
00080 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00081 #undef prefix_
00082 
00083 
00084 // Local Variables:
00085 // mode: c++
00086 // fill-column: 100
00087 // comment-column: 40
00088 // c-file-style: "senf"
00089 // indent-tabs-mode: nil
00090 // ispell-local-dictionary: "american"
00091 // compile-command: "scons -u test"
00092 // End:

Contact: senf-dev@lists.berlios.de | © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research