Skip to content

Commit b5b1ede

Browse files
committed
feat(file_transfer): re-grouping file transfer packets from messenger packets
1 parent ee424cd commit b5b1ede

File tree

7 files changed

+196
-152
lines changed

7 files changed

+196
-152
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*! The implementation of file transfer.
2+
*/
3+
4+
pub mod packet;

src/toxcore/messenger/packet/file_control.rs renamed to src/toxcore/messenger/file_transfer/packet/file_control.rs

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,7 @@
22
It is used to control transferring file to a friend.
33
*/
44

5-
use nom::{le_u8, be_u64};
6-
7-
use crate::toxcore::binary_io::*;
8-
9-
/// Whether I am a sender or receiver of file data packet
10-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11-
pub enum TransferDirection {
12-
/// I am a sender
13-
Send = 0,
14-
/// I am a receiver
15-
Receive
16-
}
17-
18-
impl FromBytes for TransferDirection {
19-
named!(from_bytes<TransferDirection>,
20-
switch!(le_u8,
21-
0 => value!(TransferDirection::Send) |
22-
1 => value!(TransferDirection::Receive)
23-
)
24-
);
25-
}
26-
27-
/// Control types for transferring file data
28-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
29-
pub enum ControlType {
30-
/// Accept a request of transferring file from a peer
31-
Accept,
32-
/// Pause transferring
33-
Pause ,
34-
/// Stop transferring and quit session
35-
Kill,
36-
/// Seek to position of file stream and holds seek parameter
37-
Seek(u64)
38-
}
39-
40-
impl ToBytes for ControlType {
41-
fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> {
42-
match self {
43-
ControlType::Accept => do_gen!(buf, gen_be_u8!(0x00)),
44-
ControlType::Pause => do_gen!(buf, gen_be_u8!(0x01)),
45-
ControlType::Kill => do_gen!(buf, gen_be_u8!(0x02)),
46-
ControlType::Seek(seek_param) => do_gen!(buf,
47-
gen_be_u8!(0x03) >>
48-
gen_be_u64!(*seek_param))
49-
}
50-
}
51-
}
52-
53-
impl FromBytes for ControlType {
54-
named!(from_bytes<ControlType>,
55-
switch!(le_u8,
56-
0 => value!(ControlType::Accept) |
57-
1 => value!(ControlType::Pause) |
58-
2 => value!(ControlType::Kill) |
59-
3 => do_parse!(
60-
seek_param: be_u64 >>
61-
(ControlType::Seek(seek_param)))
62-
)
63-
);
64-
}
5+
use super::*;
656

667
/** FileControl is a struct that holds info to handle transferring file to a friend.
678

src/toxcore/messenger/packet/file_data.rs renamed to src/toxcore/messenger/file_transfer/packet/file_data.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
It is used to transfer chunk of file data to a friend.
33
*/
44

5-
use nom::{le_u8, rest, AsBytes};
5+
use nom::{rest, AsBytes};
66

7-
use crate::toxcore::binary_io::*;
8-
9-
/// Maximum size in bytes of chunk of file data
10-
const MAX_FILE_DATA_SIZE: usize = 1371;
7+
use super::*;
118

129
/** FileData is a struct that holds chunk of data of a file to transfer to a friend.
1310

src/toxcore/messenger/packet/file_send_request.rs renamed to src/toxcore/messenger/file_transfer/packet/file_send_request.rs

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,11 @@
22
It is used to start transferring file to a friend.
33
*/
44

5-
use nom::{rest, le_u8, be_u32, be_u64};
5+
use nom::rest;
66

77
use std::str;
88

