2 * Copyright (c) 2011 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 rpc_function.h
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the header file for RPC function
22 #ifndef DPL_RPC_FUNCTION_H
23 #define DPL_RPC_FUNCTION_H
25 #include <dpl/exception.h>
26 #include <dpl/binary_queue.h>
27 #include <dpl/scoped_array.h>
28 #include <dpl/string.h>
29 #include <dpl/serialization.h>
37 class RPCFunction : public IStream
40 BinaryQueue m_buffer; ///< Serialized RPC function call as a binary queue
46 DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
47 DECLARE_EXCEPTION_TYPE(Base, ParseFailed)
60 * @param buffer Binary queue to copy initialization data from
62 RPCFunction(const BinaryQueue &buffer)
64 m_buffer.AppendCopyFrom(buffer);
70 virtual ~RPCFunction()
75 * Append argument to call
77 * @param[in] arg Template based argument to append
79 * @warning Carefully add any pointers to buffer because of template nature of this method
81 template<typename Type>
82 void AppendArg(const Type &arg)
84 size_t argSize = sizeof(arg);
85 m_buffer.AppendCopy(&argSize, sizeof(argSize));
86 m_buffer.AppendCopy(&arg, sizeof(arg));
90 * Append @a std::string argument to call
92 * @param[in] arg String to append to function call
95 void AppendArg(const std::string &arg)
97 size_t argSize = arg.size();
98 m_buffer.AppendCopy(&argSize, sizeof(argSize));
99 m_buffer.AppendCopy(arg.c_str(), argSize);
103 * Append @a DPL::String argument to call
105 * @param[in] arg String to append to function call
108 void AppendArg(const String &arg)
110 std::string localStdString = ToUTF8String(arg);
111 AppendArg(localStdString);
115 * Consume argument from call. Arguments are retrieved in non-reversed order
116 * (same as they were pushed onto RPC function argument stack)
118 * @param[out] arg Reference to output template based argument
119 * @warning Carefully add any pointers to buffer because of template nature of this method
122 template<typename Type>
123 void ConsumeArg(Type &arg)
127 size_t argSize = sizeof(arg);
128 m_buffer.FlattenConsume(&argSize, sizeof(argSize));
130 if (argSize != sizeof(arg))
131 ThrowMsg(Exception::ParseFailed, "Stream parse CRC failed");
133 m_buffer.FlattenConsume(&arg, sizeof(arg));
135 Catch (BinaryQueue::Exception::OutOfData)
137 ReThrowMsg(Exception::ParseFailed, "Unexpected end of stream");
142 * Consume @a std::string argument from call. Arguments are retrieved in non-reversed order
143 * (same as they were pushed onto RPC function argument stack)
145 * @param[out] arg Reference to output @a std::string argument
148 void ConsumeArg(std::string &arg)
152 std::string::size_type size;
153 m_buffer.FlattenConsume(&size, sizeof(size));
154 ScopedArray<char> str(new char[size]);
155 m_buffer.FlattenConsume(str.Get(), size);
156 arg = std::string(str.Get(), str.Get() + size);
158 Catch (BinaryQueue::Exception::OutOfData)
160 ReThrowMsg(Exception::ParseFailed, "Unexpected end of stream");
165 * Consume @a DPL::String argument from call. Arguments are converted to UTF-8 string
167 * @param[out] arg Reference to output @a DPL::String argument
170 void ConsumeArg(String &arg)
172 std::string consumedStdString;
173 ConsumeArg(consumedStdString);
174 arg = FromUTF8String(consumedStdString);
178 * Serialize all function parameters into single binary queue
180 * @return Serialized binary queue representation of RPC function
182 BinaryQueue Serialize() const
188 * Reads binary data from serialized stream
190 * @param num number of bytes to read
191 * @param bytes buffer for read data
193 virtual void Read(size_t num, void * bytes)
195 m_buffer.FlattenConsume(bytes, num);
199 * Writes binary data to serialized stream
201 * @param num number of bytes to write to serialization buffer
202 * @param bytes buffer for data to write
204 virtual void Write(size_t num, const void * bytes)
206 m_buffer.AppendCopy(bytes, num);
213 #endif // DPL_RPC_FUNCTION_H