Created
February 9, 2019 13:39
-
-
Save aep/bd44329ddc58c81efb242932fa59aece 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
#![feature(futures_api, async_await, await_macro, arbitrary_self_types)] | |
#[macro_use] | |
extern crate lazy_static; | |
use std::future::{Future}; | |
use std::task::{Poll}; | |
use std::pin::Pin; | |
use std::{thread, time}; | |
use std::sync::mpsc; | |
use mio::net::UdpSocket; | |
lazy_static! { | |
static ref POLL : std::sync::Arc<mio::Poll> = std::sync::Arc::new(mio::Poll::new().unwrap()); | |
} | |
#[derive(Clone)] | |
struct Udp{ | |
sock: std::sync::Arc<UdpSocket>, | |
} | |
impl Udp { | |
pub fn new() -> Self { | |
let sock = UdpSocket::bind(&"127.0.0.1:11111".parse().unwrap()).unwrap(); | |
POLL.register(&sock, mio::Token(0), mio::Ready::readable(), mio::PollOpt::level()).unwrap(); | |
Self{sock: std::sync::Arc::new(sock)} | |
} | |
} | |
impl Future for Udp { | |
type Output = Vec<u8>; | |
fn poll(self: std::pin::Pin<&mut Self>, wk: &std::task::LocalWaker) -> Poll<Vec<u8>> { | |
println!("activate"); | |
let mut b = vec![0;1024]; | |
match self.sock.recv_from(&mut b) { | |
Ok((len, _)) => { | |
b.truncate(len); | |
Poll::Ready(b) | |
} | |
Err(_) => Poll::Pending, | |
} | |
} | |
} | |
async fn boop(beep: Udp) { | |
loop { | |
let a = await!(beep.clone()); | |
println!("{}", String::from_utf8_lossy(&a)); | |
} | |
} | |
struct Wakething { | |
} | |
impl std::task::Wake for Wakething { | |
fn wake(arc_self: &std::sync::Arc<Self>) { | |
} | |
} | |
fn main() { | |
let udp = Udp::new(); | |
let mut then = Box::pin(boop(udp)); | |
let wk = unsafe { std::task::local_waker(std::sync::Arc::new(Wakething{}))}; | |
let mut events = mio::Events::with_capacity(128); | |
loop { | |
let re = Pin::new(&mut then).poll(&wk); | |
println!("{:?}", re); | |
println!("poll"); | |
POLL.poll(&mut events, None).unwrap(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment