OpenVPN 3 Core Library
Loading...
Searching...
No Matches
memq.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12// A queue of buffers, implemented as std::deque<BufferPtr>.
13
14#ifndef OPENVPN_BUFFER_MEMQ_H
15#define OPENVPN_BUFFER_MEMQ_H
16
17#include <deque>
18
21
22namespace openvpn {
23
25{
26 public:
28 : length(0)
29 {
30 }
31
32 size_t size() const
33 {
34 return q.size();
35 }
36
37 bool empty() const
38 {
39 return q.empty();
40 }
41
42 size_t total_length() const
43 {
44 return length;
45 }
46
47 void clear()
48 {
49 while (!q.empty())
50 q.pop_back();
51 length = 0;
52 }
53
54 void write_buf(const BufferPtr &bp)
55 {
56 // An empty buffer has nothing to queue but would still make empty() lie, since that
57 // answers for the queue and not for what is in it: the queue would look ready while
58 // reading it yields nothing, and a reader with no bytes and no reason to wait takes
59 // that for the end of the stream.
60 if (bp->empty())
61 return;
62
63 q.push_back(bp);
64 length += bp->size();
65 }
66
68 {
69 BufferPtr ret = q.front();
70 q.pop_front();
71 length -= ret->size();
72 return ret;
73 }
74
76 {
77 return q.front();
78 }
79
80 void pop()
81 {
82 length -= q.front()->size();
83 q.pop_front();
84 }
85
86 void resize(const size_t cap)
87 {
88 q.resize(cap);
89 }
90
91 protected:
92 using q_type = std::deque<BufferPtr>;
93 size_t length;
95};
96
97} // namespace openvpn
98
99#endif // OPENVPN_BUFFER_MEMQ_H
bool empty() const
Definition memq.hpp:37
size_t total_length() const
Definition memq.hpp:42
size_t size() const
Definition memq.hpp:32
void resize(const size_t cap)
Definition memq.hpp:86
std::deque< BufferPtr > q_type
Definition memq.hpp:92
size_t length
Definition memq.hpp:93
BufferPtr read_buf()
Definition memq.hpp:67
void write_buf(const BufferPtr &bp)
Definition memq.hpp:54
BufferPtr & peek()
Definition memq.hpp:75