From f96ffb747eecb2e02812f4b4457201fe95de6d44 Mon Sep 17 00:00:00 2001 From: g0dil <g0dil@wiback.org> Date: Mon, 7 Apr 2008 08:24:23 +0000 Subject: [PATCH] Socket/Protocols: Add SO_ERROR getsockopt (BSDSocketProtocol::error()) Scheduler: Implement binding helper --- Scheduler/Binding.cci | 68 ++++++++++++++++ Scheduler/Binding.cti | 52 ++++++++++++ Scheduler/Binding.hh | 112 ++++++++++++++++++++++++++ Scheduler/Binding.test.cc | 51 ++++++++++++ Socket/Protocols/BSDSocketProtocol.cc | 10 +++ Socket/Protocols/BSDSocketProtocol.hh | 5 ++ 6 files changed, 298 insertions(+) create mode 100644 Scheduler/Binding.cci create mode 100644 Scheduler/Binding.cti create mode 100644 Scheduler/Binding.hh create mode 100644 Scheduler/Binding.test.cc diff --git a/Scheduler/Binding.cci b/Scheduler/Binding.cci new file mode 100644 index 00000000..fd2cec2a --- /dev/null +++ b/Scheduler/Binding.cci @@ -0,0 +1,68 @@ +// $Id$ +// +// Copyright (C) 2008 +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief Binding inline non-template implementation */ + +//#include "Binding.ih" + +// Custom includes + +#define prefix_ inline +///////////////////////////////cci.p/////////////////////////////////////// + +prefix_ senf::SchedulerBinding::~SchedulerBinding() +{ + disable(); +} + +prefix_ void senf::SchedulerBinding::enable() +{ + if (! enabled_) { + senf::Scheduler::instance().add(fd_, cb_, eventMask_); + enabled_ = true; + } +} + +prefix_ void senf::SchedulerBinding::disable() +{ + if (enabled_) { + senf::Scheduler::instance().remove(fd_); + enabled_ = false; + } +} + +prefix_ bool senf::SchedulerBinding::enabled() +{ + return enabled_; +} + +///////////////////////////////cci.e/////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: diff --git a/Scheduler/Binding.cti b/Scheduler/Binding.cti new file mode 100644 index 00000000..3ae11e89 --- /dev/null +++ b/Scheduler/Binding.cti @@ -0,0 +1,52 @@ +// $Id$ +// +// Copyright (C) 2008 +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief Binding inline template implementation */ + +//#include "Binding.ih" + +// Custom includes + +#define prefix_ inline +///////////////////////////////cti.p/////////////////////////////////////// + +template <class Handle> +prefix_ senf::SchedulerBinding::SchedulerBinding(Handle const & handle, + Scheduler::FdCallback cb, int eventMask, + bool enabled) + : fd_ (retrieve_filehandle(handle)), cb_ (cb), eventMask_ (eventMask), enabled_ (false) +{ + if (enabled) + enable(); +} + +///////////////////////////////cti.e/////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: diff --git a/Scheduler/Binding.hh b/Scheduler/Binding.hh new file mode 100644 index 00000000..e8b535bd --- /dev/null +++ b/Scheduler/Binding.hh @@ -0,0 +1,112 @@ +// $Id$ +// +// Copyright (C) 2008 +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief Binding public header */ + +#ifndef HH_Binding_ +#define HH_Binding_ 1 + +// Custom includes +#include <boost/utility.hpp> +#include "Scheduler.hh" + +//#include "Binding.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace senf { + + /** \brief Manage scheduler handle binding + + This class will manage the binding of an arbitrary handle to the scheduler: The handle will + automatically be removed from the Scheduler when this instance is destroyed. Example using a + SocketHandle instance for the handle: + \code + class Foo + { + public: + Foo(SocketHandle handle) : binding_ (handle, senf::membind(&cb, this), + senf::Scheduler::EV_READ) {} + + void blarf() { binding_.disable(); } + + private: + void cb(Scheduler::EventId event); + + senf::SchedulerBinding binding_; + }; + \endcode + + The handle will be registered automatically in the constructor and will be unregistered in + the destructor. Additionally, the helper has enable() and disable() members to register or + remove the handle to/from the Scheduler. + */ + class SchedulerBinding + : boost::noncopyable + { + public: + template <class Handle> + SchedulerBinding(Handle const & handle, Scheduler::FdCallback cb, + int eventMask = Scheduler::EV_ALL, bool enabled = true); + ///< Register handle with the Scheduler + /**< This constructor is like calling Scheduler::add() + unless \a enabled is \c false, in which case the handle + is \e not initially registered (it may be registered by + caling enable() + \param[in] handle Handle to register + \param[in] cb Callback + \param[in] eventMask type of events to register for + \param[in] enabled wether to add handle to Scheduler + automatically */ + + ~SchedulerBinding(); ///< Remove scheduler registration + + void enable(); ///< Add handle to Scheduler + /**< Adds the handle if it is not already registered */ + void disable(); ///< Remove handle from Scheduler + /**< Remove handle from Scheduler if registered */ + + bool enabled(); ///< \c true, if handle is registered + + protected: + + private: + int fd_; + Scheduler::FdCallback cb_; + int eventMask_; + bool enabled_; + }; +} + +///////////////////////////////hh.e//////////////////////////////////////// +#include "Binding.cci" +//#include "Binding.ct" +#include "Binding.cti" +#endif + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: diff --git a/Scheduler/Binding.test.cc b/Scheduler/Binding.test.cc new file mode 100644 index 00000000..30e9526c --- /dev/null +++ b/Scheduler/Binding.test.cc @@ -0,0 +1,51 @@ +// $Id$ +// +// Copyright (C) 2008 +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief Binding.test unit tests */ + +//#include "Binding.test.hh" +//#include "Binding.test.ih" + +// Custom includes +#include "Binding.hh" + +#include <boost/test/auto_unit_test.hpp> +#include <boost/test/test_tools.hpp> + +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +BOOST_AUTO_UNIT_TEST(binding) +{ +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: diff --git a/Socket/Protocols/BSDSocketProtocol.cc b/Socket/Protocols/BSDSocketProtocol.cc index 79754e5f..cfd06b5e 100644 --- a/Socket/Protocols/BSDSocketProtocol.cc +++ b/Socket/Protocols/BSDSocketProtocol.cc @@ -75,6 +75,16 @@ prefix_ void senf::BSDSocketProtocol::priority(boost::uint8_t value) SENF_THROW_SYSTEM_EXCEPTION(""); } +prefix_ int senf::BSDSocketProtocol::error() + const +{ + int err; + socklen_t len (sizeof(err)); + if (::getsockopt(fd(),SOL_SOCKET,SO_ERROR,&err,&len) < 0) + SENF_THROW_SYSTEM_EXCEPTION(""); + return err; +} + prefix_ unsigned senf::BSDSocketProtocol::rcvbuf() const { diff --git a/Socket/Protocols/BSDSocketProtocol.hh b/Socket/Protocols/BSDSocketProtocol.hh index 341b70ba..fbffeba8 100644 --- a/Socket/Protocols/BSDSocketProtocol.hh +++ b/Socket/Protocols/BSDSocketProtocol.hh @@ -71,6 +71,11 @@ namespace senf { sets the TOS field. \param[in] value new socket priority */ + int error() const; ///< Get and clear pending socket error + /**< This call will get <em>and clear</em> a pending socket + error. This includes asynchronous errors received via + the network (e.g. via ICMP). */ + unsigned rcvbuf() const; ///< Check receive buffer size /**< \returns size of receive buffer in bytes \internal Linux doubles the buffer size internally when -- GitLab