-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreactor.cpp
More file actions
194 lines (179 loc) · 6.13 KB
/
Copy pathreactor.cpp
File metadata and controls
194 lines (179 loc) · 6.13 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
#include "reactor.h"
Reactor::Reactor()
{
}
void Reactor::delete_event(int fd, int state)
{
ev.data.fd = fd;
ev.events = state | EPOLLET;
epoll_ctl(_epfd,EPOLL_CTL_DEL,fd,&ev);
}
void Reactor::add_event(int fd, int state)
{
ev.data.fd = fd;
ev.events = state | EPOLLET;
epoll_ctl(_epfd,EPOLL_CTL_ADD,fd,&ev);
}
void Reactor::modify_event(int fd, int state)
{
ev.data.fd = fd;
ev.events = state | EPOLLET;
epoll_ctl(_epfd,EPOLL_CTL_MOD,fd,&ev);
}
int Reactor::bind(const int sockfd){
_sockfd = sockfd;
ev.data.fd = _sockfd;
ev.events = EPOLLET | EPOLLIN;
epoll_ctl(_epfd,EPOLL_CTL_ADD,_sockfd,&ev);
}
int Reactor::bind(ReactorInterface& binder)
{
_binder = &binder;
// bind(_binder.getNetfd());
}
int Reactor::init(int maxPoller)
{
_maxPoller = maxPoller;
_epfd = epoll_create(_maxPoller);
_nfds = -1;
events = (epoll_event*)calloc(_maxPoller,sizeof(epoll_event));
return 1;
}
Reactor::~Reactor()
{
close(_sockfd);
close(_epfd);
free(events);
}
void Reactor::WaitEpollEvents()
{
_nfds = epoll_wait(_epfd,events,_maxPoller,-1);
}
int Reactor::runEventLoop()
{
int i = 0;
int clntfd;
sockaddr_in clnt_addr;
const int buffer_lenth = 1024;
char recev_buffer[buffer_lenth];
char send_buffer[buffer_lenth] = "welcome to myserver!";
int tmp_N;
char* clnt_ip_str;
int flag;
int clnt_addr_len = sizeof(sockaddr_in);
int cnt1 = 0,cnt2 = 0;
printf("debug1\n");
// while(1)
// {
WaitEpollEvents();
printf("debug2\n");
for(i = 0;i < _nfds;++ i)
{
if(events[i].data.fd == _sockfd)
{
clntfd = accept(_sockfd,(sockaddr*)&clnt_addr,(socklen_t*)&clnt_addr_len);
if(clntfd < 0)
{
perror("accept error\n");
continue;
}
//Cheack
if(_binder->CheackClientProcess(&clnt_addr,clntfd) == ERROR_TYPE_ERROR)
{//can not connect
close(clntfd);
continue;
}
printf("accept\n");
clnt_ip_str = inet_ntoa(clnt_addr.sin_addr);
printf("accept the client ip: %s\n",clnt_ip_str);
add_event(clntfd,EPOLLIN);
}else if(events[i].events & EPOLLOUT){
printf("EPOLLOUT cnt1[%d]\n",cnt1);
++cnt1;
if((clntfd = events[i].data.fd) < 0)
{
continue;
}
flag = _binder->OutputProcess(clntfd);
if(flag == ERROR_TYPE_ERROR)
{
close(clntfd);
delete_event(clntfd,EPOLLOUT);
printf("-------------EPOLLOUT error---------\n");
printf("close client, fd[%d] \n",clntfd);
}else if(flag == ERROR_TYPE_FINISH)
{
close(clntfd);
delete_event(clntfd,EPOLLOUT);
printf("-------------EPOLLOUT finish---------\n");
printf("close client, fd[%d] \n",clntfd);
}else if(flag == ERROR_TYPE_CONTINUE)
{
printf("-------------EPOLLOUT finish and continue connect---------\n");
modify_event(clntfd,EPOLLIN);
}else if(flag == ERROR_TYPE_TEST_CONTINUE)
{
printf("-------------EPOLLOUT Test OutputProcess and continue connect---------\n");
flag = write(clntfd,send_buffer,buffer_lenth);
if(flag < 0)
{
perror("write error!\n");
continue;
}
modify_event(clntfd,EPOLLIN);
}
}else if(events[i].events & EPOLLIN)
{
printf("EPOLLIN cnt2[%d]\n",cnt2);
++cnt2;
if((clntfd = events[i].data.fd) < 0){
continue;
}
flag = _binder->InputProcess(clntfd);
if(flag == ERROR_TYPE_TEST_CONTINUE)
{
printf("-------------EPOLLIN Test InputProcess and continue connect---------\n");
bzero(recev_buffer,sizeof(recev_buffer));
printf("recev_buffer : [%s]\n",recev_buffer);
if((tmp_N = read(clntfd,recev_buffer,buffer_lenth)) < 0)
{
if(errno == ECONNRESET)
{
close(clntfd);
delete_event(clntfd,EPOLLIN);
printf("errno ECONRESET! \n");
}else{
perror("readbuffer error!\n");
}
}else if(tmp_N == 0)
{
close(clntfd);
delete_event(clntfd,EPOLLIN);
printf("client close\n");
}
if(recev_buffer[0] != 0)
{
printf("recv msg:%s bufferlen[%d]\n",recev_buffer,(int32_t)strlen(recev_buffer));
modify_event(clntfd,EPOLLOUT);
}
}else if(flag == ERROR_TYPE_FINISH)
{
close(clntfd);
delete_event(clntfd,EPOLLIN);
printf("-------------EPOLLIN finish---------\n");
printf("close client, fd[%d] \n",clntfd);
}else if(flag == ERROR_TYPE_CONTINUE)
{
printf("-------------EPOLLIN finish and continue connect---------\n");
modify_event(clntfd,EPOLLOUT);
}else if(flag == ERROR_TYPE_ERROR)
{
close(clntfd);
delete_event(clntfd,EPOLLIN);
printf("-------------EPOLLIN error---------\n");
printf("close client, fd[%d] \n",clntfd);
}
}
}
// }
}