From 76c2fd91a71585bfccf8ae9e30834a37514de24f Mon Sep 17 00:00:00 2001 From: Frederico Linhares Date: Fri, 28 Mar 2025 14:53:08 -0300 Subject: feat Simplify interface for net connections --- src/blu_cat/net/server/server.cpp | 41 ++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 24 deletions(-) (limited to 'src/blu_cat/net/server/server.cpp') diff --git a/src/blu_cat/net/server/server.cpp b/src/blu_cat/net/server/server.cpp index 575e203..b9d007e 100644 --- a/src/blu_cat/net/server/server.cpp +++ b/src/blu_cat/net/server/server.cpp @@ -36,20 +36,23 @@ Server::wait_for_client_connections() if(this->free_connection_slots.size() > 0) { - unsigned long pos{this->free_connection_slots.pop_front()}; + unsigned long pos{this->free_connection_slots.front()}; + this->free_connection_slots.pop(); std::cout << "Working " << pos << std::endl; - Connection *new_connection = new Connection( - this->callback_instantiator(), this, this->io_context, - std::move(socket), pos); - this->connections[pos] = new_connection; + std::unique_ptr new_connection( + std::make_unique( + this, this->io_context, std::move(socket), pos)); + this->connections[pos] = new_connection.get(); + this->get_new_connection(std::move(new_connection)); } else { unsigned long pos{this->connections.size()}; - Connection *new_connection = new Connection( - this->callback_instantiator(), this, this->io_context, - std::move(socket), pos); - this->connections.push_back(new_connection); + std::unique_ptr new_connection( + std::make_unique( + this, this->io_context, std::move(socket), pos)); + this->connections.push_back(new_connection.get()); + this->get_new_connection(std::move(new_connection)); } } else @@ -64,27 +67,17 @@ Server::wait_for_client_connections() } void -Server::read_messages() -{ - std::scoped_lock lock(this->mut); - for(auto i{0}; i < this->connections.size(); i++) - { - Connection *c{this->connections[i]}; - if(c != nullptr) c->read_messages(); - } -} - -void -Server::end_connection(unsigned long index) +Server::disconnect(unsigned long index) { std::scoped_lock lock(this->mut); delete this->connections[index]; this->connections[index] = nullptr; - this->free_connection_slots.push_back(index); + this->free_connection_slots.push(index); } -Server::Server(MessageCallback *(*callback_instantiator)(), const uint16_t port): - callback_instantiator{callback_instantiator}, +Server::Server(void (*get_new_connection)(std::unique_ptr c), + const uint16_t port): + get_new_connection{get_new_connection}, acceptor{io_context, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port)} { this->thread_context = std::thread([this](){this->io_context.run();}); -- cgit v1.2.3