Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eventloop.hh: Allow the user to provide a custom error handler. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions src/util/eventloop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,32 @@ EventLoop::BasicRule::BasicRule( const size_t category_id, const InterestT& inte
, cancel_requested( false )
{}

EventLoop::FDRule::FDRule( BasicRule&& base, FileDescriptor&& fd, const Direction direction, const CallbackT& cancel )
EventLoop::FDRule::FDRule( BasicRule&& base,
FileDescriptor&& fd,
const Direction direction,
const CallbackT& cancel,
const optional<CallbackT>& error )
: BasicRule( base )
, fd( move( fd ) )
, direction( direction )
, cancel( cancel )
, error( error )
{}

EventLoop::RuleHandle EventLoop::add_rule( const size_t category_id,
const FileDescriptor& fd,
const Direction direction,
const CallbackT& callback,
const InterestT& interest,
const CallbackT& cancel )
const CallbackT& cancel,
const optional<CallbackT>& error )
{
if ( category_id >= _rule_categories.size() ) {
throw out_of_range( "bad category_id" );
}

_fd_rules.emplace_back(
make_shared<FDRule>( BasicRule { category_id, interest, callback }, fd.duplicate(), direction, cancel ) );
make_shared<FDRule>( BasicRule { category_id, interest, callback }, fd.duplicate(), direction, cancel, error ) );

return _fd_rules.back();
}
Expand Down Expand Up @@ -168,21 +174,26 @@ EventLoop::Result EventLoop::wait_next_event( const int timeout_ms )

const auto poll_error = static_cast<bool>( this_pollfd.revents & ( POLLERR | POLLNVAL ) );
if ( poll_error ) {
/* see if fd is a socket */
int socket_error = 0;
socklen_t optlen = sizeof( socket_error );
const int ret = getsockopt( this_rule.fd.fd_num(), SOL_SOCKET, SO_ERROR, &socket_error, &optlen );
if ( ret == -1 and errno == ENOTSOCK ) {
throw runtime_error( "error on polled file descriptor for rule \""
+ _rule_categories.at( this_rule.category_id ).name + "\"" );
} else if ( ret == -1 ) {
throw unix_error( "getsockopt" );
} else if ( optlen != sizeof( socket_error ) ) {
throw runtime_error( "unexpected length from getsockopt: " + to_string( optlen ) );
} else if ( socket_error ) {
throw unix_error( "error on polled socket for rule \"" + _rule_categories.at( this_rule.category_id ).name
+ "\"",
socket_error );
/* do we have an error handler? */
if ( this_rule.error.has_value() ) {
( *this_rule.error )();
} else {
/* see if fd is a socket */
int socket_error = 0;
socklen_t optlen = sizeof( socket_error );
const int ret = getsockopt( this_rule.fd.fd_num(), SOL_SOCKET, SO_ERROR, &socket_error, &optlen );
if ( ret == -1 and errno == ENOTSOCK ) {
throw runtime_error( "error on polled file descriptor for rule \""
+ _rule_categories.at( this_rule.category_id ).name + "\"" );
} else if ( ret == -1 ) {
throw unix_error( "getsockopt" );
} else if ( optlen != sizeof( socket_error ) ) {
throw runtime_error( "unexpected length from getsockopt: " + to_string( optlen ) );
} else if ( socket_error ) {
throw unix_error( "error on polled socket for rule \"" + _rule_categories.at( this_rule.category_id ).name
+ "\"",
socket_error );
}
}

this_rule.cancel();
Expand Down
37 changes: 21 additions & 16 deletions src/util/eventloop.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <functional>
#include <list>
#include <memory>
#include <optional>
#include <string_view>

#include <poll.h>
Expand Down Expand Up @@ -43,11 +44,16 @@ private:

struct FDRule : public BasicRule
{
FileDescriptor fd; //!< FileDescriptor to monitor for activity.
Direction direction; //!< Direction::In for reading from fd, Direction::Out for writing to fd.
CallbackT cancel; //!< A callback that is called when the rule is cancelled (e.g. on hangup)
FileDescriptor fd; //!< FileDescriptor to monitor for activity.
Direction direction; //!< Direction::In for reading from fd, Direction::Out for writing to fd.
CallbackT cancel; //!< A callback that is called when the rule is cancelled (e.g. on hangup)
std::optional<CallbackT> error; //<! A callback that is called when an error happens

FDRule( BasicRule&& base, FileDescriptor&& fd, const Direction direction, const CallbackT& cancel );
FDRule( BasicRule&& base,
FileDescriptor&& fd,
const Direction direction,
const CallbackT& cancel,
const std::optional<CallbackT>& error );

//! Returns the number of times fd has been read or written, depending on the value of Rule::direction.
//! \details This function is used internally by EventLoop; you will not need to call it
Expand Down Expand Up @@ -82,18 +88,17 @@ public:
void cancel();
};

RuleHandle add_rule(
const size_t category_id,
const FileDescriptor& fd,
const Direction direction,
const CallbackT& callback,
const InterestT& interest = [] { return true; },
const CallbackT& cancel = [] {} );

RuleHandle add_rule(
const size_t category_id,
const CallbackT& callback,
const InterestT& interest = [] { return true; } );
RuleHandle add_rule( const size_t category_id,
const FileDescriptor& fd,
const Direction direction,
const CallbackT& callback,
const InterestT& interest = [] { return true; },
const CallbackT& cancel = [] {},
const std::optional<CallbackT>& error = std::nullopt );

RuleHandle add_rule( const size_t category_id, const CallbackT& callback, const InterestT& interest = [] {
return true;
} );

//! Calls [poll(2)](\ref man2::poll) and then executes callback for each ready fd.
Result wait_next_event( const int timeout_ms );
Expand Down