|
18 | 18 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
19 | 19 | // DEALINGS IN THE SOFTWARE.
|
20 | 20 |
|
21 |
| -pub struct AutoNat; |
| 21 | +pub use crate::protocol::DialResponse; |
| 22 | +use crate::protocol::{AutoNatCodec, AutoNatProtocol}; |
| 23 | +use libp2p_core::{ |
| 24 | + connection::{ConnectionId, ListenerId}, |
| 25 | + ConnectedPoint, Multiaddr, PeerId, |
| 26 | +}; |
| 27 | +use libp2p_request_response::{ |
| 28 | + handler::RequestResponseHandlerEvent, ProtocolSupport, RequestResponse, RequestResponseConfig, |
| 29 | + RequestResponseEvent, RequestResponseMessage, |
| 30 | +}; |
| 31 | +use libp2p_swarm::{ |
| 32 | + IntoProtocolsHandler, NetworkBehaviour, NetworkBehaviourAction, PollParameters, |
| 33 | +}; |
| 34 | +use std::{ |
| 35 | + iter, |
| 36 | + task::{Context, Poll}, |
| 37 | +}; |
22 | 38 |
|
23 |
| -pub enum AutoNatEvent {} |
| 39 | +#[derive(Clone, Debug, Eq, PartialEq)] |
| 40 | +pub enum AutoNatEvent { |
| 41 | + DialResponse(DialResponse), |
| 42 | +} |
| 43 | + |
| 44 | +pub struct AutoNat { |
| 45 | + inner: RequestResponse<AutoNatCodec>, |
| 46 | +} |
| 47 | + |
| 48 | +impl AutoNat { |
| 49 | + pub fn new() -> Self { |
| 50 | + let protocols = iter::once((AutoNatProtocol, ProtocolSupport::Full)); |
| 51 | + let cfg = RequestResponseConfig::default(); |
| 52 | + let inner = RequestResponse::new(AutoNatCodec, protocols, cfg); |
| 53 | + Self { inner } |
| 54 | + } |
| 55 | + |
| 56 | + pub fn add_address(&mut self, peer_id: &PeerId, addr: Multiaddr) { |
| 57 | + self.inner.add_address(peer_id, addr) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl NetworkBehaviour for AutoNat { |
| 62 | + type ProtocolsHandler = <RequestResponse<AutoNatCodec> as NetworkBehaviour>::ProtocolsHandler; |
| 63 | + type OutEvent = AutoNatEvent; |
| 64 | + |
| 65 | + fn new_handler(&mut self) -> Self::ProtocolsHandler { |
| 66 | + self.inner.new_handler() |
| 67 | + } |
| 68 | + |
| 69 | + fn addresses_of_peer(&mut self, peer: &PeerId) -> Vec<Multiaddr> { |
| 70 | + self.inner.addresses_of_peer(peer) |
| 71 | + } |
| 72 | + |
| 73 | + fn inject_connected(&mut self, peer: &PeerId) { |
| 74 | + self.inner.inject_connected(peer) |
| 75 | + } |
| 76 | + |
| 77 | + fn inject_disconnected(&mut self, peer: &PeerId) { |
| 78 | + self.inner.inject_disconnected(peer) |
| 79 | + } |
| 80 | + |
| 81 | + fn inject_connection_established( |
| 82 | + &mut self, |
| 83 | + peer: &PeerId, |
| 84 | + conn: &ConnectionId, |
| 85 | + endpoint: &ConnectedPoint, |
| 86 | + ) { |
| 87 | + self.inner |
| 88 | + .inject_connection_established(peer, conn, endpoint) |
| 89 | + } |
| 90 | + |
| 91 | + fn inject_connection_closed( |
| 92 | + &mut self, |
| 93 | + peer: &PeerId, |
| 94 | + conn: &ConnectionId, |
| 95 | + endpoint: &ConnectedPoint, |
| 96 | + handler: <Self::ProtocolsHandler as IntoProtocolsHandler>::Handler, |
| 97 | + ) { |
| 98 | + self.inner |
| 99 | + .inject_connection_closed(peer, conn, endpoint, handler) |
| 100 | + } |
| 101 | + |
| 102 | + fn inject_address_change( |
| 103 | + &mut self, |
| 104 | + peer: &PeerId, |
| 105 | + conn: &ConnectionId, |
| 106 | + old: &ConnectedPoint, |
| 107 | + new: &ConnectedPoint, |
| 108 | + ) { |
| 109 | + self.inner.inject_address_change(peer, conn, old, new) |
| 110 | + } |
| 111 | + |
| 112 | + fn inject_event( |
| 113 | + &mut self, |
| 114 | + peer: PeerId, |
| 115 | + conn: ConnectionId, |
| 116 | + event: RequestResponseHandlerEvent<AutoNatCodec>, |
| 117 | + ) { |
| 118 | + self.inner.inject_event(peer, conn, event) |
| 119 | + } |
| 120 | + |
| 121 | + fn inject_addr_reach_failure( |
| 122 | + &mut self, |
| 123 | + peer_id: Option<&PeerId>, |
| 124 | + addr: &Multiaddr, |
| 125 | + error: &dyn std::error::Error, |
| 126 | + ) { |
| 127 | + self.inner.inject_addr_reach_failure(peer_id, addr, error) |
| 128 | + } |
| 129 | + |
| 130 | + fn inject_dial_failure( |
| 131 | + &mut self, |
| 132 | + peer_id: &PeerId, |
| 133 | + handler: Self::ProtocolsHandler, |
| 134 | + error: libp2p_swarm::DialError, |
| 135 | + ) { |
| 136 | + self.inner.inject_dial_failure(peer_id, handler, error) |
| 137 | + } |
| 138 | + |
| 139 | + fn inject_listen_failure( |
| 140 | + &mut self, |
| 141 | + local_addr: &Multiaddr, |
| 142 | + send_back_addr: &Multiaddr, |
| 143 | + handler: Self::ProtocolsHandler, |
| 144 | + ) { |
| 145 | + self.inner |
| 146 | + .inject_listen_failure(local_addr, send_back_addr, handler) |
| 147 | + } |
| 148 | + |
| 149 | + fn inject_new_listener(&mut self, id: ListenerId) { |
| 150 | + self.inner.inject_new_listener(id) |
| 151 | + } |
| 152 | + |
| 153 | + fn inject_new_listen_addr(&mut self, id: ListenerId, addr: &Multiaddr) { |
| 154 | + self.inner.inject_new_listen_addr(id, addr) |
| 155 | + } |
| 156 | + |
| 157 | + fn inject_expired_listen_addr(&mut self, id: ListenerId, addr: &Multiaddr) { |
| 158 | + self.inner.inject_expired_listen_addr(id, addr) |
| 159 | + } |
| 160 | + |
| 161 | + fn inject_listener_error(&mut self, id: ListenerId, err: &(dyn std::error::Error + 'static)) { |
| 162 | + self.inner.inject_listener_error(id, err) |
| 163 | + } |
| 164 | + |
| 165 | + fn inject_listener_closed(&mut self, id: ListenerId, reason: Result<(), &std::io::Error>) { |
| 166 | + self.inner.inject_listener_closed(id, reason) |
| 167 | + } |
| 168 | + |
| 169 | + fn inject_new_external_addr(&mut self, addr: &Multiaddr) { |
| 170 | + self.inner.inject_new_external_addr(addr) |
| 171 | + } |
| 172 | + |
| 173 | + fn inject_expired_external_addr(&mut self, addr: &Multiaddr) { |
| 174 | + self.inner.inject_expired_external_addr(addr) |
| 175 | + } |
| 176 | + |
| 177 | + fn poll( |
| 178 | + &mut self, |
| 179 | + cx: &mut Context<'_>, |
| 180 | + params: &mut impl PollParameters, |
| 181 | + ) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler>> { |
| 182 | + loop { |
| 183 | + match self.inner.poll(cx, params) { |
| 184 | + Poll::Ready(NetworkBehaviourAction::GenerateEvent( |
| 185 | + RequestResponseEvent::Message { peer, message }, |
| 186 | + )) => match message { |
| 187 | + RequestResponseMessage::Request { |
| 188 | + request_id, |
| 189 | + request, |
| 190 | + channel, |
| 191 | + } => { |
| 192 | + println!("{} {:?} {:?} {:?}", peer, request_id, request, channel); |
| 193 | + } |
| 194 | + RequestResponseMessage::Response { |
| 195 | + request_id, |
| 196 | + response, |
| 197 | + } => { |
| 198 | + println!("{} {:?} {:?}", peer, request_id, response); |
| 199 | + } |
| 200 | + }, |
| 201 | + Poll::Ready(NetworkBehaviourAction::GenerateEvent( |
| 202 | + RequestResponseEvent::ResponseSent { peer, request_id }, |
| 203 | + )) => { |
| 204 | + println!("response sent {} {:?}", peer, request_id); |
| 205 | + } |
| 206 | + Poll::Ready(NetworkBehaviourAction::GenerateEvent( |
| 207 | + RequestResponseEvent::OutboundFailure { |
| 208 | + peer, |
| 209 | + request_id, |
| 210 | + error, |
| 211 | + }, |
| 212 | + )) => { |
| 213 | + println!("outbound failure {} {:?} {:?}", peer, request_id, error); |
| 214 | + } |
| 215 | + Poll::Ready(NetworkBehaviourAction::GenerateEvent( |
| 216 | + RequestResponseEvent::InboundFailure { |
| 217 | + peer, |
| 218 | + error, |
| 219 | + request_id, |
| 220 | + }, |
| 221 | + )) => { |
| 222 | + println!("inbound failure {} {:?} {:?}", peer, request_id, error); |
| 223 | + } |
| 224 | + Poll::Ready(NetworkBehaviourAction::DialAddress { address, handler }) => { |
| 225 | + return Poll::Ready(NetworkBehaviourAction::DialAddress { address, handler }) |
| 226 | + } |
| 227 | + Poll::Ready(NetworkBehaviourAction::DialPeer { |
| 228 | + peer_id, |
| 229 | + condition, |
| 230 | + handler, |
| 231 | + }) => { |
| 232 | + return Poll::Ready(NetworkBehaviourAction::DialPeer { |
| 233 | + peer_id, |
| 234 | + condition, |
| 235 | + handler, |
| 236 | + }) |
| 237 | + } |
| 238 | + Poll::Ready(NetworkBehaviourAction::CloseConnection { |
| 239 | + peer_id, |
| 240 | + connection, |
| 241 | + }) => { |
| 242 | + return Poll::Ready(NetworkBehaviourAction::CloseConnection { |
| 243 | + peer_id, |
| 244 | + connection, |
| 245 | + }) |
| 246 | + } |
| 247 | + Poll::Ready(NetworkBehaviourAction::NotifyHandler { |
| 248 | + peer_id, |
| 249 | + handler, |
| 250 | + event, |
| 251 | + }) => { |
| 252 | + return Poll::Ready(NetworkBehaviourAction::NotifyHandler { |
| 253 | + peer_id, |
| 254 | + handler, |
| 255 | + event, |
| 256 | + }) |
| 257 | + } |
| 258 | + Poll::Ready(NetworkBehaviourAction::ReportObservedAddr { address, score }) => { |
| 259 | + return Poll::Ready(NetworkBehaviourAction::ReportObservedAddr { |
| 260 | + address, |
| 261 | + score, |
| 262 | + }) |
| 263 | + } |
| 264 | + Poll::Pending => return Poll::Pending, |
| 265 | + } |
| 266 | + } |
| 267 | + } |
| 268 | +} |
0 commit comments