Commit 7d72cacd authored by knowledge4igor's avatar knowledge4igor

Improve error handling on accept call

parent a7ed13fa
#pragma once
#include <stdexcept>
namespace Pistache {
namespace Tcp {
class SocketError: public std::runtime_error
{
public:
explicit SocketError(const char* what_arg) : std::runtime_error(what_arg)
{ }
};
class ServerError: public std::runtime_error
{
public:
explicit ServerError(const char* what_arg) : std::runtime_error(what_arg)
{ }
};
} // Tcp
} // Pistache
\ No newline at end of file
......@@ -8,6 +8,7 @@
#include <pistache/common.h>
#include <pistache/os.h>
#include <pistache/transport.h>
#include <pistache/errors.h>
#include <sys/socket.h>
#include <netinet/in.h>
......@@ -225,8 +226,18 @@ Listener::run() {
else {
if (event.flags.hasFlag(Polling::NotifyOn::Read)) {
auto fd = event.tag.value();
if (static_cast<ssize_t>(fd) == listen_fd)
handleNewConnection();
if (static_cast<ssize_t>(fd) == listen_fd) {
try {
handleNewConnection();
}
catch (SocketError& ex) {
std::cerr << "Server: " << ex.what() << std::endl;
}
catch (ServerError& ex) {
std::cerr << "Server: " << ex.what() << std::endl;
throw;
}
}
}
}
}
......@@ -313,7 +324,10 @@ Listener::handleNewConnection() {
socklen_t peer_addr_len = sizeof(peer_addr);
int client_fd = ::accept(listen_fd, (struct sockaddr *)&peer_addr, &peer_addr_len);
if (client_fd < 0) {
throw std::runtime_error(strerror(errno));
if (errno == EBADF || errno == ENOTSOCK)
throw ServerError(strerror(errno));
else
throw SocketError(strerror(errno));
}
make_non_blocking(client_fd);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment