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>
29 #include <dpl/log/log.h>
30 #include "SocketStream.h"
32 #define READ_TIEMOUT_SEC 1
33 #define READ_TIMEUOT_NSEC 0
34 #define WRITE_TIMEOUT_SEC 0
35 #define WRITE_TIMEOUT_NSEC 100000000
36 #define MAX_BUFFER 10240
38 void SocketStream::throwWithErrnoMessage(std::string function_name){
39 LogError(function_name << " : " << strerror(errno));
40 ThrowMsg(Exception::SocketStreamException, function_name << " : " << strerror(errno));
43 void SocketStream::Read(size_t num, void * bytes){
46 LogError("Null pointer to buffer");
47 ThrowMsg(Exception::SocketStreamException, "Null pointer to buffer");
52 if(m_bytesRead > MAX_BUFFER){
53 LogError("Too big buffer requested!");
54 ThrowMsg(Exception::SocketStreamException, "Too big buffer requested!");
57 char part_buffer[MAX_BUFFER];
58 std::string whole_buffer;
62 ssize_t bytes_read = 0, bytes_to_read = (ssize_t) num;
70 FD_SET(m_socketFd, &allset);
74 while(bytes_to_read != 0){
75 timeout.tv_sec = READ_TIEMOUT_SEC;
76 timeout.tv_nsec = READ_TIMEUOT_NSEC;
79 if(-1 == (returned_value = pselect(max_fd, &rset, NULL, NULL, &timeout, NULL))){
83 LogInfo("pselect() : " << strerror(errno));
84 throwWithErrnoMessage("pselect()");
87 if(0 == returned_value){
88 //This means pselect got timedout
89 //This is not a proper behavior in reading data from UDS
90 //And could mean we got corrupted connection
91 LogError("Couldn't read whole data. continue..");
94 if(FD_ISSET(m_socketFd, &rset)){
95 bytes_read = read(m_socketFd, part_buffer, num);
97 if(errno == ECONNRESET || errno == ENOTCONN || errno == ETIMEDOUT){
98 LogInfo("Connection closed : " << strerror(errno));
99 ThrowMsg(Exception::SocketStreamException,
100 "Connection closed : " << strerror(errno) << ". Couldn't read whole data");
101 }else if (errno != EAGAIN && errno != EWOULDBLOCK){
102 throwWithErrnoMessage("read()");
106 whole_buffer.append(part_buffer, bytes_read);
107 bytes_to_read-=bytes_read;
113 memcpy(bytes, whole_buffer.c_str(), num);
116 void SocketStream::Write(size_t num, const void * bytes){
119 LogError("Null pointer to buffer");
120 ThrowMsg(Exception::SocketStreamException, "Null pointer to buffer");
125 if(m_bytesWrote > MAX_BUFFER){
126 LogError("Too big buffer requested!");
127 ThrowMsg(Exception::SocketStreamException, "Too big buffer requested!");
139 FD_SET(m_socketFd, &allset);
143 int write_res, bytes_to_write = num;
144 unsigned int current_offset = 0;
146 while(current_offset != num){
147 timeout.tv_sec = WRITE_TIMEOUT_SEC;
148 timeout.tv_nsec = WRITE_TIMEOUT_NSEC;
151 if(-1 == (returned_value = pselect(max_fd, NULL, &wset, NULL, &timeout, NULL))){
152 if(errno == EINTR) continue;
153 throwWithErrnoMessage("pselect()");
156 if(FD_ISSET(m_socketFd, &wset)){
157 if(-1 == (write_res = write(m_socketFd, reinterpret_cast<const char *>(bytes) + current_offset, bytes_to_write))){
158 if(errno == ECONNRESET || errno == EPIPE){
159 LogInfo("Connection closed : " << strerror(errno));
160 ThrowMsg(Exception::SocketStreamException,
161 "Connection closed : " << strerror(errno) << ". Couldn't write whole data");
163 }else if(errno != EAGAIN && errno != EWOULDBLOCK){
164 throwWithErrnoMessage("write()");
167 current_offset += write_res;
168 bytes_to_write -= write_res;