Browse Source

Add network prototype

This prototype is untested, since there is currently no client to test it with.

Related to #2
feature_link
Anton Tarasenko 3 years ago
parent
commit
26b7d1dd91
  1. 56
      src/link/mod.rs
  2. 14
      src/main.rs

56
src/link/mod.rs

@ -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 reader;
mod writer; mod writer;
pub use reader::MessageReader; pub use reader::MessageReader;
pub use writer::MessageWriter; 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;
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 {
let mut buffer = [0; 1024];
// Reading cycle
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();
}
});
}
}
pub fn write(&mut self, message: &str) {
self.writer.push(message);
}
}

14
src/main.rs

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

Loading…
Cancel
Save