Skip to content

Commit

Permalink
core: add and implement socket_interface::connect()
Browse files Browse the repository at this point in the history
  • Loading branch information
deniskovalchuk committed Mar 31, 2024
1 parent 591149d commit 65b4d01
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
27 changes: 27 additions & 0 deletions include/ftp/detail/socket_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#define LIBFTP_SOCKET_BASE_HPP

#include <ftp/detail/socket_interface.hpp>
#include <boost/asio/connect.hpp>

namespace ftp::detail
{
Expand All @@ -34,6 +35,32 @@ template<typename SocketType>
class socket_base : public socket_interface
{
public:
void connect(std::string_view hostname, std::uint16_t port, boost::system::error_code & ec) override
{
SocketType & socket = get_sock();

boost::asio::ip::tcp::resolver resolver(socket.get_executor());
boost::asio::ip::tcp::resolver::results_type endpoints =
resolver.resolve(hostname, std::to_string(port), ec);

if (ec)
return;

boost::asio::connect(socket, endpoints, 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);
}
}

SocketType & get_socket() override
{
return get_sock();
Expand Down
2 changes: 2 additions & 0 deletions include/ftp/detail/socket_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ namespace ftp::detail
class socket_interface
{
public:
virtual void connect(std::string_view hostname, std::uint16_t port, boost::system::error_code & ec) = 0;

// TODO: Remove.
virtual boost::asio::ip::tcp::socket & get_socket() = 0;

Expand Down
22 changes: 1 addition & 21 deletions src/control_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <ftp/detail/control_connection.hpp>
#include <ftp/detail/socket_factory.hpp>
#include <ftp/detail/utils.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/read_until.hpp>
#include <boost/asio/write.hpp>

Expand All @@ -50,31 +49,12 @@ control_connection::control_connection(net_context & net_context)

void control_connection::connect(std::string_view hostname, std::uint16_t port)
{
boost::asio::ip::tcp::socket & socket = socket_ptr_->get_socket();
boost::asio::ip::tcp::resolver resolver(socket.get_executor());
boost::system::error_code ec;

boost::asio::ip::tcp::resolver::results_type endpoints =
resolver.resolve(hostname, std::to_string(port), ec);

if (ec)
{
throw ftp_exception(ec, "Cannot open control connection");
}

boost::asio::connect(socket, endpoints, ec);
socket_ptr_->connect(hostname, port, 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 control connection");
}
}
Expand Down

0 comments on commit 65b4d01

Please sign in to comment.