forked from ravinet/mahimahi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoller.hh
69 lines (55 loc) · 1.93 KB
/
poller.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* -*-mode:c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#ifndef POLLER_HH
#define POLLER_HH
#include <functional>
#include <vector>
#include <cassert>
#include <poll.h>
#include "file_descriptor.hh"
class Poller
{
public:
struct Action
{
struct Result
{
enum class Type { Continue, Exit, Cancel } result;
unsigned int exit_status;
Result( const Type & s_result = Type::Continue, const unsigned int & s_status = EXIT_SUCCESS )
: result( s_result ), exit_status( s_status ) {}
};
typedef std::function<Result(void)> CallbackType;
FileDescriptor & fd;
enum PollDirection : short { In = POLLIN, Out = POLLOUT } direction;
CallbackType callback;
std::function<bool(void)> when_interested;
bool active;
Action( FileDescriptor & s_fd,
const PollDirection & s_direction,
const CallbackType & s_callback,
const std::function<bool(void)> & s_when_interested = [] () { return true; } )
: fd( s_fd ), direction( s_direction ), callback( s_callback ),
when_interested( s_when_interested ), active( true ) {}
unsigned int service_count( void ) const;
};
private:
std::vector< Action > actions_;
std::vector< pollfd > pollfds_;
public:
struct Result
{
enum class Type { Success, Timeout, Exit } result;
unsigned int exit_status;
Result( const Type & s_result, const unsigned int & s_status = EXIT_SUCCESS )
: result( s_result ), exit_status( s_status ) {}
};
Poller() : actions_(), pollfds_() {}
void add_action( Action action );
Result poll( const int & timeout_ms );
};
namespace PollerShortNames {
typedef Poller::Action::Result Result;
typedef Poller::Action::Result::Type ResultType;
typedef Poller::Action::PollDirection Direction;
}
#endif