Merge branch 'devel/tizen' into tizen
[platform/core/system/sensord.git] / src / shared / stream_socket.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include "stream_socket.h"
21
22 #include <sys/types.h>
23 #include <sys/socket.h>
24
25 #include "sensor_log.h"
26
27 #define SLEEP_10_MS usleep(10000)
28
29 using namespace ipc;
30
31 stream_socket::stream_socket()
32 : socket()
33 {
34 }
35
36 stream_socket::~stream_socket()
37 {
38 }
39
40 bool stream_socket::create(const std::string &path)
41 {
42         return socket::create_by_type(path, SOCK_STREAM);
43 }
44
45 ssize_t stream_socket::on_send(const void *buffer, size_t size) const
46 {
47         ssize_t len = 0;
48         size_t total_size = 0;
49
50         do {
51                 len = ::send(get_fd(),
52                                 reinterpret_cast<const char *>(buffer) + total_size,
53                                 size - total_size, get_mode());
54
55                 if (len < 0) {
56                         if ((errno == EINTR) || (errno == EAGAIN) || (errno == EWOULDBLOCK)) {
57                                 SLEEP_10_MS;
58                                 continue;
59                         }
60
61                         _ERRNO(errno, _E, "Failed to send(%d, %#p, %x, %d) = %d",
62                                         get_fd(), buffer, total_size, size - total_size, len);
63                         return -errno;
64                 }
65
66                 total_size += len;
67         } while (total_size < size);
68
69         return total_size;
70 }
71
72 ssize_t stream_socket::on_recv(void *buffer, size_t size) const
73 {
74         ssize_t len = 0;
75         size_t total_size = 0;
76
77         do {
78                 len = ::recv(get_fd(),
79                                 reinterpret_cast<char *>(buffer) + total_size,
80                                 size - total_size,
81                                 socket::get_mode());
82
83                 if (len == 0) {
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);
86                         return -1;
87                 }
88
89                 if (len < 0) {
90                         if ((errno == EINTR) || (errno == EAGAIN) || (errno == EWOULDBLOCK)) {
91                                 SLEEP_10_MS;
92                                 continue;
93                         }
94
95                         _ERRNO(errno, _E, "Failed to recv(%d, %#p, %x, %d) = %d",
96                                         get_fd(), buffer, total_size, size - total_size, len);
97                         return -errno;
98                 }
99
100                 total_size += len;
101         } while (total_size < size);
102
103         return total_size;
104 }