forked from dyninc/OpenBFDD
-
Notifications
You must be signed in to change notification settings - Fork 1
/
KeventScheduler.cpp
137 lines (111 loc) · 3.2 KB
/
KeventScheduler.cpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**************************************************************
* Copyright (c) 2010-2013, Dynamic Network Services, Inc.
* Jake Montgomery ([email protected]) & Tom Daly ([email protected])
* Distributed under the FreeBSD License - see LICENSE
***************************************************************/
#include "config.h"
#ifdef USE_KEVENT_SCHEDULER
#include "common.h"
#include "KeventScheduler.h"
#include "utils.h"
#include <errno.h>
using namespace std;
namespace openbfdd
{
KeventScheduler::KeventScheduler() : SchedulerBase(),
m_totalEvents(0),
m_foundEvents(0),
m_nextCheckEvent(0),
m_events(m_totalEvents + 1)
{
m_kqueue = ::kqueue();
if (m_kqueue < 0)
gLog.Message(Log::Critical, "Failed to create Scheduler kqueue. Can not proceed.");
}
KeventScheduler::~KeventScheduler()
{
}
bool KeventScheduler::waitForEvents(const struct timespec &timeout)
{
m_nextCheckEvent = 0;
m_foundEvents = ::kevent(m_kqueue, NULL, 0, &m_events.front(), m_events.size(), &timeout);
if (m_foundEvents < 0)
{
m_foundEvents = 0;
gLog.LogError("kevent failed: %s", ErrnoToString());
}
else if (m_foundEvents == 0)
{
if (timeout.tv_nsec != 0 || timeout.tv_sec != 0)
gLog.Optional(Log::TimerDetail, "kevent timeout");
}
else
gLog.Optional(Log::TimerDetail, "kevent received %d events", m_foundEvents);
return m_foundEvents > 0;
}
int KeventScheduler::getNextSocketEvent()
{
if (!LogVerify(m_foundEvents <= int(m_events.size())))
m_foundEvents = m_events.size();
for (; m_nextCheckEvent < m_foundEvents; m_nextCheckEvent++)
{
// TODO check for EV_ERROR in flags?
if (m_events[m_nextCheckEvent].filter == EVFILT_READ)
{
// We have a socket event
return m_events[m_nextCheckEvent++].ident;
}
else
{
// We should only have socket events
gLog.LogError("Unexpected kevent event %" PRIuPTR " got result of %hu",
m_events[m_nextCheckEvent].ident,
m_events[m_nextCheckEvent].filter);
}
}
return -1;
}
bool KeventScheduler::watchSocket(int fd)
{
struct kevent change;
if (!LogVerify(m_kqueue != -1))
return false;
EV_SET(&change, fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, NULL);
if (kevent(m_kqueue, &change, 1, NULL, 0, NULL) < 0)
{
gLog.ErrnoError(errno, "Failed to add socket to kqueue");
return false;
}
m_totalEvents++;
resizeEvents();
return true;
}
void KeventScheduler::unWatchSocket(int fd)
{
struct kevent change;
LogAssert(m_kqueue != -1);
EV_SET(&change, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
if (kevent(m_kqueue, &change, 1, NULL, 0, NULL) < 0)
gLog.ErrnoError(errno, "Failed to remove socket to kqueue");
else
{
if (m_totalEvents > 0)
m_totalEvents--;
resizeEvents();
}
}
/**
* resizes m_events.
*
* @throw - May throw.
*
*/
void KeventScheduler::resizeEvents()
{
// Can not resize if we may be using it.
if (m_totalEvents < m_foundEvents)
return;
m_events.resize(m_totalEvents + 1);
}
}
#endif // USE_KEVENT_SCHEDULER