summaryrefslogtreecommitdiff
path: root/src/manager/common/message-buffer.cpp
blob: 0aa2d1107ea0ef8d87c09738f0f5ef65868377d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 *  Copyright (c) 2000-2019 Samsung Electronics Co., Ltd. All rights reserved
 *
 *  Contact: Dongsun Lee <ds73.lee@samsung.com>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 */
/*
 * @file        message-buffer.cpp
 * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
 * @author      Zofia Abramowska (z.abramowska@samsung.com)
 * @version     1.0
 * @brief       Implementation of MessageBuffer.
 */

#include <message-buffer.h>

#include <dpl/log/log.h>

namespace CKM {

void MessageBuffer::Push(const RawBuffer &data)
{
	m_buffer.AppendCopy(&data[0], data.size());
}

RawBuffer MessageBuffer::Pop()
{
	size_t size = m_buffer.Size();
	RawBuffer buffer;
	buffer.resize(size + sizeof(size_t));
	memcpy(&buffer[0], &size, sizeof(size_t));
	m_buffer.FlattenConsume(&buffer[sizeof(size_t)], size);
	return buffer;
}

bool MessageBuffer::Ready()
{
	CountBytesLeft();

	if (m_bytesLeft == 0)
		return false;

	if (m_bytesLeft > m_buffer.Size())
		return false;

	return true;
}

void MessageBuffer::Read(size_t num, void *bytes)
{
	CountBytesLeft();

	if (num > m_bytesLeft) {
		LogDebug("Protocol broken. OutOfData. Asked for: " << num << " Ready: " <<
				 m_bytesLeft << " Buffer.size(): " << m_buffer.Size());
		Throw(Exception::OutOfData);
	}

	m_buffer.FlattenConsume(bytes, num);
	m_bytesLeft -= num;
}

void MessageBuffer::Write(size_t num, const void *bytes)
{
	m_buffer.AppendCopy(bytes, num);
}

} // namespace CKM