Skip to content

Commit

Permalink
chore(Socket): removed Socket exceptions
Browse files Browse the repository at this point in the history
- replaced exception with THROW
- moved ServerConfig serverAddr assignment to initializer list
- changed std::exception e to std::exception error
  • Loading branch information
Taanviir committed Apr 24, 2024
1 parent ae7d6e2 commit 297e4fc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 78 deletions.
8 changes: 4 additions & 4 deletions sources/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ void Server::handle_connection(fd incoming, fd_set& activeSockets)
delete handler;
} catch (std::ios_base::failure& f) {
DEBUG_MSG(ERR_PARSE, R);
} catch (std::exception& e) {
} catch (std::exception& error) {
ConnectionManager::remove_connection(incoming, activeSockets);
DEBUG_MSG(e.what(), R);
DEBUG_MSG(error.what(), R);
}
}

Expand Down Expand Up @@ -219,14 +219,14 @@ void Server::kqueue_strat()
delete handler;
} catch (std::ios_base::failure& f) {
DEBUG_MSG(ERR_PARSE, R);
} catch (std::exception& e) {
} catch (std::exception& error) {
EV_SET(&changeList, eventList[i].ident, EVFILT_READ, EV_DELETE, 0, 0,
0);
if (kevent(kq, &changeList, 1, NULL, 0, NULL) == -1) {
close(kq);
THROW_EXCEPTION_WITH_INFO(strerror(errno));
}
DEBUG_MSG(e.what(), R);
DEBUG_MSG(error.what(), R);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions sources/server/ServerConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ struct ServerConfig {
bool autoindex;
in_addr_t serverAddr;

ServerConfig() : listenerPort(8080), maxBodySize(1000000), autoindex(false)
{
serverAddr = htonl(INADDR_ANY);
}
ServerConfig()
: listenerPort(8080), maxBodySize(1000000), autoindex(false),
serverAddr(htonl(INADDR_ANY))
{}

void print(void) const
{
Expand Down
64 changes: 8 additions & 56 deletions sources/server/socket/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Socket::Socket(int family, int type, int protocol, int flags)
int option = 1;
setsockopt(socketFD, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
if (socketFD == invalidFD)
throw Socket::Exception(ERR_CREAT);
THROW_EXCEPTION_WITH_INFO(ERR_CREAT);

std::memset(&address, 0, sizeof(address));

Expand All @@ -46,7 +46,7 @@ Socket::Socket(int family, int type, int protocol, int flags)
*
* @return void
*/
Socket::~Socket() throw()
Socket::~Socket()
{
if (socketFD != invalidFD) {
DEBUG_MSG("socket fd[" << socketFD << "] closed!!", R);
Expand All @@ -57,8 +57,6 @@ Socket::~Socket() throw()

/* -------------------------------- Interface ------------------------------- */

#define MAX_PORT 65535

/**
* Sets the port for the Socket object.
*
Expand All @@ -71,15 +69,12 @@ Socket::~Socket() throw()
*/
void Socket::set_port(int port)
{
if (port < 0 || port > MAX_PORT)
throw Socket::Exception("Invalid Socket descriptor\n");

// cast address to socketaddr_in and set sin_port
((struct sockaddr_in*) (&address))->sin_port = htons(port);
}


fd Socket::get_fd() const throw()
fd Socket::get_fd() const
{
return socketFD;
}
Expand All @@ -95,7 +90,7 @@ fd Socket::get_fd() const throw()
void Socket::bind() const
{
if (::bind(socketFD, (struct sockaddr*) &address, sizeof(address)) == -1)
throw Socket::Exception(ERR_BIND);
THROW_EXCEPTION_WITH_INFO(ERR_BIND);
isBound = true;
}

Expand All @@ -109,10 +104,10 @@ void Socket::bind() const
void Socket::listen(int backlog) const
{
if (!isBound)
throw Socket::Exception(ERR_NBIND);
THROW_EXCEPTION_WITH_INFO(ERR_NBIND);

if (::listen(socketFD, backlog) == -1)
throw Socket::Exception(ERR_LIST);
THROW_EXCEPTION_WITH_INFO(ERR_LIST);
isListening = true;
}

Expand All @@ -131,7 +126,7 @@ void Socket::listen(int backlog) const
fd Socket::accept()
{
if (!isListening)
throw Socket::Exception(ERR_NLIST);
THROW_EXCEPTION_WITH_INFO(ERR_NLIST);

// This structure is filled in with the address of the peer socket,
struct sockaddr peerInfo; // does this need to be stored?
Expand All @@ -145,50 +140,7 @@ fd Socket::accept()
if (newSocket == invalidFD) {
// if set to non_blocking it returns EAGAIN or EWOULDBLOCK if no connection
if (errno != EAGAIN && errno != EWOULDBLOCK)
throw Socket::Exception(ERR_ACCP);
THROW_EXCEPTION_WITH_INFO(ERR_ACCP);
}
return newSocket;
}

/* ---------------------------- Socket Exception ---------------------------- */

/**
* @brief Constructor for the Socket Exception class
*
* This constructor initializes a new instance of the Socket Exception class with the
* provided error message.
*
* @param error_message The error message associated with the exception
* @return A new instance of the Socket Exception class
*
* @throws None
*/
Socket::Exception::Exception(const string& error_message)
{
this->error_message = compose_msg(error_message);
};

/**
* @brief Returns a C-style string describing the error message associated with this
* exception.
*
* @return const char* a C-style string containing the error message.
*/
const char* Socket::Exception::what() const throw()
{
return error_message.c_str();
}

/**
* Composes an error message by combining the provided message with the system error
* message corresponding to the current value of errno.
*
* @param message The message to be included in the error message.
* @return A string containing the composed error message.
*/
string Socket::Exception::compose_msg(const string& message)
{
stringstream _msg;
_msg << message << (errno ? ": " + string(strerror(errno)) : "");
return _msg.str();
}
16 changes: 2 additions & 14 deletions sources/server/socket/Socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,14 @@ static const int SOCK_FLAG = 0;
*/
class Socket {
public:
virtual ~Socket() throw();
virtual ~Socket();
/* -------------------------------- Interface ------------------------------- */
void set_port(int port);
fd get_fd() const throw();
fd get_fd() const;
void bind() const;
void listen(int backlog) const;
fd accept();

/* ---------------------------- Socket Exception ---------------------------- */
class Exception: public std::exception {
public:
explicit Exception(const string& error_message);
~Exception() throw() {};
const char* what() const throw();

private:
string error_message;
string compose_msg(const string& message);
};

protected:
/* ------------------------------- Constructor ------------------------------ */
Socket(int family, int type, int protocol, int flags);
Expand Down

0 comments on commit 297e4fc

Please sign in to comment.