Unverified Commit ab940731 authored by Joseph Noir's avatar Joseph Noir Committed by GitHub

Merge pull request #41 from actor-framework/topic/add-transport-base

Add transport_base class
parents 82e36424 421aa67f
This diff is collapsed.
...@@ -22,11 +22,16 @@ ...@@ -22,11 +22,16 @@
#include "caf/intrusive_ptr.hpp" #include "caf/intrusive_ptr.hpp"
namespace caf { namespace caf::net {
namespace net {
// -- templates ---------------------------------------------------------------- // -- templates ----------------------------------------------------------------
template <class Application>
class stream_transport;
template <class Factory>
class datagram_transport;
template <class Application, class IdType = unit_t> template <class Application, class IdType = unit_t>
class transport_worker; class transport_worker;
...@@ -49,6 +54,8 @@ struct socket; ...@@ -49,6 +54,8 @@ struct socket;
struct stream_socket; struct stream_socket;
struct tcp_accept_socket; struct tcp_accept_socket;
struct tcp_stream_socket; struct tcp_stream_socket;
struct datagram_socket;
struct udp_datagram_socket;
// -- smart pointers ----------------------------------------------------------- // -- smart pointers -----------------------------------------------------------
...@@ -58,5 +65,4 @@ using multiplexer_ptr = std::shared_ptr<multiplexer>; ...@@ -58,5 +65,4 @@ using multiplexer_ptr = std::shared_ptr<multiplexer>;
using socket_manager_ptr = intrusive_ptr<socket_manager>; using socket_manager_ptr = intrusive_ptr<socket_manager>;
using weak_multiplexer_ptr = std::weak_ptr<multiplexer>; using weak_multiplexer_ptr = std::weak_ptr<multiplexer>;
} // namespace net } // namespace caf::net
} // namespace caf
This diff is collapsed.
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2019 Dominik Charousset *
* *
* 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 *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#pragma once
#include "caf/detail/overload.hpp"
#include "caf/fwd.hpp"
#include "caf/logger.hpp"
#include "caf/net/defaults.hpp"
#include "caf/net/receive_policy.hpp"
namespace caf::net {
/// Implements base class for transports.
template <class Transport, class NextLayer, class Handle, class Application,
class IdType>
class transport_base {
public:
// -- member types -----------------------------------------------------------
using next_layer_type = NextLayer;
using handle_type = Handle;
using transport_type = Transport;
using application_type = Application;
using id_type = IdType;
using buffer_type = std::vector<byte>;
using buffer_cache_type = std::vector<buffer_type>;
// -- constructors, destructors, and assignment operators --------------------
transport_base(handle_type handle, application_type application)
: next_layer_(std::move(application)),
handle_(handle),
// max_consecutive_reads_(0),
manager_(nullptr) {
// nop
}
// -- properties -------------------------------------------------------------
handle_type handle() const noexcept {
return handle_;
}
actor_system& system() {
return manager().system();
}
application_type& application() {
return next_layer_.application();
}
transport_type& transport() {
return *reinterpret_cast<transport_type*>(this);
}
endpoint_manager& manager() {
return *manager_;
}
// -- transport member functions ---------------------------------------------
virtual error init(endpoint_manager& parent) {
CAF_LOG_TRACE("");
manager_ = &parent;
auto& cfg = system().config();
auto max_header_bufs = get_or(cfg, "middleman.max-header-buffers",
defaults::middleman::max_header_buffers);
header_bufs_.reserve(max_header_bufs);
auto max_payload_bufs = get_or(cfg, "middleman.max-payload-buffers",
defaults::middleman::max_payload_buffers);
payload_bufs_.reserve(max_payload_bufs);
if (auto err = next_layer_.init(*this))
return err;
return none;
}
auto resolve(endpoint_manager&, const uri& locator, const actor& listener) {
CAF_LOG_TRACE(CAF_ARG(locator) << CAF_ARG(listener));
auto f = detail::make_overload(
[&](auto& layer) -> decltype(layer.resolve(*this, locator, listener)) {
return layer.resolve(*this, locator, listener);
},
[&](auto& layer) -> decltype(
layer.resolve(*this, locator.path(), listener)) {
return layer.resolve(*this, locator.path(), listener);
});
f(next_layer_);
}
void new_proxy(endpoint_manager&, const node_id& peer, actor_id id) {
next_layer_.new_proxy(*this, peer, id);
}
void local_actor_down(endpoint_manager&, const node_id& peer, actor_id id,
error reason) {
next_layer_.local_actor_down(*this, peer, id, std::move(reason));
}
void timeout(endpoint_manager&, atom_value value, uint64_t id) {
next_layer_.timeout(*this, value, id);
}
void set_timeout(uint64_t timeout_id, id_type id) {
next_layer_.set_timeout(timeout_id, id);
}
void handle_error(sec code) {
next_layer_.handle_error(code);
}
// -- (pure) virtual functions -----------------------------------------------
virtual void configure_read(receive_policy::config){
// nop
};
virtual bool handle_read_event(endpoint_manager&) = 0;
virtual bool handle_write_event(endpoint_manager& parent) = 0;
virtual void write_packet(id_type id, span<buffer_type*> buffers) = 0;
// -- buffer management ------------------------------------------------------
buffer_type next_header_buffer() {
return next_buffer_impl(header_bufs_);
}
buffer_type next_payload_buffer() {
return next_buffer_impl(payload_bufs_);
}
private:
// -- utility functions ------------------------------------------------------
static buffer_type next_buffer_impl(buffer_cache_type cache) {
if (cache.empty()) {
return {};
}
auto buf = std::move(cache.back());
cache.pop_back();
return buf;
}
protected:
next_layer_type next_layer_;
handle_type handle_;
buffer_cache_type header_bufs_;
buffer_cache_type payload_bufs_;
buffer_type read_buf_;
// TODO implement retries using this member! Should this go into stream_trans?
// size_t max_consecutive_reads_;
endpoint_manager* manager_;
};
} // namespace caf::net
...@@ -18,16 +18,12 @@ ...@@ -18,16 +18,12 @@
#pragma once #pragma once
#include "caf/byte.hpp" #include "caf/net/endpoint_manager_queue.hpp"
#include "caf/ip_endpoint.hpp"
#include "caf/net/endpoint_manager.hpp"
#include "caf/net/fwd.hpp" #include "caf/net/fwd.hpp"
#include "caf/net/packet_writer_decorator.hpp" #include "caf/net/packet_writer_decorator.hpp"
#include "caf/span.hpp"
#include "caf/unit.hpp" #include "caf/unit.hpp"
namespace caf { namespace caf::net {
namespace net {
/// Implements a worker for transport protocols. /// Implements a worker for transport protocols.
template <class Application, class IdType> template <class Application, class IdType>
...@@ -41,7 +37,8 @@ public: ...@@ -41,7 +37,8 @@ public:
// -- constructors, destructors, and assignment operators -------------------- // -- constructors, destructors, and assignment operators --------------------
transport_worker(application_type application, id_type id = id_type{}) explicit transport_worker(application_type application,
id_type id = id_type{})
: application_(std::move(application)), id_(std::move(id)) { : application_(std::move(application)), id_(std::move(id)) {
// nop // nop
} }
...@@ -119,5 +116,4 @@ template <class Application, class IdType = unit_t> ...@@ -119,5 +116,4 @@ template <class Application, class IdType = unit_t>
using transport_worker_ptr = std::shared_ptr< using transport_worker_ptr = std::shared_ptr<
transport_worker<Application, IdType>>; transport_worker<Application, IdType>>;
} // namespace net } // namespace caf::net
} // namespace caf
...@@ -18,34 +18,27 @@ ...@@ -18,34 +18,27 @@
#pragma once #pragma once
#include <caf/logger.hpp>
#include <caf/sec.hpp>
#include <unordered_map> #include <unordered_map>
#include "caf/byte.hpp" #include "caf/logger.hpp"
#include "caf/ip_endpoint.hpp" #include "caf/net/endpoint_manager_queue.hpp"
#include "caf/net/endpoint_manager.hpp"
#include "caf/net/fwd.hpp" #include "caf/net/fwd.hpp"
#include "caf/net/packet_writer_decorator.hpp" #include "caf/net/packet_writer_decorator.hpp"
#include "caf/net/transport_worker.hpp" #include "caf/net/transport_worker.hpp"
#include "caf/sec.hpp"
#include "caf/send.hpp" #include "caf/send.hpp"
#include "caf/span.hpp"
#include "caf/unit.hpp"
namespace caf { namespace caf::net {
namespace net {
/// Implements a dispatcher that dispatches between transport and workers. /// Implements a dispatcher that dispatches between transport and workers.
template <class Transport, class IdType> template <class Factory, class IdType>
class transport_worker_dispatcher { class transport_worker_dispatcher {
public: public:
// -- member types ----------------------------------------------------------- // -- member types -----------------------------------------------------------
using id_type = IdType; using id_type = IdType;
using transport_type = Transport; using factory_type = Factory;
using factory_type = typename transport_type::factory_type;
using application_type = typename factory_type::application_type; using application_type = typename factory_type::application_type;
...@@ -55,8 +48,8 @@ public: ...@@ -55,8 +48,8 @@ public:
// -- constructors, destructors, and assignment operators -------------------- // -- constructors, destructors, and assignment operators --------------------
transport_worker_dispatcher(transport_type& transport, factory_type factory) explicit transport_worker_dispatcher(factory_type factory)
: factory_(std::move(factory)), transport_(&transport) { : factory_(std::move(factory)) {
// nop // nop
} }
...@@ -141,6 +134,7 @@ public: ...@@ -141,6 +134,7 @@ public:
template <class Parent> template <class Parent>
expected<worker_ptr> add_new_worker(Parent& parent, node_id node, expected<worker_ptr> add_new_worker(Parent& parent, node_id node,
id_type id) { id_type id) {
CAF_LOG_TRACE(CAF_ARG(node) << CAF_ARG(id));
auto application = factory_.make(); auto application = factory_.make();
auto worker = std::make_shared<worker_type>(std::move(application), id); auto worker = std::make_shared<worker_type>(std::move(application), id);
if (auto err = worker->init(parent)) if (auto err = worker->init(parent))
...@@ -176,8 +170,6 @@ private: ...@@ -176,8 +170,6 @@ private:
std::unordered_map<uint64_t, worker_ptr> workers_by_timeout_id_; std::unordered_map<uint64_t, worker_ptr> workers_by_timeout_id_;
factory_type factory_; factory_type factory_;
transport_type* transport_;
}; };
} // namespace net } // namespace caf::net
} // namespace caf
...@@ -25,9 +25,9 @@ ...@@ -25,9 +25,9 @@
#include "caf/byte.hpp" #include "caf/byte.hpp"
#include "caf/detail/scope_guard.hpp" #include "caf/detail/scope_guard.hpp"
#include "caf/ip_endpoint.hpp"
#include "caf/make_actor.hpp" #include "caf/make_actor.hpp"
#include "caf/net/actor_proxy_impl.hpp" #include "caf/net/actor_proxy_impl.hpp"
#include "caf/net/endpoint_manager.hpp"
#include "caf/net/multiplexer.hpp" #include "caf/net/multiplexer.hpp"
#include "caf/serializer_impl.hpp" #include "caf/serializer_impl.hpp"
#include "caf/span.hpp" #include "caf/span.hpp"
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "caf/net/test/host_fixture.hpp" #include "caf/net/test/host_fixture.hpp"
#include "caf/test/dsl.hpp" #include "caf/test/dsl.hpp"
#include "caf/ip_endpoint.hpp"
#include "caf/make_actor.hpp" #include "caf/make_actor.hpp"
#include "caf/monitorable_actor.hpp" #include "caf/monitorable_actor.hpp"
#include "caf/node_id.hpp" #include "caf/node_id.hpp"
...@@ -182,12 +183,12 @@ uri operator"" _u(const char* cstr, size_t cstr_len) { ...@@ -182,12 +183,12 @@ uri operator"" _u(const char* cstr, size_t cstr_len) {
} }
struct fixture : host_fixture { struct fixture : host_fixture {
using dispatcher_type = transport_worker_dispatcher<dummy_transport, using dispatcher_type = transport_worker_dispatcher<dummy_application_factory,
ip_endpoint>; ip_endpoint>;
fixture() fixture()
: buf{std::make_shared<buffer_type>()}, : buf{std::make_shared<buffer_type>()},
dispatcher{dummy, dummy_application_factory{buf}}, dispatcher{dummy_application_factory{buf}},
dummy{buf} { dummy{buf} {
add_new_workers(); add_new_workers();
} }
...@@ -272,7 +273,7 @@ struct fixture : host_fixture { ...@@ -272,7 +273,7 @@ struct fixture : host_fixture {
CAF_TEST_FIXTURE_SCOPE(transport_worker_dispatcher_test, fixture) CAF_TEST_FIXTURE_SCOPE(transport_worker_dispatcher_test, fixture)
CAF_TEST(init) { CAF_TEST(init) {
dispatcher_type dispatcher{dummy, dummy_application_factory{buf}}; dispatcher_type dispatcher{dummy_application_factory{buf}};
CAF_CHECK_EQUAL(dispatcher.init(dummy), none); CAF_CHECK_EQUAL(dispatcher.init(dummy), none);
} }
......
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