From ed9bc433f485735dafcd248a5e35017669471594 Mon Sep 17 00:00:00 2001 From: Boram Bae Date: Thu, 5 Dec 2019 19:20:12 +0900 Subject: [PATCH] Delete pending events when disconnecting channel Change-Id: I1b7fb7dce4c14b31d18b9169f398571c34c67fbd Signed-off-by: Boram Bae --- src/shared/channel.cpp | 52 ++++++++++++++++++++++++++++++++++++++-------- src/shared/channel.h | 5 ++++- src/shared/event_handler.h | 13 ++++++++++++ src/shared/event_loop.cpp | 4 ++++ 4 files changed, 64 insertions(+), 10 deletions(-) diff --git a/src/shared/channel.cpp b/src/shared/channel.cpp index 6df1d2f..6ae8ed3 100644 --- a/src/shared/channel.cpp +++ b/src/shared/channel.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include "sensor_log.h" #include "channel_event_handler.h" @@ -40,14 +41,23 @@ public: bool handle(int fd, event_condition condition) { - if (!m_ch || !m_ch->is_connected()) + if (!m_ch) { return false; + } - if (condition & (EVENT_IN | EVENT_HUP)) + m_ch->remove_pending_event_id(m_event_id); + + if (!m_ch->is_connected()) { + return false; + } + + if (condition & (EVENT_IN | EVENT_HUP)) { return false; + } - if (!m_ch->send_sync(*m_msg)) + if (!m_ch->send_sync(*m_msg)) { return false; + } return false; } @@ -66,16 +76,24 @@ public: bool handle(int fd, event_condition condition) { - message msg; + if (!m_ch) { + return false; + } + + m_ch->remove_pending_event_id(m_event_id); - if (!m_ch || !m_ch->is_connected()) + if (!m_ch->is_connected()) { return false; + } - if (condition & (EVENT_OUT | EVENT_HUP)) + if (condition & (EVENT_OUT | EVENT_HUP)) { return false; + } - if (!m_ch->read_sync(msg, false)) + message msg; + if (!m_ch->read_sync(msg, false)) { return false; + } return false; } @@ -155,6 +173,10 @@ void channel::disconnect(void) } if (m_loop) { + for(auto id : m_pending_event_id) { + _D("Remove pending event id[%llu]", id); + m_loop->remove_event(id, true); + } _D("Remove event[%llu]", m_event_id); m_loop->remove_event(m_event_id, true); m_loop = NULL; @@ -189,12 +211,14 @@ bool channel::send(std::shared_ptr msg) send_event_handler *handler = new(std::nothrow) send_event_handler(this, msg); retvm_if(!handler, false, "Failed to allocate memory"); - if (m_loop->add_event(m_socket->get_fd(), (EVENT_OUT | EVENT_HUP | EVENT_NVAL) , handler) == 0) { + uint64_t event_id = m_loop->add_event(m_socket->get_fd(), (EVENT_OUT | EVENT_HUP | EVENT_NVAL), handler); + if (event_id == 0) { _D("Failed to add send event handler"); delete handler; return false; } + m_pending_event_id.push_back(event_id); return true; } @@ -227,12 +251,14 @@ bool channel::read(void) read_event_handler *handler = new(std::nothrow) read_event_handler(this); retvm_if(!handler, false, "Failed to allocate memory"); - if (m_loop->add_event(m_socket->get_fd(), (EVENT_IN | EVENT_HUP | EVENT_NVAL), handler) == 0) { + uint64_t event_id = m_loop->add_event(m_socket->get_fd(), (EVENT_IN | EVENT_HUP | EVENT_NVAL), handler); + if (event_id == 0) { _D("Failed to add read event handler"); delete handler; return false; } + m_pending_event_id.push_back(event_id); return true; } @@ -319,3 +345,11 @@ int channel::get_fd(void) const { return m_fd; } + +void channel::remove_pending_event_id(uint64_t id) +{ + auto it = std::find(m_pending_event_id.begin(), m_pending_event_id.end(), id); + if (it != m_pending_event_id.end()) { + m_pending_event_id.erase(it); + } +} diff --git a/src/shared/channel.h b/src/shared/channel.h index 615fd26..4aeefbc 100644 --- a/src/shared/channel.h +++ b/src/shared/channel.h @@ -22,6 +22,7 @@ #include #include +#include #include "socket.h" #include "message.h" @@ -55,7 +56,8 @@ public: bool get_option(int type, int &value) const; bool set_option(int type, int value); - int get_fd(void) const; + int get_fd(void) const; + void remove_pending_event_id(uint64_t id); private: int m_fd; @@ -63,6 +65,7 @@ private: socket *m_socket; channel_handler *m_handler; event_loop *m_loop; + std::vector m_pending_event_id; std::atomic m_connected; }; diff --git a/src/shared/event_handler.h b/src/shared/event_handler.h index 26ab899..dbe8c7d 100644 --- a/src/shared/event_handler.h +++ b/src/shared/event_handler.h @@ -20,15 +20,28 @@ #ifndef __EVENT_HANDLER_H__ #define __EVENT_HANDLER_H__ +#include namespace ipc { typedef unsigned int event_condition; class event_handler { public: + event_handler() + : m_event_id(0) + { + } + virtual ~event_handler() {} virtual bool handle(int fd, event_condition condition) = 0; + void set_event_id(int64_t event_id) + { + m_event_id = 0; + } + +protected: + int64_t m_event_id; }; } diff --git a/src/shared/event_loop.cpp b/src/shared/event_loop.cpp index d923d78..180b13e 100644 --- a/src/shared/event_loop.cpp +++ b/src/shared/event_loop.cpp @@ -129,10 +129,14 @@ uint64_t event_loop::add_event(const int fd, const event_condition cond, event_h } uint64_t id = m_sequence++; + if (m_sequence == 0) { + m_sequence = 1; + } handler_info *info = new(std::nothrow) handler_info(id, fd, ch, src, handler, this); retvm_if(!info, BAD_HANDLE, "Failed to allocate memory"); + handler->set_event_id(id); g_source_set_callback(src, (GSourceFunc) g_io_handler, info, NULL); g_source_attach(src, g_main_loop_get_context(m_mainloop)); -- 2.7.4