AbstractSocket::AbstractSocket(int fd) : fd_(fd), addr_{} {}
-AbstractSocket::~AbstractSocket() {
- Destroy();
-}
+AbstractSocket::~AbstractSocket() { Destroy(); }
int AbstractSocket::SendData(const void* buf, unsigned int size) {
auto buffer = static_cast<const unsigned char*>(buf);
int AbstractSocket::ReceiveData(void* buf, unsigned int size) {
bool is_blocking = true;
- if (fcntl(fd_, F_GETFL, 0) & O_NONBLOCK)
- is_blocking = false;
+ if (fcntl(fd_, F_GETFL, 0) & O_NONBLOCK) is_blocking = false;
auto buffer = static_cast<unsigned char*>(buf);
unsigned int left = size;
}
ret = setsockopt(fd_, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
- if (ret < 0)
- LOGE("setsockopt() is failed. fd(%d), errno(%d)", fd_, errno);
+ if (ret < 0) LOGE("setsockopt() is failed. fd(%d), errno(%d)", fd_, errno);
}
int AbstractSocket::Create() {
ClientSocket::ClientSocket(std::string path) : AbstractSocket(std::move(path)) {
int retry_cnt = 2;
int ret = Create();
- if (ret < 0)
- return;
+ if (ret < 0) return;
do {
ret = Connect();
break;
} else if (ret < -1) {
LOGE("Maybe peer not launched or peer dead. path(%s), fd(%d)",
- GetPath().c_str(), GetFd());
+ 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());
+ LOGE("Failed to connect to socket(%s), fd(%d)", GetPath().c_str(),
+ GetFd());
return;
}
} while (retry_cnt > 0);
- if (retry_cnt == 0)
- return;
+ if (retry_cnt == 0) return;
SetTimeout(5);
}
-ClientSocket::~ClientSocket() {
- Destroy();
-}
+ClientSocket::~ClientSocket() { Destroy(); }
void ClientSocket::SetTimeout(int timeout_msec) {
- if (timeout_msec == -1)
- timeout_msec = 5000;
+ if (timeout_msec == -1) timeout_msec = 5000;
if (timeout_msec < 0) {
LOGE("Invalid timeout_msec parameter");
}
struct timeval timeout = {
- .tv_sec = static_cast<time_t>(timeout_msec / 1000),
- .tv_usec = static_cast<suseconds_t>((timeout_msec % 1000) * 1000)
- };
+ .tv_sec = static_cast<time_t>(timeout_msec / 1000),
+ .tv_usec = static_cast<suseconds_t>((timeout_msec % 1000) * 1000)};
int ret = setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
- if (ret < 0)
- LOGE("setsockopt() is failed. fd(%d), errno(%d)", fd_, errno);
+ if (ret < 0) LOGE("setsockopt() is failed. fd(%d), errno(%d)", fd_, errno);
}
int ClientSocket::Connect() {
int flags = fcntl(fd_, F_GETFL, 0);
fcntl(fd_, F_SETFL, flags | O_NONBLOCK);
- int ret = connect(fd_, reinterpret_cast<struct sockaddr*>(&addr_),
- sizeof(addr_));
+ int ret =
+ connect(fd_, reinterpret_cast<struct sockaddr*>(&addr_), sizeof(addr_));
fcntl(fd_, F_SETFL, flags);
if (ret < 0) {
- if (errno != EAGAIN && errno != EINPROGRESS)
- return -2;
+ if (errno != EAGAIN && errno != EINPROGRESS) return -2;
} else if (ret == 0) {
SetOption();
return 0;
FD_ZERO(&readfds);
FD_SET(fd_, &readfds);
fd_set writefds = readfds;
- struct timeval timeout = { 0, 100 * 1000 };
+ struct timeval timeout = {0, 100 * 1000};
ret = select(fd_ + 1, &readfds, &writefds, NULL, &timeout);
if (ret == 0) {
errno = ETIMEDOUT;
if (FD_ISSET(fd_, &readfds) || FD_ISSET(fd_, &writefds)) {
int error = 0;
socklen_t len = sizeof(error);
- if (getsockopt(fd_, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
- return -1;
+ if (getsockopt(fd_, SOL_SOCKET, SO_ERROR, &error, &len) < 0) return -1;
}
return -1;
LOGD("Server socket is created(%s)", path_.c_str());
}
-ServerSocket::~ServerSocket() {
- Destroy();
-}
+ServerSocket::~ServerSocket() { Destroy(); }
int ServerSocket::Bind() {
return bind(fd_, reinterpret_cast<struct sockaddr*>(&addr_), sizeof(addr_));
}
-int ServerSocket::Listen() {
- return listen(fd_, 128);
-}
+int ServerSocket::Listen() { return listen(fd_, 128); }
int ServerSocket::Accept() {
int client_fd = accept(fd_, nullptr, nullptr);