working commit
This commit is contained in:
+20
-7
@@ -19,10 +19,23 @@ extern "C" {
|
||||
|
||||
TCPClient::TCPClient() {
|
||||
sock = 0;
|
||||
family = 0;
|
||||
}
|
||||
|
||||
std::expected<void, std::string> TCPClient::conn(const std::string address, const int port) {
|
||||
sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
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) {
|
||||
family = AF_INET;
|
||||
} else if (inet_pton(AF_INET6, address.c_str(), &(sa.sin_addr)) == 1) {
|
||||
family = AF_INET6;
|
||||
} else {
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Incorrect address " + naddress);
|
||||
}
|
||||
|
||||
sock = socket(family, SOCK_STREAM, 0);
|
||||
if (sock < 0) {
|
||||
return std::unexpected("Error opening socket");
|
||||
}
|
||||
@@ -37,7 +50,7 @@ std::expected<void, std::string> TCPClient::conn(const std::string address, cons
|
||||
}
|
||||
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = 3;
|
||||
timeout.tv_sec = 5;
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout) < 0) {
|
||||
@@ -53,7 +66,7 @@ std::expected<void, std::string> TCPClient::conn(const std::string address, cons
|
||||
}
|
||||
|
||||
|
||||
std::expected<int, std::string> TCPClient::writeBytes(std::span<const std::byte> payload) {
|
||||
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");
|
||||
@@ -61,7 +74,7 @@ std::expected<int, std::string> TCPClient::writeBytes(std::span<const std::byte>
|
||||
return n;
|
||||
}
|
||||
|
||||
std::expected<int, std::string> TCPClient::writeBytes(std::string payload) {
|
||||
std::expected<int, std::string> TCPClient::Write(std::string payload) {
|
||||
int n = 0;
|
||||
if ((n = write(sock, payload.data(), payload.size())) < 0) {
|
||||
return std::unexpected("Write error");
|
||||
@@ -69,7 +82,7 @@ std::expected<int, std::string> TCPClient::writeBytes(std::string payload) {
|
||||
return n;
|
||||
}
|
||||
|
||||
std::expected<int, std::string> TCPClient::readBytes(std::vector<std::byte>* buffer) {
|
||||
std::expected<int, std::string> TCPClient::Read(std::vector<std::byte>* buffer) {
|
||||
int n = 0;
|
||||
if ((n = read(sock, buffer->data(), buffer->size())) < 0) {
|
||||
return std::unexpected("Read error");
|
||||
@@ -77,7 +90,7 @@ std::expected<int, std::string> TCPClient::readBytes(std::vector<std::byte>* buf
|
||||
return n;
|
||||
}
|
||||
|
||||
std::expected<int, std::string> TCPClient::readBytes(std::vector<uint8_t>* buffer) {
|
||||
std::expected<int, std::string> TCPClient::Read(std::vector<uint8_t>* buffer) {
|
||||
return read(sock, buffer->data(), buffer->size());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user