This commit is contained in:
parent
69be9f7aca
commit
089def45d1
37 changed files with 2792 additions and 225 deletions
|
|
@ -23,9 +23,13 @@ getrandom-v02 = { package = "getrandom", version = "0.2", features = ["js"] }
|
|||
|
||||
mtp-common = { version = "0.1.0", path = "../common" }
|
||||
mtp-type-map = { version = "0.1.0", path = "../type-map" }
|
||||
mtp-codec = { version = "0.1.0", path = "../codec", features = ["crypto"] }
|
||||
mtp-codec = { version = "0.1.0", path = "../codec", features = ["crypto", "pipes"] }
|
||||
mtp-crypto = { version = "0.1.0", path = "../crypto", features = ["wasm"] }
|
||||
|
||||
[dev-dependencies]
|
||||
wasm-bindgen-test = "0.3"
|
||||
hex = "0.4"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
pipes = []
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ use mtp_crypto::SignatureScheme;
|
|||
|
||||
use crate::config::ConnectionConfig;
|
||||
use crate::error::js_error;
|
||||
use crate::pipe::PipeReader;
|
||||
use crate::transport::WasmTransport;
|
||||
|
||||
struct PendingRequest {
|
||||
|
|
@ -25,6 +26,57 @@ struct PingTimer {
|
|||
closure: Closure<dyn FnMut()>,
|
||||
}
|
||||
|
||||
#[wasm_bindgen(typescript_custom_section)]
|
||||
const PIPE_HANDLE_TS: &str = r#"
|
||||
export interface WasmPipeHandle {
|
||||
wait(): Promise<PipeWriter | null>;
|
||||
readonly pipeId: number;
|
||||
readonly description: string;
|
||||
}
|
||||
"#;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct WasmPipeHandle {
|
||||
pipe_id: u32,
|
||||
description: String,
|
||||
transport: WasmTransport,
|
||||
response_rx: Rc<RefCell<Option<oneshot::Receiver<Result<bool, JsValue>>>>>,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl WasmPipeHandle {
|
||||
pub async fn wait(&self) -> Result<JsValue, JsValue> {
|
||||
let rx = self
|
||||
.response_rx
|
||||
.borrow_mut()
|
||||
.take()
|
||||
.ok_or_else(|| js_error("handle already consumed"))?;
|
||||
|
||||
let accepted = rx
|
||||
.await
|
||||
.map_err(|_| js_error("pipe handle channel closed"))?;
|
||||
|
||||
match accepted {
|
||||
Ok(true) => {
|
||||
let writer = self.transport.open_pipe(self.pipe_id, &self.description).await?;
|
||||
Ok(JsValue::from(writer))
|
||||
}
|
||||
Ok(false) => Ok(JsValue::NULL),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen(getter)]
|
||||
pub fn pipe_id(&self) -> u32 {
|
||||
self.pipe_id
|
||||
}
|
||||
|
||||
#[wasm_bindgen(getter)]
|
||||
pub fn description(&self) -> String {
|
||||
self.description.clone()
|
||||
}
|
||||
}
|
||||
|
||||
fn frame_property(frame: &JsValue, key: &str) -> Option<JsValue> {
|
||||
js_sys::Reflect::get(frame, &JsValue::from_str(key))
|
||||
.ok()
|
||||
|
|
@ -110,6 +162,22 @@ fn reject_pending_requests(
|
|||
}
|
||||
}
|
||||
|
||||
fn reject_pending_pipe_creations(
|
||||
pending: &Rc<RefCell<HashMap<u32, oneshot::Sender<Result<bool, JsValue>>>>>,
|
||||
message: &str,
|
||||
) {
|
||||
let pending = std::mem::take(&mut *pending.borrow_mut());
|
||||
for (_, tx) in pending {
|
||||
let _ = tx.send(Err(js_error(message)));
|
||||
}
|
||||
}
|
||||
|
||||
fn random_pipe_id() -> Result<u32, JsValue> {
|
||||
let mut bytes = [0u8; 4];
|
||||
getrandom::fill(&mut bytes).map_err(|_| js_error("rng failed"))?;
|
||||
Ok(u32::from_be_bytes(bytes))
|
||||
}
|
||||
|
||||
fn raw_frame_preview(bytes: &[u8]) -> String {
|
||||
let shown = bytes.len().min(256);
|
||||
let mut preview = hex::encode(&bytes[..shown]);
|
||||
|
|
@ -261,6 +329,9 @@ pub struct WasmClient {
|
|||
next_subscription_id: Rc<Cell<u32>>,
|
||||
pending_requests: Rc<RefCell<HashMap<u32, PendingRequest>>>,
|
||||
ping_timer: Rc<RefCell<Option<PingTimer>>>,
|
||||
pending_pipe_creations: Rc<RefCell<HashMap<u32, oneshot::Sender<Result<bool, JsValue>>>>>,
|
||||
pending_pipes: Rc<RefCell<HashMap<u32, oneshot::Sender<PipeReader>>>>,
|
||||
on_pipe_request: Rc<RefCell<Option<js_sys::Function>>>,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
|
|
@ -282,6 +353,9 @@ impl WasmClient {
|
|||
next_subscription_id: Rc::new(Cell::new(1)),
|
||||
pending_requests: Rc::new(RefCell::new(HashMap::new())),
|
||||
ping_timer: Rc::new(RefCell::new(None)),
|
||||
pending_pipe_creations: Rc::new(RefCell::new(HashMap::new())),
|
||||
pending_pipes: Rc::new(RefCell::new(HashMap::new())),
|
||||
on_pipe_request: Rc::new(RefCell::new(None)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -686,9 +760,96 @@ impl WasmClient {
|
|||
}
|
||||
self.subscriptions.borrow_mut().clear();
|
||||
self.reject_pending_requests("disconnected");
|
||||
reject_pending_pipe_creations(&self.pending_pipe_creations, "disconnected");
|
||||
self.set_state(ConnectionState::Disconnected);
|
||||
}
|
||||
|
||||
/// Set the callback invoked when a remote peer opens a pipe request.
|
||||
/// The callback receives a plain JS object `{ pipeId: number, description: string }`.
|
||||
#[wasm_bindgen]
|
||||
pub fn set_on_pipe_request(&self, callback: Option<js_sys::Function>) {
|
||||
*self.on_pipe_request.borrow_mut() = callback;
|
||||
}
|
||||
|
||||
/// Initiate an outgoing pipe. Returns a `WasmPipeHandle` whose `wait()`
|
||||
/// method resolves after the remote peer accepts (or denies) the request.
|
||||
#[wasm_bindgen]
|
||||
pub async fn create_pipe(&self, description: &str) -> Result<WasmPipeHandle, JsValue> {
|
||||
let transport = self
|
||||
.transport
|
||||
.borrow()
|
||||
.clone()
|
||||
.ok_or_else(|| js_error("not connected"))?;
|
||||
|
||||
let pipe_id = random_pipe_id()?;
|
||||
let (tx, rx) = oneshot::channel();
|
||||
self.pending_pipe_creations
|
||||
.borrow_mut()
|
||||
.insert(pipe_id, tx);
|
||||
|
||||
let request =
|
||||
CommunicationValue::new(CommunicationType::PipeRequest).with_id(pipe_id).add_typed_default(
|
||||
DataType::Description,
|
||||
DataValue::Str(description.to_string()),
|
||||
);
|
||||
let request_bytes = request
|
||||
.to_bytes()
|
||||
.map_err(|e| js_error(&format!("encode failed: {}", e)))?;
|
||||
transport.send_frame(&request_bytes).await?;
|
||||
|
||||
Ok(WasmPipeHandle {
|
||||
pipe_id,
|
||||
description: description.to_string(),
|
||||
transport,
|
||||
response_rx: Rc::new(RefCell::new(Some(rx))),
|
||||
})
|
||||
}
|
||||
|
||||
/// Accept an incoming pipe request (identified by `pipe_id`). Sends a
|
||||
/// `PipeResponse` with `Accepted = true` and returns a `PipeReader` once
|
||||
/// the remote peer opens the pipe stream.
|
||||
#[wasm_bindgen]
|
||||
pub async fn accept_pipe(&self, pipe_id: u32) -> Result<PipeReader, JsValue> {
|
||||
let transport = self
|
||||
.transport
|
||||
.borrow()
|
||||
.clone()
|
||||
.ok_or_else(|| js_error("not connected"))?;
|
||||
|
||||
let resp = CommunicationValue::new(CommunicationType::PipeResponse)
|
||||
.with_id(pipe_id)
|
||||
.add_typed_default(DataType::Accepted, DataValue::BoolTrue);
|
||||
let resp_bytes = resp
|
||||
.to_bytes()
|
||||
.map_err(|e| js_error(&format!("encode failed: {}", e)))?;
|
||||
transport.send_frame(&resp_bytes).await?;
|
||||
|
||||
let (tx, rx) = oneshot::channel();
|
||||
self.pending_pipes.borrow_mut().insert(pipe_id, tx);
|
||||
|
||||
rx.await
|
||||
.map_err(|_| js_error("pipe closed before stream arrived"))
|
||||
}
|
||||
|
||||
/// Deny an incoming pipe request. Sends a `PipeResponse` with
|
||||
/// `Accepted = false`.
|
||||
#[wasm_bindgen]
|
||||
pub async fn deny_pipe(&self, pipe_id: u32) -> Result<(), JsValue> {
|
||||
let transport = self
|
||||
.transport
|
||||
.borrow()
|
||||
.clone()
|
||||
.ok_or_else(|| js_error("not connected"))?;
|
||||
|
||||
let resp = CommunicationValue::new(CommunicationType::PipeResponse)
|
||||
.with_id(pipe_id)
|
||||
.add_typed_default(DataType::Accepted, DataValue::BoolFalse);
|
||||
let resp_bytes = resp
|
||||
.to_bytes()
|
||||
.map_err(|e| js_error(&format!("encode failed: {}", e)))?;
|
||||
transport.send_frame(&resp_bytes).await
|
||||
}
|
||||
|
||||
fn set_state(&self, new_state: ConnectionState) {
|
||||
self.state.set(new_state);
|
||||
|
||||
|
|
@ -738,10 +899,68 @@ impl WasmClient {
|
|||
let pending_requests = self.pending_requests.clone();
|
||||
let loop_pending_requests = pending_requests.clone();
|
||||
let ping_timer = self.ping_timer.clone();
|
||||
let pending_pipe_creations = self.pending_pipe_creations.clone();
|
||||
let pending_pipes = self.pending_pipes.clone();
|
||||
let on_pipe_request = self.on_pipe_request.clone();
|
||||
let loop_pipe_creations = pending_pipe_creations.clone();
|
||||
wasm_bindgen_futures::spawn_local(async move {
|
||||
loop_transport
|
||||
.receive_loop(
|
||||
.receive_loop_with_pipes(
|
||||
move |frame: JsValue| {
|
||||
let message_type = frame_type(&frame);
|
||||
if let Some(ref msg_type) = message_type {
|
||||
if msg_type == "PipeRequest" {
|
||||
let pipe_id = frame_id(&frame).unwrap_or(0);
|
||||
let description = frame_property(&frame, "data")
|
||||
.and_then(|data| {
|
||||
let desc = js_sys::Reflect::get(
|
||||
&data,
|
||||
&JsValue::from_str("Description"),
|
||||
)
|
||||
.ok()?;
|
||||
desc.as_string()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
let cb = on_pipe_request.borrow();
|
||||
if let Some(ref callback) = *cb {
|
||||
let obj = js_sys::Object::new();
|
||||
let _ = js_sys::Reflect::set(
|
||||
&obj,
|
||||
&"pipeId".into(),
|
||||
&JsValue::from_f64(pipe_id as f64),
|
||||
);
|
||||
let _ = js_sys::Reflect::set(
|
||||
&obj,
|
||||
&"description".into(),
|
||||
&JsValue::from_str(&description),
|
||||
);
|
||||
let _ = callback.call1(&JsValue::NULL, &obj.into());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if msg_type == "PipeResponse" {
|
||||
let pipe_id = frame_id(&frame).unwrap_or(0);
|
||||
let accepted = frame_property(&frame, "data")
|
||||
.and_then(|data| {
|
||||
let acc = js_sys::Reflect::get(
|
||||
&data,
|
||||
&JsValue::from_str("Accepted"),
|
||||
)
|
||||
.ok()?;
|
||||
acc.as_bool()
|
||||
})
|
||||
.unwrap_or(false);
|
||||
|
||||
let mut pending = loop_pipe_creations.borrow_mut();
|
||||
if let Some(tx) = pending.remove(&pipe_id) {
|
||||
let _ = tx.send(Ok(accepted));
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
route_incoming_frame(
|
||||
&frame,
|
||||
&on_msg,
|
||||
|
|
@ -750,11 +969,19 @@ impl WasmClient {
|
|||
);
|
||||
},
|
||||
on_err.clone(),
|
||||
move |pipe_reader: PipeReader| {
|
||||
let pipe_id = pipe_reader.pipe_id();
|
||||
let mut pending = pending_pipes.borrow_mut();
|
||||
if let Some(tx) = pending.remove(&pipe_id) {
|
||||
let _ = tx.send(pipe_reader);
|
||||
}
|
||||
},
|
||||
)
|
||||
.await;
|
||||
state.set(ConnectionState::Disconnected);
|
||||
stop_ping_timer(&ping_timer);
|
||||
reject_pending_requests(&pending_requests, "disconnected");
|
||||
reject_pending_pipe_creations(&pending_pipe_creations, "disconnected");
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ pub mod crypto;
|
|||
pub mod error;
|
||||
pub mod frame;
|
||||
pub mod logging;
|
||||
pub mod pipe;
|
||||
pub mod subscription;
|
||||
pub mod transport;
|
||||
|
||||
|
|
|
|||
140
wasm/src/pipe.rs
Normal file
140
wasm/src/pipe.rs
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::JsCast;
|
||||
use wasm_bindgen_futures::JsFuture;
|
||||
|
||||
use crate::error::js_error;
|
||||
use crate::transport::release_writer_lock;
|
||||
|
||||
#[wasm_bindgen(typescript_custom_section)]
|
||||
const PIPE_TS: &str = r#"
|
||||
export interface PipeWriter {
|
||||
write(data: Uint8Array): Promise<void>;
|
||||
close(): Promise<void>;
|
||||
abort(): void;
|
||||
readonly pipeId: number;
|
||||
}
|
||||
|
||||
export interface PipeReader {
|
||||
read(): Promise<Uint8Array | null>;
|
||||
readonly pipeId: number;
|
||||
readonly description: string;
|
||||
}
|
||||
"#;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct PipeWriter {
|
||||
writer: JsValue,
|
||||
pipe_id: u32,
|
||||
}
|
||||
|
||||
impl PipeWriter {
|
||||
pub fn new(writer: JsValue, pipe_id: u32) -> Self {
|
||||
Self { writer, pipe_id }
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl PipeWriter {
|
||||
pub async fn write(&mut self, data: &[u8]) -> Result<(), JsValue> {
|
||||
let chunk = js_sys::Uint8Array::from(data);
|
||||
let write_fn = js_sys::Reflect::get(&self.writer, &JsValue::from_str("write"))
|
||||
.map_err(|_| js_error("missing write"))?
|
||||
.dyn_into::<js_sys::Function>()
|
||||
.map_err(|_| js_error("write not a function"))?;
|
||||
let write_promise = write_fn
|
||||
.call1(&self.writer, &chunk)
|
||||
.map_err(|e| js_error(&format!("write failed: {:?}", e)))?;
|
||||
JsFuture::from(write_promise.unchecked_into::<js_sys::Promise>()).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn close(self) -> Result<(), JsValue> {
|
||||
let close_fn = js_sys::Reflect::get(&self.writer, &JsValue::from_str("close"))
|
||||
.map_err(|_| js_error("missing close"))?
|
||||
.dyn_into::<js_sys::Function>()
|
||||
.map_err(|_| js_error("close not a function"))?;
|
||||
let close_promise = close_fn
|
||||
.call0(&self.writer)
|
||||
.map_err(|e| js_error(&format!("close failed: {:?}", e)))?;
|
||||
if let Err(e) =
|
||||
JsFuture::from(close_promise.unchecked_into::<js_sys::Promise>()).await
|
||||
{
|
||||
crate::transport::log_stream_error_code(&e, "pipe writer close");
|
||||
}
|
||||
release_writer_lock(&self.writer);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn abort(&mut self) -> Result<(), JsValue> {
|
||||
let abort_fn = js_sys::Reflect::get(&self.writer, &JsValue::from_str("abort"))
|
||||
.map_err(|_| js_error("missing abort"))?
|
||||
.dyn_into::<js_sys::Function>()
|
||||
.map_err(|_| js_error("abort not a function"))?;
|
||||
let _ = abort_fn.call0(&self.writer);
|
||||
release_writer_lock(&self.writer);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn pipe_id(&self) -> u32 {
|
||||
self.pipe_id
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct PipeReader {
|
||||
reader: JsValue,
|
||||
description: String,
|
||||
pipe_id: u32,
|
||||
pending: Vec<u8>,
|
||||
}
|
||||
|
||||
impl PipeReader {
|
||||
pub fn new(reader: JsValue, pipe_id: u32, description: String, pending: Vec<u8>) -> Self {
|
||||
Self {
|
||||
reader,
|
||||
pipe_id,
|
||||
description,
|
||||
pending,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl PipeReader {
|
||||
pub async fn read(&mut self) -> Result<JsValue, JsValue> {
|
||||
if !self.pending.is_empty() {
|
||||
let data = std::mem::take(&mut self.pending);
|
||||
return Ok(js_sys::Uint8Array::from(&data[..]).into());
|
||||
}
|
||||
|
||||
let read_fn = js_sys::Reflect::get(&self.reader, &JsValue::from_str("read"))
|
||||
.map_err(|_| js_error("missing read"))?
|
||||
.dyn_into::<js_sys::Function>()
|
||||
.map_err(|_| js_error("read not a function"))?;
|
||||
let promise = read_fn
|
||||
.call0(&self.reader)
|
||||
.map_err(|_| js_error("read call failed"))?
|
||||
.unchecked_into::<js_sys::Promise>();
|
||||
let result = JsFuture::from(promise).await?;
|
||||
|
||||
let done = js_sys::Reflect::get(&result, &JsValue::from_str("done"))
|
||||
.ok()
|
||||
.and_then(|v| v.as_bool())
|
||||
.unwrap_or(true);
|
||||
if done {
|
||||
return Ok(JsValue::NULL);
|
||||
}
|
||||
|
||||
let value = js_sys::Reflect::get(&result, &JsValue::from_str("value"))
|
||||
.map_err(|_| js_error("missing value"))?;
|
||||
Ok(js_sys::Uint8Array::new(&value).into())
|
||||
}
|
||||
|
||||
pub fn pipe_id(&self) -> u32 {
|
||||
self.pipe_id
|
||||
}
|
||||
|
||||
pub fn description(&self) -> String {
|
||||
self.description.clone()
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
use std::cell::RefCell;
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::rc::Rc;
|
||||
|
||||
use wasm_bindgen::JsCast;
|
||||
|
|
@ -11,7 +11,7 @@ use crate::frame::parse_frame_value;
|
|||
const CLOSE_FRAME_LEN: u32 = u32::MAX;
|
||||
|
||||
/// Logs the `streamErrorCode` from a stream-level WebTransportError (STOP_SENDING / RESET_STREAM). Session errors are skipped.
|
||||
fn log_stream_error_code(error: &JsValue, context: &str) {
|
||||
pub(crate) fn log_stream_error_code(error: &JsValue, context: &str) {
|
||||
let source = js_sys::Reflect::get(error, &JsValue::from_str("source"))
|
||||
.ok()
|
||||
.and_then(|v| v.as_string());
|
||||
|
|
@ -70,7 +70,7 @@ fn resolve_stream_readable(recv_stream: &JsValue) -> Result<JsValue, JsValue> {
|
|||
}
|
||||
|
||||
/// Releases a writer's lock so an abandoned writer isn't treated as an abort (which sends STOP_SENDING).
|
||||
fn release_writer_lock(writer: &JsValue) {
|
||||
pub(crate) fn release_writer_lock(writer: &JsValue) {
|
||||
if let Ok(release) = js_sys::Reflect::get(writer, &JsValue::from_str("releaseLock"))
|
||||
.and_then(|f| f.dyn_into::<js_sys::Function>().map_err(Into::into))
|
||||
{
|
||||
|
|
@ -79,7 +79,7 @@ fn release_writer_lock(writer: &JsValue) {
|
|||
}
|
||||
|
||||
/// Releases a reader's lock so an abandoned reader isn't treated as a cancel (which sends STOP_SENDING).
|
||||
fn release_reader_lock(reader: &JsValue) {
|
||||
pub(crate) fn release_reader_lock(reader: &JsValue) {
|
||||
if let Ok(release) = js_sys::Reflect::get(reader, &JsValue::from_str("releaseLock"))
|
||||
.and_then(|f| f.dyn_into::<js_sys::Function>().map_err(Into::into))
|
||||
{
|
||||
|
|
@ -119,6 +119,8 @@ pub struct WasmTransport {
|
|||
stream_reader: Rc<RefCell<Option<JsValue>>>,
|
||||
/// Bytes already read from the current stream but not yet consumed as a frame.
|
||||
buffer: Rc<RefCell<Vec<u8>>>,
|
||||
/// Set to `true` when `open_next_stream` succeeds; cleared after the first frame is parsed.
|
||||
new_stream_frame: Rc<Cell<bool>>,
|
||||
}
|
||||
|
||||
impl WasmTransport {
|
||||
|
|
@ -177,6 +179,7 @@ impl WasmTransport {
|
|||
streams_reader: Rc::new(RefCell::new(None)),
|
||||
stream_reader: Rc::new(RefCell::new(None)),
|
||||
buffer: Rc::new(RefCell::new(Vec::new())),
|
||||
new_stream_frame: Rc::new(Cell::new(false)),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -309,6 +312,7 @@ impl WasmTransport {
|
|||
.map_err(|_| js_error("stream getReader call failed"))?;
|
||||
|
||||
*self.stream_reader.borrow_mut() = Some(reader);
|
||||
self.new_stream_frame.set(true);
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
|
|
@ -451,6 +455,136 @@ impl WasmTransport {
|
|||
}
|
||||
}
|
||||
|
||||
/// Pipe-aware receive loop. Identical to `receive_loop` but detects
|
||||
/// `PipeRequest` as the first frame on a new incoming stream and routes
|
||||
/// the stream to `on_pipe` instead of `on_message`.
|
||||
pub async fn receive_loop_with_pipes<F, G>(
|
||||
&self,
|
||||
mut on_message: F,
|
||||
on_error: js_sys::Function,
|
||||
mut on_pipe: G,
|
||||
) where
|
||||
F: FnMut(JsValue),
|
||||
G: FnMut(crate::pipe::PipeReader),
|
||||
{
|
||||
let pipe_request_type = mtp_codec::CommunicationType::PipeRequest
|
||||
.to_id(&mtp_codec::TypeMap::latest());
|
||||
|
||||
loop {
|
||||
match self.next_frame().await {
|
||||
Ok(FrameOutcome::Frame(frame)) => {
|
||||
let is_first = self.new_stream_frame.get();
|
||||
if is_first {
|
||||
self.new_stream_frame.set(false);
|
||||
if let Ok(comm) = mtp_codec::CommunicationValue::from_bytes(&frame)
|
||||
&& comm.get_type() == pipe_request_type
|
||||
{
|
||||
let pipe_id = comm.get_id();
|
||||
let description = comm
|
||||
.get_str(mtp_codec::DataType::Description)
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
|
||||
let pending = {
|
||||
let mut buf = self.buffer.borrow_mut();
|
||||
std::mem::take(&mut *buf)
|
||||
};
|
||||
|
||||
if let Some(reader) = self.stream_reader.borrow_mut().take() {
|
||||
let pipe_reader = crate::pipe::PipeReader::new(
|
||||
reader,
|
||||
pipe_id,
|
||||
description,
|
||||
pending,
|
||||
);
|
||||
on_pipe(pipe_reader);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
match parse_frame_value(&frame) {
|
||||
Ok(parsed) => {
|
||||
on_message(parsed);
|
||||
}
|
||||
Err(e) => {
|
||||
let message = e.as_string().unwrap_or_else(|| format!("{:?}", e));
|
||||
let _ =
|
||||
on_error.call1(&JsValue::NULL, &JsValue::from_str(&message));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(FrameOutcome::Closed) | Ok(FrameOutcome::Ended) => break,
|
||||
Err(e) => {
|
||||
let _ = on_error.call1(&JsValue::NULL, &e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Open a new outgoing unidirectional stream and write a `PipeRequest`
|
||||
/// frame as the first frame. Returns a `PipeWriter` whose underlying
|
||||
/// `WritableStream` remains open for subsequent raw-data writes.
|
||||
pub async fn open_pipe(
|
||||
&self,
|
||||
pipe_id: u32,
|
||||
description: &str,
|
||||
) -> Result<crate::pipe::PipeWriter, JsValue> {
|
||||
let create_stream = js_sys::Reflect::get(
|
||||
&self.inner,
|
||||
&JsValue::from_str("createUnidirectionalStream"),
|
||||
)?
|
||||
.dyn_into::<js_sys::Function>()
|
||||
.map_err(|_| js_error("createUnidirectionalStream not a function"))?;
|
||||
let stream_promise = create_stream
|
||||
.call0(&self.inner)?
|
||||
.dyn_into::<js_sys::Promise>()
|
||||
.map_err(|_| js_error("createUnidirectionalStream did not return a Promise"))?;
|
||||
let stream = JsFuture::from(stream_promise).await?;
|
||||
|
||||
let writable_or_stream = resolve_stream_writable(&stream)?;
|
||||
let writer_val = js_sys::Reflect::get(&writable_or_stream, &JsValue::from_str("getWriter"))
|
||||
.map_err(|_| js_error("missing getWriter"))?
|
||||
.dyn_into::<js_sys::Function>()
|
||||
.map_err(|_| js_error("getWriter not a function"))?
|
||||
.call0(&writable_or_stream)
|
||||
.map_err(|_| js_error("getWriter call failed"))?;
|
||||
|
||||
let request = mtp_codec::CommunicationValue::new(mtp_codec::CommunicationType::PipeRequest)
|
||||
.with_id(pipe_id)
|
||||
.add_typed_default(
|
||||
mtp_codec::DataType::Description,
|
||||
mtp_codec::DataValue::Str(description.to_string()),
|
||||
);
|
||||
let frame_bytes = request
|
||||
.to_bytes()
|
||||
.map_err(|e| js_error(&format!("encode failed: {}", e)))?;
|
||||
|
||||
let len = frame_bytes.len() as u32;
|
||||
let mut wire = Vec::with_capacity(4 + frame_bytes.len());
|
||||
wire.extend_from_slice(&len.to_be_bytes());
|
||||
wire.extend_from_slice(&frame_bytes);
|
||||
|
||||
let chunk = js_sys::Uint8Array::from(&wire[..]);
|
||||
let write_fn = js_sys::Reflect::get(&writer_val, &JsValue::from_str("write"))
|
||||
.map_err(|_| js_error("missing write"))?
|
||||
.dyn_into::<js_sys::Function>()
|
||||
.map_err(|_| js_error("write not a function"))?;
|
||||
let write_promise = write_fn
|
||||
.call1(&writer_val, &chunk)
|
||||
.map_err(|e| js_error(&format!("write failed: {:?}", e)))?;
|
||||
if let Err(e) =
|
||||
JsFuture::from(write_promise.unchecked_into::<js_sys::Promise>()).await
|
||||
{
|
||||
log_stream_error_code(&e, "open_pipe write");
|
||||
release_writer_lock(&writer_val);
|
||||
return Err(e);
|
||||
}
|
||||
|
||||
Ok(crate::pipe::PipeWriter::new(writer_val, pipe_id))
|
||||
}
|
||||
|
||||
pub fn close(&self) {
|
||||
// Release reader locks before closing so they aren't treated as cancels.
|
||||
if let Some(reader) = self.stream_reader.borrow_mut().take() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue