-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_EPOLLONESHOT.cpp
93 lines (82 loc) · 2.15 KB
/
server_EPOLLONESHOT.cpp
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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <thread>
#include <iostream>
using namespace std;
using SockAddr = struct sockaddr_in;
constexpr int MAX_EVENT_NUMBER = 1024;
constexpr int BUFFER_SIZE = 1024;
struct fds{
int epollfd;
int sockfd;
};
int setnonblocking(int fd){
auto old = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, old | O_NONBLOCK);
return old;
}
void addfd(int epollfd, int fd, bool oneshot){
epoll_event event;
event.data.fd = fd;
event.events = EPOLLIN | EPOLLET;
if(oneshot){
event.events |= EPOLLONESHOT;
}
epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event);
setnonblocking(fd);
}
void reset_oneshot(int epollfd, int fd){
epoll_event event;
event.data.fd = fd;
event.events = EPOLLIN | EPOLLONESHOT | EPOLLET;
epoll_ctl(epollfd, EPOLL_CTL_MOD, fd, &event);
}
void* worker(void* arg){
int sockfd = ((fds*)arg)->sockfd;
int epollfd = ((fds*)arg)->epollfd;
cout << "start new thread to receive data on fd:" << sockfd << endl;
char buf[BUFFER_SIZE];
memset(buf, '\0', sizeof(buf));
while(1){
int ret = recv(sockfd, buf, BUFFER_SIZE - 1, 0);
if(ret == 0){
close(sockfd);
cout << "client closed the connection\n";
break;
}else if(ret < 0){
if(errno == EAGAIN){
reset_oneshot(epollfd, sockfd);
cout << "read later\n";
break;
}
}else{
cout << "get content:" << buf << endl;
sleep(5);
}
}
cout << "end thread receiving data on fd:" << sockfd << endl;
}
int main(int argc, char* argv[]){
if(argc <= 2){
cout << "error\n";
return 1;
}
const char* ip = argv[1];
int port = atoi(argv[2]);
int ret = 0;
SockAddr addr;
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
inet_pton(AF_INET, ip, &addr.sin_addr);
}