Skip to content

Commit

Permalink
core: add socket_interface class
Browse files Browse the repository at this point in the history
socket_base implements socket_interface independently of SocketType.
  • Loading branch information
deniskovalchuk committed Mar 31, 2024
1 parent 7eb03d5 commit 591149d
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 18 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
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_base.hpp>
#include <ftp/detail/socket_interface.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_base_ptr socket_ptr_;
socket_interface_ptr socket_ptr_;
};

} // 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_base.hpp>
#include <ftp/detail/socket_interface.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_base_ptr socket_ptr_;
socket_interface_ptr socket_ptr_;
boost::asio::ip::tcp::acceptor acceptor_;
};

Expand Down
6 changes: 3 additions & 3 deletions include/ftp/detail/socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
namespace ftp::detail
{

class socket : public socket_base
class socket : public socket_base<boost::asio::ip::tcp::socket>
{
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_;
};

Expand Down
17 changes: 10 additions & 7 deletions include/ftp/detail/socket_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,24 @@
#ifndef LIBFTP_SOCKET_BASE_HPP
#define LIBFTP_SOCKET_BASE_HPP

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

namespace ftp::detail
{

class socket_base
template<typename SocketType>
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<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_base.hpp>
#include <ftp/detail/socket_interface.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_base_ptr create(boost::asio::io_context & io_context);
static socket_interface_ptr create(boost::asio::io_context & io_context);
};

} // namespace ftp::detail
Expand Down
47 changes: 47 additions & 0 deletions include/ftp/detail/socket_interface.hpp
Original file line number Diff line number Diff line change
@@ -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 <boost/asio/ip/tcp.hpp>
#include <memory>

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<socket_interface>;

} // namespace ftp::detail
#endif //LIBFTP_SOCKET_INTERFACE_HPP
2 changes: 1 addition & 1 deletion src/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
}
Expand Down
2 changes: 1 addition & 1 deletion src/socket_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<socket>(io_context);
}
Expand Down

0 comments on commit 591149d

Please sign in to comment.