WebAssembly / WASI

WebAssembly System Interface
Other
4.84k stars 251 forks source link

Any chance to see WASI allowing UDP socket like to be created? #601

Closed ROBERT-MCDOWELL closed 4 months ago

ROBERT-MCDOWELL commented 4 months ago

Come back from Flash player, allowing client to accept P2P Multicast streaming after an alert confirmation I wonder if with WASI it will be possible to see one day this kind of process so developers would be able to integrate UDP datagram and avoid TCP congestion and more packet loss tolerant. thanks for your answer.

sbc100 commented 4 months ago

https://github.com/WebAssembly/wasi-sockets has both TCP and UDP doesn't it?

In fact, I don't think anyone has ever proposed that we do TCP but not UDP.

ricochet commented 4 months ago

Yes, thank you @sbc100. With wasi-sockets (now phase 3 with two implementations and available in WASI 0.2), users can use the udp interface for this use-case.

From the sockets test in wasmtime: crates/test-programs/src/sockets.rs

use crate::wasi::sockets::udp::{
    IncomingDatagram, IncomingDatagramStream, OutgoingDatagram, OutgoingDatagramStream, UdpSocket,
};

impl IncomingDatagramStream {
    pub fn blocking_receive(&self, count: Range<u64>) -> Result<Vec<IncomingDatagram>, ErrorCode> {
        let timeout = monotonic_clock::subscribe_duration(TIMEOUT_NS);
        let pollable = self.subscribe();
        let mut datagrams = vec![];

        loop {
            match self.receive(count.end - datagrams.len() as u64) {
                Ok(mut chunk) => {
                    datagrams.append(&mut chunk);

                    if datagrams.len() >= count.start as usize {
                        return Ok(datagrams);
                    } else {
                        pollable.block_until(&timeout)?;
                    }
                }
                Err(err) => return Err(err),
            }
        }
    }
}