Detect EOF to call disconnected callback 55/167855/2
authorJunghoon Park <jh9216.park@samsung.com>
Mon, 22 Jan 2018 10:55:41 +0000 (19:55 +0900)
committerJunghoon Park <jh9216.park@samsung.com>
Mon, 22 Jan 2018 11:00:09 +0000 (20:00 +0900)
To detect disconnected socket by the other side, recv function with flag
MSG_PEEK was used

Change-Id: I02705a61120d55b11023711c84b4c56de080af93
Signed-off-by: Junghoon Park <jh9216.park@samsung.com>
src/proxy-internal.cc
src/stub-internal.cc

index ba316d6..5c8f592 100644 (file)
@@ -18,6 +18,8 @@
 #define _GNU_SOURCE
 #endif
 
+#include <sys/types.h>
+#include <sys/socket.h>
 #include <dlog.h>
 
 #include "proxy-internal.h"
@@ -35,6 +37,7 @@ Proxy::Proxy(bool mock)
     : fd_broker_(mock) {}
 
 Proxy::~Proxy() {
+  LOGD("Proxy::~Proxy");
   if (src_ > 0)
     g_source_remove(src_);
 
@@ -51,6 +54,7 @@ gboolean Proxy::OnSocketDisconnected(GIOChannel *gio, GIOCondition cond,
                                      gpointer data) {
   Proxy* proxy = static_cast<Proxy*>(data);
 
+  LOGW("Socket was disconnected");
   proxy->listener_->OnDisconnected(proxy->target_appid_);
   proxy->disconn_src_ = 0;
 
@@ -60,6 +64,15 @@ gboolean Proxy::OnSocketDisconnected(GIOChannel *gio, GIOCondition cond,
 gboolean Proxy::OnDataReceived(GIOChannel *gio, GIOCondition cond,
                                gpointer data) {
   Proxy* proxy = static_cast<Proxy*>(data);
+  int fd = g_io_channel_unix_get_fd(gio);
+  char buffer[4];
+
+  if (recv(fd, buffer, sizeof(buffer), MSG_PEEK | MSG_DONTWAIT) == 0) {
+    LOGW("Socket was disconnected by stub");
+    proxy->listener_->OnDisconnected(proxy->target_appid_);
+    proxy->src_ = 0;
+    return FALSE;
+  }
 
   proxy->listener_->OnReceived(proxy->target_appid_);
 
index a1b963f..cff9295 100644 (file)
@@ -18,6 +18,8 @@
 #define _GNU_SOURCE
 #endif
 
+#include <sys/types.h>
+#include <sys/socket.h>
 #include <dlog.h>
 
 #include "stub-internal.h"
@@ -34,7 +36,9 @@ namespace internal {
 Stub::Stub(const std::string& port_name, bool mock)
     : fd_broker_(mock), port_name_(port_name) {}
 
-Stub::~Stub() {}
+Stub::~Stub() {
+  LOGD("Stub::~Stub");
+}
 
 void Stub::Listen(IEventListener* ev) {
   if (ev == nullptr)
@@ -53,12 +57,21 @@ gboolean Stub::OnDataReceived(GIOChannel *gio, GIOCondition cond,
                               gpointer data) {
   Stub* stub = static_cast<Stub*>(data);
   int fd = g_io_channel_unix_get_fd(gio);
+  char buffer[4];
 
   for (auto& p : stub->ports_) {
     if (p->GetFd() == fd) {
+      if (recv(fd, buffer, sizeof(buffer), MSG_PEEK | MSG_DONTWAIT) == 0) {
+        LOGW("Socket was disconnected from proxy");
+        stub->listener_->OnDisconnected(p->GetId());
+        stub->ports_.remove(p);
+        return FALSE;
+      }
+
       int ret = stub->listener_->OnReceived(p->GetId(), *p);
 
       if (ret != 0) {
+        LOGW("Invalid protocol");
         stub->listener_->OnDisconnected(p->GetId());
         stub->ports_.remove(p);
         return FALSE;
@@ -76,6 +89,7 @@ gboolean Stub::OnSocketDisconnected(GIOChannel *gio, GIOCondition cond,
   Stub* stub = static_cast<Stub*>(data);
   int fd = g_io_channel_unix_get_fd(gio);
 
+  LOGW("Socket was disconnected");
   for (auto& p : stub->ports_) {
     if (p->GetFd() == fd) {
       stub->listener_->OnDisconnected(p->GetId());