rotor
Event loop friendly C++ actor micro-framework
 
Loading...
Searching...
No Matches
link_client.h
1#pragma once
2
3//
4// Copyright (c) 2019-2023 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
5//
6// Distributed under the MIT Software License
7//
8
9#include "plugin_base.h"
10#include <string>
11#include <unordered_map>
12#include <functional>
13#include <forward_list>
14
15#if defined(_MSC_VER)
16#pragma warning(push)
17#pragma warning(disable : 4251)
18#endif
19
20namespace rotor::plugin {
21
34struct ROTOR_API link_client_plugin_t : public plugin_base_t {
36 using link_callback_t = std::function<void(const extended_error_ptr_t &)>;
37
43 using unlink_reaction_t = std::function<bool(message::unlink_request_t &message)>;
44
45 using plugin_base_t::plugin_base_t;
46
48 static const std::type_index class_identity;
49
50 const std::type_index &identity() const noexcept override;
51
52 void activate(actor_base_t *actor) noexcept override;
53
63 virtual void link(const address_ptr_t &address, bool operational_only = true,
64 const link_callback_t &callback = {}) noexcept;
65
67 template <typename F> void on_unlink(F &&callback) noexcept { unlink_reaction = std::forward<F>(callback); }
68
70 virtual void on_link_response(message::link_response_t &message) noexcept;
71
73 virtual void on_unlink_request(message::unlink_request_t &message) noexcept;
74
75 bool handle_shutdown(message::shutdown_request_t *message) noexcept override;
76 bool handle_init(message::init_request_t *message) noexcept override;
77
79 void forget_link(message::unlink_request_t &message) noexcept;
80
81 private:
82 enum class link_state_t { LINKING, OPERATIONAL, UNLINKING };
83 struct server_record_t {
84 link_callback_t callback;
85 link_state_t state;
86 request_id_t request_id;
87 };
88 using servers_map_t = std::unordered_map<address_ptr_t, server_record_t>;
90 using unlink_queue_t = std::list<unlink_req_t>;
91
92 void try_forget_links(bool attempt_shutdown) noexcept;
93 servers_map_t servers_map;
94 unlink_reaction_t unlink_reaction;
95 unlink_queue_t unlink_queue;
96 bool configured = false;
97};
98
99} // namespace rotor::plugin
100
101#if defined(_MSC_VER)
102#pragma warning(pop)
103#endif
intrusive_ptr_t< address_t > address_ptr_t
intrusive pointer for address
Definition address.hpp:57
intrusive_ptr_t< extended_error_t > extended_error_ptr_t
intrusive pointer to extended error type
Definition extended_error.h:25
std::size_t request_id_t
timer identifier type in the scope of the actor
Definition forward.hpp:34
boost::intrusive_ptr< T > intrusive_ptr_t
alias for intrusive pointer
Definition arc.hpp:27
universal primitive of concurrent computation
Definition actor_base.h:47
the generic message meant to hold user-specific payload
Definition message.h:80
base class for all actor plugins
Definition plugin_base.h:23