diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/binary-queue.cpp | 5 | ||||
-rw-r--r-- | src/common/dispatcher.h | 3 | ||||
-rw-r--r-- | src/common/eventfd.cpp | 3 | ||||
-rw-r--r-- | src/common/eventfd.h | 4 | ||||
-rw-r--r-- | src/common/log.cpp | 44 | ||||
-rw-r--r-- | src/common/log.h | 61 | ||||
-rw-r--r-- | src/common/mainloop.cpp | 7 | ||||
-rw-r--r-- | src/common/service.cpp | 16 | ||||
-rw-r--r-- | src/common/socket.cpp | 14 | ||||
-rw-r--r-- | src/common/timer.cpp | 11 | ||||
-rw-r--r-- | src/common/timer.h | 5 |
11 files changed, 69 insertions, 104 deletions
diff --git a/src/common/binary-queue.cpp b/src/common/binary-queue.cpp index 776f65e..34cc9f9 100644 --- a/src/common/binary-queue.cpp +++ b/src/common/binary-queue.cpp @@ -39,9 +39,7 @@ BinaryQueue::~BinaryQueue() RawBuffer BinaryQueue::pop() { RawBuffer buf(m_size); - read(m_size, buf.data()); - return buf; } @@ -78,7 +76,6 @@ void BinaryQueue::read(size_t size, void *bytes) size_t count = std::min(size, m_buckets.front()->left); cur = m_buckets.front()->extractTo(cur, count); - size -= count; m_size -= count; @@ -107,10 +104,8 @@ void *BinaryQueue::Bucket::extractTo(void *dest, size_t size) throw std::logic_error("logic error. invalid input to Bucket::extractTo."); memcpy(dest, cur, size); - cur += size; left -= size; - return static_cast<unsigned char *>(dest) + size; } diff --git a/src/common/dispatcher.h b/src/common/dispatcher.h index def73ed..9e29580 100644 --- a/src/common/dispatcher.h +++ b/src/common/dispatcher.h @@ -57,13 +57,10 @@ Type Dispatcher::methodCall(Args &&...args) connect(); m_connection->send(BinaryQueue::Serialize(std::forward<Args>(args)...).pop()); - BinaryQueue q; q.push(m_connection->receive()); - Type response; q.Deserialize(response); - return response; } diff --git a/src/common/eventfd.cpp b/src/common/eventfd.cpp index 1828b1f..a3dba42 100644 --- a/src/common/eventfd.cpp +++ b/src/common/eventfd.cpp @@ -33,6 +33,7 @@ namespace CCHECKER { EventFD::EventFD(unsigned int initval, int flags) { fd = ::eventfd(initval, flags); + if (fd == -1) { throw std::logic_error("EventFd from constructor is failed!!"); } @@ -48,6 +49,7 @@ EventFD::~EventFD() void EventFD::send() { const std::uint64_t val = 1; + if (::write(fd, &val, sizeof(val)) == -1) { throw std::logic_error("EventFd send to fd is failed!!"); } @@ -56,6 +58,7 @@ void EventFD::send() void EventFD::receive() { std::uint64_t val; + if (::read(fd, &val, sizeof(val)) == -1) { throw std::logic_error("EventFd read to fd is failed!!"); } diff --git a/src/common/eventfd.h b/src/common/eventfd.h index f10945f..64681ab 100644 --- a/src/common/eventfd.h +++ b/src/common/eventfd.h @@ -31,8 +31,8 @@ public: EventFD(unsigned int initval = 0, int flags = EFD_SEMAPHORE | EFD_CLOEXEC); ~EventFD(); - EventFD(const EventFD&) = delete; - EventFD& operator=(const EventFD&) = delete; + EventFD(const EventFD &) = delete; + EventFD &operator=(const EventFD &) = delete; void send(); void receive(); diff --git a/src/common/log.cpp b/src/common/log.cpp index c5528a2..9d83458 100644 --- a/src/common/log.cpp +++ b/src/common/log.cpp @@ -24,27 +24,27 @@ #include <systemd/sd-journal.h> void JournalLog(int logLevel, - const char *message, - const char *fileName, - int line, - const char *function) + const char *message, + const char *fileName, + int line, + const char *function) { - try { - sd_journal_send("PRIORITY=%d", logLevel, - "CODE_FILE=%s", fileName, - "CODE_FUNC=%s", function, - "CODE_LINE=%d", line, - // add file, line & function info to log message - "MESSAGE=[%s:%d] %s(): %s", fileName, line, function, message, - NULL); - } catch (const std::out_of_range&) { - sd_journal_send( - "PRIORITY=%d", LOG_ERR, - "CODE_FILE=%s", fileName, - "CODE_FUNC=%s", function, - "CODE_LINE=%d", line, - // add file, line & function info to log message - "MESSAGE=[%s:%d] %s(): Unsupported log level %d", fileName, line, function, logLevel, - NULL); - } + try { + sd_journal_send("PRIORITY=%d", logLevel, + "CODE_FILE=%s", fileName, + "CODE_FUNC=%s", function, + "CODE_LINE=%d", line, + // add file, line & function info to log message + "MESSAGE=[%s:%d] %s(): %s", fileName, line, function, message, + NULL); + } catch (const std::out_of_range &) { + sd_journal_send( + "PRIORITY=%d", LOG_ERR, + "CODE_FILE=%s", fileName, + "CODE_FUNC=%s", function, + "CODE_LINE=%d", line, + // add file, line & function info to log message + "MESSAGE=[%s:%d] %s(): Unsupported log level %d", fileName, line, function, logLevel, + NULL); + } } diff --git a/src/common/log.h b/src/common/log.h index 99d494b..b0c802d 100644 --- a/src/common/log.h +++ b/src/common/log.h @@ -26,57 +26,56 @@ #define CERT_CHECKER_LOG_H void JournalLog(int logLevel, const char *message, const char *fileName, - int line, const char *function); + int line, const char *function); /* * Replacement low overhead null logging class */ -class NullStream -{ - public: - NullStream() {} +class NullStream { +public: + NullStream() {} - template <typename T> - NullStream& operator<<(const T&) - { - return *this; - } + template <typename T> + NullStream &operator<<(const T &) + { + return *this; + } }; /* avoid warnings about unused variables */ #define DPL_MACRO_DUMMY_LOGGING(message, level) \ - do { \ - NullStream ns; \ - ns << message; \ - } while (0) + do { \ + NullStream ns; \ + ns << message; \ + } while (0) #define CERT_CHECKER_LOG(message, level) \ -do \ -{ \ - std::ostringstream platformLog; \ - platformLog << message; \ - JournalLog(level, \ - platformLog.str().c_str(), \ - __FILE__, \ - __LINE__, \ - __FUNCTION__); \ -} while (0) + do \ + { \ + std::ostringstream platformLog; \ + platformLog << message; \ + JournalLog(level, \ + platformLog.str().c_str(), \ + __FILE__, \ + __LINE__, \ + __FUNCTION__); \ + } while (0) /* always logged. */ #define LogError(message) \ - CERT_CHECKER_LOG(message, LOG_ERR) + CERT_CHECKER_LOG(message, LOG_ERR) #define LogInfo(message) \ - CERT_CHECKER_LOG(message, LOG_INFO) + CERT_CHECKER_LOG(message, LOG_INFO) #define LogWarning(message) \ - CERT_CHECKER_LOG(message, LOG_WARNING) + CERT_CHECKER_LOG(message, LOG_WARNING) /* only debug logged. */ #ifdef BUILD_TYPE_DEBUG - #define LogDebug(message) \ - CERT_CHECKER_LOG(message, LOG_DEBUG) +#define LogDebug(message) \ + CERT_CHECKER_LOG(message, LOG_DEBUG) #else - #define LogDebug(message) \ - DPL_MACRO_DUMMY_LOGGING(message, LOG_DEBUG) +#define LogDebug(message) \ + DPL_MACRO_DUMMY_LOGGING(message, LOG_DEBUG) #endif // BUILD_TYPE_DEBUG #endif //CERT_CHECKER_LOG_H diff --git a/src/common/mainloop.cpp b/src/common/mainloop.cpp index 8ce7326..0c0d19f 100644 --- a/src/common/mainloop.cpp +++ b/src/common/mainloop.cpp @@ -47,7 +47,7 @@ Mainloop::~Mainloop() { if (!m_stopped && !m_isTimedOut && !m_callbacks.empty()) throw std::logic_error("mainloop registered callbacks should be empty " - "except timed out case"); + "except timed out case"); ::close(m_pollfd); } @@ -64,7 +64,6 @@ void Mainloop::prepare() wakeupSignal.receive(); removeEventSource(wakeupSignal.getFd()); }; - addEventSource(wakeupSignal.getFd(), EPOLLIN, std::move(wakeupMainloop)); } @@ -87,9 +86,7 @@ void Mainloop::addEventSource(int fd, uint32_t event, Callback &&callback) throw std::logic_error("event source already added!"); LogDebug("Add event[" << event << "] source on fd[" << fd << "]"); - epoll_event e; - e.events = event; e.data.fd = fd; @@ -127,7 +124,6 @@ void Mainloop::dispatch(int timeout) { int nfds = -1; epoll_event event[MAX_EPOLL_EVENTS]; - LogDebug("Mainloop dispatched with timeout: " << timeout); do { @@ -158,7 +154,6 @@ void Mainloop::dispatch(int timeout) } LogDebug("event[" << event[i].events << "] polled on fd[" << fd << "]"); - m_callbacks[fd](event[i].events); } } diff --git a/src/common/service.cpp b/src/common/service.cpp index 0c14dc2..3547efa 100644 --- a/src/common/service.cpp +++ b/src/common/service.cpp @@ -32,7 +32,6 @@ namespace CCHECKER { Service::Service(const std::string &address) : m_address(address) { LogDebug("Service constructed with address[" << address << "]"); - m_timeout = -1; setNewConnectionCallback(nullptr); setCloseConnectionCallback(nullptr); @@ -45,12 +44,9 @@ Service::~Service() void Service::start() { LogInfo("Service start!"); - Socket socket(m_address); - LogDebug("Get systemd socket[" << socket.getFd() - << "] with address[" << m_address << "]"); - + << "] with address[" << m_address << "]"); m_loop.addEventSource(socket.getFd(), EPOLLIN | EPOLLHUP | EPOLLRDHUP, [&](uint32_t event) { if (event != EPOLLIN) @@ -58,7 +54,6 @@ void Service::start() m_onNewConnection(std::make_shared<Connection>(socket.accept())); }); - m_loop.run(m_timeout); } @@ -76,17 +71,14 @@ void Service::setNewConnectionCallback(const ConnCallback &/*callback*/) throw std::logic_error("onNewConnection called but ConnShPtr is nullptr."); int fd = connection->getFd(); - LogInfo("welcome! accepted client socket fd[" << fd << "]"); - /* // TODO: disable temporarily if (callback) callback(connection); */ - m_loop.addEventSource(fd, EPOLLIN | EPOLLHUP | EPOLLRDHUP, - [&, fd](uint32_t event) { + [ &, fd](uint32_t event) { LogDebug("read event comes in to fd[" << fd << "]"); if (m_connectionRegistry.count(fd) == 0) @@ -101,10 +93,8 @@ void Service::setNewConnectionCallback(const ConnCallback &/*callback*/) } LogDebug("Start message process on fd[" << fd << "]"); - onMessageProcess(conn); }); - m_connectionRegistry[fd] = connection; }; } @@ -122,10 +112,8 @@ void Service::setCloseConnectionCallback(const ConnCallback &/*callback*/) throw std::logic_error("no connection in registry to remove."); LogInfo("good-bye! close socket fd[" << fd << "]"); - m_loop.removeEventSource(fd); m_connectionRegistry.erase(fd); /* scoped-lock needed? */ - /* // TODO: disable temporarily if (callback) diff --git a/src/common/socket.cpp b/src/common/socket.cpp index 05ed75f..a30d605 100644 --- a/src/common/socket.cpp +++ b/src/common/socket.cpp @@ -85,7 +85,6 @@ Socket &Socket::operator=(Socket &&other) m_fd = other.m_fd; other.m_fd = 0; - return *this; } @@ -108,7 +107,6 @@ Socket Socket::accept() const "socket accept failed!"); LogInfo("Accept client success with fd: " << fd); - return Socket(fd); } @@ -126,7 +124,6 @@ Socket Socket::connect(const std::string &path) sockaddr_un addr; addr.sun_family = AF_UNIX; - strncpy(addr.sun_path, path.c_str(), sizeof(addr.sun_path)); if (::connect(fd, reinterpret_cast<sockaddr *>(&addr), @@ -136,7 +133,6 @@ Socket Socket::connect(const std::string &path) "socket connect failed!"); LogInfo("Connect to OCSP service success with fd:" << fd); - return Socket(fd); } @@ -148,11 +144,9 @@ int Socket::getFd() const RawBuffer Socket::read(void) const { size_t total = 0; - RawBuffer data(1024, 0); auto buf = reinterpret_cast<char *>(data.data()); auto size = data.size(); - LogDebug("Read data from stream on socket fd[" << m_fd << "]"); while (total < size) { @@ -173,20 +167,16 @@ RawBuffer Socket::read(void) const } data.resize(total); - LogDebug("Read data of size[" << total - << "] from stream on socket fd[" << m_fd << "] done."); - + << "] from stream on socket fd[" << m_fd << "] done."); return data; } void Socket::write(const RawBuffer &data) const { size_t total = 0; - auto buf = reinterpret_cast<const char *>(data.data()); auto size = data.size(); - LogDebug("Write data to stream on socket fd[" << m_fd << "]"); while (total < size) { @@ -205,7 +195,7 @@ void Socket::write(const RawBuffer &data) const } LogDebug("Write data of size[" << total << - "] to stream on socket fd[" << m_fd << "] done."); + "] to stream on socket fd[" << m_fd << "] done."); } } // namespace CCHECKER diff --git a/src/common/timer.cpp b/src/common/timer.cpp index 63267d5..395375e 100644 --- a/src/common/timer.cpp +++ b/src/common/timer.cpp @@ -38,7 +38,7 @@ void Timer::timerStart(int interval) { LogDebug("Timer start!!"); - if(m_isRunning) + if (m_isRunning) return; m_isRunning = true; @@ -63,7 +63,7 @@ void Timer::timerStop() void Timer::run() { - while(1) { + while (1) { LogDebug("[timer] running interval : " << m_interval); if (m_isStop) @@ -73,10 +73,9 @@ void Timer::run() m_cv.wait_for( lock, std::chrono::seconds(m_interval), - [this] { - return m_isStop == true; - } - ); + [this] { + return m_isStop == true; + }); job(); } diff --git a/src/common/timer.h b/src/common/timer.h index e24ac35..9029ba7 100644 --- a/src/common/timer.h +++ b/src/common/timer.h @@ -29,8 +29,7 @@ namespace CCHECKER { -class Timer -{ +class Timer { public: Timer(void); virtual ~Timer(void); @@ -45,7 +44,7 @@ public: protected: // This is for derived class member function. - virtual void job(void){} + virtual void job(void) {} void run(); |