14#include <netinet/in.h>
15#include <sys/socket.h>
21#define RECV_BUFFER_SIZE 4096
22#define EPOLL_TIMEOUT_MS 2500
23#define CONNECTION_TIMEOUT_S 60
24#define CGI_TIMEOUT_S 10
32 bool operator()(
const struct sockaddr_in& lhs,
const struct sockaddr_in& rhs)
const
34 if (lhs.sin_addr.s_addr != rhs.sin_addr.s_addr)
35 return lhs.sin_addr.s_addr < rhs.sin_addr.s_addr;
36 return lhs.sin_port < rhs.sin_port;
Will be spawned when we accept() a new connection, will handle reading from the socket,...
Stores per-server configuration directives. This class encapsulates the configuration for a specific ...
void _enqueueProcessing(Connection *conn)
Adds a connection to the processing queue if not already queued.
void _handleCgiPipeEvent(int pipeFd, uint32_t events)
Handles an epoll event on a CGI pipe fd.
void _closeAllFds()
Closes all fds (listeners + clients) during shutdown.
std::set< int > _listenFds
void addServer(const ServerConf *conf)
Creates a listening socket for the given ServerConf's IP:Port and registers the mapping.
ServerManager & operator=(const ServerManager &other)
void _runRoundRobin()
Runs a budgeted round-robin pass over _processingQueue. Calls process() once per connection,...
const ServerConf * getServerConfForFd(int clientFd) const
Uses getsockname() to find the local address of a client fd, then returns the matching ServerConf.
void _finalizeProcessed(Connection *conn)
Handles state transition for a connection that just left PROCESSING.
void _unregisterCgiPipe(int pipeFd)
Unregisters a CGI pipe fd from epoll and removes the mapping.
std::map< struct sockaddr_in, const ServerConf *, SockAddrCompare > _interfacePortPairs
std::set< Connection * > _processingSet
void _registerCgiPipe(Connection *conn)
Registers a CGI pipe fd in epoll and maps it to its Connection.
std::deque< Connection * > _processingQueue
void addPollFd(int fd, uint32_t events)
Adds or updates an fd in the epoll list (e.g., CGI output pipe).
std::vector< ServerConf * > _serverConfs
bool _readAndPrint(int fd)
Reads from a client fd and prints the raw data.
void _handleConnection(Connection *conn, uint32_t events)
Dispatches epoll events to the correct Connection handler and drives state transitions....
void _sweepTimeouts()
Scans all connections and drops any that have been idle too long.
void addListenPort(int port)
Temporary overload for early development: creates a listening socket on the given port bound to INADD...
std::map< int, time_t > _cgiStartTimes
std::map< int, uint32_t > _fdEvents
std::map< int, const ServerConf * > _listenFdToServerConf
void _acceptNewConnections(int listenFd)
Accepts all pending connections on a listening fd. Sets each client fd to O_NONBLOCK and adds it to _...
void _dequeueProcessing(Connection *conn)
Removes a connection from the processing set (queue cleanup is lazy).
std::map< int, Connection * > _cgiPipeToConn
std::vector< struct epoll_event > _eventBuffer
int _createListeningSocket(const struct sockaddr_in &addr)
Creates, binds, listens, and sets O_NONBLOCK on a socket.
void _dropConnection(int fd)
Closes a client fd and removes it from epoll and _connections.
void run()
Enters the main epoll() event loop. Blocks until g_running becomes false.
void _sweepCgiTimeouts()
Sweeps CGI processes for timeout.
std::map< int, Connection * > _connections
Custom comparator for sockaddr_in to allow its use as a key in std::map.
bool operator()(const struct sockaddr_in &lhs, const struct sockaddr_in &rhs) const