2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file SocketStream.cpp
18 * @author Zofia Abramowska (z.abramowska@samsung.com)
20 * @brief Implementation of socket stream class
24 #include <sys/socket.h>
25 #include <sys/select.h>
28 #include <dpl/log/log.h>
29 #include "SocketStream.h"
31 #define READ_TIEMOUT_SEC 60
32 #define READ_TIMEUOT_NSEC 0
33 #define WRITE_TIMEOUT_SEC 60
34 #define WRITE_TIMEOUT_NSEC 0
35 #define MAX_BUFFER 10240
37 void SocketStream::throwWithErrnoMessage(std::string function_name){
38 LogError(function_name << " : " << strerror(errno));
39 ThrowMsg(Exception::SocketStreamException, function_name << " : " << strerror(errno));
42 void SocketStream::Read(size_t num, void * bytes){
45 LogError("Null pointer to buffer");
46 ThrowMsg(Exception::SocketStreamException, "Null pointer to buffer");
51 if(m_bytesRead > MAX_BUFFER){
52 LogError("Too big buffer requested!");
53 ThrowMsg(Exception::SocketStreamException, "Too big buffer requested!");
56 char part_buffer[MAX_BUFFER];
57 std::string whole_buffer;
61 ssize_t bytes_read = 0, bytes_to_read = (ssize_t) num;
69 FD_SET(m_socketFd, &allset);
73 while(bytes_to_read != 0){
74 timeout.tv_sec = READ_TIEMOUT_SEC;
75 timeout.tv_nsec = READ_TIMEUOT_NSEC;
78 if(-1 == (returned_value = pselect(max_fd, &rset, NULL, NULL, &timeout, NULL))){
79 if(errno == EINTR) continue;
80 throwWithErrnoMessage("pselect()");
82 if(0 == returned_value){
83 //This means pselect got timedout
84 //This is not a proper behavior in reading data from UDS
85 //And could mean we got corrupted connection
86 LogError("Couldn't read whole data");
87 ThrowMsg(Exception::SocketStreamException, "Couldn't read whole data");
89 if(FD_ISSET(m_socketFd, &rset)){
90 bytes_read = read(m_socketFd, part_buffer, num);
92 if(errno == ECONNRESET || errno == ENOTCONN || errno == ETIMEDOUT){
93 LogInfo("Connection closed : " << strerror(errno));
94 ThrowMsg(Exception::SocketStreamException,
95 "Connection closed : " << strerror(errno) << ". Couldn't read whole data");
96 }else if (errno != EAGAIN && errno != EWOULDBLOCK){
97 throwWithErrnoMessage("read()");
101 whole_buffer.append(part_buffer, bytes_read);
102 bytes_to_read-=bytes_read;
108 memcpy(bytes, whole_buffer.c_str(), num);
111 void SocketStream::Write(size_t num, const void * bytes){
114 LogError("Null pointer to buffer");
115 ThrowMsg(Exception::SocketStreamException, "Null pointer to buffer");
120 if(m_bytesWrote > MAX_BUFFER){
121 LogError("Too big buffer requested!");
122 ThrowMsg(Exception::SocketStreamException, "Too big buffer requested!");
134 FD_SET(m_socketFd, &allset);
138 int write_res, bytes_to_write = num;
139 unsigned int current_offset = 0;
141 while(current_offset != num){
142 timeout.tv_sec = WRITE_TIMEOUT_SEC;
143 timeout.tv_nsec = WRITE_TIMEOUT_NSEC;
146 if(-1 == (returned_value = pselect(max_fd, NULL, &wset, NULL, &timeout, NULL))){
147 if(errno == EINTR) continue;
148 throwWithErrnoMessage("pselect()");
151 if(FD_ISSET(m_socketFd, &wset)){
152 if(-1 == (write_res = write(m_socketFd, reinterpret_cast<const char *>(bytes) + current_offset, bytes_to_write))){
153 if(errno == ECONNRESET || errno == EPIPE){
154 LogInfo("Connection closed : " << strerror(errno));
155 ThrowMsg(Exception::SocketStreamException,
156 "Connection closed : " << strerror(errno) << ". Couldn't write whole data");
158 }else if(errno != EAGAIN && errno != EWOULDBLOCK){
159 throwWithErrnoMessage("write()");
162 current_offset += write_res;
163 bytes_to_write -= write_res;