feat: 阻塞式 EpollLoop 封装通过测试
This commit is contained in:
+1
-1
@@ -10,5 +10,5 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static -g -O2")
|
||||
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/include/ouc_server)
|
||||
|
||||
add_executable(test "${PROJECT_SOURCE_DIR}/example/test_http_request.cpp")
|
||||
add_executable(test "${PROJECT_SOURCE_DIR}/examples/epoll_tcp_loop.cpp")
|
||||
target_link_libraries(test PRIVATE ouc_server_lib)
|
||||
+42
-80
@@ -1,12 +1,8 @@
|
||||
#include <sys/epoll.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <epoll/epoll_loop.hpp>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
/* set nonblock socket */
|
||||
int set_nonblock(int fd)
|
||||
{
|
||||
int flags = fcntl(fd, F_GETFL /* Get file discriptor state */, 0);
|
||||
@@ -15,102 +11,68 @@ int set_nonblock(int fd)
|
||||
|
||||
int main()
|
||||
{
|
||||
int sock_fd = socket(
|
||||
AF_INET, /* IPv4 */
|
||||
SOCK_STREAM, /* TCP */
|
||||
0 /* File Descriptor */);
|
||||
using namespace ouc_server::epoll;
|
||||
|
||||
int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock_fd < 0)
|
||||
{
|
||||
perror("socket create error");
|
||||
perror("socket");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int opt = 1;
|
||||
setsockopt(
|
||||
sock_fd,
|
||||
SOL_SOCKET,
|
||||
SO_REUSEADDR, /* Allow to bind port muiltiple times */
|
||||
&opt,
|
||||
sizeof(opt));
|
||||
setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
||||
|
||||
sockaddr_in addr{}; /* Internet socket address */
|
||||
addr.sin_family = AF_INET; /* IPv4 */
|
||||
addr.sin_port = htons(8080); /* Port */
|
||||
addr.sin_addr.s_addr = INADDR_ANY; /* listen all */
|
||||
sockaddr_in addr{};
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(8080);
|
||||
addr.sin_addr.s_addr = INADDR_ANY;
|
||||
|
||||
if (bind(sock_fd, (sockaddr *)&addr, sizeof(addr))) /* Bind file descriptor with socket address. */
|
||||
if (bind(sock_fd, (sockaddr *)&addr, sizeof(addr)) < 0)
|
||||
{
|
||||
perror("bind address error");
|
||||
perror("bind");
|
||||
return 1;
|
||||
}
|
||||
if (listen(sock_fd, SOMAXCONN /* Max wait queue that system allows. */)) /* Listen the file descriptor */
|
||||
if (listen(sock_fd, SOMAXCONN) < 0)
|
||||
{
|
||||
perror("set listen queue error");
|
||||
perror("listen");
|
||||
return 1;
|
||||
}
|
||||
set_nonblock(sock_fd);
|
||||
|
||||
puts("Server listening on port 8080...");
|
||||
|
||||
int epoll_fd = epoll_create1(0); /* create epoll instance */
|
||||
epoll_event ev{}; /* create epoll event instance*/
|
||||
ev.events = EPOLLIN; /**/
|
||||
ev.data.fd = sock_fd;
|
||||
epoll_ctl(
|
||||
epoll_fd,
|
||||
EPOLL_CTL_ADD, /* Add a file descriptor into epoll to be listened. */
|
||||
EpollLoop loop;
|
||||
|
||||
loop.add_fd(
|
||||
sock_fd,
|
||||
&ev);
|
||||
|
||||
const int MAX_EVENTS = 64;
|
||||
epoll_event events[MAX_EVENTS];
|
||||
|
||||
while (true)
|
||||
{
|
||||
int n = epoll_wait(epoll_fd, events, MAX_EVENTS, -1);
|
||||
for (int i = 0; i < n; ++i)
|
||||
EPOLLIN,
|
||||
[&]()
|
||||
{
|
||||
int fd = events[i].data.fd;
|
||||
if (fd == sock_fd)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
int client_fd = accept(sock_fd, nullptr, nullptr);
|
||||
if (client_fd < 0)
|
||||
break;
|
||||
printf("New client connected: %d\n", client_fd);
|
||||
set_nonblock(client_fd);
|
||||
while (true) {
|
||||
int client_fd = accept(sock_fd, nullptr, nullptr);
|
||||
if (client_fd < 0) break;
|
||||
set_nonblock(client_fd);
|
||||
std::cout << "New client: " << client_fd << "\n";
|
||||
|
||||
epoll_event cli_ev{};
|
||||
cli_ev.events = EPOLLIN; /* Wait for new epoll connection */
|
||||
cli_ev.data.fd = client_fd;
|
||||
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &cli_ev);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
loop.add_fd(
|
||||
client_fd,
|
||||
EPOLLIN,
|
||||
[&, client_fd]()
|
||||
{
|
||||
char buf[1024];
|
||||
memset(buf, 0, sizeof(buf));
|
||||
ssize_t r = read(fd, buf, sizeof(buf));
|
||||
write(fd, buf, r); /* Echo data into buffer */
|
||||
|
||||
if (r <= 0 || (!memcmp("exit", buf, 4)))
|
||||
{
|
||||
epoll_ctl(
|
||||
epoll_fd,
|
||||
EPOLL_CTL_DEL, /* Delete file descriptor in epoll */
|
||||
fd,
|
||||
nullptr);
|
||||
close(fd);
|
||||
continue;
|
||||
ssize_t r = read(client_fd, buf, sizeof(buf));
|
||||
if (r <= 0) {
|
||||
std::cout << "Client " << client_fd << " disconnected\n";
|
||||
loop.remove_fd(client_fd);
|
||||
close(client_fd);
|
||||
return;
|
||||
}
|
||||
std::string msg(buf, r);
|
||||
std::cout << "Recv: " << msg;
|
||||
write(client_fd, buf, r);
|
||||
});
|
||||
} });
|
||||
|
||||
printf(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
close(epoll_fd);
|
||||
close(sock_fd);
|
||||
return 0;
|
||||
loop.run();
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
#include <sys/epoll.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
/* set nonblock socket */
|
||||
int set_nonblock(int fd)
|
||||
{
|
||||
int flags = fcntl(fd, F_GETFL /* Get file discriptor state */, 0);
|
||||
return fcntl(fd, F_SETFL /* Set new file discriptor */, flags | O_NONBLOCK);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int sock_fd = socket(
|
||||
AF_INET, /* IPv4 */
|
||||
SOCK_STREAM, /* TCP */
|
||||
0 /* File Descriptor */);
|
||||
if (sock_fd < 0)
|
||||
{
|
||||
perror("socket create error");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int opt = 1;
|
||||
setsockopt(
|
||||
sock_fd,
|
||||
SOL_SOCKET,
|
||||
SO_REUSEADDR, /* Allow to bind port muiltiple times */
|
||||
&opt,
|
||||
sizeof(opt));
|
||||
|
||||
sockaddr_in addr{}; /* Internet socket address */
|
||||
addr.sin_family = AF_INET; /* IPv4 */
|
||||
addr.sin_port = htons(8080); /* Port */
|
||||
addr.sin_addr.s_addr = INADDR_ANY; /* listen all */
|
||||
|
||||
if (bind(sock_fd, (sockaddr *)&addr, sizeof(addr))) /* Bind file descriptor with socket address. */
|
||||
{
|
||||
perror("bind address error");
|
||||
return 1;
|
||||
}
|
||||
if (listen(sock_fd, SOMAXCONN /* Max wait queue that system allows. */)) /* Listen the file descriptor */
|
||||
{
|
||||
perror("set listen queue error");
|
||||
return 1;
|
||||
}
|
||||
set_nonblock(sock_fd);
|
||||
|
||||
puts("Server listening on port 8080...");
|
||||
|
||||
int epoll_fd = epoll_create1(0); /* create epoll instance */
|
||||
epoll_event ev{}; /* create epoll event instance*/
|
||||
ev.events = EPOLLIN; /**/
|
||||
ev.data.fd = sock_fd;
|
||||
epoll_ctl(
|
||||
epoll_fd,
|
||||
EPOLL_CTL_ADD, /* Add a file descriptor into epoll to be listened. */
|
||||
sock_fd,
|
||||
&ev);
|
||||
|
||||
const int MAX_EVENTS = 64;
|
||||
epoll_event events[MAX_EVENTS];
|
||||
|
||||
while (true)
|
||||
{
|
||||
int n = epoll_wait(epoll_fd, events, MAX_EVENTS, -1);
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
int fd = events[i].data.fd;
|
||||
if (fd == sock_fd)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
int client_fd = accept(sock_fd, nullptr, nullptr);
|
||||
if (client_fd < 0)
|
||||
break;
|
||||
printf("New client connected: %d\n", client_fd);
|
||||
set_nonblock(client_fd);
|
||||
|
||||
epoll_event cli_ev{};
|
||||
cli_ev.events = EPOLLIN; /* Wait for new epoll connection */
|
||||
cli_ev.data.fd = client_fd;
|
||||
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &cli_ev);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
char buf[1024];
|
||||
memset(buf, 0, sizeof(buf));
|
||||
ssize_t r = read(fd, buf, sizeof(buf));
|
||||
write(fd, buf, r); /* Echo data into buffer */
|
||||
|
||||
if (r <= 0 || (!memcmp("exit", buf, 4)))
|
||||
{
|
||||
epoll_ctl(
|
||||
epoll_fd,
|
||||
EPOLL_CTL_DEL, /* Delete file descriptor in epoll */
|
||||
fd,
|
||||
nullptr);
|
||||
close(fd);
|
||||
continue;
|
||||
}
|
||||
|
||||
printf(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
close(epoll_fd);
|
||||
close(sock_fd);
|
||||
return 0;
|
||||
}
|
||||
@@ -23,14 +23,23 @@ namespace ouc_server
|
||||
|
||||
void EpollLoop::run(int timeout_ms)
|
||||
{
|
||||
std::vector<struct epoll_event> events(1024);
|
||||
int nfds = epoll_wait(epoll_fd, events.data(), events.size(), timeout_ms);
|
||||
|
||||
for (int i = 0; i < nfds; ++i)
|
||||
std::vector<struct epoll_event> events(64);
|
||||
while (true)
|
||||
{
|
||||
int fd = events[i].data.fd;
|
||||
if (events_map.count(fd))
|
||||
events_map[fd].callback();
|
||||
int nfds = epoll_wait(epoll_fd, events.data(), events.size(), timeout_ms);
|
||||
if (nfds < 0)
|
||||
{
|
||||
perror("epoll_wait");
|
||||
break;
|
||||
}
|
||||
for (int i = 0; i < nfds; ++i)
|
||||
{
|
||||
int fd = events[i].data.fd;
|
||||
if (events_map.count(fd))
|
||||
{
|
||||
events_map[fd].callback();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user