MCPcopy Create free account
hub / github.com/bytecodealliance/rustix / server

Function server

tests/event/epoll.rs:15–66  ·  view source on GitHub ↗
(ready: Arc<(Mutex<u16>, Condvar)>)

Source from the content-addressed store, hash-verified

13const BUFFER_SIZE: usize = 20;
14
15fn server(ready: Arc<(Mutex<u16>, Condvar)>) -> ! {
16 let listen_sock = socket(AddressFamily::INET, SocketType::STREAM, None).unwrap();
17 bind(&listen_sock, &SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0)).unwrap();
18 listen(&listen_sock, 1).unwrap();
19
20 let who = SocketAddrV4::try_from(getsockname(&listen_sock).unwrap()).unwrap();
21
22 {
23 let (lock, cvar) = &*ready;
24 let mut port = lock.lock().unwrap();
25 *port = who.port();
26 cvar.notify_all();
27 }
28
29 let epoll = epoll::create(epoll::CreateFlags::CLOEXEC).unwrap();
30
31 epoll::add(
32 &epoll,
33 &listen_sock,
34 epoll::EventData::new_u64(1),
35 epoll::EventFlags::IN,
36 )
37 .unwrap();
38
39 let mut next_data = epoll::EventData::new_u64(2);
40 let mut targets = HashMap::new();
41
42 let mut event_list = Vec::with_capacity(4);
43 loop {
44 epoll::wait(&epoll, spare_capacity(&mut event_list), None).unwrap();
45 for event in event_list.drain(..) {
46 let target = event.data;
47 if target.u64() == 1 {
48 let conn_sock = accept(&listen_sock).unwrap();
49 ioctl_fionbio(&conn_sock, true).unwrap();
50 epoll::add(
51 &epoll,
52 &conn_sock,
53 next_data,
54 epoll::EventFlags::OUT | epoll::EventFlags::ET,
55 )
56 .unwrap();
57 targets.insert(next_data, conn_sock);
58 next_data = epoll::EventData::new_u64(next_data.u64() + 1);
59 } else {
60 let target = targets.remove(&target).unwrap();
61 write(&target, b"hello\n").unwrap();
62 epoll::delete(&epoll, &target).unwrap();
63 }
64 }
65 }
66}
67
68fn client(ready: Arc<(Mutex<u16>, Condvar)>) {
69 let port = {

Callers 1

test_epollFunction · 0.70

Calls 15

addFunction · 0.85
spare_capacityFunction · 0.85
ioctl_fionbioFunction · 0.85
deleteFunction · 0.85
drainMethod · 0.80
u64Method · 0.80
insertMethod · 0.80
removeMethod · 0.80
socketFunction · 0.50
bindFunction · 0.50
listenFunction · 0.50
getsocknameFunction · 0.50

Tested by 1

test_epollFunction · 0.56