Skip to content

Commit

Permalink
Lock writable stream before getting writer (#17)
Browse files Browse the repository at this point in the history
* Lock writable stream before getting writer

* Make sure stream is ready to write

* cargo fmt
  • Loading branch information
griffobeid authored Jul 13, 2024
1 parent 269ab19 commit 26007ad
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/webtransport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,18 +326,24 @@ impl WebTransportTask {
let result: Result<(), anyhow::Error> = async move {
let stream = transport.datagrams();
let stream: WritableStream = stream.writable();
if stream.locked() {
return Err(anyhow::anyhow!("Stream is locked"));
}
let writer = stream.get_writer().map_err(|e| anyhow!("{:?}", e))?;
let data = Uint8Array::from(data.as_slice());
let _stream = JsFuture::from(writer.write_with_chunk(&data))
JsFuture::from(writer.ready())
.await
.map_err(|e| anyhow::anyhow!("{:?}", e))?;
.map_err(|e| anyhow!("{:?}", e))?;
JsFuture::from(writer.write_with_chunk(&data))
.await
.map_err(|e| anyhow!("{:?}", e))?;
writer.release_lock();
Ok(())
}
.await;
if let Err(e) = result {
let e = e.to_string();
log!("error: {}", e);
log!("error: ", e);
transport_2.close();
}
});
Expand All @@ -353,6 +359,9 @@ impl WebTransportTask {
stream.map_err(|e| anyhow!("{:?}", e))?.unchecked_into();
let writer = stream.get_writer().map_err(|e| anyhow!("{:?}", e))?;
let data = Uint8Array::from(data.as_slice());
JsFuture::from(writer.ready())
.await
.map_err(|e| anyhow!("{:?}", e))?;
let _ = JsFuture::from(writer.write_with_chunk(&data))
.await
.map_err(|e| anyhow::anyhow!("{:?}", e))?;
Expand Down Expand Up @@ -420,6 +429,9 @@ impl WebTransportTask {
.get_writer()
.map_err(|e| anyhow!("{:?}", e))?;

JsFuture::from(writer.ready())
.await
.map_err(|e| anyhow!("{:?}", e))?;
let data = Uint8Array::from(data.as_slice());
let _ = JsFuture::from(writer.write_with_chunk(&data))
.await
Expand Down

0 comments on commit 26007ad

Please sign in to comment.