add: server override

This commit is contained in:
Kingtous 2022-04-28 16:06:52 +08:00
parent 8854fcbe85
commit 51caeafebd
4 changed files with 74 additions and 48 deletions

View File

@ -506,7 +506,7 @@ impl TransferJob {
match r.union {
Some(file_transfer_send_confirm_request::Union::skip(s)) => {
if s {
log::debug!("skip current file");
println!("skip current file");
self.skip_current_file();
} else {
self.set_file_confirmed(true);
@ -561,6 +561,15 @@ pub fn new_block(block: FileTransferBlock) -> Message {
msg_out
}
#[inline]
pub fn new_send_confirm(r: FileTransferSendConfirmRequest) -> Message {
let mut msg_out = Message::new();
let mut action = FileAction::new();
action.set_send_confirm(r);
msg_out.set_file_action(action);
msg_out
}
#[inline]
pub fn new_receive(id: i32, path: String, files: Vec<FileEntry>) -> Message {
let mut action = FileAction::new();
@ -678,14 +687,14 @@ pub fn create_dir(dir: &str) -> ResultType<()> {
pub fn is_write_need_confirmation(
file_path: &str,
digest: &FileTransferDigest,
) -> ResultType<bool> {
) -> ResultType<Option<FileTransferDigest>> {
let path = Path::new(file_path);
if path.exists() && path.is_file() {
let metadata = std::fs::metadata(path)?;
let modified_time = metadata.modified()?;
let remote_mt = Duration::from_secs(digest.last_edit_timestamp);
let local_mt = modified_time.duration_since(UNIX_EPOCH)?;
println!(
log::info!(
"{:?}:rm:{},lm:{},rf:{},lf:{}",
path,
remote_mt.as_secs(),
@ -693,15 +702,17 @@ pub fn is_write_need_confirmation(
digest.file_size,
metadata.len()
);
// if
// is_recv && remote_mt >= local_mt) || (!is_recv && remote_mt <= local_mt) ||
if remote_mt == local_mt && digest.file_size == metadata.len() {
// I'm recving or sending an newer modified file!
// or a
return Ok(false);
return Ok(None);
}
Ok(true)
Ok(Some(FileTransferDigest {
id: digest.id,
file_num: digest.file_num,
last_edit_timestamp: local_mt.as_secs(),
file_size: metadata.len(),
..Default::default()
}))
} else {
Ok(false)
Ok(None)
}
}

View File

@ -955,7 +955,7 @@ impl Connection {
}
}
Some(message::Union::file_action(fa)) => {
println!("recv file_action, {:?}", fa);
log::info!("recv file_action, {:?}", fa);
if self.file_transfer.is_some() {
match fa.union {
Some(file_action::Union::read_dir(rd)) => {
@ -988,7 +988,6 @@ impl Connection {
}
}
Some(file_action::Union::receive(r)) => {
println!("[connection.rs:891] recv FileTransferReceiveRequest");
self.send_fs(ipc::FS::NewWrite {
path: r.path,
id: r.id,
@ -1025,7 +1024,6 @@ impl Connection {
fs::remove_job(c.id, &mut self.read_jobs);
}
Some(file_action::Union::send_confirm(r)) => {
println!("recv send confirm request");
if let Some(job) = fs::get_job(r.id, &mut self.read_jobs) {
job.confirm(&r);
}

View File

@ -3,6 +3,8 @@ use crate::ipc::{self, new_listener, Connection, Data};
use clipboard::{
create_cliprdr_context, empty_clipboard, get_rx_clip_client, server_clip_file, set_conn_enabled,
};
use hbb_common::fs::{get_string, is_write_need_confirmation, new_send_confirm};
use hbb_common::log::log;
use hbb_common::{
allow_err,
config::Config,
@ -10,6 +12,7 @@ use hbb_common::{
message_proto::*,
protobuf::Message as _,
tokio::{self, sync::mpsc, task::spawn_blocking},
ResultType,
};
use sciter::{make_args, Element, Value, HELEMENT};
use std::{
@ -188,21 +191,45 @@ impl ConnectionManager {
modified_time,
} => {
if let Some(job) = fs::get_job(id, write_jobs) {
// TODO
let mut msg_out = Message::new();
let mut file_action = FileAction::new();
file_action.set_send_confirm(FileTransferSendConfirmRequest {
let mut req = FileTransferSendConfirmRequest {
id,
file_num,
union: Some(file_transfer_send_confirm_request::Union::offset_blk(0)),
..Default::default()
});
msg_out.set_file_action(file_action);
println!(
"[CHECK DIGEST] dig dest recved. confirmed. msg: {:?}",
msg_out
);
Self::send(msg_out, conn).await;
};
let digest = FileTransferDigest {
id,
file_num,
last_edit_timestamp: modified_time,
file_size,
..Default::default()
};
if let Some(file) = job.files().get(file_num as usize) {
let path = get_string(&job.join(&file.name));
match is_write_need_confirmation(&path, &digest) {
Ok(digest) => {
if digest.is_none() {
log::info!("skip job {}, file_num {}", id, file_num);
req.set_skip(true);
let msg_out = new_send_confirm(req);
Self::send(msg_out, conn).await;
} else {
// upload to server, but server has the same file, request
println!(
"server has the same file, send server digest to local"
);
let mut msg_out = Message::new();
let mut fr = FileResponse::new();
fr.set_digest(digest.unwrap());
msg_out.set_file_response(fr);
Self::send(msg_out, conn).await;
}
}
Err(err) => {
Self::send(fs::new_error(id, err, file_num), conn).await;
}
}
}
}
}
ipc::FS::WriteBlock {

View File

@ -11,7 +11,7 @@ use clipboard::{
get_rx_clip_client, server_clip_file,
};
use enigo::{self, Enigo, KeyboardControllable};
use hbb_common::fs::{get_string, is_file_exists};
use hbb_common::fs::{get_string, is_file_exists, new_send_confirm};
use hbb_common::log::log;
use hbb_common::{
allow_err,
@ -1760,7 +1760,7 @@ impl Remote {
async fn handle_msg_from_peer(&mut self, data: &[u8], peer: &mut Stream) -> bool {
if let Ok(msg_in) = Message::parse_from_bytes(&data) {
println!("recved msg from peer, decoded: {:?}", msg_in);
// println!("recved msg from peer, decoded: {:?}", msg_in);
match msg_in.union {
Some(message::Union::video_frame(vf)) => {
if !self.first_frame {
@ -1841,14 +1841,13 @@ impl Remote {
}
}
Some(file_response::Union::digest(digest)) => {
log::info!("recv file transfer digest");
if let Some(job) = fs::get_job(digest.id, &mut self.write_jobs) {
if let Some(file) = job.files().get(digest.file_num as usize) {
let write_path = get_string(&job.join(&file.name));
let overwrite_strategy = job.default_overwrite_strategy();
match fs::is_write_need_confirmation(&write_path, &digest) {
Ok(res) => {
if res {
if res.is_some() {
// need confirm
if overwrite_strategy.is_none() {
self.handler.call(
@ -1860,36 +1859,27 @@ impl Remote {
),
);
} else {
let mut msg = Message::new();
let mut file_action = FileAction::new();
file_action
.set_send_confirm(FileTransferSendConfirmRequest {
let msg = new_send_confirm(
FileTransferSendConfirmRequest {
id: digest.id,
file_num: digest.file_num,
union: Some(
if overwrite_strategy.unwrap() {
file_transfer_send_confirm_request::Union::offset_blk(0)
} else {
file_transfer_send_confirm_request::Union::skip(true)
},
),
union: if overwrite_strategy.unwrap() {
Some(file_transfer_send_confirm_request::Union::offset_blk(0))
} else {
Some(file_transfer_send_confirm_request::Union::skip(true))
},
..Default::default()
});
msg.set_file_action(file_action);
},
);
allow_err!(peer.send(&msg).await);
}
} else {
// file with digest need send
let mut msg = Message::new();
let mut file_action = FileAction::new();
file_action
.set_send_confirm(FileTransferSendConfirmRequest {
let msg= new_send_confirm(FileTransferSendConfirmRequest {
id: digest.id,
file_num: digest.file_num,
union: Some(file_transfer_send_confirm_request::Union::offset_blk(0)),
union: Some(file_transfer_send_confirm_request::Union::skip(true)),
..Default::default()
});
msg.set_file_action(file_action);
allow_err!(peer.send(&msg).await);
}
}