forked from netstao/connect-pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpNetWork.c
executable file
·147 lines (137 loc) · 4.8 KB
/
cpNetWork.c
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
/*
+----------------------------------------------------------------------+
| common con pool |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Xinhua Guo <woshiguo35@sina.com> |
+----------------------------------------------------------------------+
*/
#include "php_connect_pool.h"
int cpEpoll_add(int epfd, int fd, int fdtype) {
struct epoll_event e;
int ret;
bzero(&e, sizeof (struct epoll_event));
e.data.fd = fd;
e.events = fdtype;
ret = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &e);
if (ret < 0)
{
cpLog("add event fail. Error: %s[%d]", strerror(errno), errno);
return FAILURE;
}
return SUCCESS;
}
int cpEpoll_del(int epfd, int fd) {
struct epoll_event e;
int ret;
e.data.fd = fd;
if (fd <= 0)
{
return FAILURE;
}
// e.events = EPOLLIN | EPOLLET | EPOLLOUT;
ret = epoll_ctl(epfd, EPOLL_CTL_DEL, fd, &e);
if (ret < 0)
{
cpLog("epoll remove fd[=%d] fail. Error: %s[%d]", fd, strerror(errno), errno);
return SUCCESS;
}
//close时会自动从epoll事件中移除
ret = close(fd);
return SUCCESS;
}
CPINLINE int cpReactor_error() {
switch (errno)
{
case EINTR:
return SUCCESS;
}
return FAILURE;
}
int cpEpoll_wait(epoll_wait_handle *handles, struct timeval *timeo, int epfd) {
int i, n, ret, usec;
// int pack_size = sizeof (uint32_t)*8;
if (timeo == NULL)
{
usec = CP_MAX_UINT;
}
else
{
usec = timeo->tv_sec * 1000 + timeo->tv_usec / 1000;
}
// uint8_t *run;
// if (CPGL.process_type == CP_PROCESS_WORKER) {
// run = &CPGS->workers[CPWG.id].run;
// } else {
// run = &CPGL.running;
// }
struct epoll_event events[CP_REACTOR_MAXEVENTS];
while (CPGS->running)
{
n = epoll_wait(epfd, events, CP_REACTOR_MAXEVENTS, usec);
for (i = 0; i < n; i++)
{
//取出事件
// ev.fd = object->events[i].data.u64;
// ev.type = object->events[i].data.u64 >> pack_size;
// if (events[i].events & EPOLLPRI) {
// char buf[1];
// ret = handles[EPOLLPRI](events[i].data.fd);
// recv(events[i].data.fd, buf, 1, MSG_OOB);
// if (ret < 0) {
// cpLog("epoll [EPOLLPRI] handle failed. fd=%d. Error: %s[%d]", events[i].data.fd,
// strerror(errno), errno);
// }
// }
if (events[i].events & EPOLLIN)
{
ret = handles[EPOLLIN](events[i].data.fd);
if (ret < 0)
{
cpLog("epoll [EPOLLIN] handle failed. fd=%d. Error: %s[%d]", events[i].data.fd,
strerror(errno), errno);
}
}
else if (events[i].events & EPOLLOUT)
{
ret = handles[EPOLLIN](events[i].data.fd);
if (ret < 0)
{
cpLog("epoll [EPOLLOUT] handle failed. fd=%d. Error: %s[%d]", events[i].data.fd,
strerror(errno), errno);
}
}
#ifndef NO_EPOLLRDHUP
else if ((events[i].events & (EPOLLRDHUP | EPOLLERR | EPOLLHUP)))
#else
else if ((events[i].events & (EPOLLERR | EPOLLHUP)))
#endif
{
if (events[i].data.fd > 0)
{
ret = handles[EPOLL_CLOSE](events[i].data.fd);
if (ret < 0)
{
cpLog("epoll [EPOLLRDHUP] handle failed. fd=%d. Error: %s[%d]", events[i].data.fd, strerror(errno), errno);
}
}
}
}
if (n < 0)
{
if (cpReactor_error() < 0)
{
cpLog("Epoll[#%d] Error: %s[%d]", events[i].data.fd, strerror(errno), errno);
return FAILURE;
}
}
}
return 0;
}