2023-10-02 22:04:41 +03:00
|
|
|
use std::fs::{File, Metadata, self};
|
2023-09-17 07:28:18 +03:00
|
|
|
use std::time::Instant;
|
2023-09-14 08:29:32 +03:00
|
|
|
use std::net::{TcpListener, TcpStream};
|
2023-10-08 15:18:37 +03:00
|
|
|
use std::io::{Read, Write, self};
|
2023-09-16 19:15:30 +03:00
|
|
|
use std::env::{self};
|
2023-09-14 08:29:32 +03:00
|
|
|
|
2023-10-08 15:18:37 +03:00
|
|
|
|
2023-10-09 01:51:15 +03:00
|
|
|
const BUFFER_SIZE:usize = 100000;
|
2023-10-08 15:18:37 +03:00
|
|
|
|
2023-09-16 19:15:30 +03:00
|
|
|
struct FileInfo
|
2023-09-14 08:29:32 +03:00
|
|
|
{
|
2023-09-16 19:15:30 +03:00
|
|
|
file:Option<File>,
|
|
|
|
location:String,
|
2023-10-08 01:52:42 +03:00
|
|
|
size_current:usize,
|
2023-10-02 22:04:41 +03:00
|
|
|
metadata:Option<Metadata>,
|
2023-09-14 08:29:32 +03:00
|
|
|
}
|
2023-09-16 19:15:30 +03:00
|
|
|
impl FileInfo
|
2023-09-14 08:29:32 +03:00
|
|
|
{
|
2023-10-08 01:52:42 +03:00
|
|
|
fn reading_operations(&mut self, stream:&mut TcpStream)
|
2023-09-29 01:44:25 +03:00
|
|
|
{
|
2023-10-02 22:04:41 +03:00
|
|
|
self.read_metadata();
|
|
|
|
match self.metadata
|
|
|
|
{
|
|
|
|
Some(ref mut metadata) =>
|
|
|
|
{
|
|
|
|
if Metadata::is_file(metadata)
|
|
|
|
{
|
|
|
|
self.read_file();
|
2023-10-08 15:18:37 +03:00
|
|
|
self.file_to_byte(stream);
|
2023-10-02 22:04:41 +03:00
|
|
|
}
|
|
|
|
else if Metadata::is_symlink(metadata)
|
|
|
|
{
|
|
|
|
self.read_file();
|
2023-10-08 15:18:37 +03:00
|
|
|
self.file_to_byte(stream);
|
2023-10-02 22:04:41 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//path recognition and creation on the other side
|
|
|
|
//std:path
|
|
|
|
panic!("\n\tError: Folder Transfers've not Supported yet\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None =>
|
2023-10-08 01:52:42 +03:00
|
|
|
{
|
|
|
|
println!("Error: Read Metadata -> {}", self.location);
|
|
|
|
}
|
|
|
|
}
|
2023-09-29 01:44:25 +03:00
|
|
|
}
|
2023-10-08 01:52:42 +03:00
|
|
|
fn writing_operations(&mut self, stream:&mut TcpStream)
|
2023-09-29 01:44:25 +03:00
|
|
|
{
|
2023-10-08 15:18:37 +03:00
|
|
|
self.write_file(stream);
|
2023-09-29 01:44:25 +03:00
|
|
|
}
|
2023-10-02 22:04:41 +03:00
|
|
|
fn read_metadata(&mut self)
|
|
|
|
{
|
2023-10-08 01:52:42 +03:00
|
|
|
//use match, there is a chance to fail creation. don't pass with just some
|
2023-10-02 22:04:41 +03:00
|
|
|
self.metadata = Some(fs::metadata(&self.location).expect("Error: Read Metadata"));
|
|
|
|
}
|
2023-09-16 19:15:30 +03:00
|
|
|
fn read_file(&mut self)
|
|
|
|
{
|
2023-10-08 01:52:42 +03:00
|
|
|
match File::open(&self.location)
|
|
|
|
{
|
|
|
|
Ok(file) =>
|
|
|
|
{
|
|
|
|
self.file = Some(file);
|
|
|
|
}
|
|
|
|
Err(err_val) =>
|
|
|
|
{
|
|
|
|
println!("Error: Open File -> {} | Error: {}", self.location, err_val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-16 19:15:30 +03:00
|
|
|
}
|
2023-10-08 01:52:42 +03:00
|
|
|
fn file_to_byte(&mut self, stream:&mut TcpStream)
|
2023-09-16 19:15:30 +03:00
|
|
|
{
|
2023-10-08 15:18:37 +03:00
|
|
|
//replace with readbuffer ?
|
|
|
|
let mut iteration = (self.metadata.as_ref().unwrap().len()/BUFFER_SIZE as u64)+1;
|
2023-10-09 01:51:15 +03:00
|
|
|
let mut handshake = [0u8;BUFFER_SIZE];
|
|
|
|
handshake[..self.metadata.as_ref().unwrap().len().to_string().as_bytes().len()].copy_from_slice(self.metadata.as_ref().unwrap().len().to_string().as_bytes());
|
|
|
|
match stream.write_all(&mut handshake)
|
|
|
|
{
|
|
|
|
Ok(_) =>
|
|
|
|
{
|
|
|
|
let mut handshake_callback = [0u8;BUFFER_SIZE];
|
|
|
|
match stream.read_exact(&mut handshake_callback)
|
|
|
|
{
|
|
|
|
Ok(_) =>
|
|
|
|
{
|
|
|
|
println!("Done: Handshake -> {}", self.location);
|
|
|
|
}
|
|
|
|
Err(err_val) =>
|
|
|
|
{
|
|
|
|
println!("Error: Handshake Recv -> {} | Error: {}", self.location, err_val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err_val) =>
|
|
|
|
{
|
|
|
|
println!("Error: Handshake Send -> {} | Error: {}", self.location, err_val);
|
|
|
|
}
|
|
|
|
}
|
2023-10-08 15:18:37 +03:00
|
|
|
while iteration != 0
|
2023-09-16 19:15:30 +03:00
|
|
|
{
|
2023-10-08 15:18:37 +03:00
|
|
|
iteration -= 1;
|
|
|
|
let mut buffer = [0u8;BUFFER_SIZE];
|
|
|
|
match self.file.as_ref().as_mut().unwrap().read(&mut buffer)
|
2023-09-16 19:15:30 +03:00
|
|
|
{
|
2023-10-08 15:18:37 +03:00
|
|
|
Ok(read_size) =>
|
2023-09-16 19:15:30 +03:00
|
|
|
{
|
2023-10-08 15:18:37 +03:00
|
|
|
self.size_current += read_size;
|
|
|
|
if iteration != 0
|
2023-09-16 19:15:30 +03:00
|
|
|
{
|
2023-10-08 15:18:37 +03:00
|
|
|
match stream.write_all(&mut buffer)
|
2023-10-08 01:52:42 +03:00
|
|
|
{
|
|
|
|
Ok(_) =>
|
|
|
|
{
|
2023-10-09 01:51:15 +03:00
|
|
|
println!("Done: Send Bytes -> {} | Iteration = {}", self.location, iteration);
|
2023-10-08 01:52:42 +03:00
|
|
|
}
|
|
|
|
Err(err_val) =>
|
|
|
|
{
|
2023-10-08 15:18:37 +03:00
|
|
|
println!("Error: Send Bytes -> {} | Error: {}", self.location, err_val);
|
2023-10-09 01:51:15 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match stream.flush()
|
|
|
|
{
|
|
|
|
Ok(_) =>
|
|
|
|
{
|
|
|
|
println!("Done: Flush -> {}", self.location);
|
|
|
|
}
|
|
|
|
Err(err_val) =>
|
|
|
|
{
|
|
|
|
println!("Error: Flush -> {} | Error: {}", self.location, err_val);
|
2023-10-08 01:52:42 +03:00
|
|
|
}
|
|
|
|
}
|
2023-10-08 15:18:37 +03:00
|
|
|
}
|
|
|
|
else
|
2023-10-09 01:51:15 +03:00
|
|
|
{
|
|
|
|
let mut last_buffer:Vec<u8> = (&buffer[..(self.metadata.as_ref().unwrap().len()%BUFFER_SIZE as u64)as usize]).to_vec();
|
2023-10-08 15:18:37 +03:00
|
|
|
match stream.write_all(&mut last_buffer)
|
2023-10-08 01:52:42 +03:00
|
|
|
{
|
2023-10-08 15:18:37 +03:00
|
|
|
Ok(_) =>
|
|
|
|
{
|
|
|
|
println!("Done: Send Last Bytes -> {}", self.location);
|
|
|
|
}
|
|
|
|
Err(err_val) =>
|
|
|
|
{
|
|
|
|
println!("Error: Send Last Bytes -> {} | Error: {}", self.location, err_val);
|
|
|
|
}
|
2023-10-08 01:52:42 +03:00
|
|
|
}
|
|
|
|
}
|
2023-10-08 15:18:37 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
Err(err_val) =>
|
|
|
|
{
|
|
|
|
println!("Error: File to Byte -> {} | Error: {}", self.location, err_val);
|
2023-09-16 19:15:30 +03:00
|
|
|
}
|
|
|
|
}
|
2023-10-08 15:18:37 +03:00
|
|
|
|
|
|
|
|
2023-09-16 19:15:30 +03:00
|
|
|
}
|
|
|
|
}
|
2023-10-08 15:18:37 +03:00
|
|
|
fn write_file(&mut self, stream:&mut TcpStream)
|
2023-09-14 08:29:32 +03:00
|
|
|
{
|
2023-10-08 01:52:42 +03:00
|
|
|
//use match, there is a chance to fail creation. don't pass with just some
|
2023-09-16 19:15:30 +03:00
|
|
|
self.file = Some(File::create(&self.location).expect("Error: Create File"));
|
|
|
|
match self.file
|
|
|
|
{
|
|
|
|
Some(ref mut file_descriptor) =>
|
|
|
|
{
|
2023-10-09 01:51:15 +03:00
|
|
|
let mut handshake = [0u8;BUFFER_SIZE];
|
|
|
|
let mut size:u64 = 0;
|
|
|
|
match stream.read_exact(&mut handshake)
|
2023-09-16 19:15:30 +03:00
|
|
|
{
|
2023-09-16 22:08:10 +03:00
|
|
|
Ok(_) =>
|
|
|
|
{
|
2023-10-09 01:51:15 +03:00
|
|
|
todo!();
|
|
|
|
size = String::from_utf8(handshake.to_vec()).unwrap().parse().unwrap();
|
|
|
|
println!("Done: Handshake Recv -> {}", self.location);
|
|
|
|
match stream.write_all(&mut handshake)
|
|
|
|
{
|
|
|
|
Ok(_) =>
|
|
|
|
{
|
|
|
|
println!("Done: Handshake Send -> {}", self.location);
|
|
|
|
}
|
|
|
|
Err(err_val) =>
|
|
|
|
{
|
|
|
|
println!("Error: Handshake Send -> {} | Error: {}", self.location, err_val);
|
|
|
|
}
|
|
|
|
}
|
2023-09-16 22:08:10 +03:00
|
|
|
}
|
2023-10-08 15:18:37 +03:00
|
|
|
Err(err_val) =>
|
|
|
|
{
|
2023-10-09 01:51:15 +03:00
|
|
|
println!("Error: Handshake Recv -> {} | Error: {}", self.location, err_val);
|
2023-10-08 15:18:37 +03:00
|
|
|
}
|
2023-10-09 01:51:15 +03:00
|
|
|
}
|
|
|
|
let mut iteration:u64 = (size%BUFFER_SIZE as u64)+1;
|
|
|
|
while iteration != 0
|
|
|
|
{
|
|
|
|
iteration -= 1;
|
|
|
|
//let mut buffer: Vec<u8> = Vec::new();
|
|
|
|
let mut buffer = [0u8;BUFFER_SIZE];
|
|
|
|
match stream.read(&mut buffer)
|
|
|
|
{
|
|
|
|
Ok(_) =>
|
|
|
|
{
|
|
|
|
match File::write_all(file_descriptor, &mut buffer)
|
|
|
|
{
|
|
|
|
Ok(_) =>
|
|
|
|
{
|
|
|
|
self.size_current += buffer.len();
|
|
|
|
println!("Done: Write -> {} bytes", &mut self.size_current);
|
|
|
|
}
|
|
|
|
Err(err_val) => println!("Error: Write {}", err_val),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err_val) =>
|
|
|
|
{
|
|
|
|
println!("Error: Recv Bytes -> {} | Error: {}", self.location, err_val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-16 19:15:30 +03:00
|
|
|
}
|
|
|
|
None =>
|
|
|
|
{
|
|
|
|
println!("Error: File None");
|
|
|
|
}
|
|
|
|
}
|
2023-09-14 08:29:32 +03:00
|
|
|
}
|
|
|
|
}
|
2023-09-16 19:15:30 +03:00
|
|
|
enum Connection
|
|
|
|
{
|
|
|
|
Server(String, String),
|
|
|
|
Client(String, String),
|
|
|
|
}
|
|
|
|
impl Connection
|
2023-09-14 08:29:32 +03:00
|
|
|
{
|
2023-09-16 19:15:30 +03:00
|
|
|
fn server(self, file_info:&mut FileInfo)
|
|
|
|
{
|
2023-10-02 22:04:41 +03:00
|
|
|
print!("Server -> ");
|
2023-09-16 19:15:30 +03:00
|
|
|
let ip:String;
|
|
|
|
let port:String;
|
|
|
|
let address:String;
|
|
|
|
match self
|
|
|
|
{
|
|
|
|
Connection::Server(in1, in2) =>
|
|
|
|
{
|
|
|
|
ip = in1.trim_end().to_string();
|
|
|
|
port = in2.trim_end().to_string();
|
|
|
|
address = format!("{}:{}", ip, port);
|
|
|
|
println!("{}", address);
|
|
|
|
}
|
|
|
|
_ => return
|
|
|
|
}
|
|
|
|
let socket = TcpListener::bind(address);
|
|
|
|
for stream in socket.expect("Error: Can't Check Connections").incoming()
|
|
|
|
{
|
|
|
|
match stream
|
|
|
|
{
|
|
|
|
Ok(mut stream) =>
|
|
|
|
{
|
2023-10-08 15:18:37 +03:00
|
|
|
let mut stay = true;
|
2023-09-16 19:15:30 +03:00
|
|
|
while stay
|
|
|
|
{
|
2023-09-17 07:28:18 +03:00
|
|
|
let start_time = Instant::now();
|
2023-10-08 15:18:37 +03:00
|
|
|
FileInfo::writing_operations(file_info, &mut stream);
|
|
|
|
let finish_time = Instant::now();
|
|
|
|
println!("Passed: Total -> {:#?}", finish_time.duration_since(start_time));
|
|
|
|
stay = false;
|
2023-09-16 19:15:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) =>
|
|
|
|
{
|
|
|
|
println!("Error: Can't Visit Stream -> {}", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn client(self, file_info:&mut FileInfo)
|
2023-09-14 08:29:32 +03:00
|
|
|
{
|
2023-10-02 22:04:41 +03:00
|
|
|
print!("Client -> ");
|
2023-09-16 19:15:30 +03:00
|
|
|
let ip:String;
|
|
|
|
let port:String;
|
|
|
|
let address:String;
|
|
|
|
match self
|
|
|
|
{
|
|
|
|
Connection::Client(in1, in2) =>
|
|
|
|
{
|
|
|
|
ip = in1.trim_end().to_string();
|
|
|
|
port = in2.trim_end().to_string();
|
|
|
|
address = format!("{}:{}", ip, port);
|
|
|
|
println!("{}", address);
|
|
|
|
}
|
|
|
|
_ => return
|
|
|
|
}
|
|
|
|
match TcpStream::connect(address)
|
|
|
|
{
|
2023-10-08 01:52:42 +03:00
|
|
|
Ok(mut stream) =>
|
2023-09-16 19:15:30 +03:00
|
|
|
{
|
2023-09-17 07:28:18 +03:00
|
|
|
let start_time = Instant::now();
|
2023-09-16 19:15:30 +03:00
|
|
|
println!("Connected");
|
2023-10-08 01:52:42 +03:00
|
|
|
FileInfo::reading_operations(file_info, &mut stream);
|
2023-09-17 07:28:18 +03:00
|
|
|
let finish_time = Instant::now();
|
|
|
|
println!("Passed: Total -> {:#?}", finish_time.duration_since(start_time));
|
2023-09-16 19:15:30 +03:00
|
|
|
}
|
|
|
|
Err(e) =>
|
|
|
|
{
|
|
|
|
println!("Error: Connection -> {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 08:29:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-16 19:15:30 +03:00
|
|
|
fn take_string(output:String) -> String
|
|
|
|
{
|
|
|
|
let mut input = String::new();
|
|
|
|
println!("{}", output);
|
2023-10-02 22:04:41 +03:00
|
|
|
io::stdin().read_line(&mut input).expect("Error: Failed to Read from Console");
|
2023-09-16 19:15:30 +03:00
|
|
|
input
|
|
|
|
}
|
|
|
|
fn take_arg() -> String
|
|
|
|
{
|
|
|
|
env::args().last().as_deref().unwrap_or("default").to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-14 08:29:32 +03:00
|
|
|
|
|
|
|
fn main()
|
|
|
|
{
|
2023-10-02 22:04:41 +03:00
|
|
|
//DONT FORGET
|
|
|
|
//First we should check folder structure and validation then make connection.
|
2023-09-14 08:29:32 +03:00
|
|
|
println!("Hello, world!");
|
2023-09-16 19:15:30 +03:00
|
|
|
|
|
|
|
let mut data = FileInfo
|
|
|
|
{
|
|
|
|
file:None,
|
|
|
|
location:take_arg(),
|
2023-10-08 01:52:42 +03:00
|
|
|
size_current:0 as usize,
|
2023-10-02 22:04:41 +03:00
|
|
|
metadata:None,
|
2023-09-16 19:15:30 +03:00
|
|
|
};
|
|
|
|
match &take_string("Input: Server 's', Client 'c'".to_string())[0..1]
|
|
|
|
{
|
|
|
|
"s" =>
|
|
|
|
{
|
|
|
|
Connection::server
|
|
|
|
(Connection::Server(take_string("Input: Server Stream IP Address".to_string()),
|
|
|
|
take_string("Input: Server Stream Port Address".to_string())),
|
|
|
|
&mut data);
|
|
|
|
},
|
|
|
|
"c" =>
|
|
|
|
{
|
|
|
|
Connection::client
|
|
|
|
(Connection::Client(take_string("Input: Server IP Address to Connect".to_string()),
|
|
|
|
take_string("Input: Server Port Address to Connect".to_string())),
|
|
|
|
&mut data);
|
|
|
|
}
|
|
|
|
input =>
|
|
|
|
{
|
|
|
|
println!("Error: Give Valid Input, You Gave : {}", input);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-09-14 08:29:32 +03:00
|
|
|
}
|