feat(SIGTERM) is this not to global for you sir?
This commit is contained in:
parent
be39dfa4c4
commit
5e9243b064
@ -7,6 +7,7 @@
|
||||
#include <iostream> // for ios_base
|
||||
#include <string> // for allocator, basic_string, char_traits, operator+, string
|
||||
#include <vector> // for vector
|
||||
#include <csignal>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
@ -37,6 +38,7 @@ int main(int argc, char **argv)
|
||||
Log::debug("ConfigManager initialized successfully.");
|
||||
Server server(configManager);
|
||||
|
||||
::signal(SIGINT, Server::signalHandler);
|
||||
server.run();
|
||||
return 0;
|
||||
}
|
||||
@ -25,9 +25,12 @@
|
||||
#include <sys/socket.h> // for send, SOMAXCONN
|
||||
#include <sys/types.h> // for ssize_t
|
||||
#include <unistd.h> // for close
|
||||
#include <csignal>
|
||||
|
||||
class Router;
|
||||
|
||||
volatile sig_atomic_t Server::stop_ = 0;
|
||||
|
||||
Server::Server(const ConfigManager &configManager) : epoll_fd_(epoll_create1(O_CLOEXEC)), configManager_(configManager)
|
||||
{
|
||||
Log::trace(LOCATION);
|
||||
@ -269,6 +272,11 @@ void Server::handleEpoll(struct epoll_event *events, int max_events)
|
||||
int nfds = epoll_wait(epoll_fd_, events, max_events, 10); // NOLINT
|
||||
if (nfds == -1)
|
||||
{
|
||||
if (errno == EINTR)
|
||||
{
|
||||
Log::debug("epoll_wait interrupted by signal, continuing...");
|
||||
return;
|
||||
}
|
||||
Log::error("epoll_wait failed");
|
||||
throw std::runtime_error("epoll_wait failed");
|
||||
}
|
||||
@ -306,8 +314,22 @@ void Server::run()
|
||||
struct epoll_event events[MAX_EVENTS]; // NOLINT
|
||||
while (true)
|
||||
{
|
||||
if (stop_ != 0)
|
||||
{
|
||||
Log::info("Server stopping...");
|
||||
break;
|
||||
}
|
||||
pollSockets();
|
||||
pollClients();
|
||||
handleEpoll(events, MAX_EVENTS); // NOLINT (cppcoreguidelines-pro-bounds-pointer-arithmetic)
|
||||
}
|
||||
}
|
||||
|
||||
void Server::signalHandler(int signum)
|
||||
{
|
||||
|
||||
if (signum == SIGINT)
|
||||
{
|
||||
stop_ = signum;
|
||||
}
|
||||
}
|
||||
@ -7,6 +7,7 @@
|
||||
#include <webserv/socket/ASocket.hpp>
|
||||
#include <webserv/socket/ServerSocket.hpp> // for ServerSocket
|
||||
|
||||
#include <csignal>
|
||||
#include <cstdint> // for uint32_t
|
||||
#include <memory> // for unique_ptr
|
||||
#include <set> // for set
|
||||
@ -31,6 +32,7 @@ class Server
|
||||
Server &operator=(Server &&other) noexcept = delete;
|
||||
|
||||
~Server();
|
||||
static void signalHandler(int signum);
|
||||
|
||||
void run();
|
||||
void add(ASocket &socket, Client *client = nullptr);
|
||||
@ -44,6 +46,7 @@ class Server
|
||||
|
||||
private:
|
||||
int epoll_fd_;
|
||||
static volatile sig_atomic_t stop_;
|
||||
const ConfigManager &configManager_;
|
||||
std::vector<std::unique_ptr<ServerSocket>> listeners_;
|
||||
std::set<int> listener_fds_;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user