Created
May 17, 2018 09:10
-
-
Save jD91mZM2/d615124812250e6be4427b9dd2ed2a9d to your computer and use it in GitHub Desktop.
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
extern crate syscall; | |
use std::{ | |
io::prelude::*, | |
mem, | |
net::TcpStream, | |
os::unix::io::AsRawFd, | |
slice | |
}; | |
fn main() { | |
println!("New stream"); | |
let mut stream = TcpStream::connect("10.0.2.2:12345").unwrap(); | |
println!("Set nonblocking"); | |
stream.set_nonblocking(true).unwrap(); | |
println!("Open event scheme"); | |
let events = syscall::open("event:", syscall::O_RDONLY | syscall::O_CLOEXEC).unwrap(); | |
println!("Register interest"); | |
syscall::fevent(stream.as_raw_fd(), syscall::EVENT_READ).unwrap(); | |
let mut out = [0; 16]; | |
loop { | |
println!("Read"); | |
stream.read(&mut out).unwrap(); | |
println!("Write"); | |
stream.write(b"yes\n").unwrap(); | |
for _ in 0..2 { | |
println!("Wait for event"); | |
let mut event = syscall::Event::default(); | |
syscall::read(events, unsafe { slice::from_raw_parts_mut( | |
&mut event as *mut _ as *mut u8, | |
mem::size_of::<syscall::Event>() | |
) }).unwrap(); | |
println!("Event: {:?}", event); | |
} | |
println!("Buffer: {:?}", out); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment