From 648ed1575ace6b4b6252536fdfd867497198f95b Mon Sep 17 00:00:00 2001 From: pjh456 <147148383@qq.com> Date: Sun, 28 Sep 2025 12:42:27 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=98=BB=E5=A1=9E=E5=BC=8F=20EpollLoop?= =?UTF-8?q?=20=E5=B0=81=E8=A3=85=E9=80=9A=E8=BF=87=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- examples/epoll_tcp_loop.cpp | 122 ++++++++---------------- examples/raw_epoll_tcp_loop.cpp | 116 ++++++++++++++++++++++ include/ouc_server/epoll/epoll_loop.cpp | 23 +++-- 4 files changed, 175 insertions(+), 88 deletions(-) create mode 100644 examples/raw_epoll_tcp_loop.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b9379b9..4df006f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/examples/epoll_tcp_loop.cpp b/examples/epoll_tcp_loop.cpp index 1d1381c..69bb09e 100644 --- a/examples/epoll_tcp_loop.cpp +++ b/examples/epoll_tcp_loop.cpp @@ -1,12 +1,8 @@ -#include -#include -#include +#include + #include -#include -#include #include -/* 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(); } \ No newline at end of file diff --git a/examples/raw_epoll_tcp_loop.cpp b/examples/raw_epoll_tcp_loop.cpp new file mode 100644 index 0000000..1d1381c --- /dev/null +++ b/examples/raw_epoll_tcp_loop.cpp @@ -0,0 +1,116 @@ +#include +#include +#include +#include +#include +#include +#include + +/* 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; +} \ No newline at end of file diff --git a/include/ouc_server/epoll/epoll_loop.cpp b/include/ouc_server/epoll/epoll_loop.cpp index 2d4dc62..81aa05e 100644 --- a/include/ouc_server/epoll/epoll_loop.cpp +++ b/include/ouc_server/epoll/epoll_loop.cpp @@ -23,14 +23,23 @@ namespace ouc_server void EpollLoop::run(int timeout_ms) { - std::vector events(1024); - int nfds = epoll_wait(epoll_fd, events.data(), events.size(), timeout_ms); - - for (int i = 0; i < nfds; ++i) + std::vector 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(); + } + } } }