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
|
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <poll.h>
#include <fdwatcher.h>
int
fdwatcher_initialise(struct FDWatchHandle *handle, int maxWatchedFds)
{
size_t pollfdSize = maxWatchedFds * sizeof(handle->watchedFds);
size_t pointersSize = maxWatchedFds * sizeof(void *);
handle->watchedFds = malloc(pollfdSize);
if (errno != 0) {
perror("fdwatcher_initialise: malloc(3)");
return -errno;
}
handle->arbitraryData = malloc(pointersSize);
if (errno != 0) {
perror("fdwatcher_initialise: malloc(3)");
return -errno;
}
memset(handle->watchedFds, 0, pollfdSize);
handle->maxWatchedFds = maxWatchedFds;
return 0;
}
int
fdwatcher_add(struct FDWatchHandle *handle, int fd, void *arbitrary)
{
struct pollfd *watchedFd = NULL;
if (handle->maxWatchedFds == handle->watchedFdCount) {
printf("meow\n");
return -EMFILE;
}
handle->arbitraryData[handle->watchedFdCount] = arbitrary;
watchedFd = &(handle->watchedFds[handle->watchedFdCount]);
watchedFd->events = POLLIN | POLLHUP | POLLRDHUP;
watchedFd->revents = 0;
watchedFd->fd = fd;
handle->watchedFdCount = handle->watchedFdCount + 1;
return 0;
}
static int
_fdwatcher_handleEvents_revents(struct pollfd *pollie, struct FDWatchHandle *handle,
int (*meow)(struct FDWatchHandle *, enum FDWatch_EventType, int, void *),
size_t elementNumber)
{
int ret = 0;
if (pollie->revents & POLLHUP || pollie->revents & POLLRDHUP) {
ret = meow(handle, FDWATCH_EVENT_HUP, pollie->fd, handle->arbitraryData[elementNumber]);
if (ret == 0) {
return 1;
}
}
if (pollie->revents & POLLIN) {
meow(handle, FDWATCH_EVENT_INP, pollie->fd, handle->arbitraryData[elementNumber]);
}
return 0;
}
static int
_fdwatcher_handleEvents(struct FDWatchHandle *handle, int (*meow)(struct FDWatchHandle *handle, enum FDWatch_EventType, int, void *))
{
size_t i = 0;
int ret = 0;
while (1) {
if (i == handle->watchedFdCount) break;
struct pollfd *watchedFdEntry = NULL;
watchedFdEntry = &handle->watchedFds[i];
if (watchedFdEntry->revents != 0) {
ret = _fdwatcher_handleEvents_revents(watchedFdEntry, handle, meow, i);
watchedFdEntry->revents = 0;
if (ret != 0) {
return 1;
}
}
i++;
}
return 0;
}
int
fdwatcher_watch(struct FDWatchHandle *handle, int (*meow)(struct FDWatchHandle *handle, enum FDWatch_EventType, int, void *))
{
int ret = 0;
while (1) {
ret = poll(handle->watchedFds, handle->watchedFdCount, INT_MAX);
if (errno != 0) {
ret = meow(handle, FDWATCH_EVENT_ERR, 0, NULL);
if (ret == 1) break;
errno = 0;
continue;
}
ret = _fdwatcher_handleEvents(handle, meow);
if (ret == 1) {
return 1;
}
}
return 0;
}
/* TODO */
int
fdwatcher_remove(struct FDWatchHandle *handle, int fd)
{
abort();
}
|