Refactor Client & Socket 55/238855/7
authorChanggyu Choi <changyu.choi@samsung.com>
Mon, 20 Jul 2020 00:18:51 +0000 (09:18 +0900)
committerChanggyu Choi <changyu.choi@samsung.com>
Mon, 20 Jul 2020 04:36:33 +0000 (13:36 +0900)
 - Remove "is_client" from Socket
 - Add "SetTimeout()" from Socket
 - Add "timesec" parameter from "Client()"

The Difference from whether "is_client" or not is just "SetOption()".
So it more proper than using "is_client" that adding "SetTimeout()".
"timesec" need to set timeout. If "timesec == -1", recv timeout is infinity.
Default is 5sec.

Change-Id: I91728697f19c0e4dc29d3c2bf10d674f3f5579e7
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 4712bd8..9d6b01c 100644 (file)
@@ -23,7 +23,7 @@
 
 namespace aul {
 
-Client::Client(std::string path) : Socket(std::move(path), true) {
+Client::Client(std::string path, int timesec) : Socket(std::move(path)) {
   int retry = 2;
   do {
     int ret = Connect();
@@ -43,16 +43,14 @@ Client::Client(std::string path) : Socket(std::move(path), true) {
 
   if (retry == 0)
     THROW(-ECOMM);
+
+  SetTimeout(timesec);
 }
 
 int Client::Send(const Packet& packet) {
   _W("cmd(%d)", packet.GetCmd());
   auto raw = const_cast<Packet&>(packet).GetRaw();
-  int ret = Socket::Send(reinterpret_cast<void*>(&raw[0]), raw.size());
-  if (ret < 0)
-    return ret;
-
-  return 0;
+  return Socket::Send(reinterpret_cast<void*>(&raw[0]), raw.size());
 }
 
 int Client::Recv(Packet& packet) {
index 27a3eb4..5f9a2c4 100644 (file)
@@ -24,8 +24,7 @@ namespace aul {
 
 class Client : public Socket {
  public:
-  Client(std::string path);
-
+  Client(std::string path, int timesec = 5);
   int Send(const Packet& packet);
   int Recv(Packet& packet);
 };
index 6307a2a..f28cb0a 100644 (file)
 
 namespace aul {
 
-Socket::Socket(std::string path, bool is_client)
-  : path_(std::move(path)), fd_(0), is_client_(is_client) {
+Socket::Socket(std::string path) : path_(std::move(path)), fd_(0) {
   Create();
 }
 
-Socket::Socket(int fd, bool is_client)
-  : fd_(fd), is_client_(is_client) {
+Socket::Socket(int fd)
+  : fd_(fd) {
 }
 
 Socket::~Socket() {
@@ -165,13 +164,24 @@ void Socket::SetOption() {
     _E("setsockopt() is failed. fd(%d), errno(%d)", fd_, errno);
     return;
   }
+}
+
+void Socket::SetTimeout(int timesec) {
+  if (timesec == -1)
+    return;
 
-  if (is_client_) {
-    struct timeval timeout = { 5, 200 * 1000 };
-    ret = setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
-    if (ret < 0)
-      _E("setsockopt() is failed. fd(%d), errno(%d)", fd_, errno);
+  if (timesec < 0) {
+    _E("Invalid timesec parameter");
+    return;
   }
+
+  struct timeval timeout = {
+    .tv_sec = (time_t)timesec,
+    .tv_usec = 0
+  };
+  int ret = setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
+  if (ret < 0)
+    _E("setsockopt() is failed. fd(%d), errno(%d)", fd_, errno);
 }
 
 void Socket::Create() {
index 00d9e07..e07f306 100644 (file)
@@ -31,8 +31,8 @@ static const int MAX_AUL_BUFF_SIZE = 131071;
 
 class Socket {
  public:
-  Socket(std::string path, bool is_client = true);
-  Socket(int fd, bool is_client = true);
+  Socket(std::string path);
+  Socket(int fd);
   virtual ~Socket();
 
   int Send(const void* buf, unsigned int len);
@@ -41,6 +41,7 @@ class Socket {
   int Connect();
 
   int GetFd();
+  void SetTimeout(int timesec);
   std::string GetPath();
 
  private:
@@ -50,7 +51,6 @@ class Socket {
  private:
   std::string path_;
   int fd_;
-  bool is_client_;
   struct sockaddr_un addr_ = { 0, };
 };