Fix bugs; Add doc comments
This commit is contained in:
parent
4ffd828655
commit
b22150de94
21
src/tcp.cpp
21
src/tcp.cpp
@ -39,7 +39,6 @@ inline sockaddr_in get_local_address(uint16_t port) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
@ -74,14 +73,14 @@ std::expected<void, int> NonBlockingServer::start_listening(uint16_t port) {
|
||||
sizeof(serverAddress)) != 0) {
|
||||
spdlog::error("tcp::NonBlockingServer: bind() failed with errno={}",
|
||||
errno);
|
||||
close(m_serverSocket);
|
||||
close_server_socket();
|
||||
return std::unexpected{errno};
|
||||
};
|
||||
|
||||
if (::listen(m_serverSocket, 1) != 0) {
|
||||
spdlog::error("tcp::NonBlockingServer: listen() failed with errno={}",
|
||||
errno);
|
||||
close(m_serverSocket);
|
||||
close_server_socket();
|
||||
return std::unexpected{errno};
|
||||
}
|
||||
|
||||
@ -181,6 +180,7 @@ NonBlockingServer::create_server_socket() {
|
||||
if (m_serverSocket < 1) {
|
||||
spdlog::error("tcp::NonBlockingServer: socket() failed with errno={}",
|
||||
errno);
|
||||
close_server_socket();
|
||||
return std::unexpected{errno};
|
||||
}
|
||||
|
||||
@ -291,6 +291,15 @@ void NonBlockingClient::disconnect() {
|
||||
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>
|
||||
NonBlockingClient::get_last_connection_status() {
|
||||
if (!m_startedNewConAttempt) return m_lastKnownConStatus;
|
||||
@ -369,6 +378,7 @@ NonBlockingClient::recv(std::span<std::byte> buffer) {
|
||||
if (m_socket < 1) {
|
||||
spdlog::error("tcp::NonBlockingClient: socket() failed with errno={}",
|
||||
errno);
|
||||
close_socket();
|
||||
return std::unexpected{errno};
|
||||
}
|
||||
|
||||
@ -409,6 +419,11 @@ void NonBlockingClient::close_socket() {
|
||||
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>
|
||||
NonBlockingClient::socket_has_new_output_event() {
|
||||
int ret = poll(&m_pfdOut, 1, 0);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user