forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(wasi-sockets): implement UDP (bytecodealliance#7148)
* feat(wasi-sockets): implement UDP This is based on TCP implementation Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net> * refactor(wasi-sockets): simplify UDP implementation This introduces quite a few changes compared to TCP, which should most probably be integrated there as well Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net> * feat(wasi-sockets): store UDP connect address in state Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net> * fix(wasi-sockets): avoid `shutdown` on `drop` Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net> * Remove explicit bind * Simplify start_connect&finish_connect. On UDP sockets, `connect` never blocks. * Move UDP test to single file, similar to `tcp_sample_application.rs` * Update UDP tests --------- Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net> Co-authored-by: Dave Bakker <github@davebakker.io>
- Loading branch information
1 parent
269a9a8
commit 6e9ba0b
Showing
12 changed files
with
694 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
crates/test-programs/wasi-sockets-tests/src/bin/udp_sample_application.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use wasi::sockets::network::{ | ||
IpAddressFamily, IpSocketAddress, Ipv4SocketAddress, Ipv6SocketAddress, Network, | ||
}; | ||
use wasi::sockets::udp::{Datagram, UdpSocket}; | ||
use wasi_sockets_tests::*; | ||
|
||
fn test_sample_application(family: IpAddressFamily, bind_address: IpSocketAddress) { | ||
let first_message = &[]; | ||
let second_message = b"Hello, world!"; | ||
let third_message = b"Greetings, planet!"; | ||
|
||
let net = Network::default(); | ||
|
||
let server = UdpSocket::new(family).unwrap(); | ||
|
||
server.blocking_bind(&net, bind_address).unwrap(); | ||
let addr = server.local_address().unwrap(); | ||
|
||
let client_addr = { | ||
let client = UdpSocket::new(family).unwrap(); | ||
client.blocking_connect(&net, addr).unwrap(); | ||
|
||
let datagrams = [ | ||
Datagram { | ||
data: first_message.to_vec(), | ||
remote_address: addr, | ||
}, | ||
Datagram { | ||
data: second_message.to_vec(), | ||
remote_address: addr, | ||
}, | ||
]; | ||
client.blocking_send(&datagrams).unwrap(); | ||
|
||
client.local_address().unwrap() | ||
}; | ||
|
||
{ | ||
// Check that we've received our sent messages. | ||
// Not guaranteed to work but should work in practice. | ||
let datagrams = server.blocking_receive(2..100).unwrap(); | ||
assert_eq!(datagrams.len(), 2); | ||
|
||
assert_eq!(datagrams[0].data, first_message); | ||
assert_eq!(datagrams[0].remote_address, client_addr); | ||
|
||
assert_eq!(datagrams[1].data, second_message); | ||
assert_eq!(datagrams[1].remote_address, client_addr); | ||
} | ||
|
||
// Another client | ||
{ | ||
let client = UdpSocket::new(family).unwrap(); | ||
client.blocking_connect(&net, addr).unwrap(); | ||
|
||
let datagrams = [Datagram { | ||
data: third_message.to_vec(), | ||
remote_address: addr, | ||
}]; | ||
client.blocking_send(&datagrams).unwrap(); | ||
} | ||
|
||
{ | ||
// Check that we sent and received our message! | ||
let datagrams = server.blocking_receive(1..100).unwrap(); | ||
assert_eq!(datagrams.len(), 1); | ||
|
||
assert_eq!(datagrams[0].data, third_message); // Not guaranteed to work but should work in practice. | ||
} | ||
} | ||
|
||
fn main() { | ||
test_sample_application( | ||
IpAddressFamily::Ipv4, | ||
IpSocketAddress::Ipv4(Ipv4SocketAddress { | ||
port: 0, // use any free port | ||
address: (127, 0, 0, 1), // localhost | ||
}), | ||
); | ||
test_sample_application( | ||
IpAddressFamily::Ipv6, | ||
IpSocketAddress::Ipv6(Ipv6SocketAddress { | ||
port: 0, // use any free port | ||
address: (0, 0, 0, 0, 0, 0, 0, 1), // localhost | ||
flow_info: 0, | ||
scope_id: 0, | ||
}), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,5 @@ mod network; | |
mod random; | ||
mod tcp; | ||
mod tcp_create_socket; | ||
mod udp; | ||
mod udp_create_socket; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.