From 815e6c065adc7eb2697ff672b5111fe9d0d65295 Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Fri, 22 Aug 2025 13:36:17 -0400 Subject: [PATCH] Minor tidying and cleanup --- src/connection.cpp | 23 +++++------------------ src/connection.hpp | 28 ++-------------------------- src/connection_registry.hpp | 2 +- src/server.cpp | 4 ++-- src/server.hpp | 2 +- 5 files changed, 11 insertions(+), 48 deletions(-) diff --git a/src/connection.cpp b/src/connection.cpp index 75dafb5..ae88416 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -7,27 +7,14 @@ // TODO fix up this whole thing -std::unique_ptr -Connection::createForServer(struct sockaddr_storage addr, int fd, int64_t id, - size_t epoll_index, ConnectionHandler *handler, - std::weak_ptr server) { - // Use unique_ptr constructor with private access (friend function) - // We can't use make_unique here because constructor is private - return std::unique_ptr( - new Connection(addr, fd, id, epoll_index, handler, server)); -} - Connection::Connection(struct sockaddr_storage addr, int fd, int64_t id, size_t epoll_index, ConnectionHandler *handler, - std::weak_ptr server) + Server &server) : fd_(fd), id_(id), epoll_index_(epoll_index), addr_(addr), arena_(), - handler_(handler), server_(server) { - if (auto server_ptr = server_.lock()) { - server_ptr->active_connections_.fetch_add(1, std::memory_order_relaxed); - } - if (handler_) { - handler_->on_connection_established(*this); - } + handler_(handler), server_(server.weak_from_this()) { + server.active_connections_.fetch_add(1, std::memory_order_relaxed); + assert(handler_); + handler_->on_connection_established(*this); } Connection::~Connection() { diff --git a/src/connection.hpp b/src/connection.hpp index a757dac..471f591 100644 --- a/src/connection.hpp +++ b/src/connection.hpp @@ -319,34 +319,10 @@ private: * @param fd File descriptor for the socket connection * @param id Unique connection identifier generated by the server * @param handler Protocol handler for processing connection data - * @param server Weak reference to the server for safe cleanup + * @param server Reference to server associated with this connection */ Connection(struct sockaddr_storage addr, int fd, int64_t id, - size_t epoll_index, ConnectionHandler *handler, - std::weak_ptr server); - - /** - * @brief Server-only factory method for creating connections. - * - * This factory method can only be called by the Server class due to friend - * access. It provides controlled connection creation with proper resource - * management. - * - * @param addr Network address of the remote client (IPv4/IPv6 compatible) - * @param fd File descriptor for the socket connection - * @param id Unique connection identifier generated by the server - * @param handler Protocol handler for processing connection data - * @param server Weak reference to the server for safe cleanup - * - * @return std::unique_ptr to the newly created connection - * - * @note This method is only accessible to the Server class and should be used - * exclusively by I/O threads when new connections arrive. - */ - static std::unique_ptr - createForServer(struct sockaddr_storage addr, int fd, int64_t id, - size_t epoll_index, ConnectionHandler *handler, - std::weak_ptr server); + size_t epoll_index, ConnectionHandler *handler, Server &server); // Networking interface - only accessible by Server int readBytes(char *buf, size_t buffer_size); diff --git a/src/connection_registry.hpp b/src/connection_registry.hpp index 1a64544..f793435 100644 --- a/src/connection_registry.hpp +++ b/src/connection_registry.hpp @@ -5,7 +5,7 @@ #include #include -class Connection; +struct Connection; /** * mmap-based Connection Registry for tracking active connections. diff --git a/src/server.cpp b/src/server.cpp index 5d501ea..099f78f 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -196,7 +196,7 @@ int Server::createLocalConnection() { // Create Connection object auto connection = std::unique_ptr(new Connection( addr, server_fd, connection_id_.fetch_add(1, std::memory_order_relaxed), - epoll_index, &handler_, weak_from_this())); + epoll_index, &handler_, *this)); // Store in registry connection_registry_.store(server_fd, std::move(connection)); @@ -377,7 +377,7 @@ void Server::start_io_threads(std::vector &threads) { batch[batch_count] = std::unique_ptr(new Connection( addr, fd, connection_id_.fetch_add(1, std::memory_order_relaxed), - epoll_index, &handler_, weak_from_this())); + epoll_index, &handler_, *this)); batch_events[batch_count] = EPOLLIN; // New connections always start with read batch_count++; diff --git a/src/server.hpp b/src/server.hpp index 352825f..c5dd44e 100644 --- a/src/server.hpp +++ b/src/server.hpp @@ -108,7 +108,7 @@ public: static void releaseBackToServer(std::unique_ptr connection); private: - friend class Connection; + friend struct Connection; /** * Private constructor - use create() factory method instead. *