Skip to content
Snippets Groups Projects
Commit c8bbd484 authored by Benny Baumann's avatar Benny Baumann
Browse files

fix: Handle errors when calling fcntl

parent 87d33bd1
No related branches found
No related tags found
1 merge request!1First unit tests
......@@ -126,7 +126,13 @@ tcp_client::tcp_client(
if (connect(socket_candidate.get(), socket_identifier.ptr(), socket_identifier.size()) == 0) {
status = 0;
this->net_socket = std::forward<auto_fd>(socket_candidate);
fcntl(this->net_socket.get(), F_SETFL, fcntl(this->net_socket.get(), F_GETFL, 0) | O_NONBLOCK);
if (
const auto existing_fd_flags = fcntl(this->net_socket.get(), F_GETFL, 0);
existing_fd_flags == -1 || fcntl(this->net_socket.get(), F_SETFL, existing_fd_flags | O_NONBLOCK) == -1
) {
throw netio_exception("Failed to set socket mode. fcntl resulted in error:" + std::to_string(errno));
}
// Hier bin ich mir nicht sicher, wie ich das am besten mache. Auch mit socketaddr und type cast ist das irgendwie doof.
// Das Problem besteht darin, dass erst nach erfolgreichem connect der Port auf dieser Seite bekannt ist.
......
......@@ -105,7 +105,13 @@ void tcp_server_socket::await_raw_socket_incomming(
throw netio_exception("Unable to bind incoming client");
}
fcntl(client_fd_raw, F_SETFL, fcntl(client_fd_raw, F_GETFL, 0) | O_NONBLOCK);
auto flags = O_NONBLOCK;
if (
const auto existing_fd_flags = fcntl(client_fd_raw, F_GETFL, 0);
existing_fd_flags == -1 || fcntl(client_fd_raw, F_SETFL, existing_fd_flags | flags) == -1
) {
throw netio_exception("Failed to set socket mode. fcntl resulted in error:" + std::to_string(errno));
}
const std::string address = inet_ntoa(client_addr.sin_addr);
const uint16_t port = ntohs(client_addr.sin_port);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment