Sponge
CS144's user-space TCP library
eventloop.hh
Go to the documentation of this file.
1 #ifndef SPONGE_LIBSPONGE_EVENTLOOP_HH
2 #define SPONGE_LIBSPONGE_EVENTLOOP_HH
3 
4 #include "file_descriptor.hh"
5 
6 #include <cstdlib>
7 #include <functional>
8 #include <list>
9 #include <poll.h>
10 
12 class EventLoop {
13  public:
15  enum class Direction : short {
16  In = POLLIN,
17  Out = POLLOUT
18  };
19 
20  private:
21  using CallbackT = std::function<void(void)>;
22  using InterestT = std::function<bool(void)>;
23 
26  class Rule {
27  public:
33 
36  unsigned int service_count() const;
37  };
38 
40 
41  public:
43  enum class Result {
44  Success,
45  Timeout,
46  Exit
47  };
48 
50  void add_rule(const FileDescriptor &fd,
51  const Direction direction,
52  const CallbackT &callback,
53  const InterestT &interest = [] { return true; },
54  const CallbackT &cancel = [] {});
55 
57  Result wait_next_event(const int timeout_ms);
58 };
59 
61 
75 
76 #endif // SPONGE_LIBSPONGE_EVENTLOOP_HH
EventLoop::Result::Success
@ Success
At least one Rule was triggered.
std::list
file_descriptor.hh
EventLoop
Waits for events on file descriptors and executes corresponding callbacks.
Definition: eventloop.hh:12
EventLoop::Direction::Out
@ Out
Callback will be triggered when Rule::fd is writable.
EventLoop::Rule::callback
CallbackT callback
A callback that reads or writes fd.
Definition: eventloop.hh:30
EventLoop::Rule::direction
Direction direction
Direction::In for reading from fd, Direction::Out for writing to fd.
Definition: eventloop.hh:29
FileDescriptor
A reference-counted handle to a file descriptor.
Definition: file_descriptor.hh:12
std::function< void(void)>
EventLoop::Direction
Direction
Indicates interest in reading (In) or writing (Out) a polled fd.
Definition: eventloop.hh:15
EventLoop::_rules
std::list< Rule > _rules
All rules that have been added and not canceled.
Definition: eventloop.hh:39
EventLoop::wait_next_event
Result wait_next_event(const int timeout_ms)
Calls poll(2) and then executes callback for each ready fd.
Definition: eventloop.cc:61
EventLoop::Rule
Specifies a condition and callback that an EventLoop should handle.
Definition: eventloop.hh:26
EventLoop::Result::Timeout
@ Timeout
No rules were triggered before timeout.
EventLoop::CallbackT
std::function< void(void)> CallbackT
Callback for ready Rule::fd.
Definition: eventloop.hh:21
EventLoop::add_rule
void add_rule(const FileDescriptor &fd, const Direction direction, const CallbackT &callback, const InterestT &interest=[] { return true;}, const CallbackT &cancel=[] {})
Add a rule whose callback will be called when fd is ready in the specified Direction.
Definition: eventloop.cc:23
EventLoop::Rule::cancel
CallbackT cancel
A callback that is called when the rule is cancelled (e.g. on hangup)
Definition: eventloop.hh:32
EventLoop::Rule::service_count
unsigned int service_count() const
Definition: eventloop.cc:13
EventLoop::Result
Result
Returned by each call to EventLoop::wait_next_event.
Definition: eventloop.hh:43
EventLoop::Rule::fd
FileDescriptor fd
FileDescriptor to monitor for activity.
Definition: eventloop.hh:28
EventLoop::Result::Exit
@ Exit
All rules have been canceled or were uninterested; make no further calls to EventLoop::wait_next_even...
EventLoop::Direction::In
@ In
Callback will be triggered when Rule::fd is readable.
EventLoop::Rule::interest
InterestT interest
A callback that returns true whenever fd should be polled.
Definition: eventloop.hh:31
EventLoop::InterestT
std::function< bool(void)> InterestT
true return indicates Rule::fd should be polled.
Definition: eventloop.hh:22