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::Write;
use std::net;
use std::thread::spawn;
use std::time::Instant;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio_tungstenite::connect_async;
use tungstenite::protocol::Message;
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 result: Vec<u8> = Vec::new();
match socket.recv_from(&mut buf) {
match udpsocket.recv_from(&mut buf).await {
Ok((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),
}
@ -76,6 +89,7 @@ fn listen(socket: &net::UdpSocket) {
write_file((intval + 1).to_string(), "rx3.txt");
}
}
}
}
fn write_file(text: String, filename: &str) {
@ -153,31 +167,29 @@ async fn main() {
let (write, read) = ws_stream.split();
let (obstx, obsrx) = futures::channel::mpsc::unbounded();
let (udpsockettx, udpsocketrx) = futures::channel::mpsc::unbounded();
let ws_to_stdout = {
read.for_each(|message| {
async {
let data = message.unwrap().into_data();
println!("Messg");
tokio::io::stdout().write_all(&data).await.unwrap();
}
})
};
let stdin_to_ws = obsrx.map(Ok).forward(write);
pin_mut!(stdin_to_ws, ws_to_stdout);
future::select(stdin_to_ws, ws_to_stdout).await;
let programm_to_ws = obsrx.map(Ok).forward(write);
// Setup the UDP Socket
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();
pin_mut!(programm_to_ws, ws_to_stdout);
let msg = String::from("ok").into_bytes();
udpsocket
.send_to(&msg, "192.168.0.141:9000")
.expect("cannot send");
tokio::spawn(listen(udpsocketrx));
println!("Will wait now");
future::select(programm_to_ws, ws_to_stdout).await;
/*
loop {
listen(&udpsocket);
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() });
obstx
@ -187,4 +199,5 @@ async fn main() {
now = Instant::now();
}
}
*/
}