You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: include/buffer/shared_buffer.hpp
+69-56Lines changed: 69 additions & 56 deletions
Original file line number
Diff line number
Diff line change
@@ -2,34 +2,56 @@
2
2
*
3
3
* ## Overview
4
4
*
5
+
* The @c shared_buffer classes provide byte buffers with internal reference counting.
6
+
* These classes can be used within asynchronous IO / networking applications where the
7
+
* lifetime of the buffer must be kept alive until a requested IO operation completes.
8
+
*
9
+
* For example, a network write is started asynchronously, which means a write request
10
+
* is started, and the write completion will happen at a later time. The byte buffer
11
+
* must be kept alive until the completion notification. Meanwhile multiple other
12
+
* write requests can happen simultaneously (even on the same thread). For reads, a
13
+
* read request can be started, and the byte buffer used for incoming data will be
14
+
* kept alive until a read request completes - i.e. data arrives.
15
+
*
16
+
* In networking applications, this allows multiple sockets to be performing reads
17
+
* and writes under one thread (or multiple threads, such as in a thread pool).
18
+
* In particular, it eliminates the (sometimes inefficient) design of "one thread
19
+
* per socket".
20
+
*
21
+
* The Connective C++ Chops Net IP library is an asynchronous networking library (using
22
+
* Asio underneath) and it uses @c shared_buffer classes for buffer lifetime management.
23
+
*
24
+
* There are two concrete classes, @c mutable_shared_buffer and @c const_shared_buffer.
25
+
* @c mutable_shared_buffer is a reference counted modifiable buffer class with
26
+
* convenience methods for appending data. @c const_shared_buffer is a reference counted
27
+
* non-modifiable buffer class. Once the object is constructed, it cannot be modified.
28
+
*
29
+
* A @c const_shared_buffer can be efficiently constructed (no buffer copies) from a
30
+
* @c mutable shared_buffer. This allows the use case of serializing data into a
31
+
* @c mutable_shared_buffer then constructing a @c const_shared_buffer for writing to
32
+
* the network.
33
+
*
34
+
* Besides the data buffer lifetime management, these utility classes eliminate data
35
+
* copies and (obviously) do not have to be used only in networking use cases.
36
+
*
5
37
* ### Additional Details
6
38
*
7
-
* There are two concrete classes:
8
-
- `mutable_shared_buffer`, a reference counted modifiable buffer class with convenience methods for appending data.
9
-
- `const_shared_buffer`, a reference counted non-modifiable buffer class. Once the object is constructed, it cannot be modified. This class is used by the Chops Net IP library for asynchronous send buffer processing.
10
-
11
-
Internally all data is stored in a `std::vector` of `std::byte`. There are ordering methods so that shared buffer objects can be stored in sequential or associative containers.
12
-
13
-
Efficient moving of data (versus copying) is enabled in multiple ways, including:
14
-
- Allowing a `const_shared_buffer` to be move constructed from a `mutable_shared_buffer`.
15
-
- Allowing a `std::vector` of `std::byte` to be moved into either shared buffer type.
16
-
17
-
The implementation is adapted from Chris Kohlhoff's reference counted buffer examples (see [References](references.md)).
18
-
* The @c mutable_shared_buffer and @c const_shared_buffer classes provide byte
19
-
* buffer classes with internal reference counting. These classes are used within
20
-
* the Chops Net IP library to manage data buffer lifetimes. The @c mutable_shared_buffer
21
-
* class can be used to construct a data buffer, and then a @c const_shared_buffer can
22
-
* be move constructed from the @c mutable_shared_buffer for use with the asynchronous
23
-
* library functions (whether Chops Net IP or C++ Networking TS or Asio). A
24
-
* @c mutable_shared_buffer can also be constructed by moving a @c std::vector of
25
-
* @c std:;bytes into it.
39
+
* Internally all data is stored in a @c std::vector of @c std::byte. There are
40
+
* convenience templated constructors so that the @c shared_buffer objects can
41
+
* be constructed from traditional byte buffers, such as @c char @c *.
26
42
*
27
-
* Besides the data buffer lifetime management, these utility classes eliminate data
28
-
* buffer copies.
43
+
* There are ordering methods so that shared buffer objects can be stored in
44
+
* sequential or associative containers.
45
+
*
46
+
* Efficient moving of data (versus copying) is enabled in multiple ways, including
47
+
* allowing a @c const_shared_buffer to be move constructed from a
48
+
* @c mutable_shared_buffer, and allowing a @c std::vector of @c std::byte to be moved
49
+
* into either @c shared_buffer type.
29
50
*
30
-
* This code is based on and modified from Chris Kohlhoff's Asio example code. It has
31
-
* been significantly modified by adding a @c mutable_shared_buffer class as well as
32
-
* adding convenience methods to the @c const_shared_buffer class.
51
+
* The implementation is adapted from Chris Kohlhoff's reference counted buffer
52
+
* examples in the Asio library. It has been significantly modified by adding a
53
+
* @c mutable_shared_buffer class as well as adding convenience methods to the
54
+
* @c const_shared_buffer class.
33
55
*
34
56
* It is likely that this shared buffer design and code will change as the C++
35
57
* Networking TS buffer features are expanded, changed, or better understood. Currently
@@ -67,7 +89,7 @@ class const_shared_buffer;
67
89
68
90
/**
69
91
* @brief A mutable (modifiable) byte buffer class with convenience methods, internally
70
-
* reference-counted for efficient copying.
92
+
* reference-counted for efficient copying and lifetime management.
71
93
*
72
94
* This class provides ownership, copying, and lifetime management for byte oriented
73
95
* buffers. In particular, it is designed to be used in conjunction with the
@@ -76,7 +98,7 @@ class const_shared_buffer;
76
98
* a reference counted buffer can be passed among multiple layers of software without
77
99
* any one layer "owning" the buffer.
78
100
*
79
-
* A std::byte pointer returned by the @c data method may be invalidated if the
101
+
* A @c std::byte pointer returned by the @c data method may be invalidated if the
80
102
* @c mutable_shared_buffer is modified in any way (this follows the usual constraints
81
103
* on @c std::vector iterator invalidation).
82
104
*
@@ -130,6 +152,9 @@ class mutable_shared_buffer {
130
152
mutable_shared_buffer(size_type(0)) {
131
153
*m_data = std::move(bv);
132
154
}
155
+
// Add constrained templated constructor for std::vector of any valid byte type,
156
+
// specially char *; will need to use reinterpret_cast constructor of
157
+
// std::shared_ptr
133
158
/**
134
159
* @brief Construct a @c mutable_shared_buffer with an initial size, contents
0 commit comments