Have Server take list of listen fds and add createLocalConnection

This commit is contained in:
2025-08-22 12:01:00 -04:00
parent ba3258ab16
commit 0e63d5e80f
3 changed files with 259 additions and 172 deletions

View File

@@ -43,10 +43,15 @@ public:
*
* @param config Server configuration (threads, ports, limits, etc.)
* @param handler Protocol handler for processing connection data
* @param listen_fds Vector of file descriptors to accept connections on.
* Server takes ownership and will close them on
* destruction. Server will set these to non-blocking mode for safe epoll
* usage. Empty vector means no listening sockets.
* @return shared_ptr to the newly created Server
*/
static std::shared_ptr<Server> create(const weaseldb::Config &config,
ConnectionHandler &handler);
ConnectionHandler &handler,
const std::vector<int> &listen_fds);
/**
* Destructor ensures proper cleanup of all resources.
@@ -75,6 +80,20 @@ public:
*/
void shutdown();
/**
* Creates a local connection using socketpair() for testing or local IPC.
*
* Creates a socketpair, registers one end as a Connection in the server,
* and returns the other end to the caller for communication.
*
* The caller takes ownership of the returned file descriptor and must close
* it.
*
* @return File descriptor for the client end of the socketpair, or -1 on
* error
*/
int createLocalConnection();
/**
* Release a connection back to its server for continued processing.
*
@@ -95,8 +114,13 @@ private:
*
* @param config Server configuration (threads, ports, limits, etc.)
* @param handler Protocol handler for processing connection data
* @param listen_fds Vector of file descriptors to accept connections on.
* Server takes ownership and will close them on
* destruction. Server will set these to non-blocking mode for safe epoll
* usage.
*/
explicit Server(const weaseldb::Config &config, ConnectionHandler &handler);
explicit Server(const weaseldb::Config &config, ConnectionHandler &handler,
const std::vector<int> &listen_fds);
const weaseldb::Config &config_;
ConnectionHandler &handler_;
@@ -116,12 +140,12 @@ private:
// Multiple epoll file descriptors to reduce contention
std::vector<int> epoll_fds_;
int listen_sockfd_ = -1;
std::vector<int>
listen_fds_; // FDs to accept connections on (Server owns these)
// Private helper methods
void setup_shutdown_pipe();
void setup_signal_handling();
int create_listen_socket();
void create_epoll_instances();
void start_io_threads(std::vector<std::thread> &threads);