diff --git a/src/communication/bolt/v1/transport/bolt_decoder.cpp b/src/communication/bolt/v1/transport/bolt_decoder.cpp index 96248a8fc..973ce5989 100644 --- a/src/communication/bolt/v1/transport/bolt_decoder.cpp +++ b/src/communication/bolt/v1/transport/bolt_decoder.cpp @@ -90,7 +90,7 @@ std::string BoltDecoder::read_string() size = marker & 0x0F; } // if the marker is 0xD0, size is an 8-bit unsigned integer - if (marker == pack::String8) { + else if (marker == pack::String8) { size = read_byte(); } // if the marker is 0xD1, size is a 16-bit big-endian unsigned integer diff --git a/src/io/network/test.cpp b/src/io/network/test.cpp index f79be771a..a69ab52a8 100644 --- a/src/io/network/test.cpp +++ b/src/io/network/test.cpp @@ -1,15 +1,19 @@ #include <iostream> -#include <vector> -#include <thread> #include <signal.h> - -#include "debug/log.hpp" +#include <thread> +#include <vector> #include "http/request.hpp" #include "http/response.hpp" -#include "socket.hpp" #include "http/worker.hpp" +#include "socket.hpp" + +#ifndef NDEBUG +#define LOG_DEBUG(x) std::cout << x << std::endl; +#else +#define LOG_DEBUG(x) +#endif std::hash<std::thread::id> hash; @@ -18,12 +22,9 @@ constexpr unsigned K = 128; std::array<http::Parser<http::Request, http::Response>, K> workers; std::array<std::thread, K> threads; -std::atomic<bool> alive { true }; +std::atomic<bool> alive{true}; -void exiting() -{ - LOG_DEBUG("Exiting..."); -} +void exiting() { LOG_DEBUG("Exiting..."); } void sigint_handler(int) { @@ -36,15 +37,15 @@ void sigint_handler(int) int main(void) { - //std::atexit(exiting); + // std::atexit(exiting); signal(SIGINT, sigint_handler); - for(size_t i = 0; i < workers.size(); ++i) + for (size_t i = 0; i < workers.size(); ++i) { - auto& w = workers[i]; + auto &w = workers[i]; threads[i] = std::thread([i, &w]() { - while(alive) + while (alive) { LOG_DEBUG("waiting for events on thread " << i); w.wait_and_process_events(); @@ -69,71 +70,70 @@ int main(void) socket.set_non_blocking(); socket.listen(1024); - int efd, s; - struct epoll_event event; - struct epoll_event *events; + int efd, s; + struct epoll_event event; + struct epoll_event *events; - efd = epoll_create1 (0); - if (efd == -1) + efd = epoll_create1(0); + if (efd == -1) { - perror ("epoll_create"); - abort (); + perror("epoll_create"); + abort(); } - event.data.fd = socket; - event.events = EPOLLIN | EPOLLET; - s = epoll_ctl (efd, EPOLL_CTL_ADD, socket, &event); - if (s == -1) + event.data.fd = socket; + event.events = EPOLLIN | EPOLLET; + s = epoll_ctl(efd, EPOLL_CTL_ADD, socket, &event); + if (s == -1) { - perror ("epoll_ctl"); - abort (); + perror("epoll_ctl"); + abort(); } - /* Buffer where events are returned */ - events = static_cast<struct epoll_event*>(calloc (MAXEVENTS, sizeof event)); + /* Buffer where events are returned */ + events = static_cast<struct epoll_event *>(calloc(MAXEVENTS, sizeof event)); - /* The event loop */ - while (1) + /* The event loop */ + while (1) { - int n, i; + int n, i; - LOG_DEBUG("acceptor waiting for events"); - n = epoll_wait (efd, events, MAXEVENTS, -1); + LOG_DEBUG("acceptor waiting for events"); + n = epoll_wait(efd, events, MAXEVENTS, -1); - LOG_DEBUG("acceptor recieved " << n << " connection requests"); + LOG_DEBUG("acceptor recieved " << n << " connection requests"); - for (i = 0; i < n; i++) - { - if ((events[i].events & EPOLLERR) || - (events[i].events & EPOLLHUP) || - (!(events[i].events & EPOLLIN))) - { - /* An error has occured on this fd, or the socket is not - ready for reading (why were we notified then?) */ - fprintf (stderr, "epoll error\n"); - close (events[i].data.fd); - continue; - } + for (i = 0; i < n; i++) + { + if ((events[i].events & EPOLLERR) || + (events[i].events & EPOLLHUP) || + (!(events[i].events & EPOLLIN))) + { + /* An error has occured on this fd, or the socket is not + ready for reading (why were we notified then?) */ + fprintf(stderr, "epoll error\n"); + close(events[i].data.fd); + continue; + } - else if (socket == events[i].data.fd) - { - /* We have a notification on the listening socket, which - means one or more incoming connections. */ - while (true) - { - LOG_DEBUG("trying to accept connection on thread " << idx); - if(!workers[idx].accept(socket)) - break; + else if (socket == events[i].data.fd) + { + /* We have a notification on the listening socket, which + means one or more incoming connections. */ + while (true) + { + LOG_DEBUG("trying to accept connection on thread " << idx); + if (!workers[idx].accept(socket)) break; - LOG_DEBUG("Accepted a new connection on thread " << idx); - idx = (idx + 1) % workers.size(); - break; - } + LOG_DEBUG("Accepted a new connection on thread " << idx); + idx = (idx + 1) % workers.size(); + break; + } } } } - free (events); + free(events); return 0; }