4 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include "stream_socket.h"
22 #include <sys/types.h>
23 #include <sys/socket.h>
25 #include "sensor_log.h"
27 #define SLEEP_10_MS usleep(10000)
31 stream_socket::stream_socket()
36 stream_socket::~stream_socket()
40 bool stream_socket::create(const std::string &path)
42 return socket::create_by_type(path, SOCK_STREAM);
45 ssize_t stream_socket::on_send(const void *buffer, size_t size) const
48 size_t total_size = 0;
51 len = ::send(get_fd(),
52 reinterpret_cast<const char *>(buffer) + total_size,
53 size - total_size, get_mode());
56 if ((errno == EINTR) || (errno == EAGAIN) || (errno == EWOULDBLOCK)) {
61 _ERRNO(errno, _E, "Failed to send(%d, %#p, %x, %d) = %d",
62 get_fd(), buffer, total_size, size - total_size, len);
67 } while (total_size < size);
72 ssize_t stream_socket::on_recv(void *buffer, size_t size) const
75 size_t total_size = 0;
78 len = ::recv(get_fd(),
79 reinterpret_cast<char *>(buffer) + total_size,
84 _E("Failed to recv(%d, %#p + %x, %d) = %d, because the peer performed shutdown",
85 get_fd(), buffer, total_size, size - total_size, len);
90 if ((errno == EINTR) || (errno == EAGAIN) || (errno == EWOULDBLOCK)) {
95 _ERRNO(errno, _E, "Failed to recv(%d, %#p, %x, %d) = %d",
96 get_fd(), buffer, total_size, size - total_size, len);
101 } while (total_size < size);