27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
#import "OFEpollKernelEventObserver.h"
#import "OFArray.h"
#import "OFMapTable.h"
#import "OFNull.h"
#import "OFInitializationFailedException.h"
#import "OFObserveFailedException.h"
#define eventListSize 64
static const OFMapTableFunctions mapFunctions = { NULL };
@implementation OFEpollKernelEventObserver
- (instancetype)init
|
|
|
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
#import "OFEpollKernelEventObserver.h"
#import "OFArray.h"
#import "OFMapTable.h"
#import "OFNull.h"
#import "OFInitializationFailedException.h"
#import "OFObserveKernelEventsFailedException.h"
#define eventListSize 64
static const OFMapTableFunctions mapFunctions = { NULL };
@implementation OFEpollKernelEventObserver
- (instancetype)init
|
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
memset(&event, 0, sizeof(event));
event.events = (int)events | addEvents;
event.data.ptr = object;
if (epoll_ctl(_epfd, (events == 0 ? EPOLL_CTL_ADD : EPOLL_CTL_MOD),
fd, &event) == -1)
@throw [OFObserveFailedException exceptionWithObserver: self
errNo: errno];
[_FDToEvents setObject: (void *)(events | addEvents)
forKey: (void *)((intptr_t)fd + 1)];
}
- (void)of_removeObject: (id)object
fileDescriptor: (int)fd
|
>
|
|
|
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
memset(&event, 0, sizeof(event));
event.events = (int)events | addEvents;
event.data.ptr = object;
if (epoll_ctl(_epfd, (events == 0 ? EPOLL_CTL_ADD : EPOLL_CTL_MOD),
fd, &event) == -1)
@throw [OFObserveKernelEventsFailedException
exceptionWithObserver: self
errNo: errno];
[_FDToEvents setObject: (void *)(events | addEvents)
forKey: (void *)((intptr_t)fd + 1)];
}
- (void)of_removeObject: (id)object
fileDescriptor: (int)fd
|
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
|
if (epoll_ctl(_epfd, EPOLL_CTL_DEL, fd, NULL) == -1)
/*
* When an async connect fails, it seems the socket is
* automatically removed from epoll, meaning ENOENT is
* returned when we try to remove it after it failed.
*/
if (errno != ENOENT)
@throw [OFObserveFailedException
exceptionWithObserver: self
errNo: errno];
[_FDToEvents removeObjectForKey: (void *)((intptr_t)fd + 1)];
} else {
struct epoll_event event;
memset(&event, 0, sizeof(event));
event.events = (int)events;
event.data.ptr = object;
if (epoll_ctl(_epfd, EPOLL_CTL_MOD, fd, &event) == -1)
@throw [OFObserveFailedException
exceptionWithObserver: self
errNo: errno];
[_FDToEvents setObject: (void *)events
forKey: (void *)((intptr_t)fd + 1)];
}
}
|
|
|
|
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
|
if (epoll_ctl(_epfd, EPOLL_CTL_DEL, fd, NULL) == -1)
/*
* When an async connect fails, it seems the socket is
* automatically removed from epoll, meaning ENOENT is
* returned when we try to remove it after it failed.
*/
if (errno != ENOENT)
@throw [OFObserveKernelEventsFailedException
exceptionWithObserver: self
errNo: errno];
[_FDToEvents removeObjectForKey: (void *)((intptr_t)fd + 1)];
} else {
struct epoll_event event;
memset(&event, 0, sizeof(event));
event.events = (int)events;
event.data.ptr = object;
if (epoll_ctl(_epfd, EPOLL_CTL_MOD, fd, &event) == -1)
@throw [OFObserveKernelEventsFailedException
exceptionWithObserver: self
errNo: errno];
[_FDToEvents setObject: (void *)events
forKey: (void *)((intptr_t)fd + 1)];
}
}
|
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
if ([self of_processReadBuffers])
return;
events = epoll_wait(_epfd, eventList, eventListSize,
(timeInterval != -1 ? timeInterval * 1000 : -1));
if (events < 0)
@throw [OFObserveFailedException exceptionWithObserver: self
errNo: errno];
for (int i = 0; i < events; i++) {
if (eventList[i].events & EPOLLIN) {
void *pool = objc_autoreleasePoolPush();
if (eventList[i].data.ptr == nullObject) {
char buffer;
|
>
|
|
|
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
if ([self of_processReadBuffers])
return;
events = epoll_wait(_epfd, eventList, eventListSize,
(timeInterval != -1 ? timeInterval * 1000 : -1));
if (events < 0)
@throw [OFObserveKernelEventsFailedException
exceptionWithObserver: self
errNo: errno];
for (int i = 0; i < events; i++) {
if (eventList[i].events & EPOLLIN) {
void *pool = objc_autoreleasePoolPush();
if (eventList[i].data.ptr == nullObject) {
char buffer;
|