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
Specifies a condition and callback that an EventLoop should handle.
Definition: eventloop.hh:26
InterestT interest
A callback that returns true whenever fd should be polled.
Definition: eventloop.hh:31
Direction direction
Direction::In for reading from fd, Direction::Out for writing to fd.
Definition: eventloop.hh:29
CallbackT callback
A callback that reads or writes fd.
Definition: eventloop.hh:30
CallbackT cancel
A callback that is called when the rule is cancelled (e.g. on hangup)
Definition: eventloop.hh:32
unsigned int service_count() const
Definition: eventloop.cc:13
FileDescriptor fd
FileDescriptor to monitor for activity.
Definition: eventloop.hh:28
Waits for events on file descriptors and executes corresponding callbacks.
Definition: eventloop.hh:12
std::function< bool(void)> InterestT
true return indicates Rule::fd should be polled.
Definition: eventloop.hh:22
Result
Returned by each call to EventLoop::wait_next_event.
Definition: eventloop.hh:43
@ Success
At least one Rule was triggered.
@ Timeout
No rules were triggered before timeout.
@ Exit
All rules have been canceled or were uninterested; make no further calls to EventLoop::wait_next_even...
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
Result wait_next_event(const int timeout_ms)
Calls poll(2) and then executes callback for each ready fd.
Definition: eventloop.cc:61
std::function< void(void)> CallbackT
Callback for ready Rule::fd.
Definition: eventloop.hh:21
std::list< Rule > _rules
All rules that have been added and not canceled.
Definition: eventloop.hh:39
Direction
Indicates interest in reading (In) or writing (Out) a polled fd.
Definition: eventloop.hh:15
@ Out
Callback will be triggered when Rule::fd is writable.
@ In
Callback will be triggered when Rule::fd is readable.
A reference-counted handle to a file descriptor.