-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdoevent.cc
More file actions
292 lines (242 loc) · 5.34 KB
/
doevent.cc
File metadata and controls
292 lines (242 loc) · 5.34 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "doevent.h"
#include "msg.pb.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netdb.h>
#include <netinet/in.h>
#define ERR_EXIT(info)\
do\
{\
perror(info);\
exit(EXIT_FAILURE);\
} while(0)
using namespace std;
typedef vector<struct epoll_event> EventList;
static void activate_nonblock(int conn);
static int getlocalip(char *ip);
static unsigned short getlocalport(int listenfd);
void do_service(int listenfd)
{
Data::content msg;
//for epoll
vector<int> clients;
int epollfd;
epollfd = epoll_create1(EPOLL_CLOEXEC);
struct epoll_event event;
event.data.fd = listenfd;
event.events = EPOLLIN | EPOLLET;
epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &event);
EventList events(64); //init 64 events
//client's address
struct sockaddr_in peeraddr;
socklen_t len = sizeof(peeraddr);
//accept connect from client
int conn;
int i;
char recvbuf[1024] = {0};
char sendbuf = '0';
int nready;
int ret;
while(1)
{
nready = epoll_wait(epollfd,&*events.begin(),static_cast<int>(events.size()),-1);
if (nready == -1)
{
if (errno == EINTR)
continue;
ERR_EXIT("epoll_wait");
}
if (nready == 0)
continue;
if ((size_t)nready == events.size())
events.resize(events.size()*2);
for (i = 0; i < nready; ++i)
{
//client wants to connect
if (events[i].data.fd == listenfd)
{
if ((conn = accept(listenfd, (struct sockaddr*)&peeraddr, &len)) == -1)
{
ERR_EXIT("accept");
}
cout << "client:ip is "<< inet_ntoa(peeraddr.sin_addr)<<"port is "<< ntohs(peeraddr.sin_port)<<" connected."<< endl;
clients.push_back(conn);
//set nonblock
activate_nonblock(conn);
event.data.fd = conn;
event.events = EPOLLIN | EPOLLET;
epoll_ctl(epollfd, EPOLL_CTL_ADD, conn, &event);
}
//data can be read
else if (events[i].events & EPOLLIN)
{
conn = events[i].data.fd;
if (conn < 0)
continue;
memset(recvbuf, 0, sizeof(recvbuf));
ret = recv(conn, recvbuf, sizeof(recvbuf), 0);
if (ret == -1)
ERR_EXIT("recv");
if (ret == 0)
{
struct sockaddr_in clientaddr;
socklen_t client_len = sizeof(clientaddr);
if (getpeername(conn,(struct sockaddr*)&clientaddr, &client_len) == -1)
{
ERR_EXIT("getpeername");
}
cout<<"client ip is "<<inet_ntoa(clientaddr.sin_addr)<<" port is "<<ntohs(clientaddr.sin_port)<<" close."<<endl;
close(conn);
event = events[i];
epoll_ctl(epollfd, EPOLL_CTL_DEL, conn, &event);
clients.erase(remove(clients.begin(), clients.end(),conn),clients.end());
continue;
}
//decode data
string data = recvbuf;
msg.ParseFromString(data);
cout << "From "<< msg.address()<<" "<<msg.port()<<": ";
cout << msg.str()<<endl;
if (send(conn, &sendbuf, strlen(&sendbuf),0) == -1)
{
ERR_EXIT("send");
}
}
}
}
close(conn);
}
void do_client(int listenfd)
{
Data::content msg;
string str;
string data;
char sendbuf[1024] = {0};
char recvbuf = '0';
char ip[100];
unsigned short port;
//get local ip and port
getlocalip(ip);
port = getlocalport(listenfd);
//for select
fd_set rset;
FD_ZERO(&rset);
int nready;
int maxfd;
int fd_stdin = fileno(stdin);
if (fd_stdin > listenfd)
{
maxfd = fd_stdin;
}
else
{
maxfd = listenfd;
}
int flag = 1;
while(1)
{
if (flag)
{
cout << "input the msg you want to send: ";
fflush(stdout);
flag = 0;
}
FD_SET(fd_stdin, &rset);
nready = select(maxfd+1, &rset, NULL, NULL, NULL);
if (nready == -1)
{
ERR_EXIT("select");
}
if (nready == 0)
continue;
if (FD_ISSET(fd_stdin, &rset))
{
getline(cin,str);
memset(sendbuf, 0, sizeof(sendbuf));
//encode data
msg.set_port(port);
msg.set_str(str);
msg.set_address(ip);
msg.SerializeToString(&data);
strcpy(sendbuf, data.c_str());
if (send(listenfd, sendbuf, strlen(sendbuf), 0) == -1)
{
ERR_EXIT("send");
}
cout << "input the msg you want to send: ";
fflush(stdout);
}
if (FD_ISSET(listenfd, &rset))
{
int ret = recv(listenfd, &recvbuf, strlen(&recvbuf), 0);
if (ret == -1)
{
ERR_EXIT("recv");
}
else if (ret == 0)
{
cout << endl;
cout << "reminder: server closed." << endl;
break;
}
}
}
close(listenfd);
}
//set nonblock
static void activate_nonblock(int conn)
{
int ret;
int flag = fcntl(conn, F_GETFL);
if (flag == -1)
{
ERR_EXIT("fcntl get");
}
flag |= O_NONBLOCK;
ret = fcntl(conn, F_SETFL, flag);
if (ret == -1)
{
ERR_EXIT("fcntl set");
}
}
//get local ip
static int getlocalip(char *ip)
{
char host[100] = {0};
if (gethostname(host, sizeof(host)) < 0)
{
ERR_EXIT("gethostname");
}
struct hostent *he;
if ((he = gethostbyname(host)) == NULL)
{
ERR_EXIT("gethostbyname");
}
strcpy(ip, inet_ntoa(*(struct in_addr*)he->h_addr));
return 0;
}
//get local port
static unsigned short getlocalport(int listenfd)
{
unsigned short port;
struct sockaddr_in localaddr;
socklen_t addrlen = sizeof(localaddr);
if (getsockname(listenfd, (struct sockaddr*)&localaddr, &addrlen) < 0)
{
ERR_EXIT("getsockname");
}
port = ntohs(localaddr.sin_port);
return port;
}