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();
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) {
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);
};
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() {
_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() {
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);
int Connect();
int GetFd();
+ void SetTimeout(int timesec);
std::string GetPath();
private:
private:
std::string path_;
int fd_;
- bool is_client_;
struct sockaddr_un addr_ = { 0, };
};