#include <stdint.h>
#include <unistd.h>
#include <memory>
+#include <algorithm>
#include "sensor_log.h"
#include "channel_event_handler.h"
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;
}
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;
}
}
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;
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;
}
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;
}
{
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);
+ }
+}
}
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));