working commit

This commit is contained in:
2026-04-23 17:53:50 +02:00
parent cf45872d91
commit 37d9ee63cc
18 changed files with 1476 additions and 1170 deletions
+18 -19
View File
@@ -15,6 +15,12 @@ extern "C" {
#include <span>
#include <iostream>
#include <cstring>
#include <expected>
#include <string>
#include <iostream>
#include <tcpclient.hpp>
TCPClient::TCPClient() {
@@ -25,9 +31,9 @@ TCPClient::TCPClient() {
std::expected<void, std::string> TCPClient::Connect(const std::string naddress, const int port) {
struct sockaddr_in sa;
if (inet_pton(AF_INET, address.c_str(), &(sa.sin_addr)) == 1) {
if (inet_pton(AF_INET, naddress.c_str(), &(sa.sin_addr)) == 1) {
family = AF_INET;
} else if (inet_pton(AF_INET6, address.c_str(), &(sa.sin_addr)) == 1) {
} else if (inet_pton(AF_INET6, naddress.c_str(), &(sa.sin_addr)) == 1) {
family = AF_INET6;
} else {
int errnocopy = errno;
@@ -44,7 +50,7 @@ std::expected<void, std::string> TCPClient::Connect(const std::string naddress,
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
const char* addr = address.data();
const char* addr = naddress.data();
if (inet_pton(AF_INET, addr, &serv_addr.sin_addr) <= 0) {
return std::unexpected("Invalid server IP address");
}
@@ -65,15 +71,6 @@ std::expected<void, std::string> TCPClient::Connect(const std::string naddress,
return {};
}
std::expected<int, std::string> TCPClient::Write(std::span<const std::byte> payload) {
int n = 0;
if ((n = write(sock, payload.data(), payload.size())) < 0) {
return std::unexpected("Write error");
}
return n;
}
std::expected<int, std::string> TCPClient::Write(std::string payload) {
int n = 0;
if ((n = write(sock, payload.data(), payload.size())) < 0) {
@@ -82,16 +79,18 @@ std::expected<int, std::string> TCPClient::Write(std::string payload) {
return n;
}
std::expected<int, std::string> TCPClient::Read(std::vector<std::byte>* buffer) {
int n = 0;
if ((n = read(sock, buffer->data(), buffer->size())) < 0) {
std::expected<int, std::string> TCPClient::Read(std::string& res, int size) {
char buffer[size + 1];
memset(&buffer, 0, size + 1);
int rsize = 0;
if ((rsize = read(sock, &buffer, size)) < 0) {
return std::unexpected("Read error");
}
return n;
res.append(buffer, rsize);
return rsize;
}
std::expected<int, std::string> TCPClient::Read(std::vector<uint8_t>* buffer) {
return read(sock, buffer->data(), buffer->size());
TCPClient::~TCPClient() {
close(sock);
}