Skip to content

Commit

Permalink
update: server-client handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
luftaquila committed Apr 28, 2024
1 parent 69549da commit 62f663d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/bin/transistor-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() -> Result<(), Error> {

client.start()?;

loop { }
loop {}

Ok(())
}
4 changes: 2 additions & 2 deletions src/bin/transistor-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ fn main() -> Result<(), Error> {
let client_config = config_dir!().join("authorized_clients.json");
let server = Server::new()?;

server.start(PORT, client_config);
server.start(client_config);

println!("{:?}", server);

loop { }
loop {}

Ok(())
}
42 changes: 33 additions & 9 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use std::{mem, u32};
use std::{fs, net::TcpStream};
use std::collections::HashMap;
use std::io::{Error, ErrorKind::*, Read, Write};
use std::{fs, net::TcpStream};
use std::{mem, u32};

use bincode::deserialize;
use display_info::DisplayInfo;
use serde::{Deserialize, Serialize};

use crate::{display::*, tcp_stream_read_resize};
use crate::{config_dir, tcp_stream_read, tcp_stream_write};

pub type Cid = u32;
Expand All @@ -28,25 +32,45 @@ impl Client {
})
}

pub fn start(&mut self) -> Result<(), Error>{
/* send cid to server */
pub fn start(&mut self) -> Result<(), Error> {
/* transmit cid to server */
tcp_stream_write!(self.tcp, self.cid);

/* get display counts; 0 is unauthorized */
let mut buffer = [0u8; mem::size_of::<u32>()];
/* receive display counts; 0 is unauthorized */
let mut buffer = vec![0u8; mem::size_of::<u32>()];
tcp_stream_read!(self.tcp, buffer);
let disp_cnt: u32 = bincode::deserialize(&buffer).unwrap();

println!("cnt: {}", disp_cnt);
let disp_cnt: u32 = deserialize(&buffer).unwrap();

if disp_cnt < 1 {
return Err(Error::new(NotConnected, "[ERR] authorization failed"));
}

/* receive server's current display configurations */
tcp_stream_read_resize!(self.tcp, buffer);
let server_disp_map: HashMap<Did, Display> = deserialize(&buffer).unwrap();
let server_disp: Vec<Display> = server_disp_map.values().cloned().collect();

/* configure our displays' attach position */
let displays: Vec<Display> = configure_displays(server_disp);
tcp_stream_write!(self.tcp, displays);

Ok(())
}
}

fn configure_displays(server_disp: Vec<Display>) -> Vec<Display> {
let system_disp: Vec<Display> = DisplayInfo::all()
.expect("[ERR] failed to get system displays")
.into_iter()
.map(Display::from)
.collect();

/* set system display's position */
// TODO

system_disp
}

