*: Enable libp2p to run via wasm32-unknown-unknown in the browser (#2320)

Changes needed to get libp2p to run via `wasm32-unknown-unknown` in the browser
(both main thread and inside web workers).

Replaces wasm-timer with futures-timer and instant.

Co-authored-by: Oliver Wangler <oliver@wngr.de>
This commit is contained in:
Max Inden
2021-10-30 12:41:30 +02:00
committed by GitHub
parent b8f0e44b27
commit ff5d455ccf
50 changed files with 365 additions and 86 deletions

View File

@ -26,6 +26,8 @@ use crate::upgrade::SendWrapper;
use futures::prelude::*;
use futures::stream::FuturesUnordered;
use futures_timer::Delay;
use instant::Instant;
use libp2p_core::{
connection::{
ConnectionHandler, ConnectionHandlerEvent, IntoConnectionHandler, Substream,
@ -36,7 +38,6 @@ use libp2p_core::{
Connected, Multiaddr,
};
use std::{error, fmt, pin::Pin, task::Context, task::Poll, time::Duration};
use wasm_timer::{Delay, Instant};
/// Prototype for a `NodeHandlerWrapper`.
pub struct NodeHandlerWrapperBuilder<TIntoProtoHandler> {
@ -159,7 +160,7 @@ where
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
match self.timeout.poll_unpin(cx) {
Poll::Ready(Ok(_)) => {
Poll::Ready(()) => {
return Poll::Ready((
self.user_data
.take()
@ -167,14 +168,7 @@ where
Err(ProtocolsHandlerUpgrErr::Timeout),
))
}
Poll::Ready(Err(_)) => {
return Poll::Ready((
self.user_data
.take()
.expect("Future not to be polled again once ready."),
Err(ProtocolsHandlerUpgrErr::Timer),
))
}
Poll::Pending => {}
}
@ -362,10 +356,16 @@ where
(Shutdown::Later(timer, deadline), KeepAlive::Until(t)) => {
if *deadline != t {
*deadline = t;
timer.reset_at(t)
if let Some(dur) = deadline.checked_duration_since(Instant::now()) {
timer.reset(dur)
}
}
}
(_, KeepAlive::Until(t)) => {
if let Some(dur) = t.checked_duration_since(Instant::now()) {
self.shutdown = Shutdown::Later(Delay::new(dur), t)
}
}
(_, KeepAlive::Until(t)) => self.shutdown = Shutdown::Later(Delay::new_at(t), t),
(_, KeepAlive::No) => self.shutdown = Shutdown::Asap,
(_, KeepAlive::Yes) => self.shutdown = Shutdown::None,
};

View File

@ -22,10 +22,9 @@ use crate::protocols_handler::{
KeepAlive, ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr, SubstreamProtocol,
};
use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend};
use instant::Instant;
use smallvec::SmallVec;
use std::{error, fmt::Debug, task::Context, task::Poll, time::Duration};
use wasm_timer::Instant;
/// A `ProtocolsHandler` that opens a new substream for each request.
// TODO: Debug