-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlib.rs
More file actions
71 lines (56 loc) · 1.96 KB
/
lib.rs
File metadata and controls
71 lines (56 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#![no_std]
extern crate alloc;
use core::{net::Ipv4Addr, time::Duration};
use awkernel_async_lib::net::{udp::UdpConfig, IpAddr};
const INTERFACE_ADDR: Ipv4Addr = Ipv4Addr::new(192, 168, 0, 3);
const INTERFACE_ID: u64 = 1;
const BASE_PORT: u16 = 20000;
pub async fn run() {
const NUM_TASKS: [usize; 11] = [1000, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000];
awkernel_lib::net::add_ipv4_addr(INTERFACE_ID, INTERFACE_ADDR, 24);
for num_task in NUM_TASKS {
let mut join = alloc::vec::Vec::new();
for task_id in 0..num_task {
let port = BASE_PORT + task_id as u16;
let hdl = awkernel_async_lib::spawn(
"test udp".into(),
udp_server(port),
awkernel_async_lib::scheduler::SchedulerType::RR,
)
.await;
join.push(hdl);
}
for hdl in join {
let _ = hdl.join().await;
}
}
}
async fn udp_server(port: u16) {
let config = UdpConfig {
addr: IpAddr::new_v4(INTERFACE_ADDR),
port: Some(port),
..Default::default()
};
let mut socket =
awkernel_async_lib::net::udp::UdpSocket::bind_on_interface(INTERFACE_ID, &config).unwrap();
const MAX_DATAGRAM_SIZE: usize = 65_507;
let mut buf = [0u8; MAX_DATAGRAM_SIZE];
loop {
match socket.recv(&mut buf).await {
Ok((read_bytes, client_addr, port)) => {
if read_bytes == 1 {
break;
}
let received_data = &buf[..read_bytes];
if let Err(e) = socket.send(received_data, &client_addr, port).await {
log::error!("Failed to send a UDP packet: {:?}", e);
awkernel_async_lib::sleep(Duration::from_secs(1)).await;
continue;
}
}
Err(e) => {
log::error!("Error receiving data: {:?}", e);
}
}
}
}