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 1
32 #define READ_TIMEUOT_NSEC 0
33 #define WRITE_TIMEOUT_SEC 0
34 #define WRITE_TIMEOUT_NSEC 100000000
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);
109 LogInfo("Message read");
112 void SocketStream::Write(size_t num, const void * bytes){
115 LogError("Null pointer to buffer");
116 ThrowMsg(Exception::SocketStreamException, "Null pointer to buffer");
121 if(m_bytesWrote > MAX_BUFFER){
122 LogError("Too big buffer requested!");
123 ThrowMsg(Exception::SocketStreamException, "Too big buffer requested!");
135 FD_SET(m_socketFd, &allset);
139 int write_res, bytes_to_write = num;
140 unsigned int current_offset = 0;
142 while(current_offset != num){
143 timeout.tv_sec = WRITE_TIMEOUT_SEC;
144 timeout.tv_nsec = WRITE_TIMEOUT_NSEC;
147 if(-1 == (returned_value = pselect(max_fd, NULL, &wset, NULL, &timeout, NULL))){
148 if(errno == EINTR) continue;
149 throwWithErrnoMessage("pselect()");
152 if(FD_ISSET(m_socketFd, &wset)){
153 if(-1 == (write_res = write(m_socketFd, reinterpret_cast<const char *>(bytes) + current_offset, bytes_to_write))){
154 if(errno == ECONNRESET){
155 LogInfo("Connection closed : " << strerror(errno));
156 ThrowMsg(Exception::SocketStreamException,
157 "Connection closed : " << strerror(errno) << ". Couldn't write whole data");
159 }else if(errno != EAGAIN && errno != EWOULDBLOCK){
160 throwWithErrnoMessage("write()");
163 current_offset += write_res;
164 bytes_to_write -= write_res;
167 LogInfo("Message wrote");