Skip to main content

rns_net/common/
callbacks.rs

1//! Application callback trait for driver events.
2
3use crate::{DestHash, IdentityHash, InterfaceId, LinkId, PacketHash, TeardownReason};
4
5pub trait Callbacks: Send {
6    fn on_announce(&mut self, announced: crate::common::destination::AnnouncedIdentity);
7
8    fn on_path_updated(&mut self, dest_hash: DestHash, hops: u8);
9
10    fn on_local_delivery(&mut self, dest_hash: DestHash, raw: Vec<u8>, packet_hash: PacketHash);
11
12    /// Called when an interface comes online.
13    fn on_interface_up(&mut self, _id: InterfaceId) {}
14
15    /// Called when an interface goes offline.
16    fn on_interface_down(&mut self, _id: InterfaceId) {}
17
18    /// Called when a link is fully established.
19    fn on_link_established(
20        &mut self,
21        _link_id: LinkId,
22        _dest_hash: DestHash,
23        _rtt: f64,
24        _is_initiator: bool,
25    ) {
26    }
27
28    /// Called when a link is closed.
29    fn on_link_closed(&mut self, _link_id: LinkId, _reason: Option<TeardownReason>) {}
30
31    /// Called when a remote peer identifies on a link.
32    fn on_remote_identified(
33        &mut self,
34        _link_id: LinkId,
35        _identity_hash: IdentityHash,
36        _public_key: [u8; 64],
37    ) {
38    }
39
40    /// Called when a resource transfer delivers data.
41    fn on_resource_received(
42        &mut self,
43        _link_id: LinkId,
44        _data: Vec<u8>,
45        _metadata: Option<Vec<u8>>,
46    ) {
47    }
48
49    /// Called when a resource transfer completes (sender-side proof validated).
50    fn on_resource_completed(&mut self, _link_id: LinkId) {}
51
52    /// Called when a resource transfer fails.
53    fn on_resource_failed(&mut self, _link_id: LinkId, _error: String) {}
54
55    /// Called with resource transfer progress updates.
56    fn on_resource_progress(&mut self, _link_id: LinkId, _received: usize, _total: usize) {}
57
58    /// Called to ask whether to accept an incoming resource (for AcceptApp strategy).
59    /// Return true to accept, false to reject.
60    fn on_resource_accept_query(
61        &mut self,
62        _link_id: LinkId,
63        _resource_hash: Vec<u8>,
64        _transfer_size: u64,
65        _has_metadata: bool,
66    ) -> bool {
67        false
68    }
69
70    /// Called when a channel message is received on a link.
71    fn on_channel_message(&mut self, _link_id: LinkId, _msgtype: u16, _payload: Vec<u8>) {}
72
73    /// Called when generic link data is received.
74    fn on_link_data(&mut self, _link_id: LinkId, _context: u8, _data: Vec<u8>) {}
75
76    /// Called when a response is received on a link.
77    fn on_response(&mut self, _link_id: LinkId, _request_id: [u8; 16], _data: Vec<u8>) {}
78
79    /// Called when a delivery proof is received for a packet we sent.
80    /// `rtt` is the round-trip time in seconds.
81    fn on_proof(&mut self, _dest_hash: DestHash, _packet_hash: PacketHash, _rtt: f64) {}
82
83    /// Called for ProveApp strategy: should we prove this incoming packet?
84    /// Return true to generate and send a proof, false to skip.
85    fn on_proof_requested(&mut self, _dest_hash: DestHash, _packet_hash: PacketHash) -> bool {
86        true
87    }
88
89    /// Called when a direct connection is proposed by a peer (for AskApp policy).
90    /// Return true to accept, false to reject.
91    fn on_direct_connect_proposed(
92        &mut self,
93        _link_id: LinkId,
94        _peer_identity: Option<IdentityHash>,
95    ) -> bool {
96        false
97    }
98
99    /// Called when a direct P2P connection is established via hole punching.
100    fn on_direct_connect_established(&mut self, _link_id: LinkId, _interface_id: InterfaceId) {}
101
102    /// Called when a direct connection attempt fails.
103    fn on_direct_connect_failed(&mut self, _link_id: LinkId, _reason: u8) {}
104}