Use g_unix_fd_add() instead of GIOChannel 20/320920/2
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 11 Mar 2025 05:10:40 +0000 (14:10 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 11 Mar 2025 05:11:12 +0000 (14:11 +0900)
To monitor fd events, using g_unix_fd_add() is enough.
This patch uses g_unix_fd_add() instead of GIOChannel.

Change-Id: I2637bcd0c2be44df8e206b42d948503ac519025b
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/proxy-internal.cc
src/proxy-internal.hh
src/stub-internal.cc
src/stub-internal.hh

index 7f8d04bb5a726464cc541df364011f35b27de0bb..11679102914928713f9f169a86eeca810019656f 100644 (file)
@@ -517,33 +517,24 @@ Proxy::ProxyPort::~ProxyPort() {
 
   if (source_ > 0)
     g_source_remove(source_);
-
-  if (channel_ != nullptr)
-    g_io_channel_unref(channel_);
 }
 
 int Proxy::ProxyPort::Watch(bool receive) {
-  channel_ = g_io_channel_unix_new(GetFd());
-  if (channel_ == nullptr) {
-    _E("g_io_channel_unix_new() is failed");
-    return -1;
-  }
-
-  disconn_source_ = g_io_add_watch(channel_,
-      static_cast<GIOCondition>(G_IO_ERR | G_IO_HUP | G_IO_NVAL),
+  disconn_source_ = g_unix_fd_add(
+      GetFd(), static_cast<GIOCondition>(G_IO_ERR | G_IO_HUP | G_IO_NVAL),
       Proxy::ProxyPort::OnSocketDisconnected, parent_);
   if (disconn_source_ == 0) {
-    _E("g_io_add_watch() is failed");
+    _E("g_unix_fd_add() is failed");
     return -1;
   }
 
   if (!receive)
     return 0;
 
-  source_ = g_io_add_watch(channel_, static_cast<GIOCondition>(G_IO_IN),
-      Proxy::ProxyPort::OnDataReceived, parent_);
+  source_ = g_unix_fd_add(GetFd(), static_cast<GIOCondition>(G_IO_IN),
+                          Proxy::ProxyPort::OnDataReceived, parent_);
   if (source_ == 0) {
-    _E("g_io_add_watch() is failed");
+    _E("g_unix_fd_add() is failed");
     return -1;
   }
 
@@ -558,8 +549,8 @@ void Proxy::ProxyPort::SetSource(guint source_id) {
   source_ = source_id;
 }
 
-gboolean Proxy::ProxyPort::OnSocketDisconnected(GIOChannel* channel,
-    GIOCondition cond, gpointer user_data) {
+gboolean Proxy::ProxyPort::OnSocketDisconnected(gint fd, GIOCondition cond,
+                                                gpointer user_data) {
   auto* proxy = static_cast<Proxy*>(user_data);
   std::lock_guard<std::recursive_mutex> lock(proxy->GetMutex());
   auto* listener = proxy->listener_;
@@ -568,7 +559,6 @@ gboolean Proxy::ProxyPort::OnSocketDisconnected(GIOChannel* channel,
     return G_SOURCE_REMOVE;
   }
 
-  int fd = g_io_channel_unix_get_fd(channel);
   _W("Socket was disconnected. fd(%d)", fd);
   if (proxy->main_port_.get() != nullptr &&
       proxy->main_port_->GetFd() == fd) {
@@ -586,8 +576,8 @@ gboolean Proxy::ProxyPort::OnSocketDisconnected(GIOChannel* channel,
   return G_SOURCE_REMOVE;;
 }
 
-gboolean Proxy::ProxyPort::OnDataReceived(GIOChannel* channel,
-    GIOCondition cond, gpointer user_data) {
+gboolean Proxy::ProxyPort::OnDataReceived(gint fd, GIOCondition cond,
+                                          gpointer user_data) {
   auto* proxy = static_cast<Proxy*>(user_data);
   std::lock_guard<std::recursive_mutex> lock(proxy->GetMutex());
   auto* listener = proxy->listener_;
@@ -596,7 +586,6 @@ gboolean Proxy::ProxyPort::OnDataReceived(GIOChannel* channel,
     return G_SOURCE_REMOVE;
   }
 
-  int fd = g_io_channel_unix_get_fd(channel);
   if (proxy->delegate_port_->GetFd() == fd) {
     char buffer[4];
     if (recv(fd, buffer, sizeof(buffer), MSG_PEEK | MSG_DONTWAIT) == 0) {
@@ -622,9 +611,6 @@ Proxy::Client::Client(Proxy* parent) : parent_(parent) {
 }
 
 Proxy::Client::~Client() {
-  if (channel_)
-    g_io_channel_unref(channel_);
-
   if (disconn_source_ > 0)
     g_source_remove(disconn_source_);
 
@@ -672,24 +658,18 @@ Proxy::Client* Proxy::Client::Create(Proxy* parent,
 }
 
 int Proxy::Client::Watch() {
-  channel_ = g_io_channel_unix_new(GetFd());
-  if (channel_ == nullptr) {
-    _E("g_io_channel_unix_new() is failed");
-    return -1;
-  }
-
-  source_ = g_io_add_watch(channel_, static_cast<GIOCondition>(G_IO_IN),
-      Proxy::Client::OnResponseReceived, parent_);
+  source_ = g_unix_fd_add(GetFd(), static_cast<GIOCondition>(G_IO_IN),
+                          Proxy::Client::OnResponseReceived, parent_);
   if (source_ == 0) {
-    _E("g_io_add_watch() is failed");
+    _E("g_unix_fd_add() is failed");
     return -1;
   }
 
-  disconn_source_ = g_io_add_watch(channel_,
-      static_cast<GIOCondition>(G_IO_ERR | G_IO_HUP | G_IO_NVAL),
+  disconn_source_ = g_unix_fd_add(
+      GetFd(), static_cast<GIOCondition>(G_IO_ERR | G_IO_HUP | G_IO_NVAL),
       Proxy::Client::OnSocketDisconnected, parent_);
   if (disconn_source_ == 0) {
-    _E("g_io_add_watch() is failed");
+    _E("g_unix_fd_add() is failed");
     return -1;
   }
 
@@ -704,8 +684,8 @@ void Proxy::Client::SetSource(guint source) {
   source_ = source;
 }
 
-gboolean Proxy::Client::OnSocketDisconnected(GIOChannel* channel,
-    GIOCondition cond, gpointer user_data) {
+gboolean Proxy::Client::OnSocketDisconnected(gint fd, GIOCondition cond,
+                                             gpointer user_data) {
   auto* proxy = static_cast<Proxy*>(user_data);
   std::lock_guard<std::recursive_mutex> lock(proxy->GetMutex());
   proxy->UnsetConnTimer();
@@ -715,7 +695,6 @@ gboolean Proxy::Client::OnSocketDisconnected(GIOChannel* channel,
     return G_SOURCE_REMOVE;
   }
 
-  int fd = g_io_channel_unix_get_fd(channel);
   _W("Socket was disconnected. fd(%d)", fd);
   if (proxy->main_client_.get() != nullptr &&
       proxy->main_client_->GetFd() == fd) {
@@ -732,8 +711,8 @@ gboolean Proxy::Client::OnSocketDisconnected(GIOChannel* channel,
   return G_SOURCE_REMOVE;
 }
 
-gboolean Proxy::Client::OnResponseReceived(GIOChannel* channel,
-    GIOCondition cond, gpointer user_data) {
+gboolean Proxy::Client::OnResponseReceived(gint fd, GIOCondition cond,
+                                           gpointer user_data) {
   auto* proxy = static_cast<Proxy*>(user_data);
   std::lock_guard<std::recursive_mutex> lock(proxy->GetMutex());
   proxy->UnsetConnTimer();
@@ -745,7 +724,6 @@ gboolean Proxy::Client::OnResponseReceived(GIOChannel* channel,
 
   bool is_delegate = false;
   std::unique_ptr<Client> client;
-  int fd = g_io_channel_unix_get_fd(channel);
   if (proxy->main_client_.get() != nullptr &&
       proxy->main_client_->GetFd() == fd) {
     client.reset(proxy->main_client_.release());
@@ -779,7 +757,7 @@ gboolean Proxy::Client::OnResponseReceived(GIOChannel* channel,
     proxy->main_client_.reset();
     proxy->delegate_client_.reset();
     listener->OnRejected(proxy->target_appid_,
-        RPC_PORT_ERROR_PERMISSION_DENIED);
+                         RPC_PORT_ERROR_PERMISSION_DENIED);
     return G_SOURCE_REMOVE;
   }
 
@@ -790,11 +768,11 @@ gboolean Proxy::Client::OnResponseReceived(GIOChannel* channel,
     proxy->delegate_port_.reset(
         new ProxyPort(proxy, proxy->fds_[1], proxy->target_appid_));
     DebugPort::AddSession(proxy->port_name_, proxy->target_appid_,
-        proxy->fds_[0], proxy->fds_[1]);
+                          proxy->fds_[0], proxy->fds_[1]);
     listener->OnConnected(proxy->target_appid_, proxy->main_port_.get());
     _W("target_appid(%s), port_name(%s), main_fd(%d), delegate_fd(%d)",
-        proxy->target_appid_.c_str(), proxy->port_name_.c_str(),
-        proxy->fds_[0], proxy->fds_[1]);
+       proxy->target_appid_.c_str(), proxy->port_name_.c_str(), proxy->fds_[0],
+       proxy->fds_[1]);
   } else {
     proxy->fds_[0] = client_fd;
     proxy->main_port_.reset(
index c22aafcca55c8829bd8816600fc73eb1a1a58636..a6c3d5bd3ec9c69ac787026bd3fa9f7d6a2ca4f8 100644 (file)
@@ -19,7 +19,6 @@
 
 #include <aul_rpc_port.h>
 #include <glib.h>
-#include <gio/gio.h>
 #include <glib-unix.h>
 
 #include <string>
@@ -73,14 +72,13 @@ class Proxy : public std::enable_shared_from_this<Proxy> {
    private:
     int Watch(bool receive);
 
-    static gboolean OnSocketDisconnected(GIOChannel* channel, GIOCondition cond,
-        gpointer user_data);
-    static gboolean OnDataReceived(GIOChannel* channel, GIOCondition cond,
-        gpointer user_data);
+    static gboolean OnSocketDisconnected(gint fd, GIOCondition cond,
+                                         gpointer user_data);
+    static gboolean OnDataReceived(gint fd, GIOCondition cond,
+                                   gpointer user_data);
 
    private:
     Proxy* parent_ = nullptr;
-    GIOChannel* channel_ = nullptr;
     guint disconn_source_ = 0;
     guint source_ = 0;
   };
@@ -97,14 +95,13 @@ class Proxy : public std::enable_shared_from_this<Proxy> {
     void SetSource(guint source);
 
    private:
-    static gboolean OnSocketDisconnected(GIOChannel* channel, GIOCondition cond,
-        gpointer user_data);
-    static gboolean OnResponseReceived(GIOChannel* channel, GIOCondition cond,
-        gpointer user_data);
+    static gboolean OnSocketDisconnected(gint fd, GIOCondition cond,
+                                         gpointer user_data);
+    static gboolean OnResponseReceived(gint fd, GIOCondition cond,
+                                       gpointer user_data);
 
    private:
     Proxy* parent_;
-    GIOChannel* channel_ = nullptr;
     guint disconn_source_ = 0;
     guint source_ = 0;
   };
index b0bc8833d664e9f817721022a149700a89d3134b..ed3fa4c9a3854033ce1e34f4338abdfff26db017 100644 (file)
@@ -178,8 +178,8 @@ void Stub::RemoveAcceptedPorts(std::string instance) {
   }
 }
 
-gboolean Stub::AcceptedPort::OnDataReceived(GIOChannel* channel,
-    GIOCondition cond, gpointer user_data) {
+gboolean Stub::AcceptedPort::OnDataReceived(gint fd, GIOCondition cond,
+                                            gpointer user_data) {
   auto* stub = static_cast<Stub*>(user_data);
   std::lock_guard<std::recursive_mutex> lock(stub->GetMutex());
   auto* listener = stub->listener_;
@@ -188,7 +188,6 @@ gboolean Stub::AcceptedPort::OnDataReceived(GIOChannel* channel,
     return G_SOURCE_REMOVE;
   }
 
-  int fd = g_io_channel_unix_get_fd(channel);
   for (auto& p : stub->ports_) {
     if (p->GetFd() == fd && !p->IsDelegate()) {
       char buffer[4];
@@ -217,8 +216,8 @@ gboolean Stub::AcceptedPort::OnDataReceived(GIOChannel* channel,
   return G_SOURCE_CONTINUE;
 }
 
-gboolean Stub::AcceptedPort::OnSocketDisconnected(GIOChannel* channel,
-    GIOCondition cond, gpointer user_data) {
+gboolean Stub::AcceptedPort::OnSocketDisconnected(gint fd, GIOCondition cond,
+                                                  gpointer user_data) {
   auto* stub = static_cast<Stub*>(user_data);
   std::lock_guard<std::recursive_mutex> lock(stub->GetMutex());
   auto* listener = stub->listener_;
@@ -227,7 +226,6 @@ gboolean Stub::AcceptedPort::OnSocketDisconnected(GIOChannel* channel,
     return G_SOURCE_REMOVE;
   }
 
-  int fd = g_io_channel_unix_get_fd(channel);
   _W("Socket was disconnected. fd(%d)", fd);
   for (auto& p : stub->ports_) {
     if (p->GetFd() == fd) {
@@ -295,33 +293,24 @@ Stub::AcceptedPort::~AcceptedPort() {
 
   if (source_ > 0)
     g_source_remove(source_);
-
-  if (channel_ != nullptr)
-    g_io_channel_unref(channel_);
 }
 
 int Stub::AcceptedPort::Watch(bool receive) {
-  channel_ = g_io_channel_unix_new(GetFd());
-  if (channel_ == nullptr) {
-    _E("g_io_channel_unix_new() is failed");
-    return -1;
-  }
-
-  disconn_source_ = g_io_add_watch(channel_,
-      static_cast<GIOCondition>(G_IO_ERR | G_IO_HUP | G_IO_NVAL),
+  disconn_source_ = g_unix_fd_add(
+      GetFd(), static_cast<GIOCondition>(G_IO_ERR | G_IO_HUP | G_IO_NVAL),
       OnSocketDisconnected, parent_);
   if (disconn_source_ == 0) {
-    _E("g_io_add_watch() is failed");
+    _E("g_unix_fd_add() is failed");
     return -1;
   }
 
   if (!receive)
     return 0;
 
-  source_ = g_io_add_watch(channel_, static_cast<GIOCondition>(G_IO_IN),
-      OnDataReceived, parent_);
+  source_ = g_unix_fd_add(GetFd(), static_cast<GIOCondition>(G_IO_IN),
+                          OnDataReceived, parent_);
   if (source_ == 0) {
-    _E("g_io_add_watch() is failed");
+    _E("g_unix_fd_add() is failed");
     return -1;
   }
 
@@ -334,31 +323,21 @@ Stub::Server::Server(int fd, Stub* parent)
 }
 
 Stub::Server::~Server() {
-  if (channel_)
-    g_io_channel_unref(channel_);
-
-  if (source_ > 0)
-    g_source_remove(source_);
+  if (source_ > 0) g_source_remove(source_);
 }
 
 int Stub::Server::Listen() {
-  channel_ = g_io_channel_unix_new(GetFd());
-  if (channel_ == nullptr) {
-    _E("g_io_channel_unix_new() is failed");
-    return -1;
-  }
-
-  source_ = g_io_add_watch(channel_, static_cast<GIOCondition>(G_IO_IN),
-      OnRequestReceived, parent_);
+  source_ = g_unix_fd_add(GetFd(), static_cast<GIOCondition>(G_IO_IN),
+                          OnRequestReceived, parent_);
   if (source_ == 0) {
-    _E("g_io_add_watch() is failed");
+    _E("g_unix_fd_add() is failed");
     return -1;
   }
 
   return 0;
 }
 
-gboolean Stub::Server::OnRequestReceived(GIOChannel* channel, GIOCondition cond,
+gboolean Stub::Server::OnRequestReceived(gint fd, GIOCondition cond,
     gpointer user_data) {
   auto* stub = static_cast<Stub*>(user_data);
   std::lock_guard<std::recursive_mutex> lock(stub->GetMutex());
index f58413f24706de951c4d95cd1669663bc60e1afd..8911163fad926c1f4d994d6f17cd4849895c4e72 100644 (file)
@@ -18,7 +18,6 @@
 #define STUB_INTERNAL_HH_
 
 #include <glib.h>
-#include <gio/gio.h>
 #include <glib-unix.h>
 
 #include <list>
@@ -72,13 +71,12 @@ class Stub {
 
    private:
     int Watch(bool receive);
-    static gboolean OnDataReceived(GIOChannel* channel, GIOCondition cond,
-        gpointer user_data);
-    static gboolean OnSocketDisconnected(GIOChannel* channel, GIOCondition cond,
-        gpointer user_data);
+    static gboolean OnDataReceived(gint fd, GIOCondition cond,
+                                   gpointer user_data);
+    static gboolean OnSocketDisconnected(gint fd, GIOCondition cond,
+                                         gpointer user_data);
 
    private:
-    GIOChannel* channel_ = nullptr;
     guint disconn_source_ = 0;
     guint source_ = 0;
     Stub* parent_;
@@ -93,12 +91,11 @@ class Stub {
     int Listen();
 
    private:
-    static gboolean OnRequestReceived(GIOChannel* channel, GIOCondition cond,
-        gpointer user_data);
+    static gboolean OnRequestReceived(gint fd, GIOCondition cond,
+                                      gpointer user_data);
 
    private:
     Stub* parent_;
-    GIOChannel* channel_ = nullptr;
     guint source_ = 0;;
   };