forked from libbitcoin/libbitcoin-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.hpp
More file actions
104 lines (83 loc) · 3.72 KB
/
protocol.hpp
File metadata and controls
104 lines (83 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* Copyright (c) 2011-2026 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_HPP
#define LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_HPP
#include <memory>
#include <bitcoin/node/channels/channels.hpp>
#include <bitcoin/node/configuration.hpp>
#include <bitcoin/node/define.hpp>
// Only session.hpp.
#include <bitcoin/node/sessions/session.hpp>
namespace libbitcoin {
namespace node {
/// Abstract base for node protocols, thread safe.
/// This node::protocol is not derived from network::protocol, but given the
/// channel constructor parameter is derived from network::channel, the strand
/// is accessible despite lack of bind/post/parallel templates access. This
/// allows event subscription by derived protocols without the need to derive
/// from protocol_peer (which would prevent derivation from service protocols).
class BCN_API protocol
{
protected:
DELETE_COPY_MOVE_DESTRUCT(protocol);
/// Constructors.
/// -----------------------------------------------------------------------
// reinterpret_pointer_cast because channel is abstract.
inline protocol(const auto& session,
const network::channel::ptr& channel) NOEXCEPT
: channel_(channel), session_(session)
{
}
/// Properties.
/// -----------------------------------------------------------------------
/// Thread safe synchronous archival interface.
query& archive() const NOEXCEPT;
/// Configuration settings for all libraries.
virtual const node::configuration& node_config() const NOEXCEPT;
virtual const system::settings& system_settings() const NOEXCEPT;
virtual const database::settings& database_settings() const NOEXCEPT;
////const network::settings& network_settings() const NOEXCEPT override;
virtual const node::settings& node_settings() const NOEXCEPT;
/// The candidate|confirmed chain is current.
virtual bool is_current(bool confirmed) const NOEXCEPT;
/// Events subscription.
/// -----------------------------------------------------------------------
/// Subscribe to chaser events (max one active per protocol).
virtual void subscribe_events(event_notifier&& handler) NOEXCEPT;
/// Override to handle subscription completion (stranded).
virtual void subscribed(const code& ec, object_key key) NOEXCEPT;
/// Unsubscribe from chaser events.
/// Subscribing protocol must invoke from overridden stopping().
virtual void unsubscribe_events() NOEXCEPT;
/// Get the subscription key (for notify_one).
virtual object_key events_key() const NOEXCEPT;
private:
void handle_subscribed(const code& ec, object_key key) NOEXCEPT;
void handle_subscribe(const code& ec, object_key key,
const event_completer& complete) NOEXCEPT;
// This channel requires stranded calls, base is thread safe.
const network::channel::ptr channel_;
// This is thread safe.
const node::session::ptr session_;
// This is protected by singular subscription.
object_key key_{};
};
} // namespace node
} // namespace libbitcoin
#endif