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/common/connection.cpp | 21 ++++++-------- src/blu_cat/net/common/connection.hpp | 9 ++---- src/blu_cat/net/common/connection_callback.hpp | 2 +- src/blu_cat/net/common/message_callback.hpp | 40 -------------------------- src/blu_cat/net/common/tsqueue.hpp | 1 + 5 files changed, 14 insertions(+), 59 deletions(-) delete mode 100644 src/blu_cat/net/common/message_callback.hpp (limited to 'src/blu_cat/net/common') 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 &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 &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 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 &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 +#include namespace BluCat::NET { -- cgit v1.2.3