688186a8a029ea3cf87eafcfa6a8f30a27b8fe5e
[framework/security/security-server.git] / src / server2 / common / socket-buffer.cpp
1 /*
2  *  Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Bumjin Im <bj.im@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        socket-buffer.cpp
20  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
21  * @version     1.0
22  * @brief       Implementation of SocketBuffer.
23  */
24
25 #include <socket-buffer.h>
26
27 #include <dpl/log/log.h>
28
29 namespace SecurityServer {
30
31 void SocketBuffer::Push(const RawBuffer &data) {
32     LogDebug("Push data: " << data.size());
33     m_buffer.AppendCopy(&data[0], data.size());
34 }
35
36 RawBuffer SocketBuffer::Pop() {
37     size_t size = m_buffer.Size();
38     RawBuffer buffer;
39     buffer.resize(size + sizeof(size_t));
40     memcpy(&buffer[0], &size, sizeof(size_t));
41     m_buffer.FlattenConsume(&buffer[sizeof(size_t)], size);
42     return buffer;
43 }
44
45 bool SocketBuffer::Ready() {
46     CountBytesLeft();
47     LogInfo("m_buffer.Size() == " << m_buffer.Size());
48     if (m_bytesLeft == 0)
49         return false;
50     if (m_bytesLeft > m_buffer.Size())
51         return false;
52     return true;
53 }
54
55 void SocketBuffer::Read(size_t num, void *bytes) {
56     CountBytesLeft();
57     if (num > m_bytesLeft) {
58         LogDebug("Protocol broken. OutOfData. Asked for: " << num << " Ready: " << m_bytesLeft << " Buffer.size(): " << m_buffer.Size());
59         Throw(Exception::OutOfData);
60     }
61
62     m_buffer.FlattenConsume(bytes, num);
63     m_bytesLeft -= num;
64 }
65
66 void SocketBuffer::Write(size_t num, const void *bytes) {
67     m_buffer.AppendCopy(bytes, num);
68 }
69
70 } // namespace SecurityServer
71