Skip to content

Commit

Permalink
add: server-client message communication
Browse files Browse the repository at this point in the history
  • Loading branch information
luftaquila committed May 16, 2024
1 parent a61c0e3 commit b67b0dd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
2 changes: 0 additions & 2 deletions src/bin/transistor-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@ fn main() -> Result<(), Error> {
let mut client = Client::new(server, 1.0)?;
client.start()?;

loop {}

Ok(())
}
13 changes: 12 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use bincode::deserialize;
use display_info::DisplayInfo;
use serde::{Deserialize, Serialize};

use crate::comm::*;
use crate::display::*;
use crate::*;

Expand Down Expand Up @@ -104,7 +105,17 @@ impl Client {

println!("[INF] connected!");

Ok(())
loop {
if let Err(e) = tcp_read(&mut self.tcp, &mut buffer) {
return Err(Error::new(
UnexpectedEof,
format!("warp in failed: {:?}", e),
));
};

let msg: Message = deserialize(&buffer).unwrap();
println!("[DBG] msg: {:?}", msg);
}
}

fn set_display_position(&mut self, server_conf: Vec<Display>) {
Expand Down
39 changes: 24 additions & 15 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,26 +334,35 @@ fn transceive(
// check if there is something to transmit
match rx.try_recv() {
Ok(msg) => {
println!("msg: {:?}", msg);
// TODO: transmit
}
Err(TryRecvError::Empty) => {
// do nothing
println!("[DBG] msg: {:?}", msg);

if let Err(e) = tcp_write(&mut cur.tcp, msg) {
eprintln!("[ERR] msg transfer failed: {}", e);
continue;
}

loop {
// check if cursor warped back
if let Err(e) = tcp_read(&mut cur.tcp, &mut buffer) {
if e.kind() == WouldBlock {
continue;
}

// TODO: proper error handling
println!("[DBG] tcperr: {:?}", e);

break;
}

println!("[DBG] cursor warped back!");
break;
}
}
Err(TryRecvError::Empty) => {} // do nothing on empty recv
Err(e) => {
eprintln!("[ERR] message receive failed: {}", e);
}
}

// check if cursor warped back
if let Err(e) = tcp_read(&mut cur.tcp, &mut buffer) {
if e.kind() == WouldBlock {
continue;
}
println!("tcperr: {:?}", e);

continue;
}
}
}

Expand Down

0 comments on commit b67b0dd

Please sign in to comment.