Add unit tests
This commit is contained in:
22
src/tcp.cpp
22
src/tcp.cpp
@@ -63,6 +63,11 @@ NonBlockingServer::NonBlockingServer(ServerSettings settings)
|
||||
: m_settings{settings}, m_serverAddress(get_local_address(settings.port)) {
|
||||
}
|
||||
|
||||
NonBlockingServer::~NonBlockingServer() {
|
||||
close_client_socket();
|
||||
close_server_socket();
|
||||
}
|
||||
|
||||
std::expected<void, int> NonBlockingServer::start_listening() {
|
||||
/// Create socket
|
||||
|
||||
@@ -254,6 +259,10 @@ void NonBlockingServer::close_client_socket() {
|
||||
//
|
||||
|
||||
|
||||
NonBlockingClient::~NonBlockingClient() {
|
||||
close_socket();
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<void, int>
|
||||
NonBlockingClient::connect(HostString host, uint16_t port) {
|
||||
/// Resolve host
|
||||
@@ -280,14 +289,14 @@ NonBlockingClient::connect(HostString host, uint16_t port) {
|
||||
return std::unexpected{errno};
|
||||
}
|
||||
|
||||
m_startedNewConAttemt = true;
|
||||
m_startedNewConAttempt = true;
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<int, int>
|
||||
NonBlockingClient::get_last_connection_status() {
|
||||
if (!m_startedNewConAttemt) return m_lastKnownConStatus;
|
||||
if (!m_startedNewConAttempt) return m_lastKnownConStatus;
|
||||
|
||||
/// Check if connect operation has been completed
|
||||
|
||||
@@ -295,7 +304,7 @@ NonBlockingClient::get_last_connection_status() {
|
||||
if (!pendingOpRes) return std::unexpected{pendingOpRes.error()};
|
||||
if (!pendingOpRes.value()) return EINPROGRESS;
|
||||
|
||||
m_startedNewConAttemt = false;
|
||||
m_startedNewConAttempt = false;
|
||||
|
||||
/// Check for connection errors
|
||||
|
||||
@@ -317,6 +326,7 @@ NonBlockingClient::get_last_connection_status() {
|
||||
}
|
||||
|
||||
void NonBlockingClient::disconnect() {
|
||||
m_lastKnownConStatus = ENOTCONN;
|
||||
close_socket();
|
||||
}
|
||||
|
||||
@@ -325,7 +335,7 @@ NonBlockingClient::send(std::span<const std::byte> data) {
|
||||
// TODO: Do we need this?
|
||||
auto conRes = get_last_connection_status();
|
||||
if (!conRes) return std::unexpected{conRes.error()};
|
||||
if (conRes != EISCONN) {
|
||||
if (conRes.value() != EISCONN) {
|
||||
spdlog::error("tcp::NonBlockingClient: Socket not connected");
|
||||
return std::unexpected{ENOTCONN};
|
||||
}
|
||||
@@ -345,7 +355,7 @@ NonBlockingClient::send(std::span<const std::byte> data) {
|
||||
bool NonBlockingClient::data_available() {
|
||||
auto conRes = get_last_connection_status();
|
||||
if (!conRes) return false;
|
||||
if (conRes != EISCONN) return false;
|
||||
if (conRes.value() != EISCONN) return false;
|
||||
|
||||
int ret = poll(&m_pfdIn, 1, 0);
|
||||
if (ret < 0) {
|
||||
@@ -354,7 +364,6 @@ bool NonBlockingClient::data_available() {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
return (m_pfdIn.revents & POLLIN);
|
||||
}
|
||||
|
||||
@@ -425,6 +434,7 @@ NonBlockingClient::new_error_codes_available() {
|
||||
return std::unexpected{errno};
|
||||
}
|
||||
|
||||
// ret == 0 means the operation timed out, i.e., no new events
|
||||
return (ret != 0);
|
||||
}
|
||||
|
||||
|
||||
13
src/tcp.hpp
13
src/tcp.hpp
@@ -39,13 +39,13 @@ struct ServerSettings {
|
||||
class NonBlockingServer {
|
||||
public:
|
||||
NonBlockingServer(ServerSettings settings);
|
||||
~NonBlockingServer();
|
||||
|
||||
[[nodiscard]] std::expected<void, int> start_listening();
|
||||
|
||||
bool next_client_available();
|
||||
[[nodiscard]] std::expected<void, int> accept_next_client();
|
||||
void disconnect();
|
||||
// bool socket_closed() const;
|
||||
|
||||
[[nodiscard]] std::expected<int, int>
|
||||
send(std::span<const std::byte> data) const;
|
||||
@@ -86,15 +86,16 @@ private:
|
||||
|
||||
class NonBlockingClient {
|
||||
public:
|
||||
~NonBlockingClient();
|
||||
|
||||
// clang-format off
|
||||
[[nodiscard]] std::expected<void, int>
|
||||
connect(HostString host, uint16_t port);
|
||||
// clang-format off
|
||||
// clang-format on
|
||||
|
||||
void disconnect();
|
||||
|
||||
[[nodiscard]] std::expected<int, int>
|
||||
get_last_connection_status();
|
||||
[[nodiscard]] std::expected<int, int> get_last_connection_status();
|
||||
|
||||
bool data_available();
|
||||
|
||||
@@ -106,8 +107,8 @@ private:
|
||||
pollfd m_pfdOut = {.fd = -1, .events = POLLOUT};
|
||||
pollfd m_pfdIn = {.fd = -1, .events = POLLIN};
|
||||
|
||||
bool m_startedNewConAttemt = false;
|
||||
int m_lastKnownConStatus = 0;
|
||||
bool m_startedNewConAttempt = false;
|
||||
int m_lastKnownConStatus = ENOTCONN;
|
||||
|
||||
void close_socket();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user