Refactoring of directory structure and CMake files
[platform/core/security/security-manager.git] / src / common / include / message-buffer.h
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Rafal Krypa <r.krypa@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  * @version     1.0
22  * @brief       Implementatin of MessageBuffer.
23  */
24
25 #ifndef _SECURITY_MANAGER_SOCKET_BUFFER_
26 #define _SECURITY_MANAGER_SOCKET_BUFFER_
27
28 #include <vector>
29
30 #include <dpl/binary_queue.h>
31 #include <dpl/exception.h>
32 #include <dpl/serialization.h>
33
34 namespace SecurityManager {
35
36 typedef std::vector<unsigned char> RawBuffer;
37
38 class MessageBuffer : public SecurityManager::IStream {
39 public:
40     class Exception
41     {
42     public:
43         DECLARE_EXCEPTION_TYPE(SecurityManager::Exception, Base)
44         DECLARE_EXCEPTION_TYPE(Base, OutOfData)
45     };
46
47     MessageBuffer()
48       : m_bytesLeft(0)
49     {}
50
51     void Push(const RawBuffer &data);
52
53     RawBuffer Pop();
54
55     bool Ready();
56
57     virtual void Read(size_t num, void *bytes);
58
59     virtual void Write(size_t num, const void *bytes);
60
61 protected:
62
63     inline void CountBytesLeft() {
64         if (m_bytesLeft > 0)
65             return;  // we already counted m_bytesLeft nothing to do
66
67         if (m_buffer.Size() < sizeof(size_t))
68             return;  // we cannot count m_bytesLeft because buffer is too small
69
70         m_buffer.FlattenConsume(&m_bytesLeft, sizeof(size_t));
71     }
72
73     size_t m_bytesLeft;
74     SecurityManager::BinaryQueue m_buffer;
75 };
76
77 } // namespace SecurityManager
78
79 #endif // _SECURITY_MANAGER_SOCKET_BUFFER_