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 SocketConnection.h
18 * @author Zofia Abramowska (z.abramowska@samsung.com)
20 * @brief This file is a header of Socket Connection class with implemented templates
23 #ifndef SOCKETCONNECTION_H_
24 #define SOCKETCONNECTION_H_
26 #include <dpl/serialization.h>
27 #include <dpl/log/log.h>
29 #include "SocketStream.h"
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.
38 * Throws SocketConnectionException when read/write will not succeed or if any bad allocation
39 * exception occurs during read.
42 class SocketConnection {
49 DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
50 DECLARE_EXCEPTION_TYPE(Base, SocketConnectionException)
53 explicit SocketConnection(int socket_fd) : m_socketStream(socket_fd){
57 template<typename T, typename ...Args>
58 void read(T* out, const Args&... args ){
66 DPL::Deserialization::Deserialize(m_socketStream, *out);
69 Catch (std::bad_alloc){
70 LogError("Bad allocation error");
71 ThrowMsg(Exception::SocketConnectionException, "Bad allocation error");
74 Catch (SocketStream::Exception::SocketStreamException) {
75 LogError("Socket stream error");
76 ReThrowMsg(Exception::SocketConnectionException, "Socket stream error");
80 template<typename T, typename ...Args>
81 void write(const T& in, const Args&... args){
87 void write(const T& in){
89 DPL::Serialization::Serialize(m_socketStream, in);
90 } Catch (SocketStream::Exception::SocketStreamException) {
91 LogError("Socket stream error");
92 ReThrowMsg(Exception::SocketConnectionException, "Socket stream error");
96 template<typename T, typename ...Args>
97 void write(const T* in, const Args&... args){
103 void write(const T* in){
105 DPL::Serialization::Serialize(m_socketStream, in);
106 } Catch (SocketStream::Exception::SocketStreamException) {
107 LogError("Socket stream error");
108 ReThrowMsg(Exception::SocketConnectionException, "Socket stream error");
113 SocketStream m_socketStream;
116 #endif /* SOCKETCONNECTION_H_ */