Skip to content

Commit

Permalink
core: get rid of socket_interface class
Browse files Browse the repository at this point in the history
  • Loading branch information
deniskovalchuk committed Apr 1, 2024
1 parent 915a5f3 commit f1c3d9c
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 123 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ set(sources
include/ftp/detail/socket_base.hpp
include/ftp/detail/socket_factory.hpp
include/ftp/detail/utils.hpp
include/ftp/detail/socket_interface.hpp
include/ftp/stream/input_stream.hpp
include/ftp/stream/istream_adapter.hpp
include/ftp/stream/ostream_adapter.hpp
Expand Down
4 changes: 2 additions & 2 deletions include/ftp/detail/control_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

#include <ftp/reply.hpp>
#include <ftp/detail/net_context.hpp>
#include <ftp/detail/socket_interface.hpp>
#include <ftp/detail/socket_base.hpp>
#include <boost/asio/ip/tcp.hpp>

namespace ftp::detail
Expand Down Expand Up @@ -62,7 +62,7 @@ class control_connection
static bool is_last_line(std::string_view line, std::uint16_t status_code);

std::string buffer_;
socket_interface_ptr socket_;
socket_base_ptr socket_;
};

} // namespace ftp::detail
Expand Down
4 changes: 2 additions & 2 deletions include/ftp/detail/data_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <ftp/stream/output_stream.hpp>
#include <ftp/transfer_callback.hpp>
#include <ftp/detail/net_context.hpp>
#include <ftp/detail/socket_interface.hpp>
#include <ftp/detail/socket_base.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <memory>
#include <string_view>
Expand Down Expand Up @@ -63,7 +63,7 @@ class data_connection
[[nodiscard]] boost::asio::ip::tcp::endpoint get_listen_endpoint() const;

private:
socket_interface_ptr socket_;
socket_base_ptr socket_;
boost::asio::ip::tcp::acceptor acceptor_;
};

