|
|
|
@ -15,7 +15,6 @@ GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
|
|
use crate::util::error::Error; |
|
|
|
|
use crate::util::buf_read::{read_u128_slice, read_utf8_string_slice}; |
|
|
|
|
use crate::protocol::constants; |
|
|
|
|
use crate::protocol::specs::*; |
|
|
|
|
use crate::fsys::file::ChunkedFile; |
|
|
|
|
|
|
|
|
@ -46,7 +45,7 @@ impl Packet for TextPacket {
|
|
|
|
|
fn as_bytes(&self) -> Vec<u8> { |
|
|
|
|
let mut packet_as_bytes: Vec<u8> = Vec::<u8>::new(); |
|
|
|
|
|
|
|
|
|
packet_as_bytes.extend_from_slice(&constants::TEXT_PACKET_ID.to_be_bytes()); |
|
|
|
|
packet_as_bytes.extend_from_slice(&TEXT_PACKET_ID.to_be_bytes()); |
|
|
|
|
|
|
|
|
|
let text_length: u128 = self.text.len() as u128; |
|
|
|
|
packet_as_bytes.extend_from_slice(&text_length.to_be_bytes()); |
|
|
|
@ -64,7 +63,7 @@ impl Packet for TextPacket {
|
|
|
|
|
|
|
|
|
|
// retrieve and check for packet type byte
|
|
|
|
|
match packet_bytes[0].to_be() { |
|
|
|
|
constants::TEXT_PACKET_ID => {} |
|
|
|
|
TEXT_PACKET_ID => {} |
|
|
|
|
_ => { |
|
|
|
|
return Some(Error::new("packet type is not of a text packet")); |
|
|
|
|
} |
|
|
|
@ -207,7 +206,7 @@ impl Packet for FileInfoPacket {
|
|
|
|
|
pub struct FileDataPacket { |
|
|
|
|
pub file_id: u128, |
|
|
|
|
pub chunk_no: u128, |
|
|
|
|
pub chunk: [u8; constants::CHUNK_SIZE], |
|
|
|
|
pub chunk: [u8; CHUNK_SIZE], |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl FileDataPacket { |
|
|
|
@ -215,7 +214,7 @@ impl FileDataPacket {
|
|
|
|
|
return FileDataPacket { |
|
|
|
|
file_id: 0, |
|
|
|
|
chunk_no: 0, |
|
|
|
|
chunk: [0; constants::CHUNK_SIZE], |
|
|
|
|
chunk: [0; CHUNK_SIZE], |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -272,7 +271,39 @@ impl Packet for ConnectionShutdownPacket {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn as_bytes(&self) -> Vec<u8> { |
|
|
|
|
return vec!(constants::CONNECTION_SHUTDOWN_PACKET_ID.to_be_bytes()[0]); |
|
|
|
|
return vec!(CONNECTION_SHUTDOWN_PACKET_ID.to_be_bytes()[0]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn from_bytes(&mut self, _packet_bytes: Vec<u8>) -> Option<Error> { |
|
|
|
|
return None; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub struct HandshakePaket { |
|
|
|
|
pub protocol_version: u8, |
|
|
|
|
pub use_encryption: bool, |
|
|
|
|
pub encryption_type: u8, |
|
|
|
|
pub encryption_key: Vec<u8>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl HandshakePaket { |
|
|
|
|
fn empty() -> HandshakePaket { |
|
|
|
|
return HandshakePaket{ |
|
|
|
|
protocol_version: PROTOCOL_LATEST_VERSION, |
|
|
|
|
use_encryption: false, |
|
|
|
|
encryption_type: ECRYPTION_XOR, |
|
|
|
|
encryption_key: vec!(0, 0, 0, 0, 0, 0, 0, 0), |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl Packet for HandshakePaket { |
|
|
|
|
fn get_type(&self) -> PacketType { |
|
|
|
|
return PacketType::Handshake; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn as_bytes(&self) -> Vec<u8> { |
|
|
|
|
return vec!(HANDSHAKE_PACKET_ID.to_be_bytes()[0]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn from_bytes(&mut self, _packet_bytes: Vec<u8>) -> Option<Error> { |
|
|
|
|