Skip to content

Commit

Permalink
Add non-throwing getters for socket info (#4319)
Browse files Browse the repository at this point in the history
* Refs #20262: Added non-throwing asio calls

Signed-off-by: Jesus Perez <jesusperez@eprosima.com>

* Refs #20262: Uncrustify

Signed-off-by: Jesus Perez <jesusperez@eprosima.com>

* Refs #20262: Apply suggestions

Signed-off-by: Jesus Perez <jesusperez@eprosima.com>

* Refs #20262: Apply suggestions

Signed-off-by: Jesus Perez <jesusperez@eprosima.com>

---------

Signed-off-by: Jesus Perez <jesusperez@eprosima.com>
  • Loading branch information
jepemi authored Feb 8, 2024
1 parent 7adc833 commit 6b484da
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/cpp/rtps/transport/TCPChannelResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,36 @@ 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;

virtual void set_options(
const TCPTransportDescriptor* options) = 0;

Expand Down
13 changes: 12 additions & 1 deletion src/cpp/rtps/transport/TCPChannelResourceBasic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,18 @@ asio::ip::tcp::endpoint TCPChannelResourceBasic::remote_endpoint() const

asio::ip::tcp::endpoint TCPChannelResourceBasic::local_endpoint() const
{
std::error_code ec;
return socket_->local_endpoint();
}

asio::ip::tcp::endpoint TCPChannelResourceBasic::remote_endpoint(
asio::error_code& ec) const
{
return socket_->remote_endpoint(ec);
}

asio::ip::tcp::endpoint TCPChannelResourceBasic::local_endpoint(
asio::error_code& ec) const
{
return socket_->local_endpoint(ec);
}

Expand Down
7 changes: 7 additions & 0 deletions src/cpp/rtps/transport/TCPChannelResourceBasic.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,16 @@ class TCPChannelResourceBasic : public TCPChannelResource
size_t size,
asio::error_code& ec) override;

// Throwing asio calls
asio::ip::tcp::endpoint remote_endpoint() const override;
asio::ip::tcp::endpoint local_endpoint() const override;

// Non-throwing asio calls
asio::ip::tcp::endpoint remote_endpoint(
asio::error_code& ec) const override;
asio::ip::tcp::endpoint local_endpoint(
asio::error_code& ec) const override;

void set_options(
const TCPTransportDescriptor* options) override;

Expand Down
12 changes: 12 additions & 0 deletions src/cpp/rtps/transport/TCPChannelResourceSecure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,18 @@ asio::ip::tcp::endpoint TCPChannelResourceSecure::local_endpoint() const
return secure_socket_->lowest_layer().local_endpoint();
}

asio::ip::tcp::endpoint TCPChannelResourceSecure::remote_endpoint(
asio::error_code& ec) const
{
return secure_socket_->lowest_layer().remote_endpoint(ec);
}

asio::ip::tcp::endpoint TCPChannelResourceSecure::local_endpoint(
asio::error_code& ec) const
{
return secure_socket_->lowest_layer().local_endpoint(ec);
}

void TCPChannelResourceSecure::set_options(
const TCPTransportDescriptor* options)
{
Expand Down
7 changes: 7 additions & 0 deletions src/cpp/rtps/transport/TCPChannelResourceSecure.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,16 @@ class TCPChannelResourceSecure : public TCPChannelResource
size_t size,
asio::error_code& ec) override;

// Throwing asio calls
asio::ip::tcp::endpoint remote_endpoint() const override;
asio::ip::tcp::endpoint local_endpoint() const override;

// Non-throwing asio calls
asio::ip::tcp::endpoint remote_endpoint(
asio::error_code& ec) const override;
asio::ip::tcp::endpoint local_endpoint(
asio::error_code& ec) const override;

void set_options(
const TCPTransportDescriptor* options) override;

Expand Down
20 changes: 19 additions & 1 deletion src/cpp/rtps/transport/TCPTransportInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,19 @@ void TCPTransportInterface::clean()
}
}

Locator TCPTransportInterface::remote_endpoint_to_locator(
const std::shared_ptr<TCPChannelResource>& 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<TCPChannelResource>& channel)
{
Expand Down Expand Up @@ -898,10 +911,11 @@ void TCPTransportInterface::perform_listen_operation(
if (rtcp_message_manager)
{
channel = channel_weak.lock();
endpoint_to_locator(channel->remote_endpoint(), remote_locator);

if (channel)
{
remote_locator = remote_endpoint_to_locator(channel);

uint32_t port = channel->local_endpoint().port();
set_name_to_current_thread("dds.tcp.%u", port);

Expand Down Expand Up @@ -1178,6 +1192,10 @@ bool TCPTransportInterface::Receive(
}
else
{
if (!IsLocatorValid(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 \
<< " - " << receive_buffer_size << " bytes.");
Expand Down
6 changes: 6 additions & 0 deletions src/cpp/rtps/transport/TCPTransportInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<TCPChannelResource>& channel) const;

/**
* Shutdown method to close the connections of the transports.
*/
Expand Down
16 changes: 16 additions & 0 deletions test/unittest/transport/mock/MockTCPChannelResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ asio::ip::tcp::endpoint MockTCPChannelResource::local_endpoint() const
return ep;
}

asio::ip::tcp::endpoint MockTCPChannelResource::remote_endpoint(
asio::error_code& ec) const
{
ec = asio::error_code(); // Indicate no error
asio::ip::tcp::endpoint ep;
return ep;
}

asio::ip::tcp::endpoint MockTCPChannelResource::local_endpoint(
asio::error_code& ec) const
{
ec = asio::error_code(); // Indicate no error
asio::ip::tcp::endpoint ep;
return ep;
}

void MockTCPChannelResource::set_options(
const TCPTransportDescriptor*)
{
Expand Down
6 changes: 6 additions & 0 deletions test/unittest/transport/mock/MockTCPChannelResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ class MockTCPChannelResource : public TCPChannelResource

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

asio::ip::tcp::endpoint remote_endpoint(
asio::error_code& ec) const override;

asio::ip::tcp::endpoint local_endpoint(
asio::error_code& ec) const override;

void set_options(
const TCPTransportDescriptor* options) override;

Expand Down

0 comments on commit 6b484da

Please sign in to comment.