From 4de57f6d7bcc4352e99ea0147af13f321cc11a86 Mon Sep 17 00:00:00 2001 From: Changgyu Choi Date: Mon, 20 Jul 2020 09:18:51 +0900 Subject: [PATCH] Refactor Client & Socket - 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 --- aul/socket/client.cc | 10 ++++------ aul/socket/client.hh | 3 +-- aul/socket/socket.cc | 28 +++++++++++++++++++--------- aul/socket/socket.hh | 6 +++--- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/aul/socket/client.cc b/aul/socket/client.cc index 4712bd8..9d6b01c 100644 --- a/aul/socket/client.cc +++ b/aul/socket/client.cc @@ -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).GetRaw(); - int ret = Socket::Send(reinterpret_cast(&raw[0]), raw.size()); - if (ret < 0) - return ret; - - return 0; + return Socket::Send(reinterpret_cast(&raw[0]), raw.size()); } int Client::Recv(Packet& packet) { diff --git a/aul/socket/client.hh b/aul/socket/client.hh index 27a3eb4..5f9a2c4 100644 --- a/aul/socket/client.hh +++ b/aul/socket/client.hh @@ -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); }; diff --git a/aul/socket/socket.cc b/aul/socket/socket.cc index 6307a2a..f28cb0a 100644 --- a/aul/socket/socket.cc +++ b/aul/socket/socket.cc @@ -23,13 +23,12 @@ 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() { diff --git a/aul/socket/socket.hh b/aul/socket/socket.hh index 00d9e07..e07f306 100644 --- a/aul/socket/socket.hh +++ b/aul/socket/socket.hh @@ -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, }; }; -- 2.7.4