diff options
author | Frederico Linhares <fred@linhares.blue> | 2025-03-28 14:53:08 -0300 |
---|---|---|
committer | Frederico Linhares <fred@linhares.blue> | 2025-03-28 14:53:08 -0300 |
commit | 76c2fd91a71585bfccf8ae9e30834a37514de24f (patch) | |
tree | e4a717639d6329c2e6638defadef3b62cf522259 | |
parent | 600a4f7586a31990cfdb02ab811768ca6afc909e (diff) |
feat Simplify interface for net connections
-rw-r--r-- | src/blu_cat/net/client/client.cpp | 43 | ||||
-rw-r--r-- | src/blu_cat/net/client/client.hpp | 23 | ||||
-rw-r--r-- | src/blu_cat/net/common/connection.cpp | 21 | ||||
-rw-r--r-- | src/blu_cat/net/common/connection.hpp | 9 | ||||
-rw-r--r-- | src/blu_cat/net/common/connection_callback.hpp | 2 | ||||
-rw-r--r-- | src/blu_cat/net/common/message_callback.hpp | 40 | ||||
-rw-r--r-- | src/blu_cat/net/common/tsqueue.hpp | 1 | ||||
-rw-r--r-- | src/blu_cat/net/server/server.cpp | 41 | ||||
-rw-r--r-- | src/blu_cat/net/server/server.hpp | 15 |
9 files changed, 77 insertions, 118 deletions
diff --git a/src/blu_cat/net/client/client.cpp b/src/blu_cat/net/client/client.cpp index 13601e5..6ee6046 100644 --- a/src/blu_cat/net/client/client.cpp +++ b/src/blu_cat/net/client/client.cpp @@ -20,44 +20,45 @@ namespace BluCat::NET { void -Client::read_messages() +Client::connect(const char *host, const UI16 port) { - std::scoped_lock lock(this->mut); - if(this->connection) this->connection->read_messages(); + if(this->connection) return; + + asio::error_code error; + asio::ip::tcp::endpoint endpoint{asio::ip::make_address(host, error), port}; + + asio::ip::tcp::socket socket(this->io_context); + socket.connect(endpoint); + + this->connection = new Connection( + this, this->io_context, std::move(socket), 0); + + this->thread_context = std::thread([this](){this->io_context.run();}); } void -Client::end_connection(unsigned long index) +Client::disconnect(unsigned long index) { - std::scoped_lock lock(this->mut); + if(!this->connection) return; + this->io_context.stop(); delete this->connection; this->connection = nullptr; } -Client::Client( - MessageCallback *(*callback_instantiator)(), const char *host, - const uint16_t port): - callback_instantiator{callback_instantiator} +Client::Client(const char *host, const UI16 port) { - asio::error_code error; - asio::ip::tcp::endpoint endpoint{asio::ip::make_address(host, error), port}; - - asio::ip::tcp::socket socket(this->io_context); - socket.connect(endpoint); - - this->connection = new Connection( - this->callback_instantiator(), this, this->io_context, std::move(socket), - 0); + this->connect(host, port); +} - this->thread_context = std::thread([this](){this->io_context.run();}); +Client::Client() +{ } Client::~Client() { - this->io_context.stop(); + this->disconnect(0); if(this->thread_context.joinable()) this->thread_context.join(); - if(this->connection) delete this->connection; } } diff --git a/src/blu_cat/net/client/client.hpp b/src/blu_cat/net/client/client.hpp index 60190c8..ad0e298 100644 --- a/src/blu_cat/net/client/client.hpp +++ b/src/blu_cat/net/client/client.hpp @@ -17,6 +17,7 @@ #ifndef BLU_CAT_NET_CLIENT_CLIENT_H #define BLU_CAT_NET_CLIENT_CLIENT_H 1 +#include "../../com/numbers.hpp" #include "../common/connection.hpp" #include "../common/connection_callback.hpp" @@ -25,23 +26,33 @@ namespace BluCat::NET class Client: public ConnectionCallback { - std::mutex mut; asio::io_context io_context; std::thread thread_context; - MessageCallback *(*callback_instantiator)(); Connection *connection; public: + inline bool + send(const UI32 id, std::vector<UI8> &msg) + {return this->connection->send(id, msg);}; + + inline bool + read_message(Message *m) + {return this->connection->read_message(m);}; + + inline bool + is_connected() + {return this->connection != nullptr;}; + void - read_messages(); + connect(const char *host, const UI16 port); void - end_connection(unsigned long index); + disconnect(unsigned long index); Client( - MessageCallback *(*callback_instantiator)(), const char *host, - const uint16_t port); + const char *host, const UI16 port); + Client(); ~Client(); }; diff --git a/src/blu_cat/net/common/connection.cpp b/src/blu_cat/net/common/connection.cpp index b4e8f1b..643d30c 100644 --- a/src/blu_cat/net/common/connection.cpp +++ b/src/blu_cat/net/common/connection.cpp @@ -46,7 +46,7 @@ Connection::read_header() else { std::cout << "Failed to read header: " << error.message() << std::endl; - this->connection_callback->end_connection(this->index); + this->connection_callback->disconnect(this->index); } }); } @@ -67,7 +67,7 @@ Connection::read_body() else { std::cout << "Failed to read body." << std::endl; - this->connection_callback->end_connection(this->index); + this->connection_callback->disconnect(this->index); } }); } @@ -96,7 +96,7 @@ Connection::send(const uint32_t id, const std::vector<uint8_t> &msg) { std::cout << "Failed to send message: " << error.message() << std::endl; - this->connection_callback->end_connection(this->index); + this->connection_callback->disconnect(this->index); } delete buffered_msg; }); @@ -104,20 +104,19 @@ Connection::send(const uint32_t id, const std::vector<uint8_t> &msg) return true; } -void -Connection::read_messages() +bool +Connection::read_message(Message *m) { - while(this->messages.size() > 0) - this->message_callback->read_message( - std::move(this->messages.pop_back())); + if(this->messages.size() <= 0) return false; + + *m = std::move(this->messages.pop_back()); + return true; } Connection::Connection( - MessageCallback *message_callback, ConnectionCallback *connection_callback, asio::io_context &io_context, asio::ip::tcp::socket socket, unsigned long index): - message_callback{message_callback}, connection_callback{connection_callback}, io_context{io_context}, socket{std::move(socket)}, @@ -128,8 +127,6 @@ Connection::Connection( Connection::~Connection() { - this->message_callback->disconnect(); - delete message_callback; this->socket.close(); } diff --git a/src/blu_cat/net/common/connection.hpp b/src/blu_cat/net/common/connection.hpp index c5683cb..bdce8b3 100644 --- a/src/blu_cat/net/common/connection.hpp +++ b/src/blu_cat/net/common/connection.hpp @@ -26,7 +26,6 @@ #include "connection_callback.hpp" #include "message.hpp" -#include "message_callback.hpp" #include "tsqueue.hpp" namespace BluCat::NET @@ -42,7 +41,6 @@ protected: TSQueue<Message> messages; Message reading_message; - MessageCallback *message_callback; ConnectionCallback *connection_callback; void @@ -55,11 +53,10 @@ public: bool send(const uint32_t id, const std::vector<uint8_t> &msg); - void - read_messages(); + bool + read_message(Message *m); - Connection(MessageCallback *message_callback, - ConnectionCallback *connection_callback, + Connection(ConnectionCallback *connection_callback, asio::io_context &io_context, asio::ip::tcp::socket socket, unsigned long index); diff --git a/src/blu_cat/net/common/connection_callback.hpp b/src/blu_cat/net/common/connection_callback.hpp index 815d393..5241c76 100644 --- a/src/blu_cat/net/common/connection_callback.hpp +++ b/src/blu_cat/net/common/connection_callback.hpp @@ -28,7 +28,7 @@ class Connection; struct ConnectionCallback { virtual void - end_connection(unsigned long index)=0; + disconnect(unsigned long index)=0; virtual ~ConnectionCallback(){}; diff --git a/src/blu_cat/net/common/message_callback.hpp b/src/blu_cat/net/common/message_callback.hpp deleted file mode 100644 index b17886c..0000000 --- a/src/blu_cat/net/common/message_callback.hpp +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2022-2025 Frederico de Oliveira Linhares - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef BLU_CAT_NET_COMMON_MESSAGE_CALLBACK_H -#define BLU_CAT_NET_COMMON_MESSAGE_CALLBACK_H 1 - -#include "message.hpp" - -namespace BluCat::NET -{ - -struct MessageCallback -{ - virtual void - read_message(Message m)=0; - - virtual void - disconnect()=0; - - virtual - ~MessageCallback(){}; -}; - -} - - -#endif /* BLU_CAT_NET_COMMON_MESSAGE_CALLBACK_H */ diff --git a/src/blu_cat/net/common/tsqueue.hpp b/src/blu_cat/net/common/tsqueue.hpp index 4cdd21d..673fe3c 100644 --- a/src/blu_cat/net/common/tsqueue.hpp +++ b/src/blu_cat/net/common/tsqueue.hpp @@ -18,6 +18,7 @@ #define BLU_CAT_NET_COMMON_TSQUEUE_H 1 #include <deque> +#include <mutex> namespace BluCat::NET { 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(); }; |