Add network link to ue-server implementation #13
@ -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;
|
||||||
dkanus marked this conversation as resolved
Outdated
|
|||||||
pub use reader::MessageReader;
|
pub use reader::MessageReader;
|
||||||
pub use writer::MessageWriter;
|
pub use writer::MessageWriter;
|
||||||
dkanus marked this conversation as resolved
Outdated
Ggg_123
commented
Wtf Wtf
|
|||||||
|
|
||||||
dkanus marked this conversation as resolved
Outdated
Ggg_123
commented
Wtf Wtf
|
|||||||
|
pub struct Link {
|
||||||
|
reader: MessageReader,
|
||||||
|
writer: MessageWriter,
|
||||||
dkanus marked this conversation as resolved
Outdated
Ggg_123
commented
Remove " Value itself is arbitrary", change to "Arbitrary value indicating that ..."? Remove " Value itself is arbitrary", change to "Arbitrary value indicating that ..."?
|
|||||||
|
}
|
||||||
dkanus marked this conversation as resolved
Outdated
Ggg_123
commented
Weird empty line Weird empty line
|
|||||||
|
|
||||||
|
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);
|
||||||
dkanus marked this conversation as resolved
Outdated
Ggg_123
commented
with_capacity != limit. with_capacity != limit.
Should it really be a constant?
I'd just inline it.
dkanus
commented
It is expected limit that we use as a capacity. It is expected limit that we use as a capacity.
dkanus
commented
But upon further consideration I agree that there is no sense in defining this as a constant. But upon further consideration I agree that there is no sense in defining this as a constant.
|
|||||||
|
loop {
|
||||||
|
// Listen to new (multiple) connection
|
||||||
Ggg_123
commented
Can be simplified => // Listen to new connections Can be simplified => // Listen to new connections
|
|||||||
|
let mut reading_stream = listener.accept()?.0;
|
||||||
Ggg_123 marked this conversation as resolved
Ggg_123
commented
Probably needs a timeout, to avoid waiting forever: 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
|
||||||
dkanus marked this conversation as resolved
Outdated
Ggg_123
commented
Unclear meaning. Unclear meaning.
Ggg_123
commented
rename to ue_received_bytes rename to ue_received_bytes
|
|||||||
|
thread::spawn(move || loop {
|
||||||
Ggg_123
commented
sent next message => sent this message? sent next message => sent this message?
|
|||||||
|
let mut buffer = [0; 1024];
|
||||||
Ggg_123
commented
1024? Not 4096? 1024? Not 4096?
dkanus
commented
4096 is limitation on how much we can write, it's unrelated to reading and this is an arbitrary constant. Honestly I don't know what to use, but one option is to add 4096 is limitation on how much we can write, it's unrelated to reading and this is an arbitrary constant. Honestly I don't know what to use, but one option is to add `BufReader` and read byte-by-byte.
dkanus
commented
Did some tests with ue-server. As of now, buffer size does not make a difference in speed, since bottleneck is ue-server's side by far. And it's unlikely that situation will change even with multiple ue-servers connecting to Avarice. Did some tests with ue-server. As of now, buffer size does not make a difference in speed, since bottleneck is ue-server's side by far. And it's unlikely that situation will change even with multiple ue-servers connecting to Avarice.
|
|||||||
|
// Reading cycle
|
||||||
Ggg_123
commented
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) {
|
||||||
dkanus marked this conversation as resolved
Outdated
Ggg_123
commented
"For converting byte stream expected to be generated"? "For converting byte stream expected to be generated"?
|
|||||||
|
Ok(n) => avarice_link.reader.push(&buffer[..n]).unwrap(),
|
||||||
|
_ => panic!("Connection issue!"),
|
||||||
dkanus marked this conversation as resolved
Outdated
Ggg_123
commented
> 4 bytes
u32? i32? BEi32? LEi32?
|
|||||||
|
};
|
||||||
|
// Handling cycle
|
||||||
|
while let Some(message) = avarice_link.reader.pop() {
|
||||||
|
handler_clone(&mut avarice_link, &message);
|
||||||
dkanus marked this conversation as resolved
Outdated
Ggg_123
commented
buffer => length_buffer, since it's used only for storing length bytes buffer => length_buffer, since it's used only for storing length bytes
dkanus
commented
Agreed Agreed
|
|||||||
|
}
|
||||||
|
// 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();
|
||||||
|
}
|
||||||
Ggg_123
commented
Maybe remove continue? Maybe remove continue?
|
|||||||
|
});
|
||||||
dkanus marked this conversation as resolved
Outdated
Ggg_123
commented
with_capactiy? with_capactiy?
Ggg_123
commented
+ // will be recreated with with_capacity in push()
|
|||||||
|
}
|
||||||
dkanus marked this conversation as resolved
Outdated
Ggg_123
commented
with_capactiy? with_capactiy?
|
|||||||
|
}
|
||||||
|
|
||||||
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Wtf
replace with array[u8;4] and
pub fn array_of_u8_to_u32(bytes: [u8; 4]) -> u32 {
(u32::from(bytes[0]) << 24)
+ (u32::from(bytes[1]) << 16)
+ (u32::from(bytes[2]) << 8)
+ (u32::from(bytes[3]))
}