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 /src/blu_cat/net/client | |
parent | 600a4f7586a31990cfdb02ab811768ca6afc909e (diff) |
feat Simplify interface for net connections
Diffstat (limited to 'src/blu_cat/net/client')
-rw-r--r-- | src/blu_cat/net/client/client.cpp | 43 | ||||
-rw-r--r-- | src/blu_cat/net/client/client.hpp | 23 |
2 files changed, 39 insertions, 27 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(); }; |