-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add udp backend #76
Add udp backend #76
Changes from all commits
545dddc
c260250
3f74c77
c125c14
616b4cf
2a39d52
f9891c1
a0d726f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/****************************************************************************** | ||
* ____ _ _____ * | ||
* / ___| / \ | ___| C++ * | ||
* | | / _ \ | |_ Actor * | ||
* | |___ / ___ \| _| Framework * | ||
* \____/_/ \_|_| * | ||
* * | ||
* Copyright 2011-2020 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 <map> | ||
#include <mutex> | ||
|
||
#include "caf/detail/net_export.hpp" | ||
#include "caf/error.hpp" | ||
#include "caf/expected.hpp" | ||
#include "caf/net/basp/application.hpp" | ||
#include "caf/net/fwd.hpp" | ||
#include "caf/net/make_endpoint_manager.hpp" | ||
#include "caf/net/middleman.hpp" | ||
#include "caf/net/middleman_backend.hpp" | ||
#include "caf/net/multiplexer.hpp" | ||
#include "caf/net/udp_datagram_socket.hpp" | ||
#include "caf/node_id.hpp" | ||
|
||
namespace caf::net::backend { | ||
|
||
/// Minimal backend for udp communication. | ||
class CAF_NET_EXPORT udp : public middleman_backend { | ||
public: | ||
// -- constructors, destructors, and assignment operators -------------------- | ||
|
||
udp(middleman& mm); | ||
|
||
~udp() override; | ||
|
||
// -- interface functions ---------------------------------------------------- | ||
|
||
error init() override; | ||
|
||
void stop() override; | ||
|
||
expected<endpoint_manager_ptr> get_or_connect(const uri& locator) override; | ||
|
||
endpoint_manager_ptr peer(const node_id&) override; | ||
|
||
void resolve(const uri& locator, const actor& listener) override; | ||
|
||
strong_actor_ptr make_proxy(node_id nid, actor_id aid) override; | ||
|
||
void set_last_hop(node_id*) override; | ||
|
||
// -- properties ------------------------------------------------------------- | ||
|
||
uint16_t port() const noexcept override { | ||
return listening_port_; | ||
} | ||
|
||
expected<endpoint_manager_ptr> emplace(udp_datagram_socket sock, | ||
uint16_t port); | ||
|
||
private: | ||
middleman& mm_; | ||
|
||
endpoint_manager_ptr ep_manager_; | ||
|
||
std::vector<node_id> node_ids_; | ||
|
||
proxy_registry proxies_; | ||
|
||
uint16_t listening_port_; | ||
|
||
std::mutex lock_; | ||
}; | ||
|
||
} // namespace caf::net::backend |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/****************************************************************************** | ||
* ____ _ _____ * | ||
* / ___| / \ | ___| C++ * | ||
* | | / _ \ | |_ Actor * | ||
* | |___ / ___ \| _| Framework * | ||
* \____/_/ \_|_| * | ||
* * | ||
* Copyright 2011-2020 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/net_export.hpp" | ||
#include "caf/error.hpp" | ||
#include "caf/net/basp/application.hpp" | ||
#include "caf/net/datagram_adaptor.hpp" | ||
#include "caf/proxy_registry.hpp" | ||
|
||
namespace caf::net::basp { | ||
|
||
/// Factory for datagram oriented basp::applications. | ||
/// @relates transport_worker_dispatcher | ||
class CAF_NET_EXPORT datagram_application_factory { | ||
public: | ||
using application_type = datagram_adaptor<basp::application>; | ||
|
||
datagram_application_factory(proxy_registry& proxies) : proxies_(proxies) { | ||
// nop | ||
} | ||
|
||
template <class Parent> | ||
error init(Parent&) { | ||
return none; | ||
} | ||
|
||
application_type make() const { | ||
return application_type{basp::application{proxies_}}; | ||
} | ||
|
||
private: | ||
proxy_registry& proxies_; | ||
}; | ||
|
||
} // namespace caf::net::basp |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/****************************************************************************** | ||
* ____ _ _____ * | ||
* / ___| / \ | ___| C++ * | ||
* | | / _ \ | |_ Actor * | ||
* | |___ / ___ \| _| Framework * | ||
* \____/_/ \_|_| * | ||
* * | ||
* Copyright 2011-2020 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/byte_buffer.hpp" | ||
#include "caf/net/basp/header.hpp" | ||
|
||
namespace caf::net { | ||
|
||
/// Implements an adaption layer for datagram oriented transport protocols. | ||
template <class Application> | ||
class datagram_adaptor { | ||
public: | ||
datagram_adaptor(Application application) | ||
: application_(std::move(application)) { | ||
// nop | ||
} | ||
|
||
template <class Parent> | ||
error init(Parent& parent) { | ||
return application_.init(parent); | ||
} | ||
|
||
template <class Parent> | ||
error write_message(Parent& parent, | ||
std::unique_ptr<endpoint_manager_queue::message> msg) { | ||
return application_.write_message(parent, std::move(msg)); | ||
} | ||
|
||
template <class Parent> | ||
error handle_data(Parent& parent, span<const byte> received) { | ||
if (auto err = application_.handle_data( | ||
parent, make_span(received.data(), basp::header_size))) | ||
return err; | ||
auto data = received.data() + basp::header_size; | ||
auto size = received.size() - basp::header_size; | ||
if (size > 0) | ||
return application_.handle_data(parent, make_span(data, size)); | ||
return none; | ||
} | ||
|
||
template <class Parent> | ||
void resolve(Parent& parent, string_view path, const actor& listener) { | ||
application_.resolve(parent, path, listener); | ||
} | ||
|
||
template <class Parent> | ||
void new_proxy(Parent& parent, actor_id id) { | ||
application_.new_proxy(parent, id); | ||
} | ||
|
||
template <class Parent> | ||
void local_actor_down(Parent& parent, actor_id id, error reason) { | ||
application_.local_actor_down(parent, id, std::move(reason)); | ||
} | ||
|
||
template <class Parent> | ||
void timeout(Parent& parent, std::string tag, uint64_t id) { | ||
application_.timeout(parent, std::move(tag), id); | ||
} | ||
|
||
void handle_error(sec error) { | ||
application_.handle_error(error); | ||
} | ||
|
||
private: | ||
Application application_; | ||
}; | ||
|
||
} // namespace caf::net |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,7 @@ CAF_NET_EXPORT extern const size_t max_header_buffers; | |
/// Port to listen on for tcp. | ||
CAF_NET_EXPORT extern const uint16_t tcp_port; | ||
|
||
/// Port to listen on for udp. | ||
CAF_NET_EXPORT extern const uint16_t udp_port; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need two constants? I assume this is for exposing BASP. Since we're going to use the locator (URI) as identifier for the CAF node, we must use either TCP or UDP. In other words, one default port would suffice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought that BASP could be used with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the new model, the node ID is the locator in order to get rid of the pseudo-DNS CAF is currently doing. This means there must be exactly one valid way to connect to a BASP instance. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I forgot about the fixed node ID.. Still I am not sure if that is the best way to solve this problem. Is it really necessary to allow only one possible way to reach a node via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By definition, a node can only have a single ID. If we allow multiple IDs pointing to the same BASP, we have the exact same issues we wanted to get rid of with the redesign that @josephnoir initiated. We could of course offer a "connect there and see who's responding" function, but it would convolute the design quite a bit. Because then, we can't use the ID any longer to identify connections. I honestly don't see an upside here that would be worth the troubles in the first place. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can understand that. I'll remove the duplicate port field when I'm fixing the other remark. |
||
|
||
} // namespace caf::defaults::middleman |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This hardwires the
datagram_adaptor
to BASP. Both in terms of constants and in terms of processing logic (header, then payload).If you see this as a one-shot class to fit BASP into a datagram manager, then we should rename it. If we want to generalize this adaptor abstraction, we should also rethink the interface to the application. We could split
handle_data
intohandle_header
andhandle_payload
for example. Alternatively, we always callhandle_data
on the application and it dispatches internally (more flexible). In any case we'd also need to propagate how much of the buffer was consumed (if any). Not all protocols have fixed sizes. HTTP, for example, has to read the header until hitting\r\n\r\n
. I was already running into this exact problem in another branch I was working on, so maybe should sort that out first? If we'd go with that, the adaptor would simply callhandle_data
until the application either has consumed the entire datagram or discarded some of it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TBH I think with the proposal of #81 the
datagram_adaptor
would be obsolete. We should discuss that issue first before renaming or rethinking thedatagram_adaptor
forBASP
.