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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
// Copyright 2019 Parity Technologies (UK) Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use crate::{
Multiaddr,
muxing::StreamMuxer,
connection::{
self,
Close,
Connected,
Connection,
ConnectionError,
ConnectionHandler,
IntoConnectionHandler,
PendingConnectionError,
Substream,
},
};
use futures::{prelude::*, channel::mpsc, stream};
use std::{pin::Pin, task::Context, task::Poll};
use super::ConnectResult;
/// Identifier of a [`Task`] in a [`Manager`](super::Manager).
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct TaskId(pub(super) usize);
/// Commands that can be sent to a [`Task`].
#[derive(Debug)]
pub enum Command<T> {
/// Notify the connection handler of an event.
NotifyHandler(T),
/// Gracefully close the connection (active close) before
/// terminating the task.
Close,
}
/// Events that a task can emit to its manager.
#[derive(Debug)]
pub enum Event<T, H, TE, HE> {
/// A connection to a node has succeeded.
Established { id: TaskId, info: Connected },
/// A pending connection failed.
Failed { id: TaskId, error: PendingConnectionError<TE>, handler: H },
/// A node we are connected to has changed its address.
AddressChange { id: TaskId, new_address: Multiaddr },
/// Notify the manager of an event from the connection.
Notify { id: TaskId, event: T },
/// A connection closed, possibly due to an error.
///
/// If `error` is `None`, the connection has completed
/// an active orderly close.
Closed { id: TaskId, error: Option<ConnectionError<HE>> }
}
impl<T, H, TE, HE> Event<T, H, TE, HE> {
pub fn id(&self) -> &TaskId {
match self {
Event::Established { id, .. } => id,
Event::Failed { id, .. } => id,
Event::AddressChange { id, .. } => id,
Event::Notify { id, .. } => id,
Event::Closed { id, .. } => id,
}
}
}
/// A `Task` is a [`Future`] that handles a single connection.
pub struct Task<F, M, H, I, O, E>
where
M: StreamMuxer,
H: IntoConnectionHandler,
H::Handler: ConnectionHandler<Substream = Substream<M>>
{
/// The ID of this task.
id: TaskId,
/// Sender to emit events to the manager of this task.
events: mpsc::Sender<Event<O, H, E, <H::Handler as ConnectionHandler>::Error>>,
/// Receiver for commands sent by the manager of this task.
commands: stream::Fuse<mpsc::Receiver<Command<I>>>,
/// Inner state of this `Task`.
state: State<F, M, H, O, E>,
}
impl<F, M, H, I, O, E> Task<F, M, H, I, O, E>
where
M: StreamMuxer,
H: IntoConnectionHandler,
H::Handler: ConnectionHandler<Substream = Substream<M>>
{
/// Create a new task to connect and handle some node.
pub fn pending(
id: TaskId,
events: mpsc::Sender<Event<O, H, E, <H::Handler as ConnectionHandler>::Error>>,
commands: mpsc::Receiver<Command<I>>,
future: F,
handler: H
) -> Self {
Task {
id,
events,
commands: commands.fuse(),
state: State::Pending {
future: Box::pin(future),
handler,
},
}
}
/// Create a task for an existing node we are already connected to.
pub fn established(
id: TaskId,
events: mpsc::Sender<Event<O, H, E, <H::Handler as ConnectionHandler>::Error>>,
commands: mpsc::Receiver<Command<I>>,
connection: Connection<M, H::Handler>
) -> Self {
Task {
id,
events,
commands: commands.fuse(),
state: State::Established { connection, event: None },
}
}
}
/// The state associated with the `Task` of a connection.
enum State<F, M, H, O, E>
where
M: StreamMuxer,
H: IntoConnectionHandler,
H::Handler: ConnectionHandler<Substream = Substream<M>>
{
/// The connection is being negotiated.
Pending {
/// The future that will attempt to reach the node.
// TODO: don't pin this Future; this requires deeper changes though
future: Pin<Box<F>>,
/// The intended handler for the established connection.
handler: H,
},
/// The connection is established.
Established {
connection: Connection<M, H::Handler>,
/// An event to send to the `Manager`. If `None`, the `connection`
/// is polled for new events in this state, otherwise the event
/// must be sent to the `Manager` before the connection can be
/// polled again.
event: Option<Event<O, H, E, <H::Handler as ConnectionHandler>::Error>>
},
/// The connection is closing (active close).
Closing(Close<M>),
/// The task is terminating with a final event for the `Manager`.
Terminating(Event<O, H, E, <H::Handler as ConnectionHandler>::Error>),
/// The task has finished.
Done
}
impl<F, M, H, I, O, E> Unpin for Task<F, M, H, I, O, E>
where
M: StreamMuxer,
H: IntoConnectionHandler,
H::Handler: ConnectionHandler<Substream = Substream<M>>
{
}
impl<F, M, H, I, O, E> Future for Task<F, M, H, I, O, E>
where
M: StreamMuxer,
F: Future<Output = ConnectResult<M, E>>,
H: IntoConnectionHandler,
H::Handler: ConnectionHandler<Substream = Substream<M>, InEvent = I, OutEvent = O>
{
type Output = ();
// NOTE: It is imperative to always consume all incoming commands from
// the manager first, in order to not prevent it from making progress because
// it is blocked on the channel capacity.
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
let this = &mut *self;
let id = this.id;
'poll: loop {
match std::mem::replace(&mut this.state, State::Done) {
State::Pending { mut future, handler } => {
// Check whether the task is still registered with a `Manager`
// by polling the commands channel.
match this.commands.poll_next_unpin(cx) {
Poll::Pending => {},
Poll::Ready(None) => {
// The manager has dropped the task; abort.
return Poll::Ready(())
}
Poll::Ready(Some(_)) => panic!(
"Task received command while the connection is pending."
)
}
// Check if the connection succeeded.
match future.poll_unpin(cx) {
Poll::Ready(Ok((info, muxer))) => {
this.state = State::Established {
connection: Connection::new(
muxer,
handler.into_handler(&info),
),
event: Some(Event::Established { id, info })
}
}
Poll::Pending => {
this.state = State::Pending { future, handler };
return Poll::Pending
}
Poll::Ready(Err(error)) => {
// Don't accept any further commands and terminate the
// task with a final event.
this.commands.get_mut().close();
let event = Event::Failed { id, handler, error };
this.state = State::Terminating(event)
}
}
}
State::Established { mut connection, event } => {
// Check for commands from the `Manager`.
loop {
match this.commands.poll_next_unpin(cx) {
Poll::Pending => break,
Poll::Ready(Some(Command::NotifyHandler(event))) =>
connection.inject_event(event),
Poll::Ready(Some(Command::Close)) => {
// Don't accept any further commands.
this.commands.get_mut().close();
// Discard the event, if any, and start a graceful close.
this.state = State::Closing(connection.close());
continue 'poll
}
Poll::Ready(None) => {
// The manager has dropped the task or disappeared; abort.
return Poll::Ready(())
}
}
}
if let Some(event) = event {
// Send the event to the manager.
match this.events.poll_ready(cx) {
Poll::Pending => {
this.state = State::Established { connection, event: Some(event) };
return Poll::Pending
}
Poll::Ready(result) => {
if result.is_ok() {
if let Ok(()) = this.events.start_send(event) {
this.state = State::Established { connection, event: None };
continue 'poll
}
}
// The manager is no longer reachable; abort.
return Poll::Ready(())
}
}
} else {
// Poll the connection for new events.
match Connection::poll(Pin::new(&mut connection), cx) {
Poll::Pending => {
this.state = State::Established { connection, event: None };
return Poll::Pending
}
Poll::Ready(Ok(connection::Event::Handler(event))) => {
this.state = State::Established {
connection,
event: Some(Event::Notify { id, event })
};
}
Poll::Ready(Ok(connection::Event::AddressChange(new_address))) => {
this.state = State::Established {
connection,
event: Some(Event::AddressChange { id, new_address })
};
}
Poll::Ready(Err(error)) => {
// Don't accept any further commands.
this.commands.get_mut().close();
// Terminate the task with the error, dropping the connection.
let event = Event::Closed { id, error: Some(error) };
this.state = State::Terminating(event);
}
}
}
}
State::Closing(mut closing) => {
// Try to gracefully close the connection.
match closing.poll_unpin(cx) {
Poll::Ready(Ok(())) => {
let event = Event::Closed { id: this.id, error: None };
this.state = State::Terminating(event);
}
Poll::Ready(Err(e)) => {
let event = Event::Closed {
id: this.id,
error: Some(ConnectionError::IO(e))
};
this.state = State::Terminating(event);
}
Poll::Pending => {
this.state = State::Closing(closing);
return Poll::Pending
}
}
}
State::Terminating(event) => {
// Try to deliver the final event.
match this.events.poll_ready(cx) {
Poll::Pending => {
self.state = State::Terminating(event);
return Poll::Pending
}
Poll::Ready(result) => {
if result.is_ok() {
let _ = this.events.start_send(event);
}
return Poll::Ready(())
}
}
}
State::Done => panic!("`Task::poll()` called after completion.")
}
}
}
}