Expand description
This library lets you offer and transfer files to another peer, assuming you already have a reliable connection established.
§Example
The peers first establish a direct TCP connection using gday_hole_punch, and encrypt it with gday_encryption.
Peer A and peer B are on different computers in this example.
// Peer A offers files and folders they'd like to send
let paths_to_send = ["folder/to/send/".into(), "a/file.txt".into()];
let offer = create_file_offer(&paths_to_send)?;
write_to_async(&offer.offer, &mut stream1).await?;
// Peer B responds to the offer
let offer_msg: FileOfferMsg = read_from_async(&mut stream2).await?;
let requests_msg = FileRequestsMsg::accept_only_new_and_interrupted(
&offer_msg,
Path::new("save/the/files/here/"),
)?;
write_to_async(requests_msg, &mut stream2).await?;
// Peer A sends the accepted files
let requests_msg: FileRequestsMsg = read_from_async(&mut stream1).await?;
send_files(&offer, &requests_msg, &mut stream1, |progress| {}).await?;
// Peer B receives the accepted files
let save_path = Path::new("save/the/files/here/");
receive_files(
&offer_msg,
&requests_msg,
save_path,
&mut stream2,
|progress| {},
)
.await?;
Structs§
- File
Metadata - Information about an offered file.
- File
Offer Msg - The sending peer sends this message to offer files,
and the receiver replies with
FileRequestsMsg
. - File
Requests Msg - The receiving peer replies with this message after getting a
FileOfferMsg
. - Local
File Offer - The sending peer uses this struct to
store its
FileOfferMsg
and a mapping from the shortened offered paths to the local on-disk file paths. - Single
File Request - A part of
FileRequestsMsg
- TmpInfo
File - Information about the file currently being downloaded.
Saved in
TMP_INFO_FILE
as json before the download, and deleted after the download. - Transfer
Report - Holds the status of a file transfer
Enums§
- Error
gday_file_transfer
error.
Constants§
- PROTOCOL_
VERSION - Version of the protocol. Different numbers wound indicate incompatible protocol breaking changes.
- TMP_
DOWNLOAD_ FILE - TMP_
INFO_ FILE
Functions§
- already_
exists - Returns
true
iff a file is already saved atget_last_occupied_version(path)
with the same length as inmetadata
. - create_
file_ offer - Returns a
LocalFileOffer
referring to all the files and directories withinpaths
. - delete_
tmp_ info_ file - detect_
interrupted_ download - Checks if
TMP_DOWNLOAD_FILE
andTMP_INFO_FILE
indownload_dir
indicate a file download was interrupted, andoffer
is re-offering that same file. - get_
download_ path - Joins
save_dir
andoffered_path
. - get_
last_ occupied_ version - Returns the occupied
path
with the greatest numerical suffix. - get_
unoccupied_ version - Returns a version of
path
that isn’t occupied. - read_
from - Reads a message from
reader
usingserde_json
. - read_
from_ async - Asynchronously reads a message from
reader
usingserde_json
. - read_
tmp_ info_ file - receive_
files - Receives the requested files from
reader
. - send_
files - Transfers the requested files to
writer
. - write_
tmp_ info_ file - write_
to - Writes
msg
towriter
usingserde_json
, and flushes. - write_
to_ async - Asynchronously writes
msg
towriter
usingserde_json
, and flushes.