add lisener for socket

This commit is contained in:
2026-09-10 08:27:06 +02:00
parent bdb434d9d5
commit 751b54db18
8 changed files with 164 additions and 49 deletions
+60
View File
@@ -0,0 +1,60 @@
/*********************************/
/* */
/* o.riabenkyi@gmail.com */
/* */
/*********************************/
#ifndef LISTENERPLAN_HPP
#define LISTENERPLAN_HPP
#include <sys/socket.h>
#include <exception>
#include <string>
#include <vector>
#include "config/Config.hpp"
namespace webserv {
// Turns a validated Config into the deduplicated set of sockets the server
// must bind. Two server blocks may list the same host:port to share one
// socket (virtual hosting, picked apart later by server_name), so this
// groups listen directives by unique host:port instead of binding one
// socket per directive, which would make the second bind() fail.
//
// The ServerConfig pointers in routes() point into the Config passed to
// build(); that Config must outlive the ListenerPlan.
class ListenerPlan final {
public:
class Error : public std::exception {
public:
explicit Error(std::string message);
const char* what() const noexcept override;
private:
std::string msg_;
};
static ListenerPlan build(const Config& config);
const std::vector<sockaddr_storage>& endpoints() const noexcept {
return endpoints_;
}
// routes()[i] lists, in config order, every ServerConfig reachable
// through endpoints()[i]. Not consumed yet: it becomes the input to
// Host-header based virtual host selection once HTTP request routing
// exists.
const std::vector<std::vector<const ServerConfig*>>& routes() const noexcept {
return routes_;
}
private:
std::vector<sockaddr_storage> endpoints_;
std::vector<std::vector<const ServerConfig*>> routes_;
};
} // namespace webserv
#endif