fn load_or_generate_cid() -> Result<Cid, Error> {
let cid_file = config_dir!().join("cid");

Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
mod utils;
mod display;
mod client;
mod display;
mod server;
mod utils;

pub use utils::*;
pub use display::*;
pub use client::*;
pub use display::*;
pub use server::*;
pub use utils::*;

pub const PORT: u16 = 2426;
pub const MAX_DISPLAYS: u32 = 128;
56 changes: 33 additions & 23 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
use std::collections::HashMap;
use std::fs;
use std::mem;
use std::io::{Error, ErrorKind::*, Read, Write};
use std::mem;
use std::net::{TcpListener, TcpStream};
use std::path::PathBuf;
use std::sync::{mpsc, Arc, Mutex};
use std::sync::{mpsc, Arc, RwLock};
use std::thread;

use bincode::deserialize;
use display_info::DisplayInfo;
use mouce::Mouse;

use crate::tcp_stream_write;
use crate::{client::*, tcp_stream_read};
use crate::client::*;
use crate::display::*;
use crate::*;
use crate::{tcp_stream_read, tcp_stream_write};

#[derive(Debug)]
pub struct Server {
clients: Arc<Mutex<HashMap<Cid, Client>>>,
displays: HashMap<Did, Display>,
clients: Arc<RwLock<HashMap<Cid, Client>>>,
displays: Arc<RwLock<HashMap<Did, Display>>>,
disp_ids: AssignedDisplays,
current: Did,
}
Expand All @@ -36,10 +38,10 @@ impl Server {

let system = disp.iter().map(|x| x.id).collect();
let current = disp.iter().find(|x| x.is_primary).unwrap_or(&disp[0]).id;
let displays = disp.into_iter().map(|x| (x.id, x)).collect();
let displays = Arc::new(RwLock::new(disp.into_iter().map(|x| (x.id, x)).collect()));

Ok(Server {
clients: Arc::new(Mutex::new(HashMap::new())),
clients: Arc::new(RwLock::new(HashMap::new())),
displays,
disp_ids: AssignedDisplays {
system,
Expand All @@ -49,43 +51,51 @@ impl Server {
})
}

pub fn start(&self, port: u16, client_config: PathBuf) {
pub fn start(&self, client_config: PathBuf) {
// let (tx, rx) = mpsc::channel();
let clients = self.clients.clone();
let displays = self.displays.clone();

thread::spawn(move || {
handle_client(port, clients, client_config);
handle_client(clients, displays, client_config);
});
}
}

fn handle_client(port: u16, clients: Arc<Mutex<HashMap<Cid, Client>>>, config: PathBuf) -> Result<(), Error> {
let tcp = TcpListener::bind(("0.0.0.0", port)).expect("[ERR] TCP binding failed");
fn handle_client(
clients: Arc<RwLock<HashMap<Cid, Client>>>,
displays: Arc<RwLock<HashMap<Did, Display>>>,
config: PathBuf,
) -> Result<(), Error> {
let tcp = TcpListener::bind(("0.0.0.0", PORT)).expect("[ERR] TCP binding failed");
let authorized = authorized_clients(config).expect("[ERR] failed to read client config");

for mut stream in tcp.incoming().filter_map(Result::ok) {
/* read cid from remote client */
let mut buffer = [0u8; mem::size_of::<Cid>()];
let mut buffer = vec![0u8; mem::size_of::<Cid>()];
tcp_stream_read!(stream, buffer);
let cid = bincode::deserialize(&buffer).unwrap();

println!("incoming cid: {}", cid);
let cid = deserialize(&buffer).unwrap();

/* reject not known client */
if !authorized.contains(&cid) {
tcp_stream_write!(stream, 0);
}

let client = Client {
tcp: stream,
cid,
};
/* transmit display counts to client */
tcp_stream_write!(stream, displays.read().unwrap().len() as u32);

clients.lock().unwrap().insert(cid, client);
/* transmit current displays */
let disp = displays.read().unwrap();
tcp_stream_write!(stream, *disp);

/* send current displays */
/* receive display attach request */
tcp_stream_read_resize!(stream, buffer);
let client_disp: Vec<Display> = deserialize(&buffer).unwrap();

/* receive attach request */
/* add accepted client */
// TODO
let client = Client { tcp: stream, cid };
clients.write().unwrap().insert(cid, client);
}

Ok(())
Expand Down
20 changes: 17 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,29 @@ macro_rules! config_dir {

#[macro_export]
macro_rules! tcp_stream_read {
($stream:expr, $buffer:expr) => {
($stream:expr, $buffer:expr) => {{
let mut size = [0u8; 4];
$stream.read_exact(&mut size)?;

let len = u32::from_be_bytes(size) as usize;
$stream.read_exact(&mut $buffer[..len])?;

len
}};
}

#[macro_export]
macro_rules! tcp_stream_read_resize {
($stream:expr, $buffer:expr) => {{
let mut size = [0u8; 4];
$stream.read_exact(&mut size)?;

let len = u32::from_be_bytes(size) as usize;
$buffer.resize(len, 0);
$stream.read_exact(&mut $buffer)?;

$stream.read_exact(&mut $buffer[..len])?;
};
len
}};
}

#[macro_export]
Expand Down

0 comments on commit 62f663d

Please sign in to comment.