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}/examples/test_thread_pool.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)
|
||||||
+26
-17
@@ -1,6 +1,7 @@
|
|||||||
#include <epoll/epoll_loop.hpp>
|
#include <epoll/epoll_loop.hpp>
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <string.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int set_nonblock(int fd)
|
int set_nonblock(int fd)
|
||||||
@@ -42,37 +43,45 @@ int main()
|
|||||||
|
|
||||||
puts("Server listening on port 8080...");
|
puts("Server listening on port 8080...");
|
||||||
|
|
||||||
EpollLoop loop;
|
EpollLoop loop(4);
|
||||||
|
|
||||||
loop.add_fd(
|
loop.add_fd(
|
||||||
sock_fd,
|
sock_fd,
|
||||||
EPOLLIN,
|
EPOLLIN,
|
||||||
[&]()
|
[&](int fd)
|
||||||
{
|
{
|
||||||
while (true) {
|
while (true)
|
||||||
int client_fd = accept(sock_fd, nullptr, nullptr);
|
{
|
||||||
if (client_fd < 0) break;
|
int client_fd = accept(fd, nullptr, nullptr);
|
||||||
|
if (client_fd < 0)
|
||||||
|
{
|
||||||
|
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
||||||
|
break;
|
||||||
|
perror("accept");
|
||||||
|
break;
|
||||||
|
}
|
||||||
set_nonblock(client_fd);
|
set_nonblock(client_fd);
|
||||||
std::cout << "New client: " << client_fd << "\n";
|
|
||||||
|
|
||||||
loop.add_fd(
|
loop.add_fd(
|
||||||
client_fd,
|
client_fd,
|
||||||
EPOLLIN,
|
EPOLLIN,
|
||||||
[&, client_fd]()
|
[](int cfd)
|
||||||
{
|
{
|
||||||
char buf[1024];
|
char buf[4096];
|
||||||
ssize_t r = read(client_fd, buf, sizeof(buf));
|
ssize_t n = read(cfd, buf, sizeof(buf));
|
||||||
if (r <= 0) {
|
write(cfd, buf, n);
|
||||||
std::cout << "Client " << client_fd << " disconnected\n";
|
if ((!memcmp("exit", buf, 4)))
|
||||||
loop.remove_fd(client_fd);
|
{
|
||||||
close(client_fd);
|
close(cfd);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::string msg(buf, r);
|
std::string data(buf, n);
|
||||||
std::cout << "Recv: " << msg;
|
std::cout<< "recv:" << data;
|
||||||
write(client_fd, buf, r);
|
|
||||||
});
|
});
|
||||||
} });
|
} });
|
||||||
|
|
||||||
loop.run();
|
while (true)
|
||||||
|
{
|
||||||
|
loop.poll(10);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -11,9 +11,12 @@ namespace ouc_server
|
|||||||
namespace epoll
|
namespace epoll
|
||||||
{
|
{
|
||||||
|
|
||||||
EpollLoop::EpollLoop()
|
EpollLoop::EpollLoop(size_t n)
|
||||||
|
: pool(n)
|
||||||
{
|
{
|
||||||
epoll_fd = epoll_create1(0);
|
epoll_fd = epoll_create1(0);
|
||||||
|
if (epoll_fd < 0)
|
||||||
|
perror("epoll_create1");
|
||||||
}
|
}
|
||||||
|
|
||||||
EpollLoop::~EpollLoop()
|
EpollLoop::~EpollLoop()
|
||||||
@@ -21,24 +24,27 @@ namespace ouc_server
|
|||||||
close(epoll_fd);
|
close(epoll_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EpollLoop::run(int timeout_ms)
|
void EpollLoop::poll(const int timeout_ms, const int MAX_EVENTS)
|
||||||
{
|
{
|
||||||
std::vector<struct epoll_event> events(64);
|
epoll_event events[MAX_EVENTS];
|
||||||
while (true)
|
|
||||||
{
|
int nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, timeout_ms);
|
||||||
int nfds = epoll_wait(epoll_fd, events.data(), events.size(), timeout_ms);
|
|
||||||
if (nfds < 0)
|
if (nfds < 0)
|
||||||
{
|
{
|
||||||
|
if (errno == EINTR)
|
||||||
|
return;
|
||||||
perror("epoll_wait");
|
perror("epoll_wait");
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 (callbacks.count(fd))
|
||||||
{
|
{
|
||||||
events_map[fd].callback();
|
pool.sumbit(
|
||||||
}
|
[ev = callbacks[fd], fd]()
|
||||||
|
{ ev.callback(fd); });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -47,7 +53,7 @@ namespace ouc_server
|
|||||||
{
|
{
|
||||||
auto ev = pack_event(fd, event_flags);
|
auto ev = pack_event(fd, event_flags);
|
||||||
int code = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev);
|
int code = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev);
|
||||||
events_map[fd] = Event{fd, event_flags, callback};
|
callbacks[fd] = Event{fd, event_flags, std::move(callback)};
|
||||||
return code == 0;
|
return code == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,14 +61,14 @@ namespace ouc_server
|
|||||||
{
|
{
|
||||||
auto ev = pack_event(fd, event_flags);
|
auto ev = pack_event(fd, event_flags);
|
||||||
int code = epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &ev);
|
int code = epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &ev);
|
||||||
events_map[fd].events = event_flags;
|
callbacks[fd].events = event_flags;
|
||||||
return code == 0;
|
return code == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EpollLoop::remove_fd(int fd)
|
bool EpollLoop::remove_fd(int fd)
|
||||||
{
|
{
|
||||||
int code = epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, nullptr);
|
int code = epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, nullptr);
|
||||||
events_map.erase(fd);
|
callbacks.erase(fd);
|
||||||
return code == 0;
|
return code == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,11 +10,13 @@
|
|||||||
#include <sys/epoll.h>
|
#include <sys/epoll.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
|
||||||
|
#include <utils/thread_pool.hpp>
|
||||||
|
|
||||||
namespace ouc_server
|
namespace ouc_server
|
||||||
{
|
{
|
||||||
namespace epoll
|
namespace epoll
|
||||||
{
|
{
|
||||||
using EpollCallback = std::function<void()>;
|
using EpollCallback = std::function<void(int)>;
|
||||||
|
|
||||||
struct Event
|
struct Event
|
||||||
{
|
{
|
||||||
@@ -25,18 +27,19 @@ namespace ouc_server
|
|||||||
|
|
||||||
class EpollLoop
|
class EpollLoop
|
||||||
{
|
{
|
||||||
public:
|
|
||||||
private:
|
private:
|
||||||
int epoll_fd;
|
int epoll_fd;
|
||||||
std::unordered_map<int, Event> events_map;
|
std::unordered_map<int, Event> callbacks;
|
||||||
|
|
||||||
|
ouc_server::utils::ThreadPool pool;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
EpollLoop();
|
EpollLoop(size_t = 64);
|
||||||
|
|
||||||
~EpollLoop();
|
~EpollLoop();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void run(int = -1);
|
void poll(const int = 0, const int = 64);
|
||||||
|
|
||||||
bool add_fd(int, uint32_t, EpollCallback);
|
bool add_fd(int, uint32_t, EpollCallback);
|
||||||
bool modify_fd(int, uint32_t);
|
bool modify_fd(int, uint32_t);
|
||||||
|
|||||||
Reference in New Issue
Block a user