Skip to content
This repository has been archived by the owner on Apr 6, 2019. It is now read-only.

Commit

Permalink
make __TACOPIE_THROW calls __TACOPIE_LOG
Browse files Browse the repository at this point in the history
  • Loading branch information
Cylix committed Dec 11, 2016
1 parent 1b037a4 commit a30083c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 65 deletions.
5 changes: 4 additions & 1 deletion includes/tacopie/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ class tacopie_error : public std::runtime_error {
} //! tacopie

//! macro for convenience
#define __TACOPIE_THROW(what) throw tacopie::tacopie_error((what), __FILE__, __LINE__);
#define __TACOPIE_THROW(level, what) { \
__TACOPIE_LOG(level, (what)); \
throw tacopie::tacopie_error((what), __FILE__, __LINE__); \
}
6 changes: 2 additions & 4 deletions sources/network/io_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ io_service::io_service(void)
{
__TACOPIE_LOG(debug, "create io_service");

if (pipe(m_notif_pipe_fds) == -1) {
__TACOPIE_LOG(error, "pipe() failure");
__TACOPIE_THROW("io_service::io_service: pipe() failure");
}
if (pipe(m_notif_pipe_fds) == -1)
{ __TACOPIE_THROW(error, "pipe() failure"); }

//! Start worker after everything has been initialized
m_poll_worker = std::thread(std::bind(&io_service::poll, this));
Expand Down
18 changes: 6 additions & 12 deletions sources/network/tcp_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ tcp_client::tcp_client(const tcp_socket& socket)

void
tcp_client::connect(const std::string& host, std::uint32_t port) {
if (is_connected()) {
__TACOPIE_LOG(warn, "tcp_client is already connected");
__TACOPIE_THROW("tcp_client::connect: tcp_client is already connected");
}
if (is_connected())
{ __TACOPIE_THROW(warn, "tcp_client is already connected"); }

m_socket.connect(host, port);
m_io_service->track(m_socket);
Expand Down Expand Up @@ -185,10 +183,8 @@ tcp_client::process_write(write_result& result) {

void
tcp_client::async_read(const read_request& request) {
if (not is_connected()) {
__TACOPIE_LOG(warn, "tcp_client is not connected");
__TACOPIE_THROW("tcp_client::async_read: tcp_client is disconnected");
}
if (not is_connected())
{ __TACOPIE_THROW(warn, "tcp_client is disconnected"); }

std::lock_guard<std::mutex> lock(m_read_requests_mtx);

Expand All @@ -200,10 +196,8 @@ tcp_client::async_read(const read_request& request) {

void
tcp_client::async_write(const write_request& request) {
if (not is_connected()) {
__TACOPIE_LOG(warn, "tcp_client is not connected");
__TACOPIE_THROW("tcp_client::async_write: tcp_client is disconnected");
}
if (not is_connected())
{ __TACOPIE_THROW(warn, "tcp_client is disconnected"); }

std::lock_guard<std::mutex> lock(m_write_requests_mtx);

Expand Down
6 changes: 2 additions & 4 deletions sources/network/tcp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ tcp_server::~tcp_server(void) {

void
tcp_server::start(const std::string& host, std::uint32_t port, const on_new_connection_callback_t& callback) {
if (is_running()) {
__TACOPIE_LOG(warn, "tcp_server is already running");
__TACOPIE_THROW("tcp_server::start: tcp_server is already running");
}
if (is_running())
{ __TACOPIE_THROW(warn, "tcp_server is already running"); }

m_socket.bind(host, port);
m_socket.listen(__TACOPIE_CONNECTION_QUEUE_SIZE);
Expand Down
66 changes: 22 additions & 44 deletions sources/network/tcp_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,11 @@ tcp_socket::recv(std::size_t size_to_read) {

ssize_t rd_size = ::recv(m_fd, const_cast<char*>(data.data()), size_to_read, 0);

if (rd_size == -1) {
__TACOPIE_LOG(error, "create recv() failure");
__TACOPIE_THROW("tcp_socket::recv: recv() failure");
}
if (rd_size == -1)
{ __TACOPIE_THROW(error, "recv() failure"); }

if (rd_size == 0) {
__TACOPIE_LOG(warn, "socket closed by remote host");
__TACOPIE_THROW("tcp_socket::recv: nothing to read, socket has been closed by remote host");
}
if (rd_size == 0)
{ __TACOPIE_THROW(warn, "nothing to read, socket has been closed by remote host"); }

return data;
}
Expand All @@ -67,10 +63,8 @@ tcp_socket::send(const std::vector<char>& data, std::size_t size_to_write) {

ssize_t wr_size = ::send(m_fd, data.data(), size_to_write, 0);

if (wr_size == -1) {
__TACOPIE_LOG(error, "send failure");
__TACOPIE_THROW("tcp_socket::send: send() failure");
}
if (wr_size == -1)
{ __TACOPIE_THROW(error, "send() failure"); }

return wr_size;
}
Expand All @@ -82,21 +76,17 @@ tcp_socket::connect(const std::string& host, std::uint32_t port) {

struct hostent* addr = gethostbyname(host.c_str());

if (not addr) {
__TACOPIE_LOG(error, "gethostbyname() failure");
__TACOPIE_THROW("tcp_socket::bind: gethostbyname() failure");
}
if (not addr)
{ __TACOPIE_THROW(error, "gethostbyname() failure"); }

struct sockaddr_in server_addr;
std::memset(&server_addr, 0, sizeof(server_addr));
std::memcpy(&server_addr.sin_addr.s_addr, addr->h_addr, addr->h_length);
server_addr.sin_port = htons(port);
server_addr.sin_family = AF_INET;

if (::connect(m_fd, (const struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
__TACOPIE_LOG(error, "connect() failure");
__TACOPIE_THROW("tcp_socket::bind: connect() failure");
}
if (::connect(m_fd, (const struct sockaddr *)&server_addr, sizeof(server_addr)) == -1)
{ __TACOPIE_THROW(error, "connect() failure"); }
}

//!
Expand All @@ -110,32 +100,26 @@ tcp_socket::bind(const std::string& host, std::uint32_t port) {

struct hostent* addr = gethostbyname(host.c_str());

if (not addr) {
__TACOPIE_LOG(error, "gethostbyname() failure");
__TACOPIE_THROW("tcp_socket::bind: gethostbyname() failure");
}
if (not addr)
{ __TACOPIE_THROW(error, "gethostbyname() failure"); }

struct sockaddr_in server_addr;
std::memset(&server_addr, 0, sizeof(server_addr));
std::memcpy(&server_addr.sin_addr.s_addr, addr->h_addr, addr->h_length);
server_addr.sin_port = htons(port);
server_addr.sin_family = AF_INET;

if (::bind(m_fd, (const struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
__TACOPIE_LOG(error, "bind() failure");
__TACOPIE_THROW("tcp_socket::bind: bind() failure");
}
if (::bind(m_fd, (const struct sockaddr *)&server_addr, sizeof(server_addr)) == -1)
{ __TACOPIE_THROW(error, "bind() failure"); }
}

void
tcp_socket::listen(std::size_t max_connection_queue) {
create_socket_if_necessary();
check_or_set_type(type::SERVER);

if (::listen(m_fd, max_connection_queue) == -1) {
__TACOPIE_LOG(debug, "listen() failure");
__TACOPIE_THROW("tcp_socket::listen: listen() failure");
}
if (::listen(m_fd, max_connection_queue) == -1)
{ __TACOPIE_THROW(debug, "listen() failure"); }
}

tcp_socket
Expand All @@ -148,10 +132,8 @@ tcp_socket::accept(void) {

fd_t client_fd = ::accept(m_fd, (struct sockaddr *)&client_info, &client_info_struct_size);

if (client_fd == -1) {
__TACOPIE_LOG(error, "accept failure");
__TACOPIE_THROW("tcp_socket::accept: accept() failure");
}
if (client_fd == -1)
{ __TACOPIE_THROW(error, "accept() failure"); }

//! TODO: init with real client addr
return { client_fd, "", client_info.sin_port, type::CLIENT };
Expand Down Expand Up @@ -185,10 +167,8 @@ tcp_socket::create_socket_if_necessary(void) {
m_fd = socket(AF_INET, SOCK_STREAM, 0);
m_type = type::UNKNOWN;

if (m_fd == -1) {
__TACOPIE_LOG(error, "socket() failure");
__TACOPIE_THROW("tcp_socket::create_socket_if_necessary: socket() failure");
}
if (m_fd == -1)
{ __TACOPIE_THROW(error, "tcp_socket::create_socket_if_necessary: socket() failure"); }
}

//!
Expand All @@ -198,10 +178,8 @@ tcp_socket::create_socket_if_necessary(void) {

void
tcp_socket::check_or_set_type(type t) {
if (m_type != type::UNKNOWN and m_type != t) {
__TACOPIE_LOG(error, "trying to perform invalid operation on socket");
__TACOPIE_THROW("tcp_socket::check_or_set_type: trying to perform invalid operation on socket");
}
if (m_type != type::UNKNOWN and m_type != t)
{ __TACOPIE_THROW(error, "trying to perform invalid operation on socket"); }

m_type = t;
}
Expand Down

0 comments on commit a30083c

Please sign in to comment.