2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Rafal Krypa <r.krypa@samsung.com>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * @file message-buffer.cpp
20 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
22 * @brief Implementation of MessageBuffer.
25 #include <message-buffer.h>
27 #include <dpl/log/log.h>
29 namespace SecurityManager {
31 void MessageBuffer::Push(const RawBuffer &data) {
32 m_buffer.AppendCopy(&data[0], data.size());
35 RawBuffer MessageBuffer::Pop() {
36 size_t size = m_buffer.Size();
38 buffer.resize(size + sizeof(size_t));
39 memcpy(&buffer[0], &size, sizeof(size_t));
40 m_buffer.FlattenConsume(&buffer[sizeof(size_t)], size);
44 bool MessageBuffer::Ready() {
48 if (m_bytesLeft > m_buffer.Size())
53 void MessageBuffer::Read(size_t num, void *bytes) {
55 if (num > m_bytesLeft) {
56 LogDebug("Protocol broken. OutOfData. Asked for: " << num << " Ready: " << m_bytesLeft << " Buffer.size(): " << m_buffer.Size());
57 Throw(Exception::OutOfData);
60 m_buffer.FlattenConsume(bytes, num);
64 void MessageBuffer::Write(size_t num, const void *bytes) {
65 m_buffer.AppendCopy(bytes, num);
68 } // namespace SecurityManager