Add network link to ue-server implementation #13

Open
dkanus wants to merge 23 commits from feature_link into master
2 changed files with 61 additions and 9 deletions
Showing only changes of commit 26b7d1dd91 - Show all commits

View File

@ -1,4 +1,60 @@
use std::error::Error;
use std::io::{Read, Write};
use std::net::{SocketAddr, TcpListener};
use std::sync::Arc;
use std::{str, thread};
mod reader;
mod writer;
pub use reader::MessageReader;
pub use writer::MessageWriter;
pub struct Link {
reader: MessageReader,
writer: MessageWriter,
}
impl Link {
pub fn run<F>(port: u16, handler: F) -> Result<(), Box<dyn Error>>
where
F: Fn(&mut Link, &str) -> () + Send + Sync + 'static,
{
let address = SocketAddr::from(([0, 0, 0, 0], port));
let listener = TcpListener::bind(address)?;
let handler = Arc::new(handler);
loop {
// Listen to new (multiple) connection
let mut reading_stream = listener.accept()?.0;
Ggg_123 marked this conversation as resolved
Review

Probably needs a timeout, to avoid waiting forever:
stream
.set_read_timeout(Some(Duration::from_secs(5)))
.unwrap();

Probably needs a timeout, to avoid waiting forever: stream .set_read_timeout(Some(Duration::from_secs(5))) .unwrap();
let mut writing_stream = reading_stream.try_clone()?;
let mut avarice_link = Link {
reader: MessageReader::new(),
writer: MessageWriter::new(),
};
let handler_clone = handler.clone();
// On connection - spawn a new thread
thread::spawn(move || loop {
Review

sent next message => sent this message?

sent next message => sent this message?
let mut buffer = [0; 1024];
// Reading cycle
Review

Confusing name, since it's not related to https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next, which is very common.

Confusing name, since it's not related to https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next, which is very common.
match reading_stream.read(&mut buffer) {
Ok(n) => avarice_link.reader.push(&buffer[..n]).unwrap(),
_ => panic!("Connection issue!"),
};
// Handling cycle
while let Some(message) = avarice_link.reader.pop() {
handler_clone(&mut avarice_link, &message);
}
// Writing
avarice_link
.writer
.update_ue_received_bytes(avarice_link.reader.ue_received_bytes());
if let Some(bytes) = avarice_link.writer.try_pop() {
writing_stream.write_all(&bytes).unwrap();
}
Review

Maybe remove continue?

Maybe remove continue?
});
}
}
pub fn write(&mut self, message: &str) {
self.writer.push(message);
}
}

View File

@ -1,13 +1,9 @@
use std::env;
use std::path::Path;
mod link;
mod unreal_config;
use link::Link;
fn main() {
let args: Vec<String> = env::args().collect();
let filename = &args[1];
let config = unreal_config::load_file(Path::new(filename));
if let Ok(config) = config {
print!("{}", config);
}
match Link::run(1234, |link, message| { link.write(message);}) {
Ok(_) => print!("Connect!"),
_ => print!("Connection error!"),
};
}