Skip to content

Commit

Permalink
seastar::socket: Introduce API
Browse files Browse the repository at this point in the history
This patch adds the seastar::socket class to the API. This class
is responsible for establishing a connection between two endpoints.
It allows for the connection attempt to be canceled.

Signed-off-by: Duarte Nunes <[email protected]>
  • Loading branch information
duarten committed May 8, 2016
1 parent ab74536 commit 62d881b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
39 changes: 39 additions & 0 deletions net/api.hh
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ struct tcp_keepalive_params {

/// \cond internal
class connected_socket_impl;
class socket_impl;
class server_socket_impl;
class udp_channel_impl;
class get_impl;
Expand Down Expand Up @@ -236,6 +237,43 @@ public:
/// \addtogroup networking-module
/// @{

namespace seastar {

/// The seastar socket.
///
/// A \c socket that allows a connection to be established between
/// two endpoints.
class socket {
std::unique_ptr<net::socket_impl> _si;
public:
~socket();

/// \cond internal
explicit socket(std::unique_ptr<net::socket_impl> si);
/// \endcond
/// Moves a \c seastar::socket object.
socket(socket&&) noexcept;
/// Move-assigns a \c seastar::socket object.
seastar::socket& operator=(seastar::socket&&) noexcept;

/// Attempts to establish the connection.
///
/// \return a \ref connected_socket representing the connection.
future<connected_socket> connect(socket_address sa, socket_address local = socket_address(::sockaddr_in{AF_INET, INADDR_ANY, {0}}));
/// Stops any in-flight connection attempt.
///
/// Cancels the connection attempt if it's still in progress, and
/// terminates the connection if it has already been established.
void shutdown();
};

} /* namespace seastar */

/// @}

/// \addtogroup networking-module
/// @{

/// A listening socket, waiting to accept incoming network connections.
class server_socket {
std::unique_ptr<net::server_socket_impl> _ssi;
Expand Down Expand Up @@ -274,6 +312,7 @@ public:
virtual server_socket listen(socket_address sa, listen_options opts) = 0;
// FIXME: local parameter assumes ipv4 for now, fix when adding other AF
virtual future<connected_socket> connect(socket_address sa, socket_address local = socket_address(::sockaddr_in{AF_INET, INADDR_ANY, {0}})) = 0;
virtual seastar::socket socket() = 0;
virtual net::udp_channel make_udp_channel(ipv4_addr addr = {}) = 0;
virtual future<> initialize() {
return make_ready_future();
Expand Down
3 changes: 3 additions & 0 deletions net/native-stack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ class native_network_stack : public network_stack {
explicit native_network_stack(boost::program_options::variables_map opts, std::shared_ptr<device> dev);
virtual server_socket listen(socket_address sa, listen_options opt) override;
virtual future<connected_socket> connect(socket_address sa, socket_address local) override;
virtual ::seastar::socket socket() override {
throw "not implemented";
}
virtual udp_channel make_udp_channel(ipv4_addr addr) override;
virtual future<> initialize() override;
static future<std::unique_ptr<network_stack>> create(boost::program_options::variables_map opts) {
Expand Down
3 changes: 3 additions & 0 deletions net/posix-stack.hh
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public:
explicit posix_network_stack(boost::program_options::variables_map opts) : _reuseport(engine().posix_reuseport_available()) {}
virtual server_socket listen(socket_address sa, listen_options opts) override;
virtual future<connected_socket> connect(socket_address sa, socket_address local) override;
virtual ::seastar::socket socket() override {
throw "not implemented";
}
virtual net::udp_channel make_udp_channel(ipv4_addr addr) override;
static future<std::unique_ptr<network_stack>> create(boost::program_options::variables_map opts) {
return make_ready_future<std::unique_ptr<network_stack>>(std::unique_ptr<network_stack>(new posix_network_stack(opts)));
Expand Down
19 changes: 19 additions & 0 deletions net/stack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ future<> connected_socket::shutdown_input() {
return _csi->shutdown_input();
}

seastar::socket::~socket()
{}

seastar::socket::socket(
std::unique_ptr<net::socket_impl> si)
: _si(std::move(si)) {
}

seastar::socket::socket(seastar::socket&&) noexcept = default;
seastar::socket& seastar::socket::operator=(seastar::socket&&) noexcept = default;

future<connected_socket> seastar::socket::connect(socket_address sa, socket_address local) {
return _si->connect(sa, local);
}

void seastar::socket::shutdown() {
_si->shutdown();
}

server_socket::server_socket() {
}

Expand Down
7 changes: 7 additions & 0 deletions net/stack.hh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ public:
virtual tcp_keepalive_params get_keepalive_parameters() const = 0;
};

class socket_impl {
public:
virtual ~socket_impl() {}
virtual future<connected_socket> connect(socket_address sa, socket_address local) = 0;
virtual void shutdown() = 0;
};

class server_socket_impl {
public:
virtual ~server_socket_impl() {}
Expand Down

0 comments on commit 62d881b

Please sign in to comment.