Change contact information to Mr Dongsun Lee
[platform/core/security/key-manager.git] / src / manager / common / message-buffer.h
1 /*
2  *  Copyright (c) 2000-2019 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  *  Contact: Dongsun Lee <ds73.lee@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18 /*
19  * @file        secket-buffer.h
20  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
21  * @author      Zofia Abramowska (z.abramowska@samsung.com)
22  * @version     1.0
23  * @brief       Implementatin of MessageBuffer.
24  */
25
26 #ifndef _CENT_KEY_MNG_SOCKET_BUFFER_
27 #define _CENT_KEY_MNG_SOCKET_BUFFER_
28
29 #include <dpl/binary_queue.h>
30 #include <dpl/exception.h>
31 #include <dpl/serialization.h>
32 #include <dpl/raw-buffer.h>
33 #include <symbol-visibility.h>
34
35 namespace CKM {
36
37 class COMMON_API MessageBuffer : public CKM::IStream {
38 public:
39         class Exception {
40         public:
41                 DECLARE_EXCEPTION_TYPE(CKM::Exception, Base)
42                 DECLARE_EXCEPTION_TYPE(Base, OutOfData)
43         };
44
45         MessageBuffer() : m_bytesLeft(0) {}
46
47         MessageBuffer(MessageBuffer &&) = default;
48         MessageBuffer &operator=(MessageBuffer &&) = default;
49
50         void Push(const RawBuffer &data);
51
52         RawBuffer Pop();
53
54         bool Ready();
55
56         virtual void Read(size_t num, void *bytes);
57
58         virtual void Write(size_t num, const void *bytes);
59
60         // generic serialization
61         template <typename... Args>
62         static MessageBuffer Serialize(const Args &... args)
63         {
64                 MessageBuffer buffer;
65                 Serializer<Args...>::Serialize(buffer, args...);
66                 return buffer;
67         }
68
69         // generic deserialization
70         template <typename... Args>
71         void Deserialize(Args &... args)
72         {
73                 Deserializer<Args...>::Deserialize(*this, args...);
74         }
75
76 protected:
77         inline void CountBytesLeft()
78         {
79                 if (m_bytesLeft > 0)
80                         return;  // we already counted m_bytesLeft nothing to do
81
82                 if (m_buffer.Size() < sizeof(size_t))
83                         return;  // we cannot count m_bytesLeft because buffer is too small
84
85                 m_buffer.FlattenConsume(&m_bytesLeft, sizeof(size_t));
86         }
87
88         size_t m_bytesLeft;
89         CKM::BinaryQueue m_buffer;
90 };
91
92 } // namespace CKM
93
94 #endif // _CENT_KEY_MNG_SOCKET_BUFFER_