9-
use crate::toxcore::binary_io::*;
10-
use crate::toxcore::crypto_core::*;
11-
12-
/// Type of file to transfer
13-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
14-
pub enum FileType {
15-
/// Normal data file.
16-
Data = 0,
17-
/// Avatar image file.
18-
Avatar,
19-
}
20-
21-
/// Maximum file name size in bytes
22-
const MAX_FILESEND_FILENAME_LENGTH: usize = 255;
23-
24-
impl FromBytes for FileType {
25-
named!(from_bytes<FileType>,
26-
switch!(be_u32,
27-
0 => value!(FileType::Data) |
28-
1 => value!(FileType::Avatar)
29-
)
30-
);
31-
}
32-
33-
const FILE_UID_BYTES: usize = 32;
34-
35-
/// A type for random 32 bytes which is used as file unique id.
36-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
37-
pub struct FileUID([u8; FILE_UID_BYTES]);
38-
39-
impl FileUID {
40-
/// Create new object
41-
pub fn new() -> FileUID {
42-
let mut array = [0; FILE_UID_BYTES];
43-
randombytes_into(&mut array);
44-
FileUID(array)
45-
}
46-
47-
fn from_slice(bs: &[u8]) -> Option<FileUID> {
48-
if bs.len() != FILE_UID_BYTES {
49-
return None
50-
}
51-
let mut n = FileUID([0; FILE_UID_BYTES]);
52-
for (ni, &bsi) in n.0.iter_mut().zip(bs.iter()) {
53-
*ni = bsi
54-
}
55-
Some(n)
56-
}
57-
}
58-
59-
impl FromBytes for FileUID {
60-
named!(from_bytes<FileUID>, map_opt!(take!(FILE_UID_BYTES), FileUID::from_slice));
61-
}
9+
use super::*;
6210

