Skip to content

Commit ae790cd

Browse files
cferreiragonzelianalf
authored andcommitted
Backport of #4319 && Partial backport of #4300
Signed-off-by: cferreiragonz <[email protected]>
1 parent fc323e7 commit ae790cd

9 files changed

+109
-10
lines changed

src/cpp/rtps/transport/TCPChannelResource.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,36 @@ class TCPChannelResource : public ChannelResource
130130
size_t size,
131131
asio::error_code& ec) = 0;
132132

133+
/**
134+
* @brief Gets the remote endpoint of the socket connection.
135+
* @throws Exception on failure.
136+
* @return asio::ip::tcp::endpoint of the remote endpoint.
137+
*/
133138
virtual asio::ip::tcp::endpoint remote_endpoint() const = 0;
134139

140+
/**
141+
* @brief Gets the local endpoint of the socket connection.
142+
* @throws Exception on failure.
143+
* @return asio::ip::tcp::endpoint of the local endpoint.
144+
*/
135145
virtual asio::ip::tcp::endpoint local_endpoint() const = 0;
136146

147+
/**
148+
* @brief Gets the remote endpoint, setting error code if any.
149+
* @param ec Set to indicate what error occurred, if any.
150+
* @return asio::ip::tcp::endpoint of the remote endpoint or returns a default-constructed endpoint object if an error occurred.
151+
*/
152+
virtual asio::ip::tcp::endpoint remote_endpoint(
153+
asio::error_code& ec) const = 0;
154+
155+
/**
156+
* @brief Gets the local endpoint, setting error code if any.
157+
* @param ec Set to indicate what error occurred, if any.
158+
* @return asio::ip::tcp::endpoint of the remote endpoint or returns a default-constructed endpoint object if an error occurred.
159+
*/
160+
virtual asio::ip::tcp::endpoint local_endpoint(
161+
asio::error_code& ec) const = 0;
162+
137163
virtual void set_options(
138164
const TCPTransportDescriptor* options) = 0;
139165

src/cpp/rtps/transport/TCPChannelResourceBasic.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,18 @@ asio::ip::tcp::endpoint TCPChannelResourceBasic::remote_endpoint() const
182182

183183
asio::ip::tcp::endpoint TCPChannelResourceBasic::local_endpoint() const
184184
{
185-
std::error_code ec;
185+
return socket_->local_endpoint();
186+
}
187+
188+
asio::ip::tcp::endpoint TCPChannelResourceBasic::remote_endpoint(
189+
asio::error_code& ec) const
190+
{
191+
return socket_->remote_endpoint(ec);
192+
}
193+
194+
asio::ip::tcp::endpoint TCPChannelResourceBasic::local_endpoint(
195+
asio::error_code& ec) const
196+
{
186197
return socket_->local_endpoint(ec);
187198
}
188199

src/cpp/rtps/transport/TCPChannelResourceBasic.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,16 @@ class TCPChannelResourceBasic : public TCPChannelResource
6565
size_t size,
6666
asio::error_code& ec) override;
6767

68+
// Throwing asio calls
6869
asio::ip::tcp::endpoint remote_endpoint() const override;
6970
asio::ip::tcp::endpoint local_endpoint() const override;
7071

72+
// Non-throwing asio calls
73+
asio::ip::tcp::endpoint remote_endpoint(
74+
asio::error_code& ec) const override;
75+
asio::ip::tcp::endpoint local_endpoint(
76+
asio::error_code& ec) const override;
77+
7178
void set_options(
7279
const TCPTransportDescriptor* options) override;
7380

src/cpp/rtps/transport/TCPChannelResourceSecure.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,18 @@ asio::ip::tcp::endpoint TCPChannelResourceSecure::local_endpoint() const
264264
return secure_socket_->lowest_layer().local_endpoint();
265265
}
266266

267+
asio::ip::tcp::endpoint TCPChannelResourceSecure::remote_endpoint(
268+
asio::error_code& ec) const
269+
{
270+
return secure_socket_->lowest_layer().remote_endpoint(ec);
271+
}
272+
273+
asio::ip::tcp::endpoint TCPChannelResourceSecure::local_endpoint(
274+
asio::error_code& ec) const
275+
{
276+
return secure_socket_->lowest_layer().local_endpoint(ec);
277+
}
278+
267279
void TCPChannelResourceSecure::set_options(
268280
const TCPTransportDescriptor* options)
269281
{

src/cpp/rtps/transport/TCPChannelResourceSecure.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,16 @@ class TCPChannelResourceSecure : public TCPChannelResource
6565
size_t size,
6666
asio::error_code& ec) override;
6767

68+
// Throwing asio calls
6869
asio::ip::tcp::endpoint remote_endpoint() const override;
6970
asio::ip::tcp::endpoint local_endpoint() const override;
7071

72+
// Non-throwing asio calls
73+
asio::ip::tcp::endpoint remote_endpoint(
74+
asio::error_code& ec) const override;
75+
asio::ip::tcp::endpoint local_endpoint(
76+
asio::error_code& ec) const override;
77+
7178
void set_options(
7279
const TCPTransportDescriptor* options) override;
7380

