Fix type confusion
This commit is contained in:
@@ -43,7 +43,7 @@ void Server::run() {
|
||||
// Add shutdown pipe to epoll instance
|
||||
struct epoll_event shutdown_event;
|
||||
shutdown_event.events = EPOLLIN;
|
||||
shutdown_event.data.fd = shutdown_pipe_[0];
|
||||
shutdown_event.data.ptr = const_cast<char *>(&shutdown_pipe_tag);
|
||||
|
||||
if (epoll_ctl(epollfd_, EPOLL_CTL_ADD, shutdown_pipe_[0], &shutdown_event) ==
|
||||
-1) {
|
||||
@@ -54,7 +54,7 @@ void Server::run() {
|
||||
// Add listen socket to epoll with EPOLLEXCLUSIVE to prevent thundering herd
|
||||
struct epoll_event listen_event;
|
||||
listen_event.events = EPOLLIN | EPOLLEXCLUSIVE;
|
||||
listen_event.data.fd = listen_sockfd_;
|
||||
listen_event.data.ptr = const_cast<char *>(&listen_socket_tag);
|
||||
if (epoll_ctl(epollfd_, EPOLL_CTL_ADD, listen_sockfd_, &listen_event) == -1) {
|
||||
perror("epoll_ctl listen socket");
|
||||
throw std::runtime_error("Failed to add listen socket to epoll");
|
||||
@@ -290,13 +290,11 @@ void Server::start_io_threads() {
|
||||
int batch_count = 0;
|
||||
for (int i = 0; i < event_count; ++i) {
|
||||
// Check for shutdown event
|
||||
// TODO type confusion in data union
|
||||
if (events[i].data.fd == shutdown_pipe_[0]) {
|
||||
if (events[i].data.ptr == &shutdown_pipe_tag) {
|
||||
return;
|
||||
}
|
||||
// Check for new connections
|
||||
// TODO type confusion in data union
|
||||
if (events[i].data.fd == listen_sockfd_) {
|
||||
if (events[i].data.ptr == &listen_socket_tag) {
|
||||
listenReady = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -110,6 +110,10 @@ private:
|
||||
int epollfd_ = -1;
|
||||
int listen_sockfd_ = -1;
|
||||
|
||||
// Unique tags for special events to avoid type confusion in epoll data union
|
||||
static inline const char listen_socket_tag = 0;
|
||||
static inline const char shutdown_pipe_tag = 0;
|
||||
|
||||
// Private helper methods
|
||||
void setup_shutdown_pipe();
|
||||
void setup_signal_handling();
|
||||
|
||||
Reference in New Issue
Block a user