refactor: replace fdToConfig_ with listener_fds_ for improved socket management
This commit is contained in:
parent
9320d83378
commit
e1acf5b515
@ -55,7 +55,7 @@ void Server::start()
|
|||||||
{
|
{
|
||||||
setupServerSocket(*config);
|
setupServerSocket(*config);
|
||||||
}
|
}
|
||||||
if (fdToConfig_.empty())
|
if (listener_fds_.empty())
|
||||||
{
|
{
|
||||||
Log::fatal("No server sockets created.");
|
Log::fatal("No server sockets created.");
|
||||||
throw std::runtime_error("No server sockets created.");
|
throw std::runtime_error("No server sockets created.");
|
||||||
@ -111,7 +111,7 @@ void Server::setupServerSocket(const ServerConfig &config)
|
|||||||
addToEpoll(*serverSocket, EPOLLIN);
|
addToEpoll(*serverSocket, EPOLLIN);
|
||||||
|
|
||||||
listeners_.push_back(std::move(serverSocket));
|
listeners_.push_back(std::move(serverSocket));
|
||||||
fdToConfig_.insert({server_fd, std::cref(config)});
|
listener_fds_.insert(server_fd);
|
||||||
Log::info("Server listening on " + host + ":" + std::to_string(port) + "...");
|
Log::info("Server listening on " + host + ":" + std::to_string(port) + "...");
|
||||||
// static_cast<std::string>(config["listen"]) + "...");
|
// static_cast<std::string>(config["listen"]) + "...");
|
||||||
}
|
}
|
||||||
@ -156,24 +156,6 @@ Client &Server::getClient(int fd) const
|
|||||||
throw std::runtime_error("Client not found for fd: " + std::to_string(fd));
|
throw std::runtime_error("Client not found for fd: " + std::to_string(fd));
|
||||||
}
|
}
|
||||||
|
|
||||||
const ServerConfig &Server::getConfig(const Socket &socket) const
|
|
||||||
{
|
|
||||||
Log::trace(LOCATION);
|
|
||||||
return getConfig(socket.getFd());
|
|
||||||
}
|
|
||||||
|
|
||||||
const ServerConfig &Server::getConfig(int fd) const
|
|
||||||
{
|
|
||||||
Log::trace(LOCATION);
|
|
||||||
auto it = fdToConfig_.find(fd);
|
|
||||||
if (it != fdToConfig_.end())
|
|
||||||
{
|
|
||||||
return (it->second.get());
|
|
||||||
}
|
|
||||||
Log::error("Config not found for fd: " + std::to_string(fd));
|
|
||||||
throw std::runtime_error("Config not found for fd: " + std::to_string(fd));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Server::handleRequest(struct epoll_event *event) const
|
void Server::handleRequest(struct epoll_event *event) const
|
||||||
{
|
{
|
||||||
Log::trace(LOCATION);
|
Log::trace(LOCATION);
|
||||||
@ -220,7 +202,7 @@ void Server::eventLoop()
|
|||||||
removeFromEpoll(getListener(event.data.fd));
|
removeFromEpoll(getListener(event.data.fd));
|
||||||
close(event.data.fd);
|
close(event.data.fd);
|
||||||
}
|
}
|
||||||
else if (fdToConfig_.contains(event.data.fd))
|
else if (listener_fds_.contains(event.data.fd))
|
||||||
{
|
{
|
||||||
handleConnection(&event);
|
handleConnection(&event);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,10 +6,11 @@
|
|||||||
#include <webserv/socket/Socket.hpp> // for Socket
|
#include <webserv/socket/Socket.hpp> // for Socket
|
||||||
|
|
||||||
#include <cstdint> // for uint32_t
|
#include <cstdint> // for uint32_t
|
||||||
#include <functional> // for reference_wrapper
|
|
||||||
#include <memory> // for unique_ptr
|
#include <memory> // for unique_ptr
|
||||||
#include <unordered_map> // for unordered_map
|
#include <unordered_map> // for unordered_map
|
||||||
#include <vector> // for vector
|
#include <vector> // for vector
|
||||||
|
#include <set> // for set
|
||||||
|
|
||||||
class Client;
|
class Client;
|
||||||
class ConfigManager;
|
class ConfigManager;
|
||||||
@ -42,13 +43,12 @@ class Server
|
|||||||
void eventLoop();
|
void eventLoop();
|
||||||
Socket &getListener(int fd) const;
|
Socket &getListener(int fd) const;
|
||||||
Client &getClient(int fd) const;
|
Client &getClient(int fd) const;
|
||||||
const ServerConfig &getConfig(int fd) const;
|
|
||||||
const ServerConfig &getConfig(const Socket &socket) const;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int epoll_fd_;
|
int epoll_fd_;
|
||||||
const ConfigManager &configManager_;
|
const ConfigManager &configManager_;
|
||||||
std::vector<std::unique_ptr<Socket>> listeners_;
|
std::vector<std::unique_ptr<Socket>> listeners_;
|
||||||
std::unordered_map<int, std::reference_wrapper<const ServerConfig>> fdToConfig_;
|
std::set<int> listener_fds_;
|
||||||
std::unordered_map<int, std::unique_ptr<Client>> clients_;
|
std::unordered_map<int, std::unique_ptr<Client>> clients_;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user