Tizen 2.1 base
[framework/security/security-server.git] / socket_connection / connection / SocketStream.cpp
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file        SocketStream.cpp
18  * @author      Zofia Abramowska (z.abramowska@samsung.com)
19  * @version     1.0
20  * @brief       Implementation of socket stream class
21  */
22
23
24 #include <sys/socket.h>
25 #include <sys/select.h>
26 #include <errno.h>
27 #include <cstring>
28 #include <dpl/log/log.h>
29 #include "SocketStream.h"
30
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
36
37 void SocketStream::throwWithErrnoMessage(std::string function_name){
38     LogError(function_name << " : " << strerror(errno));
39     ThrowMsg(Exception::SocketStreamException, function_name << " : " << strerror(errno));
40 }
41
42 void SocketStream::Read(size_t num, void * bytes){
43
44     if(NULL == bytes){
45         LogError("Null pointer to buffer");
46         ThrowMsg(Exception::SocketStreamException, "Null pointer to buffer");
47     }
48     
49     m_bytesRead += num;
50     
51     if(m_bytesRead > MAX_BUFFER){
52         LogError("Too big buffer requested!");
53         ThrowMsg(Exception::SocketStreamException, "Too big buffer requested!");
54     }
55
56     char part_buffer[MAX_BUFFER];
57     std::string whole_buffer;
58
59     fd_set rset, allset;
60     int max_fd;
61     ssize_t bytes_read = 0, bytes_to_read = (ssize_t) num;
62
63     timespec timeout;
64
65     max_fd = m_socketFd;
66     ++max_fd;
67
68     FD_ZERO(&allset);
69     FD_SET(m_socketFd, &allset);
70
71     int returned_value;
72
73     while(bytes_to_read != 0){
74         timeout.tv_sec = READ_TIEMOUT_SEC;
75         timeout.tv_nsec = READ_TIMEUOT_NSEC;
76         rset = allset;
77
78         if(-1 == (returned_value = pselect(max_fd, &rset, NULL, NULL, &timeout, NULL))){
79             if(errno == EINTR) continue;
80             throwWithErrnoMessage("pselect()");
81         }
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");
88         }
89         if(FD_ISSET(m_socketFd, &rset)){
90             bytes_read = read(m_socketFd, part_buffer, num);
91             if(bytes_read <= 0){
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()");
98                 }
99             }
100
101             whole_buffer.append(part_buffer, bytes_read);
102             bytes_to_read-=bytes_read;
103             bytes_read = 0;
104             continue;
105         }
106
107     }
108     memcpy(bytes, whole_buffer.c_str(), num);
109 }
110
111 void SocketStream::Write(size_t num, const void * bytes){
112
113     if(NULL == bytes){
114         LogError("Null pointer to buffer");
115         ThrowMsg(Exception::SocketStreamException, "Null pointer to buffer");
116     }
117     
118     m_bytesWrote += num;
119     
120     if(m_bytesWrote > MAX_BUFFER){
121         LogError("Too big buffer requested!");
122         ThrowMsg(Exception::SocketStreamException, "Too big buffer requested!");
123     }
124
125     fd_set wset, allset;
126     int max_fd;
127
128     timespec timeout;
129
130     max_fd = m_socketFd;
131     ++max_fd;
132
133     FD_ZERO(&allset);
134     FD_SET(m_socketFd, &allset);
135
136     int returned_value;
137
138     int write_res, bytes_to_write = num;
139     unsigned int current_offset = 0;
140
141     while(current_offset != num){
142         timeout.tv_sec = WRITE_TIMEOUT_SEC;
143         timeout.tv_nsec = WRITE_TIMEOUT_NSEC;
144         wset = allset;
145
146         if(-1 == (returned_value = pselect(max_fd, NULL, &wset, NULL, &timeout, NULL))){
147             if(errno == EINTR) continue;
148             throwWithErrnoMessage("pselect()");
149         }
150
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");
157
158                 }else if(errno != EAGAIN && errno != EWOULDBLOCK){
159                     throwWithErrnoMessage("write()");
160                 }
161             }
162             current_offset += write_res;
163             bytes_to_write -= write_res;
164         }
165     }
166 }