ffi.rs (1227B)
1 //! This contain the code related to the syscalls we need to communicate 2 //! with the host operating system 3 4 // This structure is used to communicate with the operating system in 5 // epoll_ctl, and the operating system uses the same structure to communicate 6 // with us in epoll_wait 7 #[derive(Debug)] 8 #[repr(C, packed)] 9 pub struct Event { 10 // Events are defined as a u32, but it’s more than just a number. 11 // This field is what we call a bitmask. 12 pub(crate) events: u32, 13 // Token to identify event: 14 // A union is much like an enum, but in contrast to Rust’s enums, 15 // it doesn’t carry any information on what type it is, so it’s up 16 // to us to make sure we know what type of data it holds. 17 pub(crate) epoll_data: usize, 18 } 19 20 impl Event { 21 pub fn token(&self) -> usize { 22 self.epoll_data 23 } 24 } 25 26 pub const EPOLL_CTL_ADD: i32 = 1; 27 pub const EPOLLIN: i32 = 0x1; 28 pub const EPOLLET: i32 = 1 << 31; 29 30 #[link(name = "c")] 31 unsafe extern "C" { 32 pub fn epoll_create(size: i32) -> i32; 33 pub fn close(fd: i32) -> i32; 34 pub fn epoll_ctl(epfd: i32, op: i32, fd: i32, event: *mut Event) -> i32; 35 pub fn epoll_wait(epfd: i32, events: *mut Event, maxevents: i32, timeout: i32) -> i32; 36 }