diff options
Diffstat (limited to 'src/blu_cat/net/server/server.cpp')
-rw-r--r-- | src/blu_cat/net/server/server.cpp | 41 |
1 files changed, 17 insertions, 24 deletions
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<Connection> new_connection( + std::make_unique<Connection>( + 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<Connection> new_connection( + std::make_unique<Connection>( + 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<Connection> 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();}); |