/src/Fast-DDS/src/cpp/rtps/transport/UDPSenderResource.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2016 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 __TRANSPORT_UDPSENDERRESOURCE_HPP__ |
16 | | #define __TRANSPORT_UDPSENDERRESOURCE_HPP__ |
17 | | |
18 | | #include <fastdds/rtps/common/Locator.hpp> |
19 | | #include <fastdds/rtps/transport/SenderResource.hpp> |
20 | | |
21 | | #include <rtps/transport/ChainingSenderResource.hpp> |
22 | | #include <rtps/transport/UDPTransportInterface.h> |
23 | | |
24 | | namespace eprosima { |
25 | | namespace fastdds { |
26 | | namespace rtps { |
27 | | |
28 | | class UDPSenderResource : public SenderResource |
29 | | { |
30 | | public: |
31 | | |
32 | | UDPSenderResource( |
33 | | UDPTransportInterface& transport, |
34 | | eProsimaUDPSocket& socket, |
35 | | bool only_multicast_purpose = false, |
36 | | bool whitelisted = false) |
37 | 0 | : SenderResource(transport.kind()) |
38 | 0 | , socket_(moveSocket(socket)) |
39 | 0 | , only_multicast_purpose_(only_multicast_purpose) |
40 | 0 | , whitelisted_(whitelisted) |
41 | 0 | , transport_(transport) |
42 | 0 | { |
43 | | // Implementation functions are bound to the right transport parameters |
44 | 0 | clean_up = [this, &transport]() |
45 | 0 | { |
46 | 0 | transport.SenderResourceHasBeenClosed(socket_); |
47 | 0 | }; |
48 | |
|
49 | 0 | send_buffers_lambda_ = [this, &transport]( |
50 | 0 | const std::vector<NetworkBuffer>& buffers, |
51 | 0 | uint32_t total_bytes, |
52 | 0 | LocatorsIterator* destination_locators_begin, |
53 | 0 | LocatorsIterator* destination_locators_end, |
54 | 0 | const std::chrono::steady_clock::time_point& max_blocking_time_point) -> bool |
55 | 0 | { |
56 | 0 | return transport.send(buffers, total_bytes, socket_, destination_locators_begin, |
57 | 0 | destination_locators_end, only_multicast_purpose_, whitelisted_, |
58 | 0 | max_blocking_time_point); |
59 | 0 | }; |
60 | 0 | } |
61 | | |
62 | | virtual ~UDPSenderResource() |
63 | 0 | { |
64 | 0 | if (clean_up) |
65 | 0 | { |
66 | 0 | clean_up(); |
67 | 0 | } |
68 | 0 | } |
69 | | |
70 | | void add_locators_to_list( |
71 | | LocatorList& locators) const override |
72 | 0 | { |
73 | 0 | Locator locator; |
74 | 0 | auto local_endpoint = getSocketPtr(socket_)->local_endpoint(); |
75 | 0 | transport_.endpoint_to_locator(local_endpoint, locator); |
76 | 0 | locators.push_back(locator); |
77 | 0 | } |
78 | | |
79 | | bool check_ip_address( |
80 | | const Locator& locator) const |
81 | 0 | { |
82 | 0 | Locator sender_resource_locator; |
83 | 0 | auto local_endpoint = getSocketPtr(socket_)->local_endpoint(); |
84 | 0 | transport_.endpoint_to_locator(local_endpoint, sender_resource_locator); |
85 | 0 | return memcmp(&sender_resource_locator.address[12], &locator.address[12], 4) == 0; |
86 | 0 | } |
87 | | |
88 | | static UDPSenderResource* cast( |
89 | | TransportInterface& transport, |
90 | | SenderResource* sender_resource) |
91 | 0 | { |
92 | 0 | UDPSenderResource* returned_resource = nullptr; |
93 | |
|
94 | 0 | if (sender_resource->kind() == transport.kind()) |
95 | 0 | { |
96 | 0 | returned_resource = dynamic_cast<UDPSenderResource*>(sender_resource); |
97 | | |
98 | | //! May be chained |
99 | 0 | if (!returned_resource) |
100 | 0 | { |
101 | 0 | auto chaining_sender = dynamic_cast<ChainingSenderResource*>(sender_resource); |
102 | |
|
103 | 0 | if (chaining_sender) |
104 | 0 | { |
105 | 0 | returned_resource = dynamic_cast<UDPSenderResource*>(chaining_sender->lower_sender_cast()); |
106 | 0 | } |
107 | 0 | } |
108 | 0 | } |
109 | |
|
110 | 0 | return returned_resource; |
111 | 0 | } |
112 | | |
113 | | private: |
114 | | |
115 | | UDPSenderResource() = delete; |
116 | | |
117 | | UDPSenderResource( |
118 | | const SenderResource&) = delete; |
119 | | |
120 | | UDPSenderResource& operator =( |
121 | | const SenderResource&) = delete; |
122 | | |
123 | | eProsimaUDPSocket socket_; |
124 | | |
125 | | bool only_multicast_purpose_; |
126 | | bool whitelisted_; |
127 | | |
128 | | UDPTransportInterface& transport_; |
129 | | }; |
130 | | |
131 | | } // namespace rtps |
132 | | } // namespace fastdds |
133 | | } // namespace eprosima |
134 | | |
135 | | #endif // __TRANSPORT_UDPSENDERRESOURCE_HPP__ |