comm_manager_epoll.c
6.34 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* \file
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2014 Georg Hopp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h>
#include <poll.h>
#include <sys/epoll.h>
#include "trbase.h"
#include "trdata.h"
#include "trevent.h"
#include "tr/comm_manager.h"
#include "tr/comm_manager_epoll.h"
#include "tr/interface/comm_manager.h"
#include "tr/comm_end_point.h"
#include "tr/connection.h"
#include "tr/connect_entry_point.h"
#define MAXEVENTS 256
struct epoll_event events[MAXEVENTS];
static
int
commManagerEpollCtor(void * _this, va_list * params)
{
TR_CommManagerEpoll this = _this;
TR_CommManager cmgr = _this;
TR_PARENTCALL(TR_CommManagerEpoll, _this, TR_Class, ctor, params);
this->handle = epoll_create(cmgr->n_endpoints);
this->read_ready = TR_new(TR_Queue);
this->write_ready = TR_new(TR_Queue);
this->read_ready->free_msgs = 0;
this->write_ready->free_msgs = 0;
return 0;
}
static
void
commManagerEpollDtor(void * _this)
{
TR_CommManagerEpoll this = _this;
TR_delete(this->read_ready);
TR_delete(this->write_ready);
close(this->handle);
TR_PARENTCALL(TR_CommManagerEpoll, _this, TR_Class, dtor);
}
static
void
TR_commManagerEpollAddEndpoint(void * _this, TR_CommEndPoint endpoint)
{
TR_CommManagerEpoll this = _this;
int handle = endpoint->transport->handle;
struct epoll_event event;
event.data.ptr = endpoint;
event.events = EPOLLIN | EPOLLOUT | EPOLLET;
epoll_ctl(this->handle, EPOLL_CTL_ADD, handle, &event);
}
static
void
TR_commManagerEpollSelect(void * _this, TR_Event event, int timeout)
{
TR_CommManagerEpoll this = _this;
int i, nevents;
TR_Queue node;
if (0 != (this->read_ready->nmsg & this->write_ready->nmsg)) {
timeout = 0;
}
nevents = epoll_wait(this->handle, events, MAXEVENTS, timeout);
for (i=0; i<nevents; i++) {
TR_CommEndPoint endpoint = (TR_CommEndPoint)events[i].data.ptr;
if ((events[i].events & EPOLLIN) == EPOLLIN) {
if (TR_INSTANCE_OF(TR_TcpSocket, endpoint->transport)
&& ((TR_TcpSocket)endpoint->transport)->listen) {
TR_eventHandlerIssueEvent((TR_EventHandler)this,
TR_eventSubjectEmit(
(TR_EventSubject)endpoint,
TR_CET_EVENT_ACC_READY,
NULL));
} else {
if (! ((TR_EventSubject)endpoint)->fin) {
TR_queuePut(this->read_ready, endpoint);
}
}
}
if ((events[i].events & EPOLLOUT) == EPOLLOUT) {
if (TR_cepHasPendingData(endpoint) &&
! ((TR_EventSubject)endpoint)->fin) {
TR_queuePut(this->write_ready, endpoint);
}
}
}
/* now issue reads and write events */
for (node=this->read_ready->first; node; node=node->next) {
TR_CommEndPoint endpoint = (TR_CommEndPoint)node->msg;
if (! TR_socketFinRd(endpoint->transport)) {
TR_eventHandlerIssueEvent(
(TR_EventHandler)this,
TR_eventSubjectEmit(
(TR_EventSubject)endpoint,
TR_CEP_EVENT_READ_READY,
NULL));
}
}
for (node=this->write_ready->first; node; node=node->next) {
TR_CommEndPoint endpoint = (TR_CommEndPoint)node->msg;
if (! TR_socketFinWr(endpoint->transport)) {
TR_eventHandlerIssueEvent(
(TR_EventHandler)this,
TR_eventSubjectEmit(
(TR_EventSubject)endpoint,
TR_CEP_EVENT_WRITE_READY,
NULL));
}
}
}
static
void
TR_commManagerEpollRemoveWrite(void * _this, TR_Event event)
{
TR_CommManagerEpoll this = _this;
TR_CommEndPoint endpoint = (TR_CommEndPoint)event->subject;
TR_queueDelete(this->write_ready, endpoint);
}
static
void
TR_commManagerEpollRemoveRead(void * _this, TR_Event event)
{
TR_CommManagerEpoll this = _this;
TR_CommEndPoint endpoint = (TR_CommEndPoint)event->subject;
TR_queueDelete(this->read_ready, endpoint);
}
static
void
TR_commManagerEpollClose(void * _this, TR_Event event)
{
TR_CommManagerEpoll this = _this;
TR_CommEndPoint endpoint = (TR_CommEndPoint)event->subject;
TR_queueDelete(this->read_ready, endpoint);
TR_queueDelete(this->write_ready, endpoint);
epoll_ctl(this->handle, EPOLL_CTL_DEL, endpoint->transport->handle, NULL);
}
static
void
TR_commManagerEpollShutRead(void * _this, TR_Event event)
{
TR_CommManagerEpoll this = _this;
TR_CommEndPoint endpoint = (TR_CommEndPoint)event->subject;
struct epoll_event _event;
TR_queueDelete(this->read_ready, endpoint);
_event.data.ptr = endpoint;
_event.events = EPOLLOUT | EPOLLET;
epoll_ctl(
this->handle,
EPOLL_CTL_MOD,
endpoint->transport->handle,
&_event);
}
static
void
TR_commManagerEpollShutWrite(void * _this, TR_Event event)
{
TR_CommManagerEpoll this = _this;
TR_CommEndPoint endpoint = (TR_CommEndPoint)event->subject;
struct epoll_event _event;
TR_queueDelete(this->write_ready, endpoint);
_event.data.ptr = endpoint;
_event.events = EPOLLIN | EPOLLET;
epoll_ctl(
this->handle,
EPOLL_CTL_MOD,
endpoint->transport->handle,
&_event);
}
static void TR_commManagerEpollNoop(void * _this, TR_Event event) {}
static
void
TR_commManagerEpollCvInit(TR_class_ptr cls) {
TR_INHERIT_CLASSVARS(TR_CommManagerEpoll, TR_CommManager);
}
TR_INIT_IFACE(TR_Class, commManagerEpollCtor, commManagerEpollDtor, NULL);
TR_INIT_IFACE(
TR_CommManager,
TR_commManagerEpollAddEndpoint, // TR_CON_EVENT_NEW_CON
TR_commManagerEpollSelect, // TR_DISPATCHER_EVENT_DATA_WAIT
TR_commManagerEpollRemoveWrite, // TR_CEP_EVENT_PENDING_DATA => WRITE_BLOCK
TR_commManagerEpollNoop, // TR_CEP_EVENT_END_DATA
TR_commManagerEpollRemoveRead, // TR_CEP_EVENT_READ_BLOCK
TR_commManagerEpollClose, // TR_CEP_EVENT_CLOSE
TR_commManagerEpollShutWrite, // TR_CEP_EVENT_SHUT_READ
TR_commManagerEpollShutRead); // TR_CEP_EVENT_SHUT_WRITE
TR_CREATE_CLASS(
TR_CommManagerEpoll,
TR_CommManager,
TR_commManagerEpollCvInit,
TR_IF(TR_Class),
TR_IF(TR_CommManager));
// vim: set ts=4 sw=4: