Extract connection handling

This commit is contained in:
momo5502
2025-01-17 18:02:35 +01:00
parent 0253592ae9
commit cf76d5b4dc
3 changed files with 100 additions and 40 deletions

View File

@@ -0,0 +1,59 @@
#include "connection_handler.hpp"
#include "checksum.hpp"
#include <thread>
using namespace std::literals;
namespace gdb_stub
{
namespace
{
bool read_from_socket(stream_processor& processor, network::tcp_client_socket& client)
{
const auto data = client.receive();
if (!data)
{
return false;
}
processor.push_stream_data(*data);
return true;
}
}
connection_handler::connection_handler(network::tcp_client_socket& client)
: client_(client)
{
this->client_.set_blocking(false);
}
std::optional<std::string> connection_handler::get_packet()
{
while (this->client_.is_valid() && !this->processor_.has_packet())
{
if (!read_from_socket(this->processor_, this->client_))
{
std::this_thread::sleep_for(100ms);
}
}
if (this->processor_.has_packet())
{
return this->processor_.get_next_packet();
}
return std::nullopt;
}
void connection_handler::send_packet(const std::string_view data) const
{
const auto checksum = compute_checksum_as_string(data);
this->send_raw_data("$" + std::string(data) + "#" + checksum);
}
void connection_handler::send_raw_data(const std::string_view data) const
{
(void)this->client_.send(data);
}
}