Replace custom error type with int
This commit is contained in:
parent
72121036a6
commit
bbf039a596
64
src/tcp.cpp
64
src/tcp.cpp
@ -1,7 +1,6 @@
|
||||
#include "tcp.hpp"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <array>
|
||||
#include <cerrno>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
@ -19,13 +18,13 @@
|
||||
namespace {
|
||||
|
||||
|
||||
std::expected<sockaddr_in, tcp::Error> get_server_address(const char* host,
|
||||
uint16_t port) {
|
||||
std::expected<sockaddr_in, int> get_server_address(const char* host,
|
||||
uint16_t port) {
|
||||
hostent* server = gethostbyname(host);
|
||||
if (server == nullptr) {
|
||||
spdlog::error("tcp: get_host_by_name() failed with h_errno={}",
|
||||
h_errno);
|
||||
return std::unexpected{tcp::Error::FailedToResolveHost};
|
||||
return std::unexpected{h_errno};
|
||||
}
|
||||
|
||||
sockaddr_in address{};
|
||||
@ -44,7 +43,7 @@ std::expected<sockaddr_in, tcp::Error> get_server_address(const char* host,
|
||||
namespace tcp {
|
||||
|
||||
|
||||
[[nodiscard]] std::expected<void, Error>
|
||||
[[nodiscard]] std::expected<void, int>
|
||||
NonBlockingClient::connect(HostString host, uint16_t port) {
|
||||
/// Resolve host
|
||||
|
||||
@ -65,17 +64,9 @@ NonBlockingClient::connect(HostString host, uint16_t port) {
|
||||
sizeof(serverAddress));
|
||||
|
||||
if (connectRes < 0) {
|
||||
if (errno == EALREADY) {
|
||||
spdlog::error(
|
||||
"tcp::NonBlockingClient: connect() failed with errno={}",
|
||||
errno);
|
||||
return std::unexpected{Error::OtherOperationInProgress};
|
||||
} else if (errno != EINPROGRESS) {
|
||||
spdlog::error(
|
||||
"tcp::NonBlockingClient: connect() failed with errno={}",
|
||||
errno);
|
||||
return std::unexpected{Error::FailedToStartConnecting};
|
||||
}
|
||||
spdlog::error("tcp::NonBlockingClient: connect() failed with errno={}",
|
||||
errno);
|
||||
return std::unexpected{errno};
|
||||
}
|
||||
|
||||
m_startedNewConAttemt = true;
|
||||
@ -83,7 +74,7 @@ NonBlockingClient::connect(HostString host, uint16_t port) {
|
||||
return {};
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<ConnectionStatus, Error>
|
||||
[[nodiscard]] std::expected<ConnectionStatus, int>
|
||||
NonBlockingClient::get_last_connection_status() {
|
||||
if (!m_startedNewConAttemt) return m_lastKnownConStatus;
|
||||
|
||||
@ -102,7 +93,7 @@ NonBlockingClient::get_last_connection_status() {
|
||||
if (getsockopt(m_socket, SOL_SOCKET, SO_ERROR, &err, &len) < 0) {
|
||||
spdlog::error(
|
||||
"tcp::NonBlockingClient: getsockopt() failed with errno={}", errno);
|
||||
return std::unexpected{Error::FailedToReadSocketError};
|
||||
return std::unexpected{errno};
|
||||
}
|
||||
|
||||
if (err == 0) {
|
||||
@ -134,13 +125,14 @@ void NonBlockingClient::disconnect() {
|
||||
close_socket();
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<int, Error>
|
||||
[[nodiscard]] std::expected<int, int>
|
||||
NonBlockingClient::send(std::span<const char> data) {
|
||||
// TODO: Do we need this?
|
||||
auto conRes = get_last_connection_status();
|
||||
if (!conRes) return std::unexpected{conRes.error()};
|
||||
if (conRes != ConnectionStatus::Successful) {
|
||||
spdlog::error("tcp::NonBlockingClient: Socket not connected");
|
||||
return std::unexpected{Error::SocketNotConnected};
|
||||
return std::unexpected{ENOTCONN};
|
||||
}
|
||||
|
||||
const int bytesWritten =
|
||||
@ -149,15 +141,7 @@ NonBlockingClient::send(std::span<const char> data) {
|
||||
if (bytesWritten <= 0) {
|
||||
spdlog::error("tcp::NonBlockingClient: send() failed with errno={}",
|
||||
errno);
|
||||
|
||||
switch (errno) {
|
||||
case EWOULDBLOCK:
|
||||
return std::unexpected{Error::BufferFull};
|
||||
case EALREADY:
|
||||
return std::unexpected{Error::OtherOperationInProgress};
|
||||
default:
|
||||
return std::unexpected{Error::SendFailed};
|
||||
}
|
||||
return std::unexpected{errno};
|
||||
}
|
||||
|
||||
return bytesWritten;
|
||||
@ -179,7 +163,7 @@ bool NonBlockingClient::data_available() {
|
||||
return (m_pfdIn.revents & POLLIN);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<int, Error>
|
||||
[[nodiscard]] std::expected<int, int>
|
||||
NonBlockingClient::recv(std::span<char> buffer) {
|
||||
const int bytesReceived =
|
||||
::recv(m_socket, (char*)buffer.data(), buffer.size(), 0);
|
||||
@ -187,13 +171,7 @@ NonBlockingClient::recv(std::span<char> buffer) {
|
||||
if (bytesReceived < 0) {
|
||||
spdlog::error("tcp::NonBlockingClient: recv() failed with errno={}",
|
||||
errno);
|
||||
|
||||
switch (errno) {
|
||||
case EWOULDBLOCK:
|
||||
return std::unexpected{Error::NoDataAvailable};
|
||||
default:
|
||||
return std::unexpected{Error::RecvFailed};
|
||||
}
|
||||
return std::unexpected{errno};
|
||||
}
|
||||
return {bytesReceived};
|
||||
}
|
||||
@ -214,12 +192,12 @@ void NonBlockingClient::close_socket() {
|
||||
m_pfdIn.fd = -1;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<void, Error> NonBlockingClient::create_socket() {
|
||||
[[nodiscard]] std::expected<void, int> NonBlockingClient::create_socket() {
|
||||
m_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (m_socket < 1) {
|
||||
spdlog::error("tcp::NonBlockingClient: socket() failed with errno={}",
|
||||
errno);
|
||||
return std::unexpected{Error::FailedToCreateSocket};
|
||||
return std::unexpected{errno};
|
||||
}
|
||||
|
||||
m_pfdOut.fd = m_socket;
|
||||
@ -230,26 +208,26 @@ void NonBlockingClient::close_socket() {
|
||||
spdlog::error("tcp::NonBlockingClient: fcntl() failed with errno={}",
|
||||
errno);
|
||||
close_socket();
|
||||
return std::unexpected{Error::FailedToCreateSocket};
|
||||
return std::unexpected{errno};
|
||||
}
|
||||
|
||||
if (fcntl(m_socket, F_SETFL, (currentFlags) | O_NONBLOCK) == -1) {
|
||||
spdlog::error("tcp::NonBlockingClient: fcntl() failed with errno={}",
|
||||
errno);
|
||||
close_socket();
|
||||
return std::unexpected{Error::FailedToCreateSocket};
|
||||
return std::unexpected{errno};
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<bool, Error>
|
||||
[[nodiscard]] std::expected<bool, int>
|
||||
NonBlockingClient::new_error_codes_available() {
|
||||
int ret = poll(&m_pfdOut, 1, 0);
|
||||
if (ret < 0) {
|
||||
spdlog::error("tcp::NonBlockingClient: poll() failed with errno={}",
|
||||
errno);
|
||||
return std::unexpected{Error::FailedToReadSocketError};
|
||||
return std::unexpected{errno};
|
||||
}
|
||||
|
||||
return (ret == 0);
|
||||
|
||||
26
src/tcp.hpp
26
src/tcp.hpp
@ -24,20 +24,6 @@
|
||||
namespace tcp {
|
||||
|
||||
|
||||
enum class Error {
|
||||
FailedToCreateSocket,
|
||||
FailedToStartConnecting,
|
||||
FailedToReadSocketError,
|
||||
FailedToResolveHost,
|
||||
OtherOperationInProgress,
|
||||
SocketNotConnected,
|
||||
BufferFull,
|
||||
NoDataAvailable,
|
||||
SendFailed,
|
||||
RecvFailed,
|
||||
};
|
||||
|
||||
|
||||
enum class ConnectionStatus {
|
||||
AttemptingConnection,
|
||||
Successful,
|
||||
@ -63,19 +49,19 @@ using HostString = std::array<char, 64>;
|
||||
class NonBlockingClient {
|
||||
public:
|
||||
// clang-format off
|
||||
[[nodiscard]] std::expected<void, Error>
|
||||
[[nodiscard]] std::expected<void, int>
|
||||
connect(HostString host, uint16_t port);
|
||||
// clang-format off
|
||||
|
||||
void disconnect();
|
||||
|
||||
[[nodiscard]] std::expected<ConnectionStatus, Error>
|
||||
[[nodiscard]] std::expected<ConnectionStatus, int>
|
||||
get_last_connection_status();
|
||||
|
||||
bool data_available();
|
||||
|
||||
[[nodiscard]] std::expected<int, Error> recv(std::span<char> buffer);
|
||||
[[nodiscard]] std::expected<int, Error> send(std::span<const char> data);
|
||||
[[nodiscard]] std::expected<int, int> recv(std::span<char> buffer);
|
||||
[[nodiscard]] std::expected<int, int> send(std::span<const char> data);
|
||||
|
||||
private:
|
||||
int m_socket = -1;
|
||||
@ -87,8 +73,8 @@ private:
|
||||
|
||||
void close_socket();
|
||||
|
||||
[[nodiscard]] std::expected<void, Error> create_socket();
|
||||
[[nodiscard]] std::expected<bool, Error> new_error_codes_available();
|
||||
[[nodiscard]] std::expected<void, int> create_socket();
|
||||
[[nodiscard]] std::expected<bool, int> new_error_codes_available();
|
||||
};
|
||||
|
||||
} // namespace tcp
|
||||
|
||||
Loading…
Reference in New Issue
Block a user