tizen 2.3.1 release
[framework/web/mobile/wrt-security.git] / socket_connection / connection / SocketConnection.h
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        SocketConnection.h
18  * @author      Zofia Abramowska (z.abramowska@samsung.com)
19  * @version     1.0
20  * @brief       This file is a header of Socket Connection class with implemented templates
21  */
22
23 #ifndef SOCKETCONNECTION_H_
24 #define SOCKETCONNECTION_H_
25
26 #include <dpl/serialization.h>
27 #include <dpl/log/log.h>
28 #include <new>
29 #include "SocketStream.h"
30
31 /*
32  * This class implements interface for generic read and write from given socket.
33  * It does not maintain socket descriptor, so any connecting and disconnecting should be
34  * done above calls to this class.
35  */
36
37 /*
38  * Throws SocketConnectionException when read/write will not succeed or if any bad allocation
39  * exception occurs during read.
40  */
41
42 class SocketConnection {
43
44 public:
45
46     class Exception
47     {
48     public:
49         DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
50         DECLARE_EXCEPTION_TYPE(Base, SocketConnectionException)
51     };
52
53     explicit SocketConnection(int socket_fd) : m_socketStream(socket_fd){
54         LogInfo("Created");
55     }
56
57     template<typename T, typename ...Args>
58     void read(T* out, const Args&... args ){
59         read(out);
60         read(args...);
61     }
62
63     template<typename T>
64     void read(T* out){
65         Try {
66             DPL::Deserialization::Deserialize(m_socketStream, *out);
67         }
68
69         Catch (std::bad_alloc){
70             LogError("Bad allocation error");
71             ThrowMsg(Exception::SocketConnectionException, "Bad allocation error");
72         }
73
74         Catch (SocketStream::Exception::SocketStreamException) {
75             LogError("Socket stream error");
76             ReThrowMsg(Exception::SocketConnectionException, "Socket stream error");
77         }
78     }
79
80     template<typename T, typename ...Args>
81     void write(const T& in, const Args&... args){
82         write(in);
83         write(args...);
84     }
85
86     template<typename T>
87     void write(const T& in){
88         Try {
89             DPL::Serialization::Serialize(m_socketStream, in);
90         } Catch (SocketStream::Exception::SocketStreamException) {
91             LogError("Socket stream error");
92             ReThrowMsg(Exception::SocketConnectionException, "Socket stream error");
93         }
94     }
95
96     template<typename T, typename ...Args>
97     void write(const T* in, const Args&... args){
98         write(in);
99         write(args...);
100     }
101
102     template<typename T>
103         void write(const T* in){
104             Try {
105                 DPL::Serialization::Serialize(m_socketStream, in);
106             } Catch (SocketStream::Exception::SocketStreamException) {
107                 LogError("Socket stream error");
108                 ReThrowMsg(Exception::SocketConnectionException, "Socket stream error");
109             }
110         }
111
112 private:
113     SocketStream m_socketStream;
114 };
115
116 #endif /* SOCKETCONNECTION_H_ */