-
Notifications
You must be signed in to change notification settings - Fork 1
/
Server.h
215 lines (195 loc) · 5.66 KB
/
Server.h
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
/*
* =====================================================================================
*
* Filename: Server.h
*
* Description:
*
* Version: 1.0
* Created: 2015年04月16日 18时44分28秒
* Revision: none
* Compiler: gcc
*
* Author: (wangweihao), 578867817@qq.com
* Company: xiyoulinuxgroup
*
* =====================================================================================
*/
#ifndef _SERVER_H_
#define _SERVER_H_
enum { Max_conn = 10000 };
#include "Acceptor.h"
#include <sys/epoll.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <string.h>
#include <vector>
#include <string>
#include <map>
class Server
{
public:
Server(int pt, std::string &ht):
port(pt), host(ht)
{
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
inet_pton(AF_INET, host.c_str(), &server_addr.sin_addr);
listen_fd = socket(PF_INET, SOCK_STREAM, 0);
if(listen_fd < 0)
{
perror("socket error\n");
exit(EXIT_FAILURE);
}
int ret = bind(listen_fd, (struct sockaddr*)&server_addr, sizeof(server_addr));
if(ret == -1)
{
perror("binf error\n");
exit(EXIT_FAILURE);
}
int ret2 = listen(listen_fd, 5);
if(ret == -1)
{
perror("listen error\n");
exit(EXIT_FAILURE);
}
}
~Server()
{
close(listen_fd);
close(epollfd);
}
/* ready_函数:执行初始化操作 */
int ready_();
/* run_运行函数 */
int run_();
private:
/* register_函数:登记新加入的连接事件 */
int register_(int fd);
/* 设置非阻塞套接字 */
int setnonblocking(int fd);
private:
/* 端口号和ip */
int port;
std::string host;
int listen_fd = 0;
int epollfd = 0;
//将来打算每个连接封装为Acceptor对象
std::vector<Acceptor>all_accpet;
struct sockaddr_in server_addr;
struct epoll_event events[Max_conn];
struct epoll_event ev;
};
int
Server::run_()
{
while(1)
{
/* 返回已经准备好的事件 */
int nfds = epoll_wait(epollfd, events, Max_conn, -1);
//std::cout << nfds << std::endl;
//std::cout << "----------" << std::endl;
if(nfds == -1)
{
perror("epoll_wait error\n");
exit(EXIT_FAILURE);
}
for(int i = 0; i < nfds; ++i)
{
std::cout << "---------------" << std::endl;
/* 新加入的连接 */
if(events[i].data.fd == listen_fd)
{
std::cout << "加入连接" << std::endl;
register_(events[i].data.fd);
sleep(1);
}
/* 读事件 */
else if(events[i].events & EPOLLIN)
{
std::cout << "读事件" << std::endl;
char buffer_in[1024];
recv(events[i].data.fd, buffer_in, 1024, 0);
std::cout << buffer_in << std::endl;
sleep(100);
}
/* 写事件 */
else if(events[i].events & EPOLLOUT)
{
std::cout << "写事件" << std::endl;
//char buffer_out[1024];
//send(events[i].data.fd, buffer_out, 1024, 0);
sleep(100);
}
/* 被挂断 */
else if(events[i].events & EPOLLHUP)
{
std::cout << "挂断事件" << std::endl;
sleep(100);
}
/* 错误 */
else if(events[i].events & EPOLLERR)
{
std::cout << "错误事件" << std::endl;
sleep(100);
}
}
}
}
/* 改进时可以设定一个阻塞队列,用epoll新特性批量添加 */
int
Server::register_(int fd)
{
struct epoll_event ev;
struct sockaddr_in client_addr;
ev.events = EPOLLIN | EPOLLOUT;
socklen_t addrlen = sizeof(struct sockaddr_in);
int conn_sock = accept4(listen_fd, (struct sockaddr*)&client_addr, &addrlen, SOCK_NONBLOCK);
//int conn_sock = accept(listen_fd, (struct sockaddr*)&client_addr, &addrlen);
std::cout << "conn_sock:" << conn_sock << std::endl;
if(conn_sock == -1){
perror("accept error");
exit(EXIT_FAILURE);
}
ev.data.fd = conn_sock;
int ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock, &ev);
//setnonblocking(conn_sock);
if(ret == -1)
{
perror("epoll_ctl error\n");
exit(EXIT_FAILURE);
}
}
int
Server::ready_()
{
epollfd = epoll_create(Max_conn);
if(epollfd == -1)
{
perror("epoll create default\n");
exit(EXIT_FAILURE);
}
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = listen_fd;
int ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_fd, &ev);
setnonblocking(listen_fd);
if(ret == -1){
perror("epoll_ctl error\n");
exit(EXIT_FAILURE);
}
std::cout << "ready_ success" << std::endl;
return 0;
}
int
Server::setnonblocking(int fd)
{
int old_option = fcntl(fd, F_GETFL);
int new_option = old_option | O_NONBLOCK;
fcntl(fd, F_SETFL, new_option);
return old_option;
}
#endif