File tree Expand file tree Collapse file tree 4 files changed +31
-0
lines changed Expand file tree Collapse file tree 4 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -75,6 +75,8 @@ namespace libp2p::host {
75
75
76
76
event::Bus &getBus () override ;
77
77
78
+ event::Handle setOnNewConnectionHandler (const NewConnectionHandler &h) const override ;
79
+
78
80
private:
79
81
std::shared_ptr<peer::IdentityManager> idmgr_;
80
82
std::unique_ptr<network::Network> network_;
Original file line number Diff line number Diff line change @@ -39,11 +39,19 @@ namespace libp2p {
39
39
using StreamResultHandler = std::function<void (
40
40
outcome::result<std::shared_ptr<connection::Stream>>)>;
41
41
42
+ using NewConnectionHandler = std::function<void (peer::PeerInfo &&)>;
43
+
42
44
/* *
43
45
* @brief Get a version of Libp2p, supported by this Host
44
46
*/
45
47
virtual std::string_view getLibp2pVersion () const = 0;
46
48
49
+ /* *
50
+ * @brief Stores OnNewConnectionHandler.
51
+ * @param h handler function to store
52
+ */
53
+ virtual event::Handle setOnNewConnectionHandler (const NewConnectionHandler &h) const = 0;
54
+
47
55
/* *
48
56
* @brief Get a version of this Libp2p client
49
57
*/
Original file line number Diff line number Diff line change @@ -109,6 +109,26 @@ namespace libp2p::host {
109
109
network_->getListener ().start ();
110
110
}
111
111
112
+ event::Handle BasicHost::setOnNewConnectionHandler (const NewConnectionHandler &h) const {
113
+ return bus_->getChannel <network::event::OnNewConnectionChannel>().subscribe (
114
+ [h{std::move (h)}](auto &&conn) {
115
+ if (auto connection = conn.lock ()) {
116
+ auto remote_peer_res = connection->remotePeer ();
117
+ if (!remote_peer_res)
118
+ return ;
119
+
120
+ auto remote_peer_addr_res = connection->remoteMultiaddr ();
121
+ if (!remote_peer_addr_res)
122
+ return ;
123
+
124
+ if (h != nullptr )
125
+ h (peer::PeerInfo{std::move (remote_peer_res.value ()),
126
+ std::vector<multi::Multiaddress>{
127
+ std::move (remote_peer_addr_res.value ())}});
128
+ }
129
+ });
130
+ }
131
+
112
132
void BasicHost::stop () {
113
133
network_->getListener ().stop ();
114
134
}
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ namespace libp2p {
16
16
public:
17
17
~HostMock () override = default ;
18
18
19
+ MOCK_CONST_METHOD1 (setOnNewConnectionHandler, event::Handle(const NewConnectionHandler &));
19
20
MOCK_CONST_METHOD0 (getLibp2pVersion, std::string_view());
20
21
MOCK_CONST_METHOD0 (getLibp2pClientVersion, std::string_view());
21
22
MOCK_CONST_METHOD0 (getId, peer::PeerId());
You can’t perform that action at this time.
0 commit comments