Reorder function definitions; Rename function
This commit is contained in:
parent
1c91b7a1d7
commit
4ffd828655
112
src/tcp.cpp
112
src/tcp.cpp
@ -30,7 +30,7 @@ std::expected<sockaddr_in, int> resolve_remote_address(const char* host,
|
||||
return address;
|
||||
}
|
||||
|
||||
inline auto get_local_address(uint16_t port) -> sockaddr_in {
|
||||
inline sockaddr_in get_local_address(uint16_t port) {
|
||||
sockaddr_in address{};
|
||||
address.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
address.sin_family = AF_INET;
|
||||
@ -62,7 +62,7 @@ std::expected<void, int> NonBlockingServer::start_listening(uint16_t port) {
|
||||
/// Create socket
|
||||
|
||||
close_server_socket();
|
||||
if (auto res = create_socket(); !res) {
|
||||
if (auto res = create_server_socket(); !res) {
|
||||
return std::unexpected{res.error()};
|
||||
}
|
||||
|
||||
@ -113,14 +113,11 @@ bool NonBlockingServer::next_client_available() {
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void NonBlockingServer::disconnect() {
|
||||
close_client_socket();
|
||||
}
|
||||
|
||||
// TODO: Do we need this?
|
||||
// bool NonBlockingServer::socket_closed() const {
|
||||
// }
|
||||
|
||||
[[nodiscard]] std::expected<std::array<char, 16>, int>
|
||||
NonBlockingServer::get_client_ip_as_string() {
|
||||
std::array<char, 16> addrStr;
|
||||
@ -140,6 +137,17 @@ NonBlockingServer::get_client_ip_as_string() {
|
||||
return addrStr;
|
||||
}
|
||||
|
||||
bool NonBlockingServer::data_available() {
|
||||
int ret = poll(&m_clientPfdIn, 1, 0);
|
||||
if (ret < 0) {
|
||||
spdlog::error("tcp::NonBlockingServer: poll() failed with errno={}",
|
||||
errno);
|
||||
return false;
|
||||
}
|
||||
|
||||
return (m_clientPfdIn.revents & POLLIN);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<int, int>
|
||||
NonBlockingServer::send(std::span<const std::byte> data) {
|
||||
const int bytesWritten =
|
||||
@ -167,18 +175,8 @@ NonBlockingServer::recv(std::span<std::byte> buffer) {
|
||||
return {bytesReceived};
|
||||
}
|
||||
|
||||
bool NonBlockingServer::data_available() {
|
||||
int ret = poll(&m_clientPfdIn, 1, 0);
|
||||
if (ret < 0) {
|
||||
spdlog::error("tcp::NonBlockingServer: poll() failed with errno={}",
|
||||
errno);
|
||||
return false;
|
||||
}
|
||||
|
||||
return (m_clientPfdIn.revents & POLLIN);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<void, int> NonBlockingServer::create_socket() {
|
||||
[[nodiscard]] std::expected<void, int>
|
||||
NonBlockingServer::create_server_socket() {
|
||||
m_serverSocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (m_serverSocket < 1) {
|
||||
spdlog::error("tcp::NonBlockingServer: socket() failed with errno={}",
|
||||
@ -288,6 +286,11 @@ NonBlockingClient::connect(const HostString& host, uint16_t port) {
|
||||
return {};
|
||||
}
|
||||
|
||||
void NonBlockingClient::disconnect() {
|
||||
m_lastKnownConStatus = ENOTCONN;
|
||||
close_socket();
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<int, int>
|
||||
NonBlockingClient::get_last_connection_status() {
|
||||
if (!m_startedNewConAttempt) return m_lastKnownConStatus;
|
||||
@ -319,33 +322,6 @@ NonBlockingClient::get_last_connection_status() {
|
||||
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() {
|
||||
auto conRes = get_last_connection_status();
|
||||
if (!conRes) return false;
|
||||
@ -361,6 +337,20 @@ bool NonBlockingClient::data_available() {
|
||||
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>
|
||||
NonBlockingClient::recv(std::span<std::byte> buffer) {
|
||||
const int bytesReceived =
|
||||
@ -374,22 +364,6 @@ NonBlockingClient::recv(std::span<std::byte> buffer) {
|
||||
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() {
|
||||
m_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (m_socket < 1) {
|
||||
@ -419,6 +393,22 @@ void NonBlockingClient::close_socket() {
|
||||
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;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<bool, int>
|
||||
NonBlockingClient::socket_has_new_output_event() {
|
||||
int ret = poll(&m_pfdOut, 1, 0);
|
||||
|
||||
@ -58,7 +58,7 @@ private:
|
||||
int m_clientSocket = -1;
|
||||
struct sockaddr m_clientAddress = {0};
|
||||
|
||||
[[nodiscard]] std::expected<void, int> create_socket();
|
||||
[[nodiscard]] std::expected<void, int> create_server_socket();
|
||||
void close_server_socket();
|
||||
void close_client_socket();
|
||||
};
|
||||
@ -89,8 +89,8 @@ public:
|
||||
|
||||
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> recv(std::span<std::byte> buffer);
|
||||
|
||||
private:
|
||||
int m_socket = -1;
|
||||
@ -100,9 +100,9 @@ private:
|
||||
bool m_startedNewConAttempt = false;
|
||||
int m_lastKnownConStatus = ENOTCONN;
|
||||
|
||||
void close_socket();
|
||||
|
||||
[[nodiscard]] std::expected<void, int> create_socket();
|
||||
void close_socket();
|
||||
|
||||
[[nodiscard]] std::expected<bool, int> socket_has_new_output_event();
|
||||
};
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user