Fix implementation
authorChanggyu Choi <changyu.choi@samsung.com>
Thu, 18 Feb 2021 08:56:23 +0000 (17:56 +0900)
committer최창규/Tizen Platform Lab(SR)/Engineer/삼성전자 <changyu.choi@samsung.com>
Fri, 19 Feb 2021 01:44:13 +0000 (10:44 +0900)
src/common/socket/abstract_socket.cc
src/common/socket/abstract_socket.hh
src/common/socket/client_socket.cc
src/common/socket/client_socket.hh
src/common/socket/data_socket.cc
src/common/socket/server_socket.cc

index b730cc1..82ac752 100644 (file)
@@ -32,7 +32,7 @@ AbstractSocket::AbstractSocket(std::string path)
 
 AbstractSocket::AbstractSocket(int fd) : fd_(fd), addr_{} {}
 
-AbstractSocket::~AbstractSocket() { Destroy(); }
+AbstractSocket::~AbstractSocket() {}
 
 int AbstractSocket::SendData(const void* buf, unsigned int size) {
   auto buffer = static_cast<const unsigned char*>(buf);
@@ -60,15 +60,15 @@ int AbstractSocket::ReceiveData(void* buf, unsigned int size) {
   while (left) {
     ssize_t recv_byte = recv(fd_, buffer, left, 0);
     if (recv_byte == 0) {
-      LOGW("Socket was disconnected. fd(%d)", fd_);
-      return -ECOMM;
+      LOGW("Socket was disconnected. fd(%d), errno(%d)", fd_, errno);
+      return -errno;
     } else if (recv_byte < 0) {
       if (errno == EINTR) {
         continue;
       } else if (errno == EAGAIN) {
         if (is_blocking) {
-          LOGE("Timed out. fd(%d)", fd_);
-          return -EAGAIN;
+          LOGE("Timed out. fd(%d), errno(%d)", fd_, errno);
+          return -errno;
         }
 
         continue;
@@ -102,6 +102,7 @@ void AbstractSocket::SetOption() {
 }
 
 int AbstractSocket::Create() {
+  if (fd_ != -1) return 0;
   fd_ = ::socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
   if (fd_ < 0) {
     LOGE("socket() is failed. errno(%d)", errno);
@@ -110,11 +111,16 @@ int AbstractSocket::Create() {
 
   addr_.sun_family = AF_UNIX;
   snprintf(addr_.sun_path, sizeof(addr_.sun_path), "%s", path_.c_str());
+  SetOption();
   return 0;
 }
 
-void AbstractSocket::Destroy() {
-  if (fd_ > 0) close(fd_);
+void AbstractSocket::Disconnect() {
+  if (fd_ > 0) {
+    LOGW("Close fd(%d)", fd_);
+    close(fd_);
+  }
+  fd_ = -1;
 }
 
 }  // namespace socket
index b572411..bbfabb0 100644 (file)
@@ -36,6 +36,7 @@ class EXPORT_API AbstractSocket {
 
   int SendData(const void* buf, unsigned int len);
   int ReceiveData(void* buf, unsigned int len);
+  void Disconnect();
 
   int GetFd();
   std::string GetPath();
@@ -43,7 +44,6 @@ class EXPORT_API AbstractSocket {
  protected:
   void SetOption();
   int Create();
-  void Destroy();
 
  protected:
   std::string path_;
index 656bcb0..1de5828 100644 (file)
@@ -28,32 +28,9 @@ namespace pkgmgr_common {
 namespace socket {
 
 ClientSocket::ClientSocket(std::string path) : AbstractSocket(std::move(path)) {
-  int retry_cnt = 2;
-  int ret = Create();
-  if (ret < 0) return;
-
-  do {
-    ret = Connect();
-    if (ret == 0) {
-      break;
-    } else if (ret < -1) {
-      LOGE("Maybe peer not launched or peer dead. path(%s), fd(%d)",
-           GetPath().c_str(), GetFd());
-      usleep(100 * 1000);
-      --retry_cnt;
-    } else if (ret < 0) {
-      LOGE("Failed to connect to socket(%s), fd(%d)", GetPath().c_str(),
-           GetFd());
-      return;
-    }
-  } while (retry_cnt > 0);
-
-  if (retry_cnt == 0) return;
-
-  SetTimeout(5);
 }
 
-ClientSocket::~ClientSocket() { Destroy(); }
+ClientSocket::~ClientSocket() {}
 
 void ClientSocket::SetTimeout(int timeout_msec) {
   if (timeout_msec == -1) timeout_msec = 5000;
@@ -70,7 +47,31 @@ void ClientSocket::SetTimeout(int timeout_msec) {
   if (ret < 0) LOGE("setsockopt() is failed. fd(%d), errno(%d)", fd_, errno);
 }
 
-int ClientSocket::Connect() {
+bool ClientSocket::Connect() {
+  if (Create() < 0) return false;
+  SetTimeout(5000);
+  int ret = -1;
+  int retry_cnt = 3;
+  do {
+    ret = TryConnection();
+    if (ret == 0) {
+      break;
+    } else if (ret < -1) {
+      LOGE("Maybe peer not launched or peer dead. path(%s), fd(%d)",
+           GetPath().c_str(), GetFd());
+      usleep(100 * 1000);
+      --retry_cnt;
+    } else if (ret < 0) {
+      LOGE("Failed to connect to socket(%s), fd(%d)", GetPath().c_str(),
+           GetFd());
+      return false;
+    }
+  } while (retry_cnt > 0);
+
+  return (retry_cnt > 0);
+}
+
+int ClientSocket::TryConnection() {
   int flags = fcntl(fd_, F_GETFL, 0);
   fcntl(fd_, F_SETFL, flags | O_NONBLOCK);
 
index edd1f69..bc08fea 100644 (file)
@@ -26,9 +26,10 @@ class EXPORT_API ClientSocket: public AbstractSocket {
  public:
   ClientSocket(std::string path);
   ~ClientSocket();
+  bool Connect();
 
  private:
-  int Connect();
+  int TryConnection();
   void SetTimeout(int timeout_msec);
 };
 
index 6877ab2..3dc9b5c 100644 (file)
@@ -21,7 +21,7 @@ namespace socket {
 
 DataSocket::DataSocket(int fd) : AbstractSocket(fd) {}
 
-DataSocket::~DataSocket() { Destroy(); }
+DataSocket::~DataSocket() {}
 
 }  // namespace socket
 }  // namespace pkgmgr_common
index e5f674b..13fc7e4 100644 (file)
@@ -66,7 +66,7 @@ ServerSocket::ServerSocket(std::string path) : AbstractSocket(std::move(path)) {
   LOGD("Server socket is created(%s)", path_.c_str());
 }
 
-ServerSocket::~ServerSocket() { Destroy(); }
+ServerSocket::~ServerSocket() {}
 
 int ServerSocket::Bind() {
   return bind(fd_, reinterpret_cast<struct sockaddr*>(&addr_), sizeof(addr_));