-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommon.h
More file actions
77 lines (63 loc) · 1.93 KB
/
Copy pathcommon.h
File metadata and controls
77 lines (63 loc) · 1.93 KB
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
70
71
72
73
74
75
76
77
#ifndef COMMON_H__
#define COMMON_H__
#include <vector>
#include <sys/epoll.h>
#define MICRO (1000 * 1000)
#define BILLION (1000 * 1000 * 1000)
struct EventsList {
std::vector<epoll_event> events;
int num_ready;
unsigned long recv_time;
};
class RSelector {
public:
virtual bool add_fd(int sockfd) = 0;
virtual void remove_current_ready() = 0;
virtual bool wait(long int timeout_ns=-1) = 0;
virtual bool next(int & sockfd, uint32_t & flags) = 0;
};
class EPollRSelector: public RSelector {
protected:
int efd;
EventsList events;
std::vector<epoll_event>::iterator current_ready;
std::vector<epoll_event>::iterator end_of_ready;
#ifdef EPOLL_CALL_STATS
unsigned long int sock_activation_count;
unsigned int wait_count;
#endif
private:
EPollRSelector();
EPollRSelector(const EPollRSelector &);
public:
EPollRSelector(int sock_count);
EPollRSelector(EPollRSelector && rsel);
~EPollRSelector();
bool ok() const {return efd != -1;}
bool add_fd(int sockfd) {
return add_fd(sockfd, EPOLLIN | EPOLLET);
}
bool add_fd(int sockfd, int events);
bool wait(long int timeout_ns=-1);
void remove_current_ready();
int ready_count() const;
bool next(int & sockfd, uint32_t & flags);
bool next(int & sockfd);
};
// epoll_wait support timeout only with ms granularity
// while we need at least us presicion
bool epoll_wait_ex(int epollfd,
EventsList & ready,
long int timeout_ns);
inline unsigned long get_fast_time() {
timespec curr_time;
if( -1 == clock_gettime( CLOCK_REALTIME, &curr_time)) {
perror( "clock gettime" );
return 0;
}
return curr_time.tv_nsec + ((unsigned long)curr_time.tv_sec) * BILLION;
// using namespace std::chrono;
// auto curr_time = high_resolution_clock::now().time_since_epoch();
// return (unsigned long) duration_cast<nanoseconds>(curr_time).count();
}
#endif //COMMON_H__