@@ -31,6 +31,11 @@ namespace libbitcoin {
3131namespace node {
3232
3333// / Abstract base for node protocols, thread safe.
34+ // / This node::protocol is not derived from network::protocol, but given the
35+ // / channel constructor parameter is derived from network::channel, the strand
36+ // / is accessible despite lack of bind/post/parallel templates access. This
37+ // / allows event subscription by derived protocols without the need to derive
38+ // / from protocol_peer (which would prevent derivation from service protocols).
3439class BCN_API protocol
3540{
3641protected:
@@ -42,8 +47,7 @@ class BCN_API protocol
4247 // reinterpret_pointer_cast because channel is abstract.
4348 inline protocol (const auto & session,
4449 const network::channel::ptr& channel) NOEXCEPT
45- : channel_(std::reinterpret_pointer_cast<node::channel>(channel)),
46- session_(session)
50+ : channel_(channel), session_(session)
4751 {
4852 }
4953
@@ -63,12 +67,35 @@ class BCN_API protocol
6367 // / The candidate|confirmed chain is current.
6468 virtual bool is_current (bool confirmed) const NOEXCEPT;
6569
70+ // / Events subscription.
71+ // / -----------------------------------------------------------------------
72+
73+ // / Subscribe to chaser events (max one active per protocol).
74+ virtual void subscribe_events (event_notifier&& handler) NOEXCEPT;
75+
76+ // / Override to handle subscription completion (stranded).
77+ virtual void subscribed (const code& ec, object_key key) NOEXCEPT;
78+
79+ // / Unsubscribe from chaser events.
80+ // / Subscribing protocol must invoke from overridden stopping().
81+ virtual void unsubscribe_events () NOEXCEPT;
82+
83+ // / Get the subscription key (for notify_one).
84+ virtual object_key events_key () const NOEXCEPT;
85+
6686private:
87+ void handle_subscribed (const code& ec, object_key key) NOEXCEPT;
88+ void handle_subscribe (const code& ec, object_key key,
89+ const event_completer& complete) NOEXCEPT;
90+
6791 // This channel requires stranded calls, base is thread safe.
68- const node ::channel::ptr channel_;
92+ const network ::channel::ptr channel_;
6993
7094 // This is thread safe.
7195 const node::session::ptr session_;
96+
97+ // This is protected by singular subscription.
98+ object_key key_{};
7299};
73100
74101} // namespace node
0 commit comments