diff --git a/src/cpp/rtps/transport/TCPChannelResource.h b/src/cpp/rtps/transport/TCPChannelResource.h index 3443e5d211c..1f795c37c6e 100644 --- a/src/cpp/rtps/transport/TCPChannelResource.h +++ b/src/cpp/rtps/transport/TCPChannelResource.h @@ -130,13 +130,33 @@ class TCPChannelResource : public ChannelResource size_t size, asio::error_code& ec) = 0; + /** + * @brief Gets the remote endpoint of the socket connection. + * @throws Exception on failure. + * @return asio::ip::tcp::endpoint of the remote endpoint. + */ virtual asio::ip::tcp::endpoint remote_endpoint() const = 0; + /** + * @brief Gets the local endpoint of the socket connection. + * @throws Exception on failure. + * @return asio::ip::tcp::endpoint of the local endpoint. + */ virtual asio::ip::tcp::endpoint local_endpoint() const = 0; + /** + * @brief Gets the remote endpoint, setting error code if any. + * @param ec Set to indicate what error occurred, if any. + * @return asio::ip::tcp::endpoint of the remote endpoint or returns a default-constructed endpoint object if an error occurred. + */ virtual asio::ip::tcp::endpoint remote_endpoint( asio::error_code& ec) const = 0; + /** + * @brief Gets the local endpoint, setting error code if any. + * @param ec Set to indicate what error occurred, if any. + * @return asio::ip::tcp::endpoint of the remote endpoint or returns a default-constructed endpoint object if an error occurred. + */ virtual asio::ip::tcp::endpoint local_endpoint( asio::error_code& ec) const = 0; diff --git a/src/cpp/rtps/transport/TCPChannelResourceBasic.h b/src/cpp/rtps/transport/TCPChannelResourceBasic.h index e0de0da0c6e..20928f0f494 100644 --- a/src/cpp/rtps/transport/TCPChannelResourceBasic.h +++ b/src/cpp/rtps/transport/TCPChannelResourceBasic.h @@ -65,11 +65,11 @@ class TCPChannelResourceBasic : public TCPChannelResource size_t size, asio::error_code& ec) override; - // Non-throwing asio calls + // Throwing asio calls asio::ip::tcp::endpoint remote_endpoint() const override; asio::ip::tcp::endpoint local_endpoint() const override; - // Throwing asio calls + // Non-throwing asio calls asio::ip::tcp::endpoint remote_endpoint( asio::error_code& ec) const override; asio::ip::tcp::endpoint local_endpoint( diff --git a/src/cpp/rtps/transport/TCPTransportInterface.cpp b/src/cpp/rtps/transport/TCPTransportInterface.cpp index 65e8b333f02..49576f1c11a 100644 --- a/src/cpp/rtps/transport/TCPTransportInterface.cpp +++ b/src/cpp/rtps/transport/TCPTransportInterface.cpp @@ -260,6 +260,19 @@ void TCPTransportInterface::clean() } } +Locator TCPTransportInterface::remote_endpoint_to_locator( + const std::shared_ptr& channel) const +{ + Locator locator; + asio::error_code ec; + endpoint_to_locator(channel->remote_endpoint(ec), locator); + if (ec) + { + LOCATOR_INVALID(locator); + } + return locator; +} + void TCPTransportInterface::bind_socket( std::shared_ptr& channel) { @@ -898,12 +911,8 @@ void TCPTransportInterface::perform_listen_operation( if (rtcp_message_manager) { channel = channel_weak.lock(); - asio::error_code ec; - endpoint_to_locator(channel->remote_endpoint(ec), remote_locator); - if (ec) - { - remote_locator.kind = LOCATOR_KIND_INVALID; - } + + remote_locator = remote_endpoint_to_locator(channel); if (channel) { @@ -1185,7 +1194,7 @@ bool TCPTransportInterface::Receive( { if (!IsLocatorValid(remote_locator)) { - endpoint_to_locator(channel->remote_endpoint(), remote_locator); + remote_locator = remote_endpoint_to_locator(channel); } IPLocator::setLogicalPort(remote_locator, tcp_header.logical_port); EPROSIMA_LOG_INFO(RTCP_MSG_IN, "[RECEIVE] From: " << remote_locator \ diff --git a/src/cpp/rtps/transport/TCPTransportInterface.h b/src/cpp/rtps/transport/TCPTransportInterface.h index f0a2ac082a2..0667fcf81be 100644 --- a/src/cpp/rtps/transport/TCPTransportInterface.h +++ b/src/cpp/rtps/transport/TCPTransportInterface.h @@ -188,6 +188,12 @@ class TCPTransportInterface : public TransportInterface const asio::ip::tcp::endpoint& endpoint, Locator& locator) const = 0; + /** + * Converts a remote endpoint to a locator if possible. Otherwise, it sets an invalid locator. + */ + Locator remote_endpoint_to_locator( + const std::shared_ptr& channel) const; + /** * Shutdown method to close the connections of the transports. */