-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathequeue.c
More file actions
86 lines (76 loc) · 2.05 KB
/
equeue.c
File metadata and controls
86 lines (76 loc) · 2.05 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
#include <stddef.h>
#include "equeue.h"
#include <sys/time.h>
// 0.05 секунды (здесь не может быть >= 1 секунда)
#define EQUEUE_WAIT_TIMEOUT_MS 50
int initEqueue() {
#ifdef __APPLE__
return kqueue();
#else
return epoll_create1(0);
#endif
}
/**
* @param equeue
* @param clientSocket
* @warning Нельзя добавлять serverSocket
*/
void addClientEqueue(int equeue, int clientSocket) {
#ifdef __APPLE__
struct kevent kEvent;
EV_SET(&kEvent, clientSocket, EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, NULL);
kevent(equeue, &kEvent, 1, NULL, 0, NULL);
#else
struct epoll_event ev;
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = clientSocket;
epoll_ctl(equeue, EPOLL_CTL_ADD, clientSocket, &ev);
#endif
}
/**
* @param equeue
* @param clientSocket
* @warning Нельзя добавлять serverSocket
*/
void deleteClientEqueue(int equeue, int clientSocket) {
#ifdef __APPLE__
// Автоматическое удаление
struct kevent kEvent;
EV_SET(&kEvent, clientSocket, EVFILT_READ, EV_DELETE, 0, 0, NULL);
kevent(equeue, &kEvent, 1, NULL, 0, NULL);
#else
struct epoll_event ev;
epoll_ctl(equeue, EPOLL_CTL_DEL, clientSocket, &ev);
#endif
}
int checkEqueue(int equeue, struct Eevent *eevent) {
#ifdef __APPLE__
struct timespec timespec;
timespec.tv_sec = 0;
timespec.tv_nsec = EQUEUE_WAIT_TIMEOUT_MS * 1000000;
return kevent(equeue, NULL, 0, eevent->evList, EQUEUE_EVENTS_EACH_LOOP, ×pec);
#else
return epoll_wait(equeue, eevent->evList, EQUEUE_EVENTS_EACH_LOOP, EQUEUE_WAIT_TIMEOUT_MS);
#endif
}
int getSocketEqueue(struct Eevent *eevent, int index) {
#ifdef __APPLE__
return (int) eevent->evList[index].ident;
#else
return eevent->evList[index].data.fd;
#endif
}
int isEof(struct Eevent *eevent, int index) {
#ifdef __APPLE__
return eevent->evList[index].flags & EV_EOF;
#else
return 0;
#endif
}
int isRead(struct Eevent *eevent, int index) {
#ifdef __APPLE__
return eevent->evList[index].filter == EVFILT_READ;
#else
return 1;
#endif
}