Add a separate client binary, with some common code to the server.
The cargo workspace thing was a PITA to figure out, but it seems to be working. Could maybe be simplified?
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
use crate::handler::FileHandler;
|
||||
use common::{Request, RESPONSE_NO, RESPONSE_OK};
|
||||
use std::io::{Error, Write};
|
||||
use std::os::unix::net::UnixStream;
|
||||
|
||||
pub struct Server {
|
||||
stream: UnixStream,
|
||||
handler: FileHandler,
|
||||
}
|
||||
|
||||
impl Server {
|
||||
pub fn new(stream: UnixStream, handler: FileHandler) -> Result<Server, Error> {
|
||||
Ok(Server { stream, handler })
|
||||
}
|
||||
|
||||
fn communicate(&mut self) -> Result<(), Error> {
|
||||
let request = Request::from_stream(&mut self.stream)?;
|
||||
|
||||
println!("userid: {}", request.userid);
|
||||
if self.handler.check_auth(&request) {
|
||||
self.stream.write_all(&RESPONSE_OK)?
|
||||
} else {
|
||||
self.stream.write_all(&RESPONSE_NO)?
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn handle_client(&mut self) {
|
||||
if let Err(error) = self.communicate() {
|
||||
eprint!("{error}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user