Skip to content

Commit

Permalink
perf: use Boost.Asio endpoints where is possible
Browse files Browse the repository at this point in the history
Get rid of extra address construction from strings.
  • Loading branch information
deniskovalchuk committed Sep 28, 2023
1 parent ca1bc41 commit 3d053b7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
2 changes: 1 addition & 1 deletion include/ftp/detail/control_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class control_connection

[[nodiscard]] boost::asio::ip::tcp::endpoint get_local_endpoint() const;

[[nodiscard]] std::string get_remote_ip() const;
[[nodiscard]] boost::asio::ip::tcp::endpoint get_remote_endpoint() const;

void send(std::string_view command);

Expand Down
2 changes: 2 additions & 0 deletions include/ftp/detail/data_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class data_connection

void open(std::string_view ip, std::uint16_t port);

void open(const boost::asio::ip::tcp::endpoint & remote_endpoint);

void listen(const boost::asio::ip::tcp::endpoint & endpoint);

void accept();
Expand Down
5 changes: 4 additions & 1 deletion src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,11 @@ data_connection_ptr client::process_epsv_command(std::string_view command, repli
reply.get_status_string());
}

boost::asio::ip::tcp::endpoint remote_endpoint = control_connection_.get_remote_endpoint();
boost::asio::ip::tcp::endpoint endpoint(remote_endpoint.address(), remote_port);

data_connection_ptr connection = std::make_unique<data_connection>();
connection->open(control_connection_.get_remote_ip(), remote_port);
connection->open(endpoint);

reply = process_command(command, replies);

Expand Down
13 changes: 3 additions & 10 deletions src/control_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,25 +123,18 @@ boost::asio::ip::tcp::endpoint control_connection::get_local_endpoint() const
return local_endpoint;
}

std::string control_connection::get_remote_ip() const
boost::asio::ip::tcp::endpoint control_connection::get_remote_endpoint() const
{
boost::system::error_code ec;

boost::asio::ip::tcp::endpoint remote_endpoint = socket_.remote_endpoint(ec);

if (ec)
{
throw ftp_exception(ec, "Cannot get IP address");
throw ftp_exception(ec, "Cannot get remote endpoint");
}

std::string ip = remote_endpoint.address().to_string(ec);

if (ec)
{
throw ftp_exception(ec, "Cannot get IP address");
}

return ip;
return remote_endpoint;
}

reply control_connection::recv()
Expand Down
21 changes: 21 additions & 0 deletions src/data_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ void data_connection::open(std::string_view ip, std::uint16_t port)
}
}

void data_connection::open(const boost::asio::ip::tcp::endpoint & remote_endpoint)
{
boost::system::error_code ec;

socket_.connect(remote_endpoint, ec);

if (ec)
{
boost::system::error_code ignored;

/* If the connect fails, and the socket was automatically opened,
* the socket is not returned to the closed state.
*
* https://www.boost.org/doc/libs/1_70_0/doc/html/boost_asio/reference/basic_stream_socket/connect/overload2.html
*/
socket_.close(ignored);

throw ftp_exception(ec, "Cannot open data connection");
}
}

void data_connection::listen(const boost::asio::ip::tcp::endpoint & endpoint)
{
boost::system::error_code ec;
Expand Down

0 comments on commit 3d053b7

Please sign in to comment.