summaryrefslogtreecommitdiff
path: root/src/blu_cat/net/server
diff options
context:
space:
mode:
Diffstat (limited to 'src/blu_cat/net/server')
-rw-r--r--src/blu_cat/net/server/server.cpp41
-rw-r--r--src/blu_cat/net/server/server.hpp15
2 files changed, 24 insertions, 32 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();});
diff --git a/src/blu_cat/net/server/server.hpp b/src/blu_cat/net/server/server.hpp
index 271d75a..00b277f 100644
--- a/src/blu_cat/net/server/server.hpp
+++ b/src/blu_cat/net/server/server.hpp
@@ -17,9 +17,10 @@
#ifndef BLU_CAT_SERVER_SERVER_H
#define BLU_CAT_SERVER_SERVER_H 1
+#include <queue>
+
#include "../common/connection.hpp"
#include "../common/connection_callback.hpp"
-#include "../common/tsqueue.hpp"
namespace BluCat::NET
{
@@ -30,9 +31,9 @@ class Server: public ConnectionCallback
asio::io_context io_context;
std::thread thread_context;
asio::ip::tcp::acceptor acceptor;
- MessageCallback *(*callback_instantiator)();
+ void (*get_new_connection)(std::unique_ptr<Connection> c);
- TSQueue<unsigned long> free_connection_slots;
+ std::queue<unsigned long> free_connection_slots;
std::vector<Connection*> connections;
void
@@ -40,12 +41,10 @@ class Server: public ConnectionCallback
public:
void
- read_messages();
-
- void
- end_connection(unsigned long index);
+ disconnect(unsigned long index);
- Server(MessageCallback *(*callback_instantiator)(), const uint16_t port);
+ Server(void (*get_new_connection)(std::unique_ptr<Connection> c),
+ const uint16_t port);
~Server();
};