Skip to content

Commit

Permalink
better non-blocking, better accept error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
FreddyMSchubert committed Dec 19, 2024
1 parent 95287a3 commit 72ed5c4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
2 changes: 2 additions & 0 deletions include/Socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ class Socket
void redirectToError(int error_code);
void sendData(Response &response);
void sendData(std::string data);

void setNonBlocking();
};
8 changes: 7 additions & 1 deletion src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ void Server::acceptNewConnections()
{
if (!_listening_socket.states.read)
return;
while (true)
int max_iterations = 10;
while (max_iterations > 0)
{
struct sockaddr_in client_addr;
socklen_t addrlen = sizeof(struct sockaddr_in);
Expand All @@ -90,9 +91,14 @@ void Server::acceptNewConnections()
{
Logger::Log(LogLevel::INFO, "New client connected");
_sockets.emplace_back(client_fd, Socket(_config, client_fd));
max_iterations--;
}
else
{
if (errno != EAGAIN && errno != EWOULDBLOCK) // i believe accept is not an i/o operation
Logger::Log(LogLevel::ERROR, "Accept error: " + std::string(strerror(errno)));
break;
}
}
}

Expand Down
21 changes: 11 additions & 10 deletions src/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,7 @@ Socket::Socket(Config &config) : _config(config)
}

Logger::Log(LogLevel::INFO, "Socket " + std::to_string(_socket_fd) + " connected!");

// set non-blocking
int flags = fcntl(_socket_fd, F_GETFL, 0);
if (flags == -1)
throw std::runtime_error("Failed to get socket " + std::to_string(_socket_fd) + " flags");
if (fcntl(_socket_fd, F_SETFL, flags | O_NONBLOCK) == -1)
throw std::runtime_error("Failed to set non-blocking mode on socket " + std::to_string(_socket_fd));
setNonBlocking();
}
catch(const std::exception &e)
{
Expand All @@ -57,6 +51,7 @@ Socket::Socket(Config &config) : _config(config)
Socket::Socket(Config &config, int fd) : _config(config)
{
_socket_fd = fd;
setNonBlocking();
Logger::Log(LogLevel::INFO, "Running Client Connection Socket " + std::to_string(_socket_fd) + "...");
}

Expand Down Expand Up @@ -98,12 +93,10 @@ void Socket::sendData(std::string data)
else
Logger::Log(LogLevel::INFO, "Data sent!");
}

void Socket::sendData(Response &response)
{
sendData(response.getRawPacket());
}

std::string Socket::receiveData()
{
std::string data;
Expand Down Expand Up @@ -137,7 +130,6 @@ void Socket::sendRedirect(const std::string& new_url)
sendData(responsePacket);
Logger::Log(LogLevel::INFO, "Redirected client to " + new_url);
}

void Socket::redirectToError(int error_code)
{
// 1. Noel's memery
Expand Down Expand Up @@ -234,3 +226,12 @@ void Socket::redirectToError(int error_code)
Logger::Log(LogLevel::INFO, "Redirecting client to " + website + " with code " + std::to_string(error_code) + ".");
sendRedirect(website);
}

void Socket::setNonBlocking()
{
int flags = fcntl(_socket_fd, F_GETFL, 0);
if (flags == -1)
throw std::runtime_error("Failed to get socket " + std::to_string(_socket_fd) + " flags");
if (fcntl(_socket_fd, F_SETFL, flags | O_NONBLOCK) == -1)
throw std::runtime_error("Failed to set non-blocking mode on socket " + std::to_string(_socket_fd));
}
1 change: 1 addition & 0 deletions todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
- [ ] run testers and check that they run through. A
- not actually required but maybe it finds somethign deasastrous, evals might use them as well
- [ ] go through eval sheet A
- [ ] test using siege A

0 comments on commit 72ed5c4

Please sign in to comment.