From 591149d36f6b84753f4109b8602374a27c8a6055 Mon Sep 17 00:00:00 2001 From: deniskovalchuk Date: Sun, 31 Mar 2024 15:34:37 +0300 Subject: [PATCH] core: add socket_interface class socket_base implements socket_interface independently of SocketType. --- CMakeLists.txt | 1 + include/ftp/detail/control_connection.hpp | 4 +- include/ftp/detail/data_connection.hpp | 4 +- include/ftp/detail/socket.hpp | 6 +-- include/ftp/detail/socket_base.hpp | 17 ++++---- include/ftp/detail/socket_factory.hpp | 4 +- include/ftp/detail/socket_interface.hpp | 47 +++++++++++++++++++++++ src/socket.cpp | 2 +- src/socket_factory.cpp | 2 +- 9 files changed, 69 insertions(+), 18 deletions(-) create mode 100644 include/ftp/detail/socket_interface.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6bd6a8b..80f05b5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,7 @@ 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 642e893..dcbc8ba 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_base_ptr socket_ptr_; + socket_interface_ptr socket_ptr_; }; } // namespace ftp::detail diff --git a/include/ftp/detail/data_connection.hpp b/include/ftp/detail/data_connection.hpp index 577483d..729a670 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_base_ptr socket_ptr_; + socket_interface_ptr socket_ptr_; boost::asio::ip::tcp::acceptor acceptor_; }; diff --git a/include/ftp/detail/socket.hpp b/include/ftp/detail/socket.hpp index 667d1ab..3350fc0 100644 --- a/include/ftp/detail/socket.hpp +++ b/include/ftp/detail/socket.hpp @@ -32,14 +32,14 @@ namespace ftp::detail { -class socket : public socket_base +class socket : public socket_base { public: explicit socket(boost::asio::io_context & io_context); - boost::asio::ip::tcp::socket & get_socket() override; - private: + boost::asio::ip::tcp::socket & get_sock() override; + boost::asio::ip::tcp::socket socket_; }; diff --git a/include/ftp/detail/socket_base.hpp b/include/ftp/detail/socket_base.hpp index 3711932..2c5bd63 100644 --- a/include/ftp/detail/socket_base.hpp +++ b/include/ftp/detail/socket_base.hpp @@ -25,21 +25,24 @@ #ifndef LIBFTP_SOCKET_BASE_HPP #define LIBFTP_SOCKET_BASE_HPP -#include -#include +#include namespace ftp::detail { -class socket_base +template +class socket_base : public socket_interface { public: - virtual boost::asio::ip::tcp::socket & get_socket() = 0; + SocketType & get_socket() override + { + return get_sock(); + } - virtual ~socket_base() = default; +protected: + // TODO: Rename to get_socket(); + virtual SocketType & get_sock() = 0; }; -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 225cc93..883555a 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_base_ptr create(boost::asio::io_context & io_context); + static socket_interface_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 new file mode 100644 index 0000000..3509c87 --- /dev/null +++ b/include/ftp/detail/socket_interface.hpp @@ -0,0 +1,47 @@ +/* + * 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 + +namespace ftp::detail +{ + +// TODO: This class will declare connect(), read(), write() methods. +class socket_interface +{ +public: + // 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 70a5cc8..7d77519 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -31,7 +31,7 @@ socket::socket(boost::asio::io_context & io_context) : socket_(io_context) {} -boost::asio::ip::tcp::socket & socket::get_socket() +boost::asio::ip::tcp::socket & socket::get_sock() { return socket_; } diff --git a/src/socket_factory.cpp b/src/socket_factory.cpp index ab21004..44da3a1 100644 --- a/src/socket_factory.cpp +++ b/src/socket_factory.cpp @@ -29,7 +29,7 @@ namespace ftp::detail { -socket_base_ptr socket_factory::create(boost::asio::io_context & io_context) +socket_interface_ptr socket_factory::create(boost::asio::io_context & io_context) { return std::make_unique(io_context); }