Minor tidying and cleanup
This commit is contained in:
@@ -7,27 +7,14 @@
|
||||
|
||||
// TODO fix up this whole thing
|
||||
|
||||
std::unique_ptr<Connection>
|
||||
Connection::createForServer(struct sockaddr_storage addr, int fd, int64_t id,
|
||||
size_t epoll_index, ConnectionHandler *handler,
|
||||
std::weak_ptr<Server> 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<Connection>(
|
||||
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 &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_(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() {
|
||||
|
||||
@@ -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> 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<Connection> 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<Connection>
|
||||
createForServer(struct sockaddr_storage addr, int fd, int64_t id,
|
||||
size_t epoll_index, ConnectionHandler *handler,
|
||||
std::weak_ptr<Server> server);
|
||||
size_t epoll_index, ConnectionHandler *handler, Server &server);
|
||||
|
||||
// Networking interface - only accessible by Server
|
||||
int readBytes(char *buf, size_t buffer_size);
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <sys/mman.h>
|
||||
#include <sys/resource.h>
|
||||
|
||||
class Connection;
|
||||
struct Connection;
|
||||
|
||||
/**
|
||||
* mmap-based Connection Registry for tracking active connections.
|
||||
|
||||
@@ -196,7 +196,7 @@ int Server::createLocalConnection() {
|
||||
// Create Connection object
|
||||
auto connection = std::unique_ptr<Connection>(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<std::thread> &threads) {
|
||||
batch[batch_count] = std::unique_ptr<Connection>(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++;
|
||||
|
||||
@@ -108,7 +108,7 @@ public:
|
||||
static void releaseBackToServer(std::unique_ptr<Connection> connection);
|
||||
|
||||
private:
|
||||
friend class Connection;
|
||||
friend struct Connection;
|
||||
/**
|
||||
* Private constructor - use create() factory method instead.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user