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_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)
|
target_link_libraries(test PRIVATE ouc_server_lib)
|
||||||
+40
-78
@@ -1,12 +1,8 @@
|
|||||||
#include <sys/epoll.h>
|
#include <epoll/epoll_loop.hpp>
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <cstring>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
/* set nonblock socket */
|
|
||||||
int set_nonblock(int fd)
|
int set_nonblock(int fd)
|
||||||
{
|
{
|
||||||
int flags = fcntl(fd, F_GETFL /* Get file discriptor state */, 0);
|
int flags = fcntl(fd, F_GETFL /* Get file discriptor state */, 0);
|
||||||
@@ -15,102 +11,68 @@ int set_nonblock(int fd)
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int sock_fd = socket(
|
using namespace ouc_server::epoll;
|
||||||
AF_INET, /* IPv4 */
|
|
||||||
SOCK_STREAM, /* TCP */
|
int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
0 /* File Descriptor */);
|
|
||||||
if (sock_fd < 0)
|
if (sock_fd < 0)
|
||||||
{
|
{
|
||||||
perror("socket create error");
|
perror("socket");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int opt = 1;
|
int opt = 1;
|
||||||
setsockopt(
|
setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
||||||
sock_fd,
|
|
||||||
SOL_SOCKET,
|
|
||||||
SO_REUSEADDR, /* Allow to bind port muiltiple times */
|
|
||||||
&opt,
|
|
||||||
sizeof(opt));
|
|
||||||
|
|
||||||
sockaddr_in addr{}; /* Internet socket address */
|
sockaddr_in addr{};
|
||||||
addr.sin_family = AF_INET; /* IPv4 */
|
addr.sin_family = AF_INET;
|
||||||
addr.sin_port = htons(8080); /* Port */
|
addr.sin_port = htons(8080);
|
||||||
addr.sin_addr.s_addr = INADDR_ANY; /* listen all */
|
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;
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
set_nonblock(sock_fd);
|
set_nonblock(sock_fd);
|
||||||
|
|
||||||
puts("Server listening on port 8080...");
|
puts("Server listening on port 8080...");
|
||||||
|
|
||||||
int epoll_fd = epoll_create1(0); /* create epoll instance */
|
EpollLoop loop;
|
||||||
epoll_event ev{}; /* create epoll event instance*/
|
|
||||||
ev.events = EPOLLIN; /**/
|
loop.add_fd(
|
||||||
ev.data.fd = sock_fd;
|
|
||||||
epoll_ctl(
|
|
||||||
epoll_fd,
|
|
||||||
EPOLL_CTL_ADD, /* Add a file descriptor into epoll to be listened. */
|
|
||||||
sock_fd,
|
sock_fd,
|
||||||
&ev);
|
EPOLLIN,
|
||||||
|
[&]()
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
|
while (true) {
|
||||||
int client_fd = accept(sock_fd, nullptr, nullptr);
|
int client_fd = accept(sock_fd, nullptr, nullptr);
|
||||||
if (client_fd < 0)
|
if (client_fd < 0) break;
|
||||||
break;
|
|
||||||
printf("New client connected: %d\n", client_fd);
|
|
||||||
set_nonblock(client_fd);
|
set_nonblock(client_fd);
|
||||||
|
std::cout << "New client: " << client_fd << "\n";
|
||||||
|
|
||||||
epoll_event cli_ev{};
|
loop.add_fd(
|
||||||
cli_ev.events = EPOLLIN; /* Wait for new epoll connection */
|
client_fd,
|
||||||
cli_ev.data.fd = client_fd;
|
EPOLLIN,
|
||||||
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &cli_ev);
|
[&, client_fd]()
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
memset(buf, 0, sizeof(buf));
|
ssize_t r = read(client_fd, buf, sizeof(buf));
|
||||||
ssize_t r = read(fd, buf, sizeof(buf));
|
if (r <= 0) {
|
||||||
write(fd, buf, r); /* Echo data into buffer */
|
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);
|
||||||
|
});
|
||||||
|
} });
|
||||||
|
|
||||||
if (r <= 0 || (!memcmp("exit", buf, 4)))
|
loop.run();
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
@@ -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,16 +23,25 @@ namespace ouc_server
|
|||||||
|
|
||||||
void EpollLoop::run(int timeout_ms)
|
void EpollLoop::run(int timeout_ms)
|
||||||
{
|
{
|
||||||
std::vector<struct epoll_event> events(1024);
|
std::vector<struct epoll_event> events(64);
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
int nfds = epoll_wait(epoll_fd, events.data(), events.size(), timeout_ms);
|
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)
|
for (int i = 0; i < nfds; ++i)
|
||||||
{
|
{
|
||||||
int fd = events[i].data.fd;
|
int fd = events[i].data.fd;
|
||||||
if (events_map.count(fd))
|
if (events_map.count(fd))
|
||||||
|
{
|
||||||
events_map[fd].callback();
|
events_map[fd].callback();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool EpollLoop::add_fd(int fd, uint32_t event_flags, EpollCallback callback)
|
bool EpollLoop::add_fd(int fd, uint32_t event_flags, EpollCallback callback)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user