use whistle::VERSIONS; use whistle::Connection; use whistle::Identity; use whistle::ClientIdentity; use whistle::MessageStorage; use whistle::Message; use whistle::MessageNum; use chacha20::ChaCha20Rng; use rand_core::SeedableRng; const SERVER_PUBLIC_KEY: &[u8] = &[8; 32]; fn main() { let mut rng = ChaCha20Rng::seed_from_u64(87638764); let _conn = Connection::<_, _, 32>::new_client( VERSIONS[0], ClientIdentity::Anonymous, VecMsgStore { incoming: Vec::new(), outgoing: Vec::new(), }, PeerIdentity::new(None, None, Some(&SERVER_PUBLIC_KEY)), None, 1234, &mut rng, ).unwrap(); } struct Msg { data: Vec, received: usize, cursor: usize, message_num: MessageNum, paused: bool, } impl Message for Msg { fn get_mut(&mut self, offset: usize, length: usize) -> Option<&mut [u8]> { self.data.get_mut(offset..)?.get_mut(..length) } fn part_transmitted(&mut self, offset: usize, length: usize) { if offset == self.received { self.received += length; } } fn tell(&mut self) -> (usize, usize, bool) { let remaining = self.data.len() - self.received; (self.received, remaining, true) } fn seek_transfer(&mut self, offset: usize) { self.cursor = offset; } fn advance(&mut self, length: usize) -> (usize, &[u8]) { let offset = self.cursor; let length = length.max(self.data.len()); self.cursor += length; (offset, &self.data[offset..][..length]) } fn message_num(&self) -> MessageNum { self.message_num } fn set_message_num(&mut self, num: MessageNum) { self.message_num = num; } fn pause(&mut self) { self.paused = true; } fn resume(&mut self) { self.paused = false; } fn is_paused(&self) -> bool { self.paused } fn is_transfer_complete(&self) -> bool { self.received == self.data.len() } } struct VecMsgStore { incoming: Vec, outgoing: Vec, } impl MessageStorage for VecMsgStore { type Msg = Msg; fn outgoing(&mut self) -> &mut [Self::Msg] { &mut self.outgoing } fn incoming(&mut self) -> &mut [Self::Msg] { &mut self.incoming } } #[derive(Clone)] struct PeerIdentity { number: Option, private_key: Option>, public_key: Option>, } impl Identity for PeerIdentity { fn number(&self) -> Option { self.number } fn private_key(&self) -> Option<&[u8]> { self.private_key.as_ref().map(|v| v.as_slice()) } fn public_key(&self) -> Option<&[u8]> { self.public_key.as_ref().map(|v| v.as_slice()) } fn new( number: Option, private_key: Option<&[u8]>, public_key: Option<&[u8]>, ) -> Self { Self { number, private_key: private_key.map(|s| s.to_vec()), public_key: public_key.map(|s| s.to_vec()), } } }