6311
/** FileSendRequest is a struct that holds info to start transferring file to a friend.
6412
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*! File transfer packets
2+
*/
3+
4+
use nom::{le_u8, be_u32, be_u64};
5+
6+
use crate::toxcore::crypto_core::*;
7+
use crate::toxcore::binary_io::*;
8+
9+
mod file_control;
10+
mod file_data;
11+
mod file_send_request;
12+
13+
pub use self::file_control::*;
14+
pub use self::file_data::*;
15+
pub use self::file_send_request::*;
16+
17+
/// Maximum size in bytes of chunk of file data
18+
const MAX_FILE_DATA_SIZE: usize = 1371;
19+
20+
/// Whether I am a sender or receiver of file data packet
21+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
22+
pub enum TransferDirection {
23+
/// I am a sender
24+
Send = 0,
25+
/// I am a receiver
26+
Receive
27+
}
28+
29+
impl FromBytes for TransferDirection {
30+
named!(from_bytes<TransferDirection>,
31+
switch!(le_u8,
32+
0 => value!(TransferDirection::Send) |
33+
1 => value!(TransferDirection::Receive)
34+
)
35+
);
36+
}
37+
38+
/// Control types for transferring file data
39+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
40+
pub enum ControlType {
41+
/// Accept a request of transferring file from a peer
42+
Accept,
43+
/// Pause transferring
44+
Pause ,
45+
/// Stop transferring and quit session
46+
Kill,
47+
/// Seek to position of file stream and holds seek parameter
48+
Seek(u64)
49+
}
50+
51+
impl ToBytes for ControlType {
52+
fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> {
53+
match self {
54+
ControlType::Accept => do_gen!(buf, gen_be_u8!(0x00)),
55+
ControlType::Pause => do_gen!(buf, gen_be_u8!(0x01)),
56+
ControlType::Kill => do_gen!(buf, gen_be_u8!(0x02)),
57+
ControlType::Seek(seek_param) => do_gen!(buf,
58+
gen_be_u8!(0x03) >>
59+
gen_be_u64!(*seek_param))
60+
}
61+
}
62+
}
63+
64+
impl FromBytes for ControlType {
65+
named!(from_bytes<ControlType>,
66+
switch!(le_u8,
67+
0 => value!(ControlType::Accept) |
68+
1 => value!(ControlType::Pause) |
69+
2 => value!(ControlType::Kill) |
70+
3 => do_parse!(
71+
seek_param: be_u64 >>
72+
(ControlType::Seek(seek_param)))
73+
)
74+
);
75+
}
76+
77+
/// Type of file to transfer
78+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
79+
pub enum FileType {
80+
/// Normal data file.
81+
Data = 0,
82+
/// Avatar image file.
83+
Avatar,
84+
}
85+
86+
/// Maximum file name size in bytes
87+
const MAX_FILESEND_FILENAME_LENGTH: usize = 255;
88+
89+
impl FromBytes for FileType {
90+
named!(from_bytes<FileType>,
91+
switch!(be_u32,
92+
0 => value!(FileType::Data) |
93+
1 => value!(FileType::Avatar)
94+
)
95+
);
96+
}
97+
98+
const FILE_UID_BYTES: usize = 32;
99+
100+
/// A type for random 32 bytes which is used as file unique id.
101+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
102+
pub struct FileUID([u8; FILE_UID_BYTES]);
103+
104+
impl FileUID {
105+
/// Create new object
106+
pub fn new() -> FileUID {
107+
let mut array = [0; FILE_UID_BYTES];
108+
randombytes_into(&mut array);
109+
FileUID(array)
110+
}
111+
112+
fn from_slice(bs: &[u8]) -> Option<FileUID> {
113+
if bs.len() != FILE_UID_BYTES {
114+
return None
115+
}
116+
let mut n = [0; FILE_UID_BYTES];
117+
n.clone_from_slice(bs);
118+
119+
Some(FileUID(n))
120+
}
121+
}
122+
123+
impl FromBytes for FileUID {
124+
named!(from_bytes<FileUID>, map_opt!(take!(FILE_UID_BYTES), FileUID::from_slice));
125+
}
126+
127+
/** FileTransfer packet enum that encapsulates all types of FileTransfer packets.
128+
*/
129+
#[derive(Clone, Debug, Eq, PartialEq)]
130+
pub enum Packet {
131+
/// [`FileControl`](./struct.FileControl.html) structure.
132+
FileControl(FileControl),
133+
/// [`FileData`](./struct.FileData.html) structure.
134+
FileData(FileData),
135+
/// [`FileSendRequest`](./struct.FileSendRequest.html) structure.
136+
FileSendRequest(FileSendRequest),
137+
}
138+
139+
impl ToBytes for Packet {
140+
fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> {
141+
match *self {
142+
Packet::FileControl(ref p) => p.to_bytes(buf),
143+
Packet::FileData(ref p) => p.to_bytes(buf),
144+
Packet::FileSendRequest(ref p) => p.to_bytes(buf),
145+
}
146+
}
147+
}
148+
149+
impl FromBytes for Packet {
150+
named!(from_bytes<Packet>, alt!(
151+
map!(FileControl::from_bytes, Packet::FileControl) |
152+
map!(FileData::from_bytes, Packet::FileData) |
153+
map!(FileSendRequest::from_bytes, Packet::FileSendRequest)
154+
));
155+
}
156+
157+
#[cfg(test)]
158+
mod tests {
159+
use super::*;
160+
161+
encode_decode_test!(
162+
packet_file_control_encode_decode,
163+
Packet::FileControl(FileControl::new(TransferDirection::Send, 1, ControlType::Seek(100)))
164+
);
165+
166+
encode_decode_test!(
167+
packet_file_data_encode_decode,
168+
Packet::FileData(FileData::new(1, vec![1,2,3,4]))
169+
);
170+
171+
encode_decode_test!(
172+
packet_file_send_request_encode_decode,
173+
Packet::FileSendRequest(FileSendRequest::new(1, FileType::Avatar, 4, FileUID::new(), "data".to_string()))
174+
);
175+
}

src/toxcore/messenger/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33

44
pub mod packet;
55
pub mod conference;
6+
pub mod file_transfer;

0 commit comments

Comments
 (0)