2 * Copyright (c) 2013 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 #ifndef _SOCKET_CONNECTION_H_
18 #define _SOCKET_CONNECTION_H_
24 #include <SocketStream.h>
25 #include <PrivacyManagerTypes.h>
29 * This class implements interface for generic read and write from given socket.
30 * It does not maintain socket descriptor, so any connecting and disconnecting should be
34 * Throws SocketConnectionException when read/write will not succeed or if any bad allocation
35 * exception occurs during read.
38 class EXTERN_API SocketConnection
43 explicit SocketConnection(int socket_fd) : m_socketStream(socket_fd){
47 template<typename T, typename ...Args>
48 int read(T* out, const Args&... args )
51 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "read : %d", res);
53 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "read : %d", res);
55 return PRIV_MGR_ERROR_SUCCESS;
62 int res = m_socketStream.readStream(sizeof(length), &length);
63 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "readStream : %d", res);
64 char* pBuf = new (std::nothrow) char[length + 1];
65 TryReturn(pBuf != NULL, PRIV_MGR_ERROR_OUT_OF_MEMORY, , "new : %d", PRIV_MGR_ERROR_OUT_OF_MEMORY);
67 res = m_socketStream.readStream(length, pBuf);
68 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, delete[] pBuf, "readStream : %d", res);
76 return PRIV_MGR_ERROR_SUCCESS;
82 int res = m_socketStream.readStream(sizeof(length), &length);
83 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "readStream : %d", res);
85 char* pBuf = new (std::nothrow) char[length + 1];
86 TryReturn(pBuf != NULL, PRIV_MGR_ERROR_OUT_OF_MEMORY, , "new : %d", PRIV_MGR_ERROR_OUT_OF_MEMORY);
88 res = m_socketStream.readStream(length, pBuf);
89 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, delete[] pBuf, "readStream : %d", res);
93 *pB = * reinterpret_cast <bool* > (pBuf);
97 return PRIV_MGR_ERROR_SUCCESS;
113 int res = m_socketStream.readStream(sizeof(length), &length);
114 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "readStream : %d", res);
116 char* pBuf = new (std::nothrow) char[length + 1];
117 TryReturn(pBuf != NULL, PRIV_MGR_ERROR_OUT_OF_MEMORY, , "new : %d", PRIV_MGR_ERROR_OUT_OF_MEMORY);
119 res = m_socketStream.readStream(length, pBuf);
120 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, delete[] pBuf, "readStream : %d", res);
124 *pI = * reinterpret_cast <int* > (pBuf);
128 return PRIV_MGR_ERROR_SUCCESS;
131 int read(std::string* pStr)
134 int res = m_socketStream.readStream(sizeof(length), &length);
135 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "readStream : %d", res);
137 char* pBuf = new (std::nothrow) char[length + 1];
138 TryReturn(pBuf != NULL, PRIV_MGR_ERROR_OUT_OF_MEMORY, , "new : %d", PRIV_MGR_ERROR_OUT_OF_MEMORY);
140 m_socketStream.readStream(length, pBuf);
141 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, delete[] pBuf, "readStream : %d", res);
145 *pStr = std::string(pBuf);
148 return PRIV_MGR_ERROR_SUCCESS;
151 int read(std::string& str)
156 template < typename T >
157 int read (std::list<T>& list)
160 int res = read(length);
161 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "read : %d", res);
163 for (int i = 0; i < length; ++i)
167 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "read : %d", res);
171 return PRIV_MGR_ERROR_SUCCESS;
174 template < typename T >
175 int read (std::list<T>* pList)
180 template < typename K, typename P >
181 void read (std::pair<K, P>& pair)
183 int res = read( pair.first);
184 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "read : %d", res);
185 res = read( pair.second);
186 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "read : %d", res);
188 return PRIV_MGR_ERROR_SUCCESS;
191 template < typename K, typename P >
192 int read (std::pair<K, P>* pPair)
194 return read( *pPair);
197 template<typename T, typename ...Args>
198 int write(const T& in, const Args&... args)
201 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "write : %d", res);
202 res = write(args...);
203 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "write : %d", res);
205 return PRIV_MGR_ERROR_SUCCESS;
208 int write(const std::string& in)
210 int length = in.size();
211 int res = m_socketStream.writeStream(sizeof(length), &length);
212 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "writeStream : %d", res);
213 res = m_socketStream.writeStream(length, in.c_str());
214 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "writeStream : %d", res);
216 return PRIV_MGR_ERROR_SUCCESS;
219 int write(const unsigned int& in)
221 int length = sizeof(in);
222 int res = m_socketStream.writeStream(sizeof(length), &length);
223 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "writeStream : %d", res);
224 res = m_socketStream.writeStream(length, &in);
225 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "writeStream : %d", res);
227 return PRIV_MGR_ERROR_SUCCESS;
230 int write(const int& in)
232 int length = sizeof(in);
233 int res = m_socketStream.writeStream(sizeof(length), &length);
234 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "writeStream : %d", res);
235 res = m_socketStream.writeStream(length, &in);
236 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "writeStream : %d", res);
238 return PRIV_MGR_ERROR_SUCCESS;
241 int write(const bool& in)
243 int length = sizeof(in);
244 int res = m_socketStream.writeStream(sizeof(length), &length);
245 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "writeStream : %d", res);
246 res = m_socketStream.writeStream(length, &in);
247 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "writeStream : %d", res);
249 return PRIV_MGR_ERROR_SUCCESS;
252 int write(const char*& in)
254 int length = strlen(in);
255 int res = m_socketStream.writeStream(sizeof(length), &length);
256 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "writeStream : %d", res);
257 res = m_socketStream.writeStream(length, in);
258 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "writeStream : %d", res);
260 return PRIV_MGR_ERROR_SUCCESS;
263 template<typename T, typename ...Args>
264 int write(const T* in, const Args&... args)
267 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "write : %d", res);
268 res = write(args...);
269 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "write : %d", res);
271 return PRIV_MGR_ERROR_SUCCESS;
274 template<typename K, typename T>
275 int write(const std::pair<K, T> p)
277 int res = write(p.first);
278 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "write : %d", res);
279 res = write(p.second);
280 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "write : %d", res);
282 return PRIV_MGR_ERROR_SUCCESS;
285 template<typename K, typename T>
286 int write(const std::pair<K, T&> p)
288 int res = write(p.first);
289 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "write : %d", res);
290 res = write(p.second);
291 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "write : %d", res);
293 return PRIV_MGR_ERROR_SUCCESS;
296 template<typename K, typename T>
297 int write(const std::pair<K, T&>* pPair)
299 return write(*pPair);
303 int write(const std::list <T> list)
305 int length = list.size();
306 int res = write(length);
307 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "write : %d", res);
308 for (typename std::list <T>::const_iterator iter = list.begin(); iter != list.end(); iter++) {
310 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "write : %d", res);
313 return PRIV_MGR_ERROR_SUCCESS;
317 int write(const std::list <T>* pList)
319 return write(*pList);
324 SocketStream m_socketStream;
327 #endif // _SOCKET_CONNECTION_H_