summaryrefslogtreecommitdiff
path: root/src/blu_cat/net/common
diff options
context:
space:
mode:
authorFrederico Linhares <fred@linhares.blue>2025-03-28 14:53:08 -0300
committerFrederico Linhares <fred@linhares.blue>2025-03-28 14:53:08 -0300
commit76c2fd91a71585bfccf8ae9e30834a37514de24f (patch)
treee4a717639d6329c2e6638defadef3b62cf522259 /src/blu_cat/net/common
parent600a4f7586a31990cfdb02ab811768ca6afc909e (diff)
feat Simplify interface for net connections
Diffstat (limited to 'src/blu_cat/net/common')
-rw-r--r--src/blu_cat/net/common/connection.cpp21
-rw-r--r--src/blu_cat/net/common/connection.hpp9
-rw-r--r--src/blu_cat/net/common/connection_callback.hpp2
-rw-r--r--src/blu_cat/net/common/message_callback.hpp40
-rw-r--r--src/blu_cat/net/common/tsqueue.hpp1
5 files changed, 14 insertions, 59 deletions
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
{