/src/Fast-DDS/src/cpp/rtps/messages/SendBuffersManager.hpp
Line | Count | Source |
1 | | // Copyright 2019 Proyectos y Sistemas de Mantenimiento SL (eProsima). |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | /** |
16 | | * @file SendBuffersManager.hpp |
17 | | */ |
18 | | |
19 | | #ifndef RTPS_MESSAGES_SENDBUFFERSMANAGER_HPP |
20 | | #define RTPS_MESSAGES_SENDBUFFERSMANAGER_HPP |
21 | | #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC |
22 | | |
23 | | #include "RTPSMessageGroup_t.hpp" |
24 | | #include <fastdds/rtps/common/GuidPrefix_t.hpp> |
25 | | #include <fastdds/utils/TimedMutex.hpp> |
26 | | #include <fastdds/utils/TimedConditionVariable.hpp> |
27 | | |
28 | | #include <vector> // std::vector |
29 | | #include <memory> // std::unique_ptr |
30 | | |
31 | | |
32 | | namespace eprosima { |
33 | | namespace fastdds { |
34 | | namespace rtps { |
35 | | |
36 | | class RTPSParticipantImpl; |
37 | | |
38 | | /** |
39 | | * Manages a pool of send buffers. |
40 | | * @ingroup WRITER_MODULE |
41 | | */ |
42 | | class SendBuffersManager |
43 | | { |
44 | | public: |
45 | | |
46 | | /** |
47 | | * Construct a SendBuffersManager. |
48 | | * @param reserved_size Initial size for the pool. |
49 | | * @param allow_growing Whether we allow creation of more than reserved_size elements. |
50 | | * @param num_network_buffers Number of network buffers to allocate for each send buffer. |
51 | | * @param inc_network_buffers Number of network buffers to dynamically allocate when growing the vector. |
52 | | */ |
53 | | SendBuffersManager( |
54 | | size_t reserved_size, |
55 | | bool allow_growing, |
56 | | ResourceLimitedContainerConfig network_buffers_config); |
57 | | |
58 | | ~SendBuffersManager() |
59 | 0 | { |
60 | | assert(pool_.size() == n_created_); |
61 | 0 | } |
62 | | |
63 | | /** |
64 | | * Initialization of pool. |
65 | | * Fills the pool to its reserved capacity. |
66 | | * @param participant Pointer to the participant creating the pool. |
67 | | */ |
68 | | void init( |
69 | | const RTPSParticipantImpl* participant); |
70 | | |
71 | | /** |
72 | | * Get one buffer from the pool. |
73 | | * @param participant Pointer to the participant asking for a buffer. |
74 | | * @param max_blocking_time Maximum time the function can be blocked. |
75 | | * @return unique pointer to a send buffer. |
76 | | */ |
77 | | std::unique_ptr<RTPSMessageGroup_t> get_buffer( |
78 | | const RTPSParticipantImpl* participant, |
79 | | const std::chrono::steady_clock::time_point& max_blocking_time); |
80 | | |
81 | | /** |
82 | | * Return one buffer to the pool. |
83 | | * @param buffer unique pointer to the buffer being returned. |
84 | | */ |
85 | | void return_buffer( |
86 | | std::unique_ptr<RTPSMessageGroup_t>&& buffer); |
87 | | |
88 | | private: |
89 | | |
90 | | void add_one_buffer( |
91 | | const RTPSParticipantImpl* participant); |
92 | | |
93 | | //!Protects all data |
94 | | TimedMutex mutex_; |
95 | | //!Send buffers pool |
96 | | std::vector<std::unique_ptr<RTPSMessageGroup_t>> pool_; |
97 | | //!Raw buffer shared by the buffers created inside init() |
98 | | std::vector<octet> common_buffer_; |
99 | | //!Creation counter |
100 | | std::size_t n_created_ = 0; |
101 | | //!Whether we allow n_created_ to grow beyond the pool_ capacity. |
102 | | bool allow_growing_ = true; |
103 | | //!To wait for a buffer to be returned to the pool. |
104 | | TimedConditionVariable available_cv_; |
105 | | //!Configuration for the network buffers. |
106 | | ResourceLimitedContainerConfig network_buffers_config_ = ResourceLimitedContainerConfig(16u, |
107 | | std::numeric_limits<size_t>::max dummy_avoid_winmax (), 16u); |
108 | | |
109 | | }; |
110 | | |
111 | | } /* namespace rtps */ |
112 | | } /* namespace fastdds */ |
113 | | } /* namespace eprosima */ |
114 | | |
115 | | #endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC |
116 | | |
117 | | #endif // RTPS_MESSAGES_SENDBUFFERSMANAGER_HPP |