src/cpp/rtps/transport/TCPTransportInterface.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -927,10 +927,11 @@ void TCPTransportInterface::perform_listen_operation(
927927
if (rtcp_message_manager)
928928
{
929929
channel = channel_weak.lock();
930-
endpoint_to_locator(channel->remote_endpoint(), remote_locator);
931930

932931
if (channel)
933932
{
933+
remote_locator = remote_endpoint_to_locator(channel);
934+
934935
if (channel->tcp_connection_type() == TCPChannelResource::TCPConnectionType::TCP_CONNECT_TYPE)
935936
{
936937
rtcp_message_manager->sendConnectionRequest(channel);
@@ -1202,6 +1203,11 @@ bool TCPTransportInterface::Receive(
12021203
}
12031204
else
12041205
{
1206+
if (!IsLocatorValid(remote_locator))
1207+
{
1208+
remote_locator = remote_endpoint_to_locator(channel);
1209+
}
1210+
12051211
IPLocator::setLogicalPort(remote_locator, tcp_header.logical_port);
12061212
EPROSIMA_LOG_INFO(RTCP_MSG_IN, "[RECEIVE] From: " << remote_locator \
12071213
<< " - " << receive_buffer_size << " bytes.");
@@ -1419,10 +1425,8 @@ void TCPTransportInterface::SocketAccepted(
14191425
channel_weak_ptr, rtcp_manager_weak_ptr));
14201426

14211427
EPROSIMA_LOG_INFO(RTCP, "Accepted connection (local: "
1422-
<< channel->local_endpoint().address() << ":"
1423-
<< channel->local_endpoint().port() << "), remote: "
1424-
<< channel->remote_endpoint().address() << ":"
1425-
<< channel->remote_endpoint().port() << ")");
1428+
<< local_endpoint_to_locator(channel) << ", remote: "
1429+
<< remote_endpoint_to_locator(channel) << ")");
14261430
}
14271431
else
14281432
{
@@ -1467,10 +1471,8 @@ void TCPTransportInterface::SecureSocketAccepted(
14671471
channel_weak_ptr, rtcp_manager_weak_ptr));
14681472

14691473
EPROSIMA_LOG_INFO(RTCP, " Accepted connection (local: "
1470-
<< socket->lowest_layer().local_endpoint().address() << ":"
1471-
<< socket->lowest_layer().local_endpoint().port() << "), remote: "
1472-
<< socket->lowest_layer().remote_endpoint().address() << ":"
1473-
<< socket->lowest_layer().remote_endpoint().port() << ")");
1474+
<< local_endpoint_to_locator(secure_channel) << ", remote: "
1475+
<< remote_endpoint_to_locator(secure_channel) << ")");
14741476
}
14751477
else
14761478
{

src/cpp/rtps/transport/TCPTransportInterface.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,18 @@ class TCPTransportInterface : public TransportInterface
187187
const asio::ip::tcp::endpoint& endpoint,
188188
Locator& locator) const = 0;
189189

190+
/**
191+
* Converts a remote endpoint to a locator if possible. Otherwise, it sets an invalid locator.
192+
*/
193+
Locator remote_endpoint_to_locator(
194+
const std::shared_ptr<TCPChannelResource>& channel) const;
195+
196+
/**
197+
* Converts a local endpoint to a locator if possible. Otherwise, it sets an invalid locator.
198+
*/
199+
Locator local_endpoint_to_locator(
200+
const std::shared_ptr<TCPChannelResource>& channel) const;
201+
190202
/**
191203
* Shutdown method to close the connections of the transports.
192204
*/

test/unittest/transport/mock/MockTCPChannelResource.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ asio::ip::tcp::endpoint MockTCPChannelResource::local_endpoint() const
6767
return ep;
6868
}
6969

70+
asio::ip::tcp::endpoint MockTCPChannelResource::remote_endpoint(
71+
asio::error_code& ec) const
72+
{
73+
ec = asio::error_code(); // Indicate no error
74+
asio::ip::tcp::endpoint ep;
75+
return ep;
76+
}
77+
78+
asio::ip::tcp::endpoint MockTCPChannelResource::local_endpoint(
79+
asio::error_code& ec) const
80+
{
81+
ec = asio::error_code(); // Indicate no error
82+
asio::ip::tcp::endpoint ep;
83+
return ep;
84+
}
85+
7086
void MockTCPChannelResource::set_options(
7187
const TCPTransportDescriptor*)
7288
{

test/unittest/transport/mock/MockTCPChannelResource.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ class MockTCPChannelResource : public TCPChannelResource
5959

6060
asio::ip::tcp::endpoint local_endpoint() const override;
6161

62+
asio::ip::tcp::endpoint remote_endpoint(
63+
asio::error_code& ec) const override;
64+
65+
asio::ip::tcp::endpoint local_endpoint(
66+
asio::error_code& ec) const override;
67+
6268
void set_options(
6369
const TCPTransportDescriptor* options) override;
6470

0 commit comments

Comments
 (0)