Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[target.wasm32-unknown-unknown]
runner = "wasm-bindgen-test-runner"

[target.wasm32-wasip2]
runner = "wasmtime"
4 changes: 2 additions & 2 deletions matchbox_socket/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ bincode = { version = "2.0", default-features = false, features = [
], optional = true }
bytes = { version = "1.1", default-features = false }

[target.'cfg(target_arch = "wasm32")'.dependencies]
[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies]
ggrs = { version = "0.11", default-features = false, optional = true, features = [
"wasm-bindgen",
] }
Expand Down Expand Up @@ -78,7 +78,7 @@ web-sys = { version = "0.3.22", default-features = false, features = [
] }
serde-wasm-bindgen = "0.6"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
[target.'cfg(any(not(target_family = "wasm"), all(target_os = "wasi", target_env = "p2")))'.dependencies]
async-tungstenite = { version = "0.29", default-features = false, features = [
"async-std-runtime",
"async-tls",
Expand Down
9 changes: 6 additions & 3 deletions matchbox_socket/src/webrtc_socket/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ pub enum SignalingError {
NegotiationFailed(#[from] Box<SignalingError>),

/// Native
#[cfg(not(target_arch = "wasm32"))]
#[cfg(any(
not(target_family = "wasm"),
all(target_os = "wasi", target_env = "p2")
))]
#[error("socket failure communicating with signaling server: {0}")]
WebSocket(#[from] async_tungstenite::tungstenite::Error),

/// WASM
#[cfg(target_arch = "wasm32")]
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
#[error("socket failure communicating with signaling server: {0}")]
WebSocket(#[from] ws_stream_wasm::WsErr),

Expand All @@ -55,7 +58,7 @@ pub enum SignalingError {
}

cfg_if! {
if #[cfg(target_arch = "wasm32")] {
if #[cfg(all(target_family = "wasm", target_os = "unknown"))] {
use wasm_bindgen::{JsValue};
use derive_more::Display;

Expand Down
35 changes: 19 additions & 16 deletions matchbox_socket/src/webrtc_socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ pub use socket::{
use std::{collections::HashMap, pin::Pin, sync::Arc, time::Duration};

cfg_if! {
if #[cfg(target_arch = "wasm32")] {
mod wasm;
type UseMessenger = wasm::WasmMessenger;
type UseSignallerBuilder = wasm::WasmSignallerBuilder;
/// A future which runs the message loop for the socket and completes
/// when the socket closes or disconnects
pub type MessageLoopFuture = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
} else {
if #[cfg(any(not(target_family = "wasm"), all(target_os = "wasi", target_env = "p2")))] {
mod native;
type UseMessenger = native::NativeMessenger;
type UseSignallerBuilder = native::NativeSignallerBuilder;
/// A future which runs the message loop for the socket and completes
/// when the socket closes or disconnects
pub type MessageLoopFuture = Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>;
} else {
mod wasm;
type UseMessenger = wasm::WasmMessenger;
type UseSignallerBuilder = wasm::WasmSignallerBuilder;
/// A future which runs the message loop for the socket and completes
/// when the socket closes or disconnects
pub type MessageLoopFuture = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
}
}

/// A builder that constructs a new [Signaller] from a room URL.
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait)]
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
pub trait SignallerBuilder: std::fmt::Debug + Sync + Send + 'static {
/// Create a new [Signaller]. The Room URL is an implementation specific identifier for joining
/// a room.
Expand Down Expand Up @@ -72,8 +72,8 @@ pub trait SignallerBuilder: std::fmt::Debug + Sync + Send + 'static {
/// 3. It passes [PeerEvent::Signal] events back and forth between them, until one of them
/// disconnects.
/// 4. It sends [PeerEvent::PeerLeft] with the ID of the disconnected peer to the remaining peer.
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait)]
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
pub trait Signaller: Sync + Send + 'static {
/// Request the signaller to pass a message to another peer.
async fn send(&mut self, request: PeerRequest) -> Result<(), SignalingError>;
Expand Down Expand Up @@ -125,9 +125,12 @@ pub type Packet = Box<[u8]>;
#[derive(Debug, thiserror::Error)]
#[error("The socket was dropped and package could not be sent")]
struct PacketSendError {
#[cfg(not(target_arch = "wasm32"))]
#[cfg(any(
not(target_family = "wasm"),
all(target_os = "wasi", target_env = "p2")
))]
source: futures_channel::mpsc::SendError,
#[cfg(target_arch = "wasm32")]
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
source: error::JsError,
}

Expand All @@ -141,8 +144,8 @@ struct HandshakeResult<D: PeerDataSender, M> {
metadata: M,
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait)]
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
trait Messenger {
type DataChannel: PeerDataSender;
type HandshakeMeta: Send;
Expand Down