From f1c3d9cdd86e33b739ca3dc0f4b139bce184ec96 Mon Sep 17 00:00:00 2001 From: deniskovalchuk Date: Mon, 1 Apr 2024 23:04:07 +0300 Subject: [PATCH] core: get rid of socket_interface class --- CMakeLists.txt | 1 - include/ftp/detail/control_connection.hpp | 4 +- include/ftp/detail/data_connection.hpp | 4 +- include/ftp/detail/socket.hpp | 31 ++++++- include/ftp/detail/socket_base.hpp | 108 ++++++++++++++-------- include/ftp/detail/socket_factory.hpp | 4 +- include/ftp/detail/socket_interface.hpp | 71 -------------- src/socket.cpp | 61 +++++++++++- src/socket_factory.cpp | 2 +- 9 files changed, 163 insertions(+), 123 deletions(-) delete mode 100644 include/ftp/detail/socket_interface.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 80f05b5..6bd6a8b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/include/ftp/detail/control_connection.hpp b/include/ftp/detail/control_connection.hpp index 085ac4f..afe2913 100644 --- a/include/ftp/detail/control_connection.hpp +++ b/include/ftp/detail/control_connection.hpp @@ -27,7 +27,7 @@ #include #include -#include +#include #include namespace ftp::detail @@ -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 diff --git a/include/ftp/detail/data_connection.hpp b/include/ftp/detail/data_connection.hpp index c1a075f..5bdba78 100644 --- a/include/ftp/detail/data_connection.hpp +++ b/include/ftp/detail/data_connection.hpp @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include #include #include @@ -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_; }; diff --git a/include/ftp/detail/socket.hpp b/include/ftp/detail/socket.hpp index a7988f4..36fc597 100644 --- a/include/ftp/detail/socket.hpp +++ b/include/ftp/detail/socket.hpp @@ -32,15 +32,38 @@ namespace ftp::detail { -class socket : public socket_base +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_; }; diff --git a/include/ftp/detail/socket_base.hpp b/include/ftp/detail/socket_base.hpp index 5cf7743..8e9dd50 100644 --- a/include/ftp/detail/socket_base.hpp +++ b/include/ftp/detail/socket_base.hpp @@ -25,92 +25,124 @@ #ifndef LIBFTP_SOCKET_BASE_HPP #define LIBFTP_SOCKET_BASE_HPP -#include +#include #include #include #include +#include namespace ftp::detail { -template -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 + 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 + 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 [[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 + 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 + 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 + 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 + 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 + 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 + 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 + 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 + 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 [[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, bool> match_eol(boost::asio::buffers_iterator begin, @@ -143,5 +175,7 @@ class socket_base : public socket_interface } }; +using socket_base_ptr = std::unique_ptr; + } // namespace ftp::detail #endif //LIBFTP_SOCKET_BASE_HPP diff --git a/include/ftp/detail/socket_factory.hpp b/include/ftp/detail/socket_factory.hpp index 883555a..225cc93 100644 --- a/include/ftp/detail/socket_factory.hpp +++ b/include/ftp/detail/socket_factory.hpp @@ -25,7 +25,7 @@ #ifndef LIBFTP_SOCKET_FACTORY_HPP #define LIBFTP_SOCKET_FACTORY_HPP -#include +#include #include namespace ftp::detail @@ -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 diff --git a/include/ftp/detail/socket_interface.hpp b/include/ftp/detail/socket_interface.hpp deleted file mode 100644 index 95c5234..0000000 --- a/include/ftp/detail/socket_interface.hpp +++ /dev/null @@ -1,71 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2024 Denis Kovalchuk - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#ifndef LIBFTP_SOCKET_INTERFACE_HPP -#define LIBFTP_SOCKET_INTERFACE_HPP - -#include -#include -#include - -namespace ftp::detail -{ - -class socket_interface -{ -public: - 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_interface() = default; -}; - -using socket_interface_ptr = std::unique_ptr; - -} // namespace ftp::detail -#endif //LIBFTP_SOCKET_INTERFACE_HPP diff --git a/src/socket.cpp b/src/socket.cpp index 4844986..edc76eb 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -31,12 +31,67 @@ socket::socket(boost::asio::io_context & io_context) : socket_(io_context) {} -const boost::asio::ip::tcp::socket & socket::get_sock() const +void socket::connect(const boost::asio::ip::tcp::resolver::results_type & eps, boost::system::error_code & ec) { - return socket_; + socket_base::connect(socket_, eps, ec); +} + +void socket::connect(const boost::asio::ip::tcp::endpoint & ep, boost::system::error_code & ec) +{ + socket_base::connect(socket_, ep, ec); +} + +bool socket::is_connected() const +{ + return socket_base::is_connected(socket_); +} + +std::size_t socket::write(const char *buf, std::size_t size, boost::system::error_code & ec) +{ + return socket_base::write(socket_, buf, size, ec); +} + +std::size_t socket::write(std::string_view buf, boost::system::error_code & ec) +{ + return socket_base::write(socket_, buf, ec); +} + +std::size_t socket::read_some(char *buf, std::size_t max_size, boost::system::error_code & ec) +{ + return socket_base::read_some(socket_, buf, max_size, ec); +} + +std::size_t socket::read_line(std::string & buf, std::size_t max_size, boost::system::error_code & ec) +{ + return socket_base::read_line(socket_, buf, max_size, ec); +} + +void socket::shutdown(boost::asio::ip::tcp::socket::shutdown_type type, boost::system::error_code & ec) +{ + socket_base::shutdown(socket_, type, ec); +} + +void socket::close(boost::system::error_code & ec) +{ + socket_base::close(socket_, ec); +} + +boost::asio::ip::tcp::endpoint socket::local_endpoint(boost::system::error_code & ec) const +{ + return socket_base::local_endpoint(socket_, ec); +} + +boost::asio::ip::tcp::endpoint socket::remote_endpoint(boost::system::error_code & ec) const +{ + return socket_base::remote_endpoint(socket_, ec); +} + +boost::asio::ip::tcp::socket::executor_type socket::get_executor() +{ + return socket_base::get_executor(socket_); } -boost::asio::ip::tcp::socket & socket::get_sock() +boost::asio::ip::tcp::socket & socket::get_socket() { return socket_; } diff --git a/src/socket_factory.cpp b/src/socket_factory.cpp index 44da3a1..ab21004 100644 --- a/src/socket_factory.cpp +++ b/src/socket_factory.cpp @@ -29,7 +29,7 @@ namespace ftp::detail { -socket_interface_ptr socket_factory::create(boost::asio::io_context & io_context) +socket_base_ptr socket_factory::create(boost::asio::io_context & io_context) { return std::make_unique(io_context); }