-
Notifications
You must be signed in to change notification settings - Fork 0
/
Loop.h
70 lines (60 loc) · 1.98 KB
/
Loop.h
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
//
// Created by jeffrey on 3/17/15.
//
#ifndef _EPOLL_TCP_EPOLLLOOP_H_
#define _EPOLL_TCP_EPOLLLOOP_H_
#include "net_include.h"
#include <sys/time.h>
#include <set>
#include <forward_list>
#include <map>
#include <functional>
#include <memory>
#include "time_util.h"
#include <vector>
using namespace std;
using fd_callback = function<void (uint32_t)>;
namespace loop_help
{
template<typename T>
class xref_wrap : public std::reference_wrapper<T>
{
public:
xref_wrap(T& x) : std::reference_wrapper<T>(x) {}
xref_wrap(const reference_wrapper<T>& x) : std::reference_wrapper<T>(x) {}
xref_wrap(const xref_wrap<T>& other) : std::reference_wrapper<T>(static_cast<const std::reference_wrapper<T>&>(other)) {}
bool operator<(xref_wrap const rhs) const {
return this->get() < rhs.get();
}
};
}
class Loop {
public:
Loop();
~Loop();
//Will be called with the number of events returned by epoll_wait and the number of timers that were called
void set_default(function<void (int events, int timers_called)> cb);
void set_poll_timeout(int millis);
int add_fd(int fd, uint32_t events, const fd_callback& cb);
void change_cb(int fd, const fd_callback& cb);
int mod_fd(int fd, uint32_t events, fd_callback cb);
int del_fd(int fd);
//void queue_event(timeval delta, function<void ()>, const string& id);
void queue_event(timeval delta, function<void ()>, uintptr_t id);
void remove_event(uintptr_t id);
int handle_events();
void exit_loop();
private:
int efd;
const static int MAX_EVENTS = 1000;
map<int,fd_callback*> fd_to_cb;
function<void (int, int)> default_cb;
map<pair<timeval, uintptr_t>, pair<function<void ()>, map<uintptr_t, timeval>::iterator>> timers;
map<uintptr_t, timeval> id_to_timer;
forward_list<fd_callback*> deleted_callbacks;
set<int> deleted_fds;
bool done;
bool handling_events;
int poll_timeout;
};
#endif //_EPOLL_TCP_EPOLLLOOP_H_