-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pop the chunk only when received the complete packet
- Loading branch information
1 parent
1c4d049
commit ad59ef1
Showing
2 changed files
with
142 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use std::error::Error; | ||
|
||
use protocol::cluster::AgentTunnelRequest; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct HandshakedChunk { | ||
data: Vec<u8>, | ||
} | ||
|
||
impl HandshakedChunk { | ||
pub fn put_buf(&mut self, buf: &[u8]) { | ||
self.data.extend_from_slice(buf); | ||
} | ||
|
||
pub fn pop_chunk(&self) -> Result<Option<(AgentTunnelRequest, Vec<u8>)>, Box<dyn Error>> { | ||
let handshake_len = u16::from_be_bytes([self.data[0], self.data[1]]) as usize; | ||
if handshake_len + 2 > self.data.len() { | ||
return Ok(None); | ||
} | ||
|
||
match AgentTunnelRequest::try_from(&self.data[2..handshake_len + 2]) { | ||
Ok(handshake) => { | ||
let data = self.data[handshake_len + 2..].to_vec(); | ||
Ok(Some((handshake, data))) | ||
} | ||
Err(e) => Err(Box::new(e)), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
fn build_sample() -> Vec<u8> { | ||
let request = AgentTunnelRequest { | ||
service: None, | ||
tls: true, | ||
domain: "ac5eb61578bebe98636148414efc482f.local.ha.8xff.io".to_string(), | ||
}; | ||
|
||
let handshake_buf = Vec::from(&request); | ||
let handshake_len = handshake_buf.len() as u16; | ||
let mut buf = handshake_len.to_be_bytes().to_vec(); | ||
buf.append(&mut handshake_buf.clone()); | ||
buf.append([1, 2, 3].to_vec().as_mut()); | ||
|
||
// println!("buf: {} {handshake_len}", buf.len()); | ||
|
||
buf | ||
} | ||
|
||
#[test] | ||
pub fn push_full_chunk() { | ||
let mut chunk = HandshakedChunk::default(); | ||
let buf = build_sample(); | ||
chunk.put_buf(&buf); | ||
|
||
let res = chunk.pop_chunk(); | ||
assert!(res.is_ok()); | ||
let (handshake, data) = res.unwrap().unwrap(); | ||
assert_eq!( | ||
handshake.domain, | ||
"ac5eb61578bebe98636148414efc482f.local.ha.8xff.io" | ||
); | ||
assert_eq!(data, [1, 2, 3]); | ||
} | ||
|
||
#[test] | ||
pub fn push_part_chunk() { | ||
let mut chunk = HandshakedChunk::default(); | ||
let buf = build_sample(); | ||
|
||
chunk.put_buf(&buf[0..2]); | ||
let res = chunk.pop_chunk().unwrap(); | ||
assert!(res.is_none()); | ||
chunk.put_buf(&buf[2..]); | ||
let res = chunk.pop_chunk(); | ||
assert!(res.is_ok()); | ||
let (handshake, data) = res.unwrap().unwrap(); | ||
assert_eq!( | ||
handshake.domain, | ||
"ac5eb61578bebe98636148414efc482f.local.ha.8xff.io" | ||
); | ||
assert_eq!(data, [1, 2, 3]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters