Skip to content

Commit

Permalink
Merge pull request #53 from genonullfree/protocol-rework
Browse files Browse the repository at this point in the history
Protocol rework
Closes #52
  • Loading branch information
genonullfree authored Nov 3, 2021
2 parents 148add0 + a4d380f commit eedb9cf
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "teleporter"
version = "0.5.3"
version = "0.5.4"
authors = ["geno nullfree <[email protected]>"]
license = "BSD-3-Clause"
description = "A small utility to send files quickly from point A to point B"
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,4 @@ Sending file 3/4: "testfile3"
=> 4.000M of 4.000M (100.00%) done!
Sending file 4/4: "testfile4"
=> 20.000M of 20.000M (100.00%) done!
```
15 changes: 11 additions & 4 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub fn run(opt: Opt) -> Result<(), Error> {

// For each filepath in the input vector...
for (num, item) in files.iter().enumerate() {
let start_time = Instant::now();

let filepath = item;
let mut filename = filepath.clone().to_string();

Expand Down Expand Up @@ -178,17 +180,22 @@ pub fn run(opt: Opt) -> Result<(), Error> {
};

let csum_recv = recv.delta.as_ref().map(|r| r.csum);
let checksum = handle.map(|s| s.join().expect("calc_file_hash panicked"));
let mut checksum: Option<Hash> = None;
if recv.ack == TeleportInitStatus::Overwrite {
checksum = handle.map(|s| s.join().expect("calc_file_hash panicked"));
}

if checksum != None && checksum == csum_recv {
// File matches hash
send_delta_complete(stream, file)?;
} else {
// Send file data
send(stream, file, header, recv.delta)?;
send(stream, file, &header, recv.delta)?;
}

println!(" done!");
let duration = start_time.elapsed();
let speed = (header.filesize as f64 * 8.0) / duration.as_secs() as f64 / 1024.0 / 1024.0;
println!(" done! Time: {:.2?} Speed: {:.3} Mbps", duration, speed);
}
Ok(())
}
Expand Down Expand Up @@ -232,7 +239,7 @@ fn send_delta_complete(mut stream: TcpStream, file: File) -> Result<(), Error> {
fn send(
mut stream: TcpStream,
mut file: File,
header: TeleportInit,
header: &TeleportInit,
delta: Option<TeleportDelta>,
) -> Result<(), Error> {
let mut buf = Vec::<u8>::new();
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::path::PathBuf;
use std::result::Result;
use std::str;
use std::thread;
use std::time::Instant;
use structopt::StructOpt;

mod client;
Expand Down
8 changes: 6 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ fn compare_versions(recv: &str, have: &str) -> bool {

/// Recv receives filenames and file data for a file
fn recv(mut stream: TcpStream, recv_list: Arc<Mutex<Vec<String>>>) -> Result<(), Error> {
let start_time = Instant::now();
let ip = stream.peer_addr().unwrap();
let mut file: File;

Expand Down Expand Up @@ -184,9 +185,12 @@ fn recv(mut stream: TcpStream, recv_list: Arc<Mutex<Vec<String>>>) -> Result<(),

if len == 0 {
if received == header.filesize {
let duration = start_time.elapsed();
let speed =
(header.filesize as f64 * 8.0) / duration.as_secs() as f64 / 1024.0 / 1024.0;
println!(
" => Received file: {} (from: {:?} v{})",
&header.filename, ip, &header.version
" => Received file: {} (from: {:?} v{}) ({:.2?} @ {:.3} Mbps)",
&header.filename, ip, &header.version, duration, speed
);
} else {
println!(" => Error receiving: {}", &header.filename);
Expand Down
42 changes: 21 additions & 21 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@ pub fn calc_delta_hash(mut file: &File) -> Result<TeleportDelta, Error> {
Ok(out)
}

fn vec_to_string(input: &[u8]) -> String {
let mut s: String = "".to_string();
for i in input.iter() {
let c: char = match (*i).try_into() {
Ok(c) => c,
Err(_) => break,
};
if c.is_ascii_graphic() || c == ' ' {
s.push(c);
} else {
break;
}
}

s
}

fn generate_checksum(input: &[u8]) -> u8 {
input.iter().map(|x| *x as u64).sum::<u64>() as u8
}
Expand Down Expand Up @@ -218,23 +235,6 @@ impl TeleportInit {
}
}

fn vec_to_string(input: &[u8]) -> String {
let mut s: String = "".to_string();
for i in input.iter() {
let c: char = match (*i).try_into() {
Ok(c) => c,
Err(_) => break,
};
if c.is_ascii_graphic() || c == ' ' {
s.push(c);
} else {
break;
}
}

s
}

pub fn deserialize(&mut self, input: Vec<u8>) -> Result<(), Error> {
validate_checksum(&input)?;
let mut buf: &[u8] = &input;
Expand All @@ -246,11 +246,11 @@ impl TeleportInit {
));
}
let mut ofs = 4;
self.protocol = TeleportInit::vec_to_string(&input[ofs..]);
self.protocol = vec_to_string(&input[ofs..]);
ofs += self.protocol.len() + 1;
self.version = TeleportInit::vec_to_string(&input[ofs..]);
self.version = vec_to_string(&input[ofs..]);
ofs += self.version.len() + 1;
self.filename = TeleportInit::vec_to_string(&input[ofs..]);
self.filename = vec_to_string(&input[ofs..]);
ofs += self.filename.len() + 1;
let mut buf: &[u8] = &input[ofs..];
self.filenum = buf.read_u64::<LittleEndian>().unwrap();
Expand Down Expand Up @@ -361,7 +361,7 @@ impl TeleportInitAck {
let mut buf: &[u8] = &input;
let size = input.len();
self.ack = buf.read_u8().unwrap().try_into().unwrap();
self.version = TeleportInit::vec_to_string(&input[1..]);
self.version = vec_to_string(&input[1..]);
if size > self.version.len() + 3 {
buf = &input[self.version.len() + 2..];
let c: [u8; 32] = input[self.version.len() + 2 + 16..self.version.len() + 2 + 16 + 32]
Expand Down

0 comments on commit eedb9cf

Please sign in to comment.