Expand Down
31 changes: 27 additions & 4 deletions include/ftp/detail/socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,38 @@
namespace ftp::detail
{

class socket : public socket_base<boost::asio::ip::tcp::socket>
class socket : public socket_base
{
public:
explicit socket(boost::asio::io_context & io_context);

private:
const boost::asio::ip::tcp::socket & get_sock() const override;
boost::asio::ip::tcp::socket & get_sock() override;
void connect(const boost::asio::ip::tcp::resolver::results_type & eps, boost::system::error_code & ec) override;

void connect(const boost::asio::ip::tcp::endpoint & ep, boost::system::error_code & ec) override;

bool is_connected() const override;

std::size_t write(const char *buf, std::size_t size, boost::system::error_code & ec) override;

std::size_t write(std::string_view buf, boost::system::error_code & ec) override;

std::size_t read_some(char *buf, std::size_t max_size, boost::system::error_code & ec) override;

std::size_t read_line(std::string & buf, std::size_t max_size, boost::system::error_code & ec) override;

void shutdown(boost::asio::ip::tcp::socket::shutdown_type type, boost::system::error_code & ec) override;

void close(boost::system::error_code & ec) override;

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

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

boost::asio::ip::tcp::socket::executor_type get_executor() override;

boost::asio::ip::tcp::socket & get_socket() override;

private:
boost::asio::ip::tcp::socket socket_;
};

Expand Down
108 changes: 71 additions & 37 deletions include/ftp/detail/socket_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,92 +25,124 @@
#ifndef LIBFTP_SOCKET_BASE_HPP
#define LIBFTP_SOCKET_BASE_HPP

#include <ftp/detail/socket_interface.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/write.hpp>
#include <boost/asio/read_until.hpp>
#include <memory>

namespace ftp::detail
{

template<typename SocketType>
class socket_base : public socket_interface
class socket_base
{
public:
void connect(const boost::asio::ip::tcp::resolver::results_type & eps, boost::system::error_code & ec) override
virtual void connect(const boost::asio::ip::tcp::resolver::results_type & eps, boost::system::error_code & ec) = 0;

virtual void connect(const boost::asio::ip::tcp::endpoint & ep, boost::system::error_code & ec) = 0;

virtual bool is_connected() const = 0;

virtual std::size_t write(const char *buf, std::size_t size, boost::system::error_code & ec) = 0;

virtual std::size_t write(std::string_view buf, boost::system::error_code & ec) = 0;

virtual std::size_t read_some(char *buf, std::size_t max_size, boost::system::error_code & ec) = 0;

virtual std::size_t read_line(std::string & buf, std::size_t max_size, boost::system::error_code & ec) = 0;

virtual void shutdown(boost::asio::ip::tcp::socket::shutdown_type type, boost::system::error_code & ec) = 0;

virtual void close(boost::system::error_code & ec) = 0;

virtual boost::asio::ip::tcp::endpoint local_endpoint(boost::system::error_code & ec) const = 0;

virtual boost::asio::ip::tcp::endpoint remote_endpoint(boost::system::error_code & ec) const = 0;

virtual boost::asio::ip::tcp::socket::executor_type get_executor() = 0;

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

virtual ~socket_base() = default;

protected:
template<typename SocketType>
void connect(SocketType & socket, const boost::asio::ip::tcp::resolver::results_type & eps, boost::system::error_code & ec)
{
boost::asio::connect(get_sock(), eps, ec);
boost::asio::connect(socket, eps, ec);
}

void connect(const boost::asio::ip::tcp::endpoint & ep, boost::system::error_code & ec) override
template<typename SocketType>
void connect(SocketType & socket, const boost::asio::ip::tcp::endpoint & ep, boost::system::error_code & ec)
{
get_sock().connect(ep, ec);
socket.connect(ep, ec);
}

template<typename SocketType>
[[nodiscard]]
bool is_connected() const override
bool is_connected(const SocketType & socket) const
{
return get_sock().is_open();
return socket.is_open();
}

std::size_t write(const char *buf, std::size_t size, boost::system::error_code & ec) override
template<typename SocketType>
std::size_t write(SocketType & socket, const char *buf, std::size_t size, boost::system::error_code & ec)
{
return boost::asio::write(get_sock(), boost::asio::buffer(buf, size), ec);
return boost::asio::write(socket, boost::asio::buffer(buf, size), ec);
}

std::size_t write(std::string_view buf, boost::system::error_code & ec) override
template<typename SocketType>
std::size_t write(SocketType & socket, std::string_view buf, boost::system::error_code & ec)
{
return boost::asio::write(get_sock(), boost::asio::buffer(buf), ec);
return boost::asio::write(socket, boost::asio::buffer(buf), ec);
}

std::size_t read_some(char *buf, std::size_t max_size, boost::system::error_code & ec) override
template<typename SocketType>
std::size_t read_some(SocketType & socket, char *buf, std::size_t max_size, boost::system::error_code & ec)
{
return get_sock().read_some(boost::asio::buffer(buf, max_size), ec);
return socket.read_some(boost::asio::buffer(buf, max_size), ec);
}

std::size_t read_line(std::string & buf, std::size_t max_size, boost::system::error_code & ec) override
template<typename SocketType>
std::size_t read_line(SocketType & socket, std::string & buf, std::size_t max_size, boost::system::error_code & ec)
{
return boost::asio::read_until(get_sock(),
return boost::asio::read_until(socket,
boost::asio::dynamic_buffer(buf, max_size),
match_eol, ec);
}

void shutdown(boost::asio::ip::tcp::socket::shutdown_type type, boost::system::error_code & ec) override
template<typename SocketType>
void shutdown(SocketType & socket, boost::asio::ip::tcp::socket::shutdown_type type, boost::system::error_code & ec)
{
get_sock().shutdown(type, ec);
socket.shutdown(type, ec);
}

void close(boost::system::error_code & ec) override
template<typename SocketType>
void close(SocketType & socket, boost::system::error_code & ec)
{
get_sock().close(ec);
socket.close(ec);
}

boost::asio::ip::tcp::endpoint local_endpoint(boost::system::error_code & ec) const override
template<typename SocketType>
boost::asio::ip::tcp::endpoint local_endpoint(const SocketType & socket, boost::system::error_code & ec) const
{
return get_sock().local_endpoint(ec);
return socket.local_endpoint(ec);
}

boost::asio::ip::tcp::endpoint remote_endpoint(boost::system::error_code & ec) const override
template<typename SocketType>
boost::asio::ip::tcp::endpoint remote_endpoint(const SocketType & socket, boost::system::error_code & ec) const
{
return get_sock().remote_endpoint(ec);
return socket.remote_endpoint(ec);
}

template<typename SocketType>
[[nodiscard]]
boost::asio::ip::tcp::socket::executor_type get_executor() override
{
return get_sock().get_executor();
}

SocketType & get_socket() override
boost::asio::ip::tcp::socket::executor_type get_executor(SocketType & socket)
{
return get_sock();
return socket.get_executor();
}

protected:
// TODO: Rename to get_socket();
virtual const SocketType & get_sock() const = 0;
virtual SocketType & get_sock() = 0;

private:
static std::pair<boost::asio::buffers_iterator<boost::asio::const_buffers_1>, bool>
match_eol(boost::asio::buffers_iterator<boost::asio::const_buffers_1> begin,
Expand Down Expand Up @@ -143,5 +175,7 @@ class socket_base : public socket_interface
}
};

using socket_base_ptr = std::unique_ptr<socket_base>;

} // namespace ftp::detail
#endif //LIBFTP_SOCKET_BASE_HPP
4 changes: 2 additions & 2 deletions include/ftp/detail/socket_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#ifndef LIBFTP_SOCKET_FACTORY_HPP
#define LIBFTP_SOCKET_FACTORY_HPP

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

namespace ftp::detail
Expand All @@ -34,7 +34,7 @@ namespace ftp::detail
class socket_factory
{
public:
static socket_interface_ptr create(boost::asio::io_context & io_context);
static socket_base_ptr create(boost::asio::io_context & io_context);
};

} // namespace ftp::detail
Expand Down
71 changes: 0 additions & 71 deletions include/ftp/detail/socket_interface.hpp

This file was deleted.

Loading

0 comments on commit f1c3d9c

Please sign in to comment.