before changing to tokio udp

This commit is contained in:
Lukas Bachschwell 2020-01-26 18:28:35 +01:00
parent 4bccd18bd6
commit 0d084cf287
Signed by: lbsadmin
GPG Key ID: CCC6AA87CC8DF425

View File

@ -8,23 +8,36 @@ use std::i64;
use std::io; use std::io;
use std::io::Write; use std::io::Write;
use std::net; use std::net;
use std::thread::spawn;
use std::time::Instant; use std::time::Instant;
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio_tungstenite::connect_async; use tokio_tungstenite::connect_async;
use tungstenite::protocol::Message; use tungstenite::protocol::Message;
use url::Url; use url::Url;
fn listen(socket: &net::UdpSocket) { async fn listen(senddata: futures::channel::mpsc::UnboundedReceiver<String>) {
// Setup the UDP Socket
let udpsocket = net::UdpSocket::bind("0.0.0.0:0").expect("failed to bind host udp socket"); // local bind port
let msg = String::from("ok").into_bytes();
udpsocket
.send_to(&msg, "127.0.0.1:9000")
.expect("cannot send");
loop {
// Handle Sending part
//senddata
/*
let msg = String::from("ok").into_bytes();
udpsocket
.send_to(&msg, "192.168.0.141:9000")
.expect("cannot send");*/
// -------------
let mut buf: [u8; 20] = [0; 20]; let mut buf: [u8; 20] = [0; 20];
let mut result: Vec<u8> = Vec::new(); let mut result: Vec<u8> = Vec::new();
match socket.recv_from(&mut buf) { match udpsocket.recv_from(&mut buf).await {
Ok((number_of_bytes, _)) => { Ok((number_of_bytes, _)) => {
result = Vec::from(&buf[0..number_of_bytes]); result = Vec::from(&buf[0..number_of_bytes]);
} }
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
return ();
}
Err(e) => println!("failed listening {:?}", e), Err(e) => println!("failed listening {:?}", e),
} }
@ -77,6 +90,7 @@ fn listen(socket: &net::UdpSocket) {
} }
} }
} }
}
fn write_file(text: String, filename: &str) { fn write_file(text: String, filename: &str) {
let mut file = std::fs::File::create(filename).expect("create failed"); let mut file = std::fs::File::create(filename).expect("create failed");
@ -153,31 +167,29 @@ async fn main() {
let (write, read) = ws_stream.split(); let (write, read) = ws_stream.split();
let (obstx, obsrx) = futures::channel::mpsc::unbounded(); let (obstx, obsrx) = futures::channel::mpsc::unbounded();
let (udpsockettx, udpsocketrx) = futures::channel::mpsc::unbounded();
let ws_to_stdout = { let ws_to_stdout = {
read.for_each(|message| { read.for_each(|message| {
async { async {
let data = message.unwrap().into_data(); let data = message.unwrap().into_data();
println!("Messg");
tokio::io::stdout().write_all(&data).await.unwrap(); tokio::io::stdout().write_all(&data).await.unwrap();
} }
}) })
}; };
let stdin_to_ws = obsrx.map(Ok).forward(write); let programm_to_ws = obsrx.map(Ok).forward(write);
pin_mut!(stdin_to_ws, ws_to_stdout);
future::select(stdin_to_ws, ws_to_stdout).await;
// Setup the UDP Socket pin_mut!(programm_to_ws, ws_to_stdout);
let udpsocket = net::UdpSocket::bind("0.0.0.0:0").expect("failed to bind host udp socket"); // local bind port
udpsocket.set_nonblocking(true).unwrap();
let msg = String::from("ok").into_bytes(); tokio::spawn(listen(udpsocketrx));
udpsocket
.send_to(&msg, "192.168.0.141:9000") println!("Will wait now");
.expect("cannot send"); future::select(programm_to_ws, ws_to_stdout).await;
/*
loop { loop {
listen(&udpsocket);
if now.elapsed().as_secs() >= 5 { if now.elapsed().as_secs() >= 5 {
let request = json!({"request-type":"SetTextFreetype2Properties", "source":source_id,"message-id": random::<f64>().to_string(), "text": now.elapsed().as_millis().to_string() }); let request = json!({"request-type":"SetTextFreetype2Properties", "source":source_id,"message-id": random::<f64>().to_string(), "text": now.elapsed().as_millis().to_string() });
obstx obstx
@ -187,4 +199,5 @@ async fn main() {
now = Instant::now(); now = Instant::now();
} }
} }
*/
} }