Compare commits
10 Commits
1847a0ea0c
...
da6159d706
| Author | SHA1 | Date | |
|---|---|---|---|
| da6159d706 | |||
| 4766d4755a | |||
| 440837cdbc | |||
| b22150de94 | |||
| 4ffd828655 | |||
| 1c91b7a1d7 | |||
| 9582dbdc88 | |||
| 47a2135093 | |||
| 9aee820625 | |||
| da346c256e |
@ -54,7 +54,6 @@ target_link_libraries(playground PRIVATE spdlog::spdlog)
|
|||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
enable_testing()
|
enable_testing()
|
||||||
|
|
||||||
add_executable(
|
add_executable(
|
||||||
|
|||||||
13
src/main.cpp
13
src/main.cpp
@ -1,12 +1,15 @@
|
|||||||
#include "tcp.hpp"
|
// Library includes
|
||||||
#include <cstddef>
|
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
|
// Project includes
|
||||||
|
#include "tcp.hpp"
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
spdlog::set_level(spdlog::level::debug);
|
spdlog::set_level(spdlog::level::debug);
|
||||||
|
|
||||||
tcp::NonBlockingServer server{{.port = 65432}};
|
tcp::NonBlockingServer server{};
|
||||||
if (auto res = server.start_listening(); !res) {
|
if (auto res = server.start_listening(65432); !res) {
|
||||||
spdlog::error("Failed to start server: {}", res.error());
|
spdlog::error("Failed to start server: {}", res.error());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -47,6 +50,6 @@ int main() {
|
|||||||
spdlog::info("Sent {} bytes", sendRes.value());
|
spdlog::info("Sent {} bytes", sendRes.value());
|
||||||
}
|
}
|
||||||
|
|
||||||
server.disconnect();
|
server.disconnect_from_client();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
215
src/tcp.cpp
215
src/tcp.cpp
@ -1,18 +1,12 @@
|
|||||||
#include "tcp.hpp"
|
#include "tcp.hpp"
|
||||||
|
|
||||||
|
// C stdlib includes
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <cerrno>
|
|
||||||
#include <cstdint>
|
|
||||||
#include <expected>
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <netinet/tcp.h>
|
// Library includes
|
||||||
#include <poll.h>
|
|
||||||
#include <span>
|
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
@ -36,7 +30,7 @@ std::expected<sockaddr_in, int> resolve_remote_address(const char* host,
|
|||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline auto get_local_address(uint16_t port) -> sockaddr_in {
|
inline sockaddr_in get_local_address(uint16_t port) {
|
||||||
sockaddr_in address{};
|
sockaddr_in address{};
|
||||||
address.sin_addr.s_addr = htonl(INADDR_ANY);
|
address.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||||
address.sin_family = AF_INET;
|
address.sin_family = AF_INET;
|
||||||
@ -45,7 +39,6 @@ inline auto get_local_address(uint16_t port) -> sockaddr_in {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
@ -59,43 +52,49 @@ namespace tcp {
|
|||||||
//
|
//
|
||||||
|
|
||||||
|
|
||||||
NonBlockingServer::NonBlockingServer(ServerSettings settings)
|
|
||||||
: m_settings{settings}, m_serverAddress(get_local_address(settings.port)) {
|
|
||||||
}
|
|
||||||
|
|
||||||
NonBlockingServer::~NonBlockingServer() {
|
NonBlockingServer::~NonBlockingServer() {
|
||||||
close_client_socket();
|
close_client_socket();
|
||||||
close_server_socket();
|
close_server_socket();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::expected<void, int> NonBlockingServer::start_listening() {
|
///
|
||||||
|
/// @details Calling this repeatedly closes the previous socket and creates a
|
||||||
|
/// new one.
|
||||||
|
///
|
||||||
|
std::expected<void, int> NonBlockingServer::start_listening(uint16_t port) {
|
||||||
/// Create socket
|
/// Create socket
|
||||||
|
|
||||||
close_server_socket();
|
close_server_socket();
|
||||||
if (auto res = create_socket(); !res) {
|
if (auto res = create_server_socket(); !res) {
|
||||||
return std::unexpected{res.error()};
|
return std::unexpected{res.error()};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Bin socket and start listening
|
/// Bin socket and start listening
|
||||||
|
|
||||||
if (::bind(m_serverSocket, (struct sockaddr*)&m_serverAddress,
|
const struct sockaddr_in serverAddress = get_local_address(port);
|
||||||
sizeof(m_serverAddress)) != 0) {
|
|
||||||
|
if (::bind(m_serverSocket, (struct sockaddr*)&serverAddress,
|
||||||
|
sizeof(serverAddress)) != 0) {
|
||||||
spdlog::error("tcp::NonBlockingServer: bind() failed with errno={}",
|
spdlog::error("tcp::NonBlockingServer: bind() failed with errno={}",
|
||||||
errno);
|
errno);
|
||||||
close(m_serverSocket);
|
close_server_socket();
|
||||||
return std::unexpected{errno};
|
return std::unexpected{errno};
|
||||||
};
|
};
|
||||||
|
|
||||||
if (::listen(m_serverSocket, 1) != 0) {
|
if (::listen(m_serverSocket, 1) != 0) {
|
||||||
spdlog::error("tcp::NonBlockingServer: listen() failed with errno={}",
|
spdlog::error("tcp::NonBlockingServer: listen() failed with errno={}",
|
||||||
errno);
|
errno);
|
||||||
close(m_serverSocket);
|
close_server_socket();
|
||||||
return std::unexpected{errno};
|
return std::unexpected{errno};
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NonBlockingServer::stop_listening() {
|
||||||
|
close_server_socket();
|
||||||
|
}
|
||||||
|
|
||||||
bool NonBlockingServer::next_client_available() {
|
bool NonBlockingServer::next_client_available() {
|
||||||
int ret = poll(&m_serverPfdIn, 1, 0);
|
int ret = poll(&m_serverPfdIn, 1, 0);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
@ -108,10 +107,12 @@ bool NonBlockingServer::next_client_available() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] std::expected<void, int> NonBlockingServer::accept_next_client() {
|
[[nodiscard]] std::expected<void, int> NonBlockingServer::accept_next_client() {
|
||||||
disconnect();
|
disconnect_from_client();
|
||||||
|
|
||||||
|
socklen_t clientAddrLen = sizeof(m_clientAddress);
|
||||||
|
|
||||||
int result = ::accept(m_serverSocket, (struct sockaddr*)&m_clientAddress,
|
int result = ::accept(m_serverSocket, (struct sockaddr*)&m_clientAddress,
|
||||||
&m_clientAddrLen);
|
&clientAddrLen);
|
||||||
if (result < 0) return std::unexpected{errno};
|
if (result < 0) return std::unexpected{errno};
|
||||||
|
|
||||||
m_clientSocket = result;
|
m_clientSocket = result;
|
||||||
@ -119,16 +120,13 @@ bool NonBlockingServer::next_client_available() {
|
|||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
void NonBlockingServer::disconnect() {
|
|
||||||
|
void NonBlockingServer::disconnect_from_client() {
|
||||||
close_client_socket();
|
close_client_socket();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Do we need this?
|
|
||||||
// bool NonBlockingServer::socket_closed() const {
|
|
||||||
// }
|
|
||||||
|
|
||||||
[[nodiscard]] std::expected<std::array<char, 16>, int>
|
[[nodiscard]] std::expected<std::array<char, 16>, int>
|
||||||
NonBlockingServer::get_client_ip_as_string() const {
|
NonBlockingServer::get_client_ip_as_string() {
|
||||||
std::array<char, 16> addrStr;
|
std::array<char, 16> addrStr;
|
||||||
|
|
||||||
if (inet_ntop(AF_INET, &((struct sockaddr_in*)&m_clientAddress)->sin_addr,
|
if (inet_ntop(AF_INET, &((struct sockaddr_in*)&m_clientAddress)->sin_addr,
|
||||||
@ -143,34 +141,7 @@ NonBlockingServer::get_client_ip_as_string() const {
|
|||||||
// return std::unexpected{errno};
|
// return std::unexpected{errno};
|
||||||
// }
|
// }
|
||||||
|
|
||||||
return {addrStr};
|
return addrStr;
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] std::expected<int, int>
|
|
||||||
NonBlockingServer::send(std::span<const std::byte> data) const {
|
|
||||||
const int bytesWritten =
|
|
||||||
::send(m_clientSocket, (const char*)data.data(), data.size(), 0);
|
|
||||||
|
|
||||||
if (bytesWritten <= 0) {
|
|
||||||
spdlog::error("tcp::NonBlockingServer: send() failed with errno={}",
|
|
||||||
errno);
|
|
||||||
return std::unexpected{errno};
|
|
||||||
}
|
|
||||||
|
|
||||||
return bytesWritten;
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] std::expected<int, int>
|
|
||||||
NonBlockingServer::recv(std::span<std::byte> buffer) const {
|
|
||||||
const int bytesReceived =
|
|
||||||
::recv(m_clientSocket, (char*)buffer.data(), buffer.size(), 0);
|
|
||||||
|
|
||||||
if (bytesReceived < 0) {
|
|
||||||
spdlog::error("tcp::NonBlockingServer: recv() failed with errno={}",
|
|
||||||
errno);
|
|
||||||
return std::unexpected{errno};
|
|
||||||
}
|
|
||||||
return {bytesReceived};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NonBlockingServer::data_available() {
|
bool NonBlockingServer::data_available() {
|
||||||
@ -184,11 +155,40 @@ bool NonBlockingServer::data_available() {
|
|||||||
return (m_clientPfdIn.revents & POLLIN);
|
return (m_clientPfdIn.revents & POLLIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] std::expected<void, int> NonBlockingServer::create_socket() {
|
[[nodiscard]] std::expected<int, int>
|
||||||
|
NonBlockingServer::send(std::span<const std::byte> data) {
|
||||||
|
const int bytesWritten =
|
||||||
|
::send(m_clientSocket, (const char*)data.data(), data.size(), 0);
|
||||||
|
|
||||||
|
if (bytesWritten <= 0) {
|
||||||
|
spdlog::error("tcp::NonBlockingServer: send() failed with errno={}",
|
||||||
|
errno);
|
||||||
|
return std::unexpected{errno};
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytesWritten;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] std::expected<int, int>
|
||||||
|
NonBlockingServer::recv(std::span<std::byte> buffer) {
|
||||||
|
const int bytesReceived =
|
||||||
|
::recv(m_clientSocket, (char*)buffer.data(), buffer.size(), 0);
|
||||||
|
|
||||||
|
if (bytesReceived < 0) {
|
||||||
|
spdlog::error("tcp::NonBlockingServer: recv() failed with errno={}",
|
||||||
|
errno);
|
||||||
|
return std::unexpected{errno};
|
||||||
|
}
|
||||||
|
return {bytesReceived};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] std::expected<void, int>
|
||||||
|
NonBlockingServer::create_server_socket() {
|
||||||
m_serverSocket = socket(AF_INET, SOCK_STREAM, 0);
|
m_serverSocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (m_serverSocket < 1) {
|
if (m_serverSocket < 1) {
|
||||||
spdlog::error("tcp::NonBlockingServer: socket() failed with errno={}",
|
spdlog::error("tcp::NonBlockingServer: socket() failed with errno={}",
|
||||||
errno);
|
errno);
|
||||||
|
close_server_socket();
|
||||||
return std::unexpected{errno};
|
return std::unexpected{errno};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -264,7 +264,7 @@ NonBlockingClient::~NonBlockingClient() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] std::expected<void, int>
|
[[nodiscard]] std::expected<void, int>
|
||||||
NonBlockingClient::connect(HostString host, uint16_t port) {
|
NonBlockingClient::connect(const HostString& host, uint16_t port) {
|
||||||
/// Resolve host
|
/// Resolve host
|
||||||
|
|
||||||
auto addrRes = resolve_remote_address(host.data(), port);
|
auto addrRes = resolve_remote_address(host.data(), port);
|
||||||
@ -294,13 +294,27 @@ NonBlockingClient::connect(HostString host, uint16_t port) {
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NonBlockingClient::disconnect() {
|
||||||
|
m_lastKnownConStatus = ENOTCONN;
|
||||||
|
close_socket();
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Get last known connection status
|
||||||
|
/// @return - expected(EINPROGRESS) if connection is still being attempted
|
||||||
|
/// - expected(EISCONN) if already connected
|
||||||
|
/// - expected(error code) if an error occurred during connection (e.g.,
|
||||||
|
/// ECONNREFUSED)
|
||||||
|
/// - unexpected(errno) if an error occurred while checking connection
|
||||||
|
/// status
|
||||||
|
///
|
||||||
[[nodiscard]] std::expected<int, int>
|
[[nodiscard]] std::expected<int, int>
|
||||||
NonBlockingClient::get_last_connection_status() {
|
NonBlockingClient::get_last_connection_status() {
|
||||||
if (!m_startedNewConAttempt) return m_lastKnownConStatus;
|
if (!m_startedNewConAttempt) return m_lastKnownConStatus;
|
||||||
|
|
||||||
/// Check if connect operation has been completed
|
/// Check if connect operation has been completed
|
||||||
|
|
||||||
auto pendingOpRes = new_error_codes_available();
|
auto pendingOpRes = socket_has_new_output_event();
|
||||||
if (!pendingOpRes) return std::unexpected{pendingOpRes.error()};
|
if (!pendingOpRes) return std::unexpected{pendingOpRes.error()};
|
||||||
if (!pendingOpRes.value()) return EINPROGRESS;
|
if (!pendingOpRes.value()) return EINPROGRESS;
|
||||||
|
|
||||||
@ -325,33 +339,6 @@ NonBlockingClient::get_last_connection_status() {
|
|||||||
return m_lastKnownConStatus;
|
return m_lastKnownConStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NonBlockingClient::disconnect() {
|
|
||||||
m_lastKnownConStatus = ENOTCONN;
|
|
||||||
close_socket();
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] std::expected<int, int>
|
|
||||||
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.value() != EISCONN) {
|
|
||||||
spdlog::error("tcp::NonBlockingClient: Socket not connected");
|
|
||||||
return std::unexpected{ENOTCONN};
|
|
||||||
}
|
|
||||||
|
|
||||||
const int bytesWritten =
|
|
||||||
::send(m_socket, (const char*)data.data(), data.size(), 0);
|
|
||||||
|
|
||||||
if (bytesWritten <= 0) {
|
|
||||||
spdlog::error("tcp::NonBlockingClient: send() failed with errno={}",
|
|
||||||
errno);
|
|
||||||
return std::unexpected{errno};
|
|
||||||
}
|
|
||||||
|
|
||||||
return bytesWritten;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool NonBlockingClient::data_available() {
|
bool NonBlockingClient::data_available() {
|
||||||
auto conRes = get_last_connection_status();
|
auto conRes = get_last_connection_status();
|
||||||
if (!conRes) return false;
|
if (!conRes) return false;
|
||||||
@ -367,6 +354,20 @@ bool NonBlockingClient::data_available() {
|
|||||||
return (m_pfdIn.revents & POLLIN);
|
return (m_pfdIn.revents & POLLIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] std::expected<int, int>
|
||||||
|
NonBlockingClient::send(std::span<const std::byte> data) {
|
||||||
|
const int bytesWritten =
|
||||||
|
::send(m_socket, (const char*)data.data(), data.size(), 0);
|
||||||
|
|
||||||
|
if (bytesWritten <= 0) {
|
||||||
|
spdlog::error("tcp::NonBlockingClient: send() failed with errno={}",
|
||||||
|
errno);
|
||||||
|
return std::unexpected{errno};
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytesWritten;
|
||||||
|
}
|
||||||
|
|
||||||
[[nodiscard]] std::expected<int, int>
|
[[nodiscard]] std::expected<int, int>
|
||||||
NonBlockingClient::recv(std::span<std::byte> buffer) {
|
NonBlockingClient::recv(std::span<std::byte> buffer) {
|
||||||
const int bytesReceived =
|
const int bytesReceived =
|
||||||
@ -380,27 +381,12 @@ NonBlockingClient::recv(std::span<std::byte> buffer) {
|
|||||||
return {bytesReceived};
|
return {bytesReceived};
|
||||||
}
|
}
|
||||||
|
|
||||||
void NonBlockingClient::close_socket() {
|
|
||||||
if (shutdown(m_socket, 0) < 0) {
|
|
||||||
spdlog::debug("tcp::NonBlockingClient: shutdown() failed with errno={}",
|
|
||||||
errno);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (close(m_socket) < 0) {
|
|
||||||
spdlog::debug("tcp::NonBlockingClient: close() failed with errno={}",
|
|
||||||
errno);
|
|
||||||
}
|
|
||||||
|
|
||||||
m_socket = -1;
|
|
||||||
m_pfdOut.fd = -1;
|
|
||||||
m_pfdIn.fd = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] std::expected<void, int> NonBlockingClient::create_socket() {
|
[[nodiscard]] std::expected<void, int> NonBlockingClient::create_socket() {
|
||||||
m_socket = socket(AF_INET, SOCK_STREAM, 0);
|
m_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (m_socket < 1) {
|
if (m_socket < 1) {
|
||||||
spdlog::error("tcp::NonBlockingClient: socket() failed with errno={}",
|
spdlog::error("tcp::NonBlockingClient: socket() failed with errno={}",
|
||||||
errno);
|
errno);
|
||||||
|
close_socket();
|
||||||
return std::unexpected{errno};
|
return std::unexpected{errno};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -425,8 +411,29 @@ void NonBlockingClient::close_socket() {
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NonBlockingClient::close_socket() {
|
||||||
|
if (shutdown(m_socket, 0) < 0) {
|
||||||
|
spdlog::debug("tcp::NonBlockingClient: shutdown() failed with errno={}",
|
||||||
|
errno);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (close(m_socket) < 0) {
|
||||||
|
spdlog::debug("tcp::NonBlockingClient: close() failed with errno={}",
|
||||||
|
errno);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_socket = -1;
|
||||||
|
m_pfdOut.fd = -1;
|
||||||
|
m_pfdIn.fd = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @return true if there is new output event, i.e., the connect() operation has
|
||||||
|
/// completed (successfully or not)
|
||||||
|
///
|
||||||
[[nodiscard]] std::expected<bool, int>
|
[[nodiscard]] std::expected<bool, int>
|
||||||
NonBlockingClient::new_error_codes_available() {
|
NonBlockingClient::socket_has_new_output_event() {
|
||||||
int ret = poll(&m_pfdOut, 1, 0);
|
int ret = poll(&m_pfdOut, 1, 0);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
spdlog::error("tcp::NonBlockingClient: poll() failed with errno={}",
|
spdlog::error("tcp::NonBlockingClient: poll() failed with errno={}",
|
||||||
|
|||||||
69
src/tcp.hpp
69
src/tcp.hpp
@ -1,24 +1,16 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
|
||||||
|
// STL includes
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <expected>
|
#include <expected>
|
||||||
#include <poll.h>
|
|
||||||
#include <span>
|
#include <span>
|
||||||
#include <spdlog/spdlog.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
|
|
||||||
// C stdlib includes
|
// C stdlib includes
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <expected>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <netdb.h>
|
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <netinet/tcp.h>
|
#include <sys/poll.h>
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
|
|
||||||
namespace tcp {
|
namespace tcp {
|
||||||
@ -26,52 +18,48 @@ namespace tcp {
|
|||||||
|
|
||||||
using HostString = std::array<char, 64>;
|
using HostString = std::array<char, 64>;
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// Non blocking server
|
// Non blocking server
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
|
|
||||||
struct ServerSettings {
|
|
||||||
uint16_t port = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class NonBlockingServer {
|
class NonBlockingServer {
|
||||||
public:
|
public:
|
||||||
NonBlockingServer(ServerSettings settings);
|
NonBlockingServer() = default;
|
||||||
|
NonBlockingServer(const NonBlockingServer&) = delete;
|
||||||
|
NonBlockingServer(NonBlockingServer&&) = delete;
|
||||||
|
NonBlockingServer& operator=(const NonBlockingServer&) = delete;
|
||||||
|
NonBlockingServer& operator=(NonBlockingServer&&) = delete;
|
||||||
~NonBlockingServer();
|
~NonBlockingServer();
|
||||||
|
|
||||||
[[nodiscard]] std::expected<void, int> start_listening();
|
[[nodiscard]] std::expected<void, int> start_listening(uint16_t port);
|
||||||
|
void stop_listening();
|
||||||
|
|
||||||
bool next_client_available();
|
bool next_client_available();
|
||||||
[[nodiscard]] std::expected<void, int> accept_next_client();
|
[[nodiscard]] std::expected<void, int> accept_next_client();
|
||||||
void disconnect();
|
void disconnect_from_client();
|
||||||
|
|
||||||
[[nodiscard]] std::expected<int, int>
|
[[nodiscard]] std::expected<std::array<char, 16>, int>
|
||||||
send(std::span<const std::byte> data) const;
|
get_client_ip_as_string();
|
||||||
|
|
||||||
[[nodiscard]] std::expected<int, int>
|
|
||||||
recv(std::span<std::byte> buffer) const;
|
|
||||||
|
|
||||||
bool data_available();
|
bool data_available();
|
||||||
|
|
||||||
[[nodiscard]] std::expected<std::array<char, 16>, int>
|
[[nodiscard]] std::expected<int, int> send(std::span<const std::byte> data);
|
||||||
get_client_ip_as_string() const;
|
[[nodiscard]] std::expected<int, int> recv(std::span<std::byte> buffer);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ServerSettings m_settings;
|
int m_serverSocket = -1;
|
||||||
const struct sockaddr_in m_serverAddress;
|
|
||||||
int m_serverSocket = -1;
|
|
||||||
|
|
||||||
pollfd m_serverPfdIn = {.fd = -1, .events = POLLIN};
|
pollfd m_serverPfdIn = {.fd = -1, .events = POLLIN};
|
||||||
pollfd m_clientPfdIn = {.fd = -1, .events = POLLIN};
|
pollfd m_clientPfdIn = {.fd = -1, .events = POLLIN};
|
||||||
|
|
||||||
int m_clientSocket = -1;
|
int m_clientSocket = -1;
|
||||||
struct sockaddr m_clientAddress;
|
struct sockaddr m_clientAddress = {0};
|
||||||
socklen_t m_clientAddrLen = sizeof(m_clientAddress);
|
|
||||||
|
|
||||||
|
[[nodiscard]] std::expected<void, int> create_server_socket();
|
||||||
[[nodiscard]] std::expected<void, int> create_socket();
|
|
||||||
void close_server_socket();
|
void close_server_socket();
|
||||||
void close_client_socket();
|
void close_client_socket();
|
||||||
};
|
};
|
||||||
@ -86,12 +74,15 @@ private:
|
|||||||
|
|
||||||
class NonBlockingClient {
|
class NonBlockingClient {
|
||||||
public:
|
public:
|
||||||
|
NonBlockingClient() = default;
|
||||||
|
NonBlockingClient(const NonBlockingClient&) = delete;
|
||||||
|
NonBlockingClient(NonBlockingClient&&) = delete;
|
||||||
|
NonBlockingClient& operator=(const NonBlockingClient&) = delete;
|
||||||
|
NonBlockingClient& operator=(NonBlockingClient&&) = delete;
|
||||||
~NonBlockingClient();
|
~NonBlockingClient();
|
||||||
|
|
||||||
// clang-format off
|
[[nodiscard]] std::expected<void, int> connect(const HostString& host,
|
||||||
[[nodiscard]] std::expected<void, int>
|
uint16_t port);
|
||||||
connect(HostString host, uint16_t port);
|
|
||||||
// clang-format on
|
|
||||||
|
|
||||||
void disconnect();
|
void disconnect();
|
||||||
|
|
||||||
@ -99,8 +90,8 @@ public:
|
|||||||
|
|
||||||
bool data_available();
|
bool data_available();
|
||||||
|
|
||||||
[[nodiscard]] std::expected<int, int> recv(std::span<std::byte> buffer);
|
|
||||||
[[nodiscard]] std::expected<int, int> send(std::span<const std::byte> data);
|
[[nodiscard]] std::expected<int, int> send(std::span<const std::byte> data);
|
||||||
|
[[nodiscard]] std::expected<int, int> recv(std::span<std::byte> buffer);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int m_socket = -1;
|
int m_socket = -1;
|
||||||
@ -110,10 +101,10 @@ private:
|
|||||||
bool m_startedNewConAttempt = false;
|
bool m_startedNewConAttempt = false;
|
||||||
int m_lastKnownConStatus = ENOTCONN;
|
int m_lastKnownConStatus = ENOTCONN;
|
||||||
|
|
||||||
void close_socket();
|
|
||||||
|
|
||||||
[[nodiscard]] std::expected<void, int> create_socket();
|
[[nodiscard]] std::expected<void, int> create_socket();
|
||||||
[[nodiscard]] std::expected<bool, int> new_error_codes_available();
|
void close_socket();
|
||||||
|
|
||||||
|
[[nodiscard]] std::expected<bool, int> socket_has_new_output_event();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
137
tests/tcp.cpp
137
tests/tcp.cpp
@ -1,13 +1,17 @@
|
|||||||
#include "tcp.hpp"
|
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
// STL includes
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
// Project includes
|
||||||
|
#include "tcp.hpp"
|
||||||
|
|
||||||
|
|
||||||
TEST(TcpServer, Accept) {
|
TEST(TcpServer, Accept) {
|
||||||
tcp::NonBlockingServer server{{.port = 1234}};
|
tcp::NonBlockingServer server;
|
||||||
tcp::NonBlockingClient client;
|
tcp::NonBlockingClient client;
|
||||||
|
|
||||||
auto lisRes = server.start_listening();
|
auto lisRes = server.start_listening(1234);
|
||||||
|
|
||||||
EXPECT_FALSE(server.next_client_available());
|
EXPECT_FALSE(server.next_client_available());
|
||||||
|
|
||||||
@ -20,8 +24,86 @@ TEST(TcpServer, Accept) {
|
|||||||
EXPECT_FALSE(server.next_client_available());
|
EXPECT_FALSE(server.next_client_available());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(TcpServer, RestartListening) {
|
||||||
|
tcp::NonBlockingServer server;
|
||||||
|
tcp::NonBlockingClient client;
|
||||||
|
|
||||||
|
auto lisRes = server.start_listening(1234);
|
||||||
|
|
||||||
|
auto conRes = client.connect({"localhost"}, 1234);
|
||||||
|
|
||||||
|
EXPECT_TRUE(server.next_client_available());
|
||||||
|
|
||||||
|
auto lisRes2 = server.start_listening(2345);
|
||||||
|
|
||||||
|
EXPECT_FALSE(server.next_client_available());
|
||||||
|
|
||||||
|
auto conRes2 = client.connect({"localhost"}, 2345);
|
||||||
|
|
||||||
|
EXPECT_TRUE(server.next_client_available());
|
||||||
|
|
||||||
|
auto statRes2 = client.get_last_connection_status();
|
||||||
|
if (!statRes2) return;
|
||||||
|
EXPECT_EQ(statRes2.value(), EISCONN);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(TcpServer, StopListening) {
|
||||||
|
tcp::NonBlockingServer server;
|
||||||
|
tcp::NonBlockingClient client;
|
||||||
|
|
||||||
|
auto lisRes = server.start_listening(1234);
|
||||||
|
|
||||||
|
auto conRes = client.connect({"localhost"}, 1234);
|
||||||
|
|
||||||
|
EXPECT_TRUE(server.next_client_available());
|
||||||
|
|
||||||
|
server.stop_listening();
|
||||||
|
|
||||||
|
EXPECT_FALSE(server.next_client_available());
|
||||||
|
|
||||||
|
auto conRes2 = client.connect({"localhost"}, 1234);
|
||||||
|
|
||||||
|
EXPECT_EQ(client.get_last_connection_status().value(), ECONNREFUSED);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(TcpServer, DataAvailable) {
|
||||||
|
tcp::NonBlockingServer server;
|
||||||
|
tcp::NonBlockingClient client;
|
||||||
|
|
||||||
|
EXPECT_FALSE(server.data_available());
|
||||||
|
|
||||||
|
auto lisRes = server.start_listening(1234);
|
||||||
|
auto conRes = client.connect({"localhost"}, 1234);
|
||||||
|
auto accRes = server.accept_next_client();
|
||||||
|
|
||||||
|
EXPECT_FALSE(server.data_available());
|
||||||
|
|
||||||
|
std::array<char, 14> txMsg = {"Hello, World!"};
|
||||||
|
auto sendRes = client.send(std::as_bytes(std::span(txMsg)));
|
||||||
|
EXPECT_EQ(sendRes.value(), txMsg.size());
|
||||||
|
EXPECT_TRUE(server.data_available());
|
||||||
|
|
||||||
|
std::array<char, 1024> rxBuffer;
|
||||||
|
auto recvRes = server.recv(std::as_writable_bytes(std::span(rxBuffer)));
|
||||||
|
EXPECT_EQ(recvRes.value(), txMsg.size());
|
||||||
|
EXPECT_FALSE(server.data_available());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(TcpServer, GetClientIPAsString) {
|
||||||
|
tcp::NonBlockingServer server;
|
||||||
|
tcp::NonBlockingClient client;
|
||||||
|
|
||||||
|
auto lisRes = server.start_listening(1234);
|
||||||
|
auto conRes = client.connect({"localhost"}, 1234);
|
||||||
|
auto accRes = server.accept_next_client();
|
||||||
|
auto ipRes = server.get_client_ip_as_string();
|
||||||
|
if (!ipRes) return;
|
||||||
|
|
||||||
|
EXPECT_STREQ(ipRes.value().data(), "127.0.0.1");
|
||||||
|
}
|
||||||
|
|
||||||
TEST(TcpClient, Connect) {
|
TEST(TcpClient, Connect) {
|
||||||
tcp::NonBlockingServer server{{.port = 1234}};
|
tcp::NonBlockingServer server;
|
||||||
tcp::NonBlockingClient client;
|
tcp::NonBlockingClient client;
|
||||||
|
|
||||||
/// Default connection status, i.e., before attempting connection
|
/// Default connection status, i.e., before attempting connection
|
||||||
@ -40,7 +122,7 @@ TEST(TcpClient, Connect) {
|
|||||||
|
|
||||||
/// Connection status when connection is acknowledged
|
/// Connection status when connection is acknowledged
|
||||||
|
|
||||||
auto lisRes = server.start_listening();
|
auto lisRes = server.start_listening(1234);
|
||||||
|
|
||||||
auto conRes2 = client.connect({"localhost"}, 1234);
|
auto conRes2 = client.connect({"localhost"}, 1234);
|
||||||
|
|
||||||
@ -58,12 +140,12 @@ TEST(TcpClient, Connect) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(TcpClient, Reconnect) {
|
TEST(TcpClient, Reconnect) {
|
||||||
tcp::NonBlockingServer server1{{.port = 1234}};
|
tcp::NonBlockingServer server1;
|
||||||
tcp::NonBlockingServer server2{{.port = 2345}};
|
tcp::NonBlockingServer server2;
|
||||||
tcp::NonBlockingClient client;
|
tcp::NonBlockingClient client;
|
||||||
|
|
||||||
auto lisRes1 = server1.start_listening();
|
auto lisRes1 = server1.start_listening(1234);
|
||||||
auto lisRes2 = server2.start_listening();
|
auto lisRes2 = server2.start_listening(2345);
|
||||||
|
|
||||||
auto conRes1 = client.connect({"localhost"}, 1234);
|
auto conRes1 = client.connect({"localhost"}, 1234);
|
||||||
|
|
||||||
@ -79,12 +161,12 @@ TEST(TcpClient, Reconnect) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(TcpClient, DataAvailable) {
|
TEST(TcpClient, DataAvailable) {
|
||||||
tcp::NonBlockingServer server{{.port = 1234}};
|
tcp::NonBlockingServer server;
|
||||||
tcp::NonBlockingClient client;
|
tcp::NonBlockingClient client;
|
||||||
|
|
||||||
EXPECT_FALSE(client.data_available());
|
EXPECT_FALSE(client.data_available());
|
||||||
|
|
||||||
auto lisRes = server.start_listening();
|
auto lisRes = server.start_listening(1234);
|
||||||
auto conRes = client.connect({"localhost"}, 1234);
|
auto conRes = client.connect({"localhost"}, 1234);
|
||||||
auto accRes = server.accept_next_client();
|
auto accRes = server.accept_next_client();
|
||||||
|
|
||||||
@ -104,34 +186,11 @@ TEST(TcpClient, DataAvailable) {
|
|||||||
EXPECT_FALSE(client.data_available());
|
EXPECT_FALSE(client.data_available());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(TcpServer, DataAvailable) {
|
|
||||||
tcp::NonBlockingServer server{{.port = 1234}};
|
|
||||||
tcp::NonBlockingClient client;
|
|
||||||
|
|
||||||
EXPECT_FALSE(server.data_available());
|
|
||||||
|
|
||||||
auto lisRes = server.start_listening();
|
|
||||||
auto conRes = client.connect({"localhost"}, 1234);
|
|
||||||
auto accRes = server.accept_next_client();
|
|
||||||
|
|
||||||
EXPECT_FALSE(server.data_available());
|
|
||||||
|
|
||||||
std::array<char, 14> txMsg = {"Hello, World!"};
|
|
||||||
auto sendRes = client.send(std::as_bytes(std::span(txMsg)));
|
|
||||||
EXPECT_EQ(sendRes.value(), txMsg.size());
|
|
||||||
EXPECT_TRUE(server.data_available());
|
|
||||||
|
|
||||||
std::array<char, 1024> rxBuffer;
|
|
||||||
auto recvRes = server.recv(std::as_writable_bytes(std::span(rxBuffer)));
|
|
||||||
EXPECT_EQ(recvRes.value(), txMsg.size());
|
|
||||||
EXPECT_FALSE(server.data_available());
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(TcpClientServer, ClientSendServerReceive) {
|
TEST(TcpClientServer, ClientSendServerReceive) {
|
||||||
tcp::NonBlockingServer server{{.port = 1234}};
|
tcp::NonBlockingServer server;
|
||||||
tcp::NonBlockingClient client;
|
tcp::NonBlockingClient client;
|
||||||
|
|
||||||
auto lisRes = server.start_listening();
|
auto lisRes = server.start_listening(1234);
|
||||||
auto conRes = client.connect({"localhost"}, 1234);
|
auto conRes = client.connect({"localhost"}, 1234);
|
||||||
auto accRes = server.accept_next_client();
|
auto accRes = server.accept_next_client();
|
||||||
|
|
||||||
@ -148,10 +207,10 @@ TEST(TcpClientServer, ClientSendServerReceive) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(TcpClientServer, ClientReceiveServerSend) {
|
TEST(TcpClientServer, ClientReceiveServerSend) {
|
||||||
tcp::NonBlockingServer server{{.port = 1234}};
|
tcp::NonBlockingServer server;
|
||||||
tcp::NonBlockingClient client;
|
tcp::NonBlockingClient client;
|
||||||
|
|
||||||
auto lisRes = server.start_listening();
|
auto lisRes = server.start_listening(1234);
|
||||||
auto conRes = client.connect({"localhost"}, 1234);
|
auto conRes = client.connect({"localhost"}, 1234);
|
||||||
auto accRes = server.accept_next_client();
|
auto accRes = server.accept_next_client();
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user