/src/Fast-DDS/src/cpp/rtps/transport/UDPChannelResource.h
Line | Count | Source (jump to first uncovered line) |
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 | | #ifndef _FASTDDS_UDP_CHANNEL_RESOURCE_INFO_ |
16 | | #define _FASTDDS_UDP_CHANNEL_RESOURCE_INFO_ |
17 | | |
18 | | #include <asio.hpp> |
19 | | |
20 | | #include <fastdds/rtps/attributes/ThreadSettings.hpp> |
21 | | #include <fastdds/rtps/common/Locator.hpp> |
22 | | #include <fastdds/rtps/common/LocatorWithMask.hpp> |
23 | | #include <fastdds/rtps/transport/network/NetmaskFilterKind.hpp> |
24 | | |
25 | | #include <rtps/transport/ChannelResource.h> |
26 | | |
27 | | namespace eprosima { |
28 | | namespace fastdds { |
29 | | namespace rtps { |
30 | | |
31 | | class TransportReceiverInterface; |
32 | | class UDPTransportInterface; |
33 | | |
34 | | #if defined(ASIO_HAS_MOVE) |
35 | | // Typedefs |
36 | | class eProsimaUDPSocket : public asio::ip::udp::socket |
37 | | { |
38 | | public: |
39 | | |
40 | | explicit eProsimaUDPSocket( |
41 | | asio::io_service& io_service) |
42 | 0 | : asio::ip::udp::socket(io_service) |
43 | 0 | { |
44 | 0 | } |
45 | | |
46 | | bool should_filter( |
47 | | const Locator& dest_locator) |
48 | 0 | { |
49 | 0 | return netmask_filter == NetmaskFilterKind::ON && !locator.matches(dest_locator); |
50 | 0 | } |
51 | | |
52 | | LocatorWithMask locator; |
53 | | NetmaskFilterKind netmask_filter = NetmaskFilterKind::AUTO; |
54 | | }; |
55 | | typedef eProsimaUDPSocket& eProsimaUDPSocketRef; |
56 | | |
57 | | // UDP |
58 | | inline eProsimaUDPSocket* getSocketPtr( |
59 | | eProsimaUDPSocket& socket) |
60 | 0 | { |
61 | 0 | return &socket; |
62 | 0 | } |
63 | | |
64 | | inline const eProsimaUDPSocket* getSocketPtr( |
65 | | const eProsimaUDPSocket& socket) |
66 | 0 | { |
67 | 0 | return &socket; |
68 | 0 | } |
69 | | |
70 | | inline eProsimaUDPSocket moveSocket( |
71 | | eProsimaUDPSocket& socket) |
72 | 0 | { |
73 | 0 | return std::move(socket); |
74 | 0 | } |
75 | | |
76 | | inline eProsimaUDPSocket createUDPSocket( |
77 | | asio::io_service& io_service) |
78 | 0 | { |
79 | 0 | return eProsimaUDPSocket(io_service); |
80 | 0 | } |
81 | | |
82 | | inline eProsimaUDPSocket& getRefFromPtr( |
83 | | eProsimaUDPSocket* socket) |
84 | 0 | { |
85 | 0 | return *socket; |
86 | 0 | } |
87 | | |
88 | | #else |
89 | | // Typedefs |
90 | | class eProsimaUDPSocket : public std::shared_ptr<asio::ip::udp::socket> |
91 | | { |
92 | | public: |
93 | | |
94 | | explicit eProsimaUDPSocket( |
95 | | asio::io_service& io_service) |
96 | | : shared_ptr<asio::ip::udp::socket>(io_service) |
97 | | { |
98 | | } |
99 | | |
100 | | bool should_filter( |
101 | | const Locator& dest_locator) |
102 | | { |
103 | | return netmask_filter == NetmaskFilterKind::ON && !locator.matches(dest_locator); |
104 | | } |
105 | | |
106 | | LocatorWithMask locator; |
107 | | NetmaskFilterKind netmask_filter = NetmaskFilterKind::AUTO; |
108 | | }; |
109 | | typedef eProsimaUDPSocket eProsimaUDPSocketRef; |
110 | | |
111 | | // UDP |
112 | | inline eProsimaUDPSocket getSocketPtr( |
113 | | eProsimaUDPSocket socket) |
114 | | { |
115 | | return socket; |
116 | | } |
117 | | |
118 | | inline const eProsimaUDPSocket& getSocketPtr( |
119 | | const eProsimaUDPSocket& socket) |
120 | | { |
121 | | return socket; |
122 | | } |
123 | | |
124 | | inline eProsimaUDPSocket moveSocket( |
125 | | eProsimaUDPSocket socket) |
126 | | { |
127 | | return socket; |
128 | | } |
129 | | |
130 | | inline eProsimaUDPSocket createUDPSocket( |
131 | | asio::io_service& io_service) |
132 | | { |
133 | | return eProsimaUDPSocket(io_service); |
134 | | } |
135 | | |
136 | | inline eProsimaUDPSocket getRefFromPtr( |
137 | | eProsimaUDPSocket socket) |
138 | | { |
139 | | return socket; |
140 | | } |
141 | | |
142 | | #endif // if defined(ASIO_HAS_MOVE) |
143 | | |
144 | | class UDPChannelResource : public ChannelResource |
145 | | { |
146 | | public: |
147 | | |
148 | | UDPChannelResource( |
149 | | UDPTransportInterface* transport, |
150 | | eProsimaUDPSocket& socket, |
151 | | uint32_t maxMsgSize, |
152 | | const Locator& locator, |
153 | | const std::string& sInterface, |
154 | | TransportReceiverInterface* receiver, |
155 | | const ThreadSettings& thread_config); |
156 | | |
157 | | virtual ~UDPChannelResource() override; |
158 | | |
159 | | UDPChannelResource& operator =( |
160 | | UDPChannelResource&& channelResource) |
161 | 0 | { |
162 | 0 | socket_ = moveSocket(channelResource.socket_); |
163 | 0 | return *this; |
164 | 0 | } |
165 | | |
166 | | void only_multicast_purpose( |
167 | | const bool value) |
168 | 0 | { |
169 | 0 | only_multicast_purpose_ = value; |
170 | 0 | } |
171 | | |
172 | | bool& only_multicast_purpose() |
173 | 0 | { |
174 | 0 | return only_multicast_purpose_; |
175 | 0 | } |
176 | | |
177 | | bool only_multicast_purpose() const |
178 | 0 | { |
179 | 0 | return only_multicast_purpose_; |
180 | 0 | } |
181 | | |
182 | | #if defined(ASIO_HAS_MOVE) |
183 | | inline eProsimaUDPSocket* socket() |
184 | | #else |
185 | | inline eProsimaUDPSocket socket() |
186 | | #endif // if defined(ASIO_HAS_MOVE) |
187 | 0 | { |
188 | 0 | return getSocketPtr(socket_); |
189 | 0 | } |
190 | | |
191 | | inline void iface( |
192 | | const std::string& iface) |
193 | 0 | { |
194 | 0 | interface_ = iface; |
195 | 0 | } |
196 | | |
197 | | inline const std::string& iface() const |
198 | 0 | { |
199 | 0 | return interface_; |
200 | 0 | } |
201 | | |
202 | | inline void message_receiver( |
203 | | TransportReceiverInterface* receiver) |
204 | 0 | { |
205 | 0 | message_receiver_ = receiver; |
206 | 0 | } |
207 | | |
208 | | inline TransportReceiverInterface* message_receiver() |
209 | 0 | { |
210 | 0 | return message_receiver_; |
211 | 0 | } |
212 | | |
213 | | inline virtual void disable() override |
214 | 0 | { |
215 | 0 | ChannelResource::disable(); |
216 | 0 | } |
217 | | |
218 | | void release(); |
219 | | |
220 | | protected: |
221 | | |
222 | | /** |
223 | | * Function to be called from a new thread, which takes cares of performing a blocking receive |
224 | | * operation on the ReceiveResource |
225 | | * @param input_locator - Locator that triggered the creation of the resource |
226 | | */ |
227 | | void perform_listen_operation( |
228 | | Locator input_locator); |
229 | | |
230 | | /** |
231 | | * Blocking Receive from the specified channel. |
232 | | * @param receive_buffer vector with enough capacity (not size) to accomodate a full receive buffer. That |
233 | | * capacity must not be less than the receive_buffer_size supplied to this class during construction. |
234 | | * @param receive_buffer_capacity Maximum size of the receive_buffer. |
235 | | * @param [out] receive_buffer_size Size of the received buffer. |
236 | | * @param [out] remote_locator Locator describing the remote restination we received a packet from. |
237 | | */ |
238 | | bool Receive( |
239 | | octet* receive_buffer, |
240 | | uint32_t receive_buffer_capacity, |
241 | | uint32_t& receive_buffer_size, |
242 | | Locator& remote_locator); |
243 | | |
244 | | private: |
245 | | |
246 | | TransportReceiverInterface* message_receiver_; //Associated Readers/Writers inside of MessageReceiver |
247 | | eProsimaUDPSocket socket_; |
248 | | bool only_multicast_purpose_; |
249 | | std::string interface_; |
250 | | UDPTransportInterface* transport_; |
251 | | |
252 | | UDPChannelResource( |
253 | | const UDPChannelResource&) = delete; |
254 | | UDPChannelResource& operator =( |
255 | | const UDPChannelResource&) = delete; |
256 | | }; |
257 | | |
258 | | } // namespace rtps |
259 | | } // namespace fastdds |
260 | | } // namespace eprosima |
261 | | |
262 | | #endif // _FASTDDS_UDP_CHANNEL_RESOURCE_INFO_ |