Change timeout parameter option 58/240158/10
authorChanggyu Choi <changyu.choi@samsung.com>
Tue, 4 Aug 2020 04:52:33 +0000 (13:52 +0900)
committerHwanKyu Jhun <h.jhun@samsung.com>
Thu, 6 Aug 2020 00:29:19 +0000 (00:29 +0000)
Change timeout parameter to millisecond.
If "timeout_msec == -1", set default timeout(5s).
Else if "timeout_msec == INT_MAX", then not set(infinity).

Change-Id: I219e8defab34c90ee9b0aeb0eb4c0afcc9533743
Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
aul/socket/client.cc
aul/socket/client.hh
aul/socket/socket.cc
aul/socket/socket.hh

index 9d6b01c..b5cef4c 100644 (file)
@@ -23,7 +23,7 @@
 
 namespace aul {
 
-Client::Client(std::string path, int timesec) : Socket(std::move(path)) {
+Client::Client(std::string path, int timeout_msec) : Socket(std::move(path)) {
   int retry = 2;
   do {
     int ret = Connect();
@@ -44,7 +44,7 @@ Client::Client(std::string path, int timesec) : Socket(std::move(path)) {
   if (retry == 0)
     THROW(-ECOMM);
 
-  SetTimeout(timesec);
+  SetTimeout(timeout_msec);
 }
 
 int Client::Send(const Packet& packet) {
index 5f9a2c4..c54bb05 100644 (file)
@@ -24,7 +24,7 @@ namespace aul {
 
 class Client : public Socket {
  public:
-  Client(std::string path, int timesec = 5);
+  Client(std::string path, int timeout_msec = 5000);
   int Send(const Packet& packet);
   int Recv(Packet& packet);
 };
index f28cb0a..1031389 100644 (file)
 
 #include <errno.h>
 #include <fcntl.h>
+#include <limits.h>
 #include <unistd.h>
 
+#include <algorithm>
+
 #include "aul/common/log_private.hh"
 #include "aul/socket/socket.hh"
 
@@ -166,18 +169,21 @@ void Socket::SetOption() {
   }
 }
 
-void Socket::SetTimeout(int timesec) {
-  if (timesec == -1)
+void Socket::SetTimeout(int timeout_msec) {
+  if (timeout_msec == INT_MAX)
     return;
 
-  if (timesec < 0) {
-    _E("Invalid timesec parameter");
+  if (timeout_msec == -1)
+    timeout_msec = 5000;
+
+  if (timeout_msec < 0) {
+    _E("Invalid timeout_msec parameter");
     return;
   }
 
   struct timeval timeout = {
-    .tv_sec = (time_t)timesec,
-    .tv_usec = 0
+    .tv_sec = static_cast<time_t>(timeout_msec / 1000),
+    .tv_usec = static_cast<useconds_t>((timeout_msec % 1000) * 1000)
   };
   int ret = setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
   if (ret < 0)
index e07f306..6cb084b 100644 (file)
@@ -41,7 +41,7 @@ class Socket {
   int Connect();
 
   int GetFd();
-  void SetTimeout(int timesec);
+  void SetTimeout(int timeout_msec);
   std::string GetPath();
 
  private: