Delete pending events when disconnecting channel 06/219506/3
authorBoram Bae <boram21.bae@samsung.com>
Thu, 5 Dec 2019 10:20:12 +0000 (19:20 +0900)
committerBoram Bae <boram21.bae@samsung.com>
Fri, 6 Dec 2019 01:06:16 +0000 (10:06 +0900)
Change-Id: I1b7fb7dce4c14b31d18b9169f398571c34c67fbd
Signed-off-by: Boram Bae <boram21.bae@samsung.com>
src/shared/channel.cpp
src/shared/channel.h
src/shared/event_handler.h
src/shared/event_loop.cpp

index 6df1d2fd208ac8ad1c55dc5140189ac4c480954c..6ae8ed364317476afa7c01daa199cf491b034511 100644 (file)
@@ -22,6 +22,7 @@
 #include <stdint.h>
 #include <unistd.h>
 #include <memory>
+#include <algorithm>
 
 #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<message> 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);
+       }
+}
index 615fd268122be243b64da9b5bea57cc32a44522d..4aeefbcba36af463b1796f097c5fc486ce6f1c05 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <unistd.h>
 #include <atomic>
+#include <vector>
 
 #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<uint64_t> m_pending_event_id;
 
        std::atomic<bool> m_connected;
 };
index 26ab8991cc9bffe17f7aedc68255d131344a2f88..dbe8c7d8cd763d2c99d5953a4f8c56aca9b5935f 100644 (file)
 #ifndef __EVENT_HANDLER_H__
 #define __EVENT_HANDLER_H__
 
+#include <stdint.h>
 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;
 };
 
 }
index d923d78c065b381634ca0333c41841367bb8f41a..180b13e0277e9dc44689d3469db7a2028d9d07e2 100644 (file)
@@ -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));