Minor tidying and cleanup
This commit is contained in:
@@ -7,27 +7,14 @@
|
|||||||
|
|
||||||
// TODO fix up this whole thing
|
// 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,
|
Connection::Connection(struct sockaddr_storage addr, int fd, int64_t id,
|
||||||
size_t epoll_index, ConnectionHandler *handler,
|
size_t epoll_index, ConnectionHandler *handler,
|
||||||
std::weak_ptr<Server> server)
|
Server &server)
|
||||||
: fd_(fd), id_(id), epoll_index_(epoll_index), addr_(addr), arena_(),
|
: fd_(fd), id_(id), epoll_index_(epoll_index), addr_(addr), arena_(),
|
||||||
handler_(handler), server_(server) {
|
handler_(handler), server_(server.weak_from_this()) {
|
||||||
if (auto server_ptr = server_.lock()) {
|
server.active_connections_.fetch_add(1, std::memory_order_relaxed);
|
||||||
server_ptr->active_connections_.fetch_add(1, std::memory_order_relaxed);
|
assert(handler_);
|
||||||
}
|
|
||||||
if (handler_) {
|
|
||||||
handler_->on_connection_established(*this);
|
handler_->on_connection_established(*this);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection::~Connection() {
|
Connection::~Connection() {
|
||||||
|
|||||||
@@ -319,34 +319,10 @@ private:
|
|||||||
* @param fd File descriptor for the socket connection
|
* @param fd File descriptor for the socket connection
|
||||||
* @param id Unique connection identifier generated by the server
|
* @param id Unique connection identifier generated by the server
|
||||||
* @param handler Protocol handler for processing connection data
|
* @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,
|
Connection(struct sockaddr_storage addr, int fd, int64_t id,
|
||||||
size_t epoll_index, ConnectionHandler *handler,
|
size_t epoll_index, ConnectionHandler *handler, Server &server);
|
||||||
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);
|
|
||||||
|
|
||||||
// Networking interface - only accessible by Server
|
// Networking interface - only accessible by Server
|
||||||
int readBytes(char *buf, size_t buffer_size);
|
int readBytes(char *buf, size_t buffer_size);
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <sys/resource.h>
|
#include <sys/resource.h>
|
||||||
|
|
||||||
class Connection;
|
struct Connection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* mmap-based Connection Registry for tracking active connections.
|
* mmap-based Connection Registry for tracking active connections.
|
||||||
|
|||||||
@@ -196,7 +196,7 @@ int Server::createLocalConnection() {
|
|||||||
// Create Connection object
|
// Create Connection object
|
||||||
auto connection = std::unique_ptr<Connection>(new Connection(
|
auto connection = std::unique_ptr<Connection>(new Connection(
|
||||||
addr, server_fd, connection_id_.fetch_add(1, std::memory_order_relaxed),
|
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
|
// Store in registry
|
||||||
connection_registry_.store(server_fd, std::move(connection));
|
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(
|
batch[batch_count] = std::unique_ptr<Connection>(new Connection(
|
||||||
addr, fd,
|
addr, fd,
|
||||||
connection_id_.fetch_add(1, std::memory_order_relaxed),
|
connection_id_.fetch_add(1, std::memory_order_relaxed),
|
||||||
epoll_index, &handler_, weak_from_this()));
|
epoll_index, &handler_, *this));
|
||||||
batch_events[batch_count] =
|
batch_events[batch_count] =
|
||||||
EPOLLIN; // New connections always start with read
|
EPOLLIN; // New connections always start with read
|
||||||
batch_count++;
|
batch_count++;
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ public:
|
|||||||
static void releaseBackToServer(std::unique_ptr<Connection> connection);
|
static void releaseBackToServer(std::unique_ptr<Connection> connection);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class Connection;
|
friend struct Connection;
|
||||||
/**
|
/**
|
||||||
* Private constructor - use create() factory method instead.
|
* Private constructor - use create() factory method instead.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user