Commit 8e1541ee authored by Joseph Noir's avatar Joseph Noir

Formatting

parent 60be58d1
...@@ -32,8 +32,7 @@ class interfaces { ...@@ -32,8 +32,7 @@ class interfaces {
public: public:
/// Returns a native IPv4 or IPv6 translation of `host`. /// Returns a native IPv4 or IPv6 translation of `host`.
static optional<std::pair<std::string, ip>> static optional<std::pair<std::string, ip>>
native_address(const std::string& host, native_address(const std::string& host, optional<ip> preferred = none);
optional<ip> preferred = none);
/// Returns the host and protocol available for a local server socket /// Returns the host and protocol available for a local server socket
static std::vector<std::pair<std::string, ip>> static std::vector<std::pair<std::string, ip>>
......
...@@ -25,10 +25,7 @@ namespace caf { ...@@ -25,10 +25,7 @@ namespace caf {
namespace net { namespace net {
// IP version tag. // IP version tag.
enum class ip { enum class ip { v4, v6 };
v4,
v6
};
/// @relates ip /// @relates ip
inline std::string to_string(ip x) { inline std::string to_string(ip x) {
......
...@@ -18,12 +18,11 @@ ...@@ -18,12 +18,11 @@
#pragma once #pragma once
#include "caf/expected.hpp" #include "caf/expected.hpp"
#include "caf/variant.hpp"
#include "caf/fwd.hpp" #include "caf/fwd.hpp"
#include "caf/net/network_socket.hpp" #include "caf/net/network_socket.hpp"
#include "caf/sec.hpp" #include "caf/sec.hpp"
#include "caf/variant.hpp"
namespace caf { namespace caf {
namespace net { namespace net {
......
...@@ -28,32 +28,30 @@ namespace caf { ...@@ -28,32 +28,30 @@ namespace caf {
namespace net { namespace net {
struct tcp { struct tcp {
/// Creates a new TCP socket to accept connections on a given port.
/// Creates a new TCP socket to accept connections on a given port. /// @param port The port to listen on.
/// @param port The port to listen on. /// @param addr Only accepts connections originating from this address.
/// @param addr Only accepts connections originating from this address. /// @param reuse_addr Optionally sets the SO_REUSEADDR option on the socket.
/// @param reuse_addr Optionally sets the SO_REUSEADDR option on the socket. /// @relates stream_socket
/// @relates stream_socket static expected<stream_socket> make_accept_socket(uint16_t port,
static expected<stream_socket> make_accept_socket(uint16_t port,
const char* addr = nullptr, const char* addr = nullptr,
bool reuse_addr = false); bool reuse_addr = false);
/// Create a `stream_socket` connected to `host`:`port` via the /// Create a `stream_socket` connected to `host`:`port` via the
/// `preferred` IP version. /// `preferred` IP version.
/// @param host The remote host to connecto to. /// @param host The remote host to connecto to.
/// @param port The port on the remote host to connect to. /// @param port The port on the remote host to connect to.
/// @preferred Preferred IP version. /// @preferred Preferred IP version.
/// @returns The connected socket or an error. /// @returns The connected socket or an error.
/// @relates stream_socket /// @relates stream_socket
static expected<stream_socket> make_connected_socket(std::string host, static expected<stream_socket>
uint16_t port, make_connected_socket(std::string host, uint16_t port,
optional<ip> preferred = none); optional<ip> preferred = none);
/// Accept a connection on `x`. /// Accept a connection on `x`.
/// @param x Listening endpoint. /// @param x Listening endpoint.
/// @returns The socket that handles the accepted connection. /// @returns The socket that handles the accepted connection.
/// @relates stream_socket /// @relates stream_socket
static expected<stream_socket> accept(stream_socket x); static expected<stream_socket> accept(stream_socket x);
}; };
} // namespace net } // namespace net
......
...@@ -28,7 +28,6 @@ ...@@ -28,7 +28,6 @@
#include "caf/policy/scribe.hpp" #include "caf/policy/scribe.hpp"
#include "caf/send.hpp" #include "caf/send.hpp"
namespace caf { namespace caf {
namespace policy { namespace policy {
...@@ -54,18 +53,19 @@ public: ...@@ -54,18 +53,19 @@ public:
template <class Parent> template <class Parent>
bool handle_read_event(Parent& parent) { bool handle_read_event(Parent& parent) {
auto sck = net::tcp::accept(net::socket_cast<net::stream_socket>(acceptor_)); auto sck = net::tcp::accept(
net::socket_cast<net::stream_socket>(acceptor_));
if (!sck) { if (!sck) {
CAF_LOG_ERROR("accept failed:" << sck.error()); CAF_LOG_ERROR("accept failed:" << sck.error());
return false; return false;
} }
auto mpx = parent.multiplexer(); auto mpx = parent.multiplexer();
if (!mpx) { if (!mpx) {
CAF_LOG_DEBUG("could not acquire multiplexer to create a new endpoint manager"); CAF_LOG_DEBUG(
"could not acquire multiplexer to create a new endpoint manager");
return false; return false;
} }
auto child = make_endpoint_manager(mpx, parent.system(), auto child = make_endpoint_manager(mpx, parent.system(), scribe{*sck},
scribe{*sck},
parent.application().make()); parent.application().make());
if (auto err = child->init()) { if (auto err = child->init()) {
return false; return false;
......
...@@ -18,10 +18,10 @@ ...@@ -18,10 +18,10 @@
#include "caf/net/interfaces.hpp" #include "caf/net/interfaces.hpp"
#include <algorithm>
#include <cerrno> #include <cerrno>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <algorithm>
#ifdef CAF_WINDOWS #ifdef CAF_WINDOWS
# ifndef _WIN32_WINNT # ifndef _WIN32_WINNT
...@@ -43,8 +43,8 @@ ...@@ -43,8 +43,8 @@
#endif #endif
#include <map> #include <map>
#include <string>
#include <memory> #include <memory>
#include <string>
#include <utility> #include <utility>
#include "caf/detail/socket_sys_includes.hpp" #include "caf/detail/socket_sys_includes.hpp"
...@@ -69,8 +69,7 @@ void* fetch_in_addr(int family, sockaddr* addr) { ...@@ -69,8 +69,7 @@ void* fetch_in_addr(int family, sockaddr* addr) {
return vptr(&reinterpret_cast<sockaddr_in6*>(addr)->sin6_addr); return vptr(&reinterpret_cast<sockaddr_in6*>(addr)->sin6_addr);
} }
int fetch_addr_str(bool get_ipv4, bool get_ipv6, int fetch_addr_str(bool get_ipv4, bool get_ipv6, char (&buf)[INET6_ADDRSTRLEN],
char (&buf)[INET6_ADDRSTRLEN],
sockaddr* addr) { sockaddr* addr) {
if (addr == nullptr) if (addr == nullptr)
return AF_UNSPEC; return AF_UNSPEC;
...@@ -85,8 +84,7 @@ int fetch_addr_str(bool get_ipv4, bool get_ipv6, ...@@ -85,8 +84,7 @@ int fetch_addr_str(bool get_ipv4, bool get_ipv6,
} // namespace } // namespace
optional<std::pair<std::string, ip>> optional<std::pair<std::string, ip>>
interfaces::native_address(const std::string& host, interfaces::native_address(const std::string& host, optional<ip> preferred) {
optional<ip> preferred) {
addrinfo hint; addrinfo hint;
memset(&hint, 0, sizeof(hint)); memset(&hint, 0, sizeof(hint));
hint.ai_socktype = SOCK_STREAM; hint.ai_socktype = SOCK_STREAM;
...@@ -100,8 +98,7 @@ interfaces::native_address(const std::string& host, ...@@ -100,8 +98,7 @@ interfaces::native_address(const std::string& host,
for (auto i = addrs.get(); i != nullptr; i = i->ai_next) { for (auto i = addrs.get(); i != nullptr; i = i->ai_next) {
auto family = fetch_addr_str(true, true, buffer, i->ai_addr); auto family = fetch_addr_str(true, true, buffer, i->ai_addr);
if (family != AF_UNSPEC) if (family != AF_UNSPEC)
return std::make_pair(buffer, family == AF_INET ? ip::v4 return std::make_pair(buffer, family == AF_INET ? ip::v4 : ip::v6);
: ip::v6);
} }
return none; return none;
} }
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* * * *
* Distributed under the terms and conditions of the BSD 3-Clause License or * * Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software * * (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. * * * License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* If you did not receive a copy of the license files, see * * If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and * * http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. * * http://www.boost.org/LICENSE_1_0.txt. *
......
...@@ -90,12 +90,8 @@ caf::expected<socket> new_ip_acceptor_impl(uint16_t port, const char* addr, ...@@ -90,12 +90,8 @@ caf::expected<socket> new_ip_acceptor_impl(uint16_t port, const char* addr,
reinterpret_cast<setsockopt_ptr>(&on), reinterpret_cast<setsockopt_ptr>(&on),
static_cast<socket_size_type>(sizeof(on)))); static_cast<socket_size_type>(sizeof(on))));
} }
using sockaddr_type = using sockaddr_type = typename std::conditional<
typename std::conditional< Family == AF_INET, sockaddr_in, sockaddr_in6>::type;
Family == AF_INET,
sockaddr_in,
sockaddr_in6
>::type;
sockaddr_type sa; sockaddr_type sa;
memset(&sa, 0, sizeof(sockaddr_type)); memset(&sa, 0, sizeof(sockaddr_type));
family_of(sa) = Family; family_of(sa) = Family;
...@@ -116,12 +112,8 @@ bool ip_connect(socket fd, const std::string& host, uint16_t port, ...@@ -116,12 +112,8 @@ bool ip_connect(socket fd, const std::string& host, uint16_t port,
CAF_LOG_TRACE("Family =" << (Family == AF_INET ? "AF_INET" : "AF_INET6") CAF_LOG_TRACE("Family =" << (Family == AF_INET ? "AF_INET" : "AF_INET6")
<< CAF_ARG(fd.id) << CAF_ARG(host)); << CAF_ARG(fd.id) << CAF_ARG(host));
static_assert(Family == AF_INET || Family == AF_INET6, "invalid family"); static_assert(Family == AF_INET || Family == AF_INET6, "invalid family");
using sockaddr_type = using sockaddr_type = typename std::conditional<
typename std::conditional< Family == AF_INET, sockaddr_in, sockaddr_in6>::type;
Family == AF_INET,
sockaddr_in,
sockaddr_in6
>::type;
sockaddr_type sa; sockaddr_type sa;
memset(&sa, 0, sizeof(sockaddr_type)); memset(&sa, 0, sizeof(sockaddr_type));
inet_pton(Family, host.c_str(), &addr_of(sa)); inet_pton(Family, host.c_str(), &addr_of(sa));
...@@ -148,7 +140,8 @@ expected<stream_socket> tcp::make_accept_socket(uint16_t port, const char* addr, ...@@ -148,7 +140,8 @@ expected<stream_socket> tcp::make_accept_socket(uint16_t port, const char* addr,
auto hostname = elem.first.c_str(); auto hostname = elem.first.c_str();
auto p = elem.second == ip::v4 auto p = elem.second == ip::v4
? new_ip_acceptor_impl<AF_INET>(port, hostname, reuse_addr, any) ? new_ip_acceptor_impl<AF_INET>(port, hostname, reuse_addr, any)
: new_ip_acceptor_impl<AF_INET6>(port, hostname, reuse_addr, any); : new_ip_acceptor_impl<AF_INET6>(port, hostname, reuse_addr,
any);
if (!p) { if (!p) {
CAF_LOG_DEBUG(p.error()); CAF_LOG_DEBUG(p.error());
continue; continue;
...@@ -159,8 +152,8 @@ expected<stream_socket> tcp::make_accept_socket(uint16_t port, const char* addr, ...@@ -159,8 +152,8 @@ expected<stream_socket> tcp::make_accept_socket(uint16_t port, const char* addr,
if (fd == invalid_socket_id) { if (fd == invalid_socket_id) {
CAF_LOG_WARNING("could not open tcp socket on:" << CAF_ARG(port) CAF_LOG_WARNING("could not open tcp socket on:" << CAF_ARG(port)
<< CAF_ARG(addr_str)); << CAF_ARG(addr_str));
return make_error(sec::cannot_open_port, "tcp socket creation failed", return make_error(sec::cannot_open_port, "tcp socket creation failed", port,
port, addr_str); addr_str);
} }
socket_guard sguard{fd}; socket_guard sguard{fd};
CAF_NET_SYSCALL("listen", tmp, !=, 0, listen(fd.id, SOMAXCONN)); CAF_NET_SYSCALL("listen", tmp, !=, 0, listen(fd.id, SOMAXCONN));
...@@ -169,8 +162,8 @@ expected<stream_socket> tcp::make_accept_socket(uint16_t port, const char* addr, ...@@ -169,8 +162,8 @@ expected<stream_socket> tcp::make_accept_socket(uint16_t port, const char* addr,
return socket_cast<stream_socket>(sguard.release()); return socket_cast<stream_socket>(sguard.release());
} }
expected<stream_socket> expected<stream_socket> tcp::make_connected_socket(std::string host,
tcp::make_connected_socket(std::string host, uint16_t port, uint16_t port,
optional<ip> preferred) { optional<ip> preferred) {
CAF_LOG_TRACE(CAF_ARG(host) << CAF_ARG(port) << CAF_ARG(preferred)); CAF_LOG_TRACE(CAF_ARG(host) << CAF_ARG(port) << CAF_ARG(preferred));
CAF_LOG_DEBUG("try to connect to:" << CAF_ARG(host) << CAF_ARG(port)); CAF_LOG_DEBUG("try to connect to:" << CAF_ARG(host) << CAF_ARG(port));
...@@ -192,8 +185,8 @@ tcp::make_connected_socket(std::string host, uint16_t port, ...@@ -192,8 +185,8 @@ tcp::make_connected_socket(std::string host, uint16_t port,
socket_guard sguard(fd); socket_guard sguard(fd);
if (proto == ip::v6) { if (proto == ip::v6) {
if (ip_connect<AF_INET6>(fd, res->first, port)) { if (ip_connect<AF_INET6>(fd, res->first, port)) {
CAF_LOG_INFO("successfully connected to (IPv6):" CAF_LOG_INFO("successfully connected to (IPv6):" << CAF_ARG(host)
<< CAF_ARG(host) << CAF_ARG(port)); << CAF_ARG(port));
return socket_cast<stream_socket>(sguard.release()); return socket_cast<stream_socket>(sguard.release());
} }
sguard.close(); sguard.close();
...@@ -202,11 +195,11 @@ tcp::make_connected_socket(std::string host, uint16_t port, ...@@ -202,11 +195,11 @@ tcp::make_connected_socket(std::string host, uint16_t port,
} }
if (!ip_connect<AF_INET>(fd, res->first, port)) { if (!ip_connect<AF_INET>(fd, res->first, port)) {
CAF_LOG_WARNING("could not connect to:" << CAF_ARG(host) << CAF_ARG(port)); CAF_LOG_WARNING("could not connect to:" << CAF_ARG(host) << CAF_ARG(port));
return make_error(sec::cannot_connect_to_node, return make_error(sec::cannot_connect_to_node, "ip_connect failed", host,
"ip_connect failed", host, port); port);
} }
CAF_LOG_INFO("successfully connected to (IPv4):" CAF_LOG_INFO("successfully connected to (IPv4):" << CAF_ARG(host)
<< CAF_ARG(host) << CAF_ARG(port)); << CAF_ARG(port));
return socket_cast<stream_socket>(sguard.release()); return socket_cast<stream_socket>(sguard.release());
} }
......
...@@ -18,11 +18,11 @@ ...@@ -18,11 +18,11 @@
#define CAF_SUITE doorman #define CAF_SUITE doorman
#include "caf/policy/doorman.hpp"
#include "caf/net/endpoint_manager.hpp" #include "caf/net/endpoint_manager.hpp"
#include "caf/net/make_endpoint_manager.hpp" #include "caf/net/make_endpoint_manager.hpp"
#include "caf/net/multiplexer.hpp" #include "caf/net/multiplexer.hpp"
#include "caf/net/tcp.hpp" #include "caf/net/tcp.hpp"
#include "caf/policy/doorman.hpp"
#include "caf/test/dsl.hpp" #include "caf/test/dsl.hpp"
...@@ -77,7 +77,8 @@ public: ...@@ -77,7 +77,8 @@ public:
template <class Transport> template <class Transport>
void resolve(Transport&, std::string path, actor listener) { void resolve(Transport&, std::string path, actor listener) {
anon_send(listener, resolve_atom::value, "the resolved path is still " + path); anon_send(listener, resolve_atom::value,
"the resolved path is still " + path);
} }
template <class Transport> template <class Transport>
...@@ -127,8 +128,7 @@ CAF_TEST(doorman accept) { ...@@ -127,8 +128,7 @@ CAF_TEST(doorman accept) {
auto acceptor = unbox(tcp::make_accept_socket(0, nullptr, false)); auto acceptor = unbox(tcp::make_accept_socket(0, nullptr, false));
auto port = unbox(local_port(socket_cast<network_socket>(acceptor))); auto port = unbox(local_port(socket_cast<network_socket>(acceptor)));
CAF_MESSAGE("opened acceptor on port " << port); CAF_MESSAGE("opened acceptor on port " << port);
auto mgr = make_endpoint_manager(mpx, sys, auto mgr = make_endpoint_manager(mpx, sys, policy::doorman{acceptor},
policy::doorman{acceptor},
dummy_application_factory{}); dummy_application_factory{});
CAF_CHECK_EQUAL(mgr->init(), none); CAF_CHECK_EQUAL(mgr->init(), none);
handle_io_event(); handle_io_event();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment