1 // Copyright (C) 2020, Cloudflare, Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright notice, 9 // this list of conditions and the following disclaimer. 10 // 11 // * Redistributions in binary form must reproduce the above copyright 12 // notice, this list of conditions and the following disclaimer in the 13 // documentation and/or other materials provided with the distribution. 14 // 15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 17 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 19 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 27 use crate::Error; 28 use crate::Result; 29 30 use std::collections::VecDeque; 31 32 /// Keeps track of DATAGRAM frames. 33 #[derive(Default)] 34 pub struct DatagramQueue { 35 queue: VecDeque<Vec<u8>>, 36 queue_max_len: usize, 37 queue_bytes_size: usize, 38 } 39 40 impl DatagramQueue { new(queue_max_len: usize) -> Self41 pub fn new(queue_max_len: usize) -> Self { 42 DatagramQueue { 43 queue: VecDeque::with_capacity(queue_max_len), 44 queue_bytes_size: 0, 45 queue_max_len, 46 } 47 } 48 push(&mut self, data: &[u8]) -> Result<()>49 pub fn push(&mut self, data: &[u8]) -> Result<()> { 50 if self.is_full() { 51 return Err(Error::Done); 52 } 53 54 self.queue.push_back(data.to_vec()); 55 self.queue_bytes_size += data.len(); 56 Ok(()) 57 } 58 peek_front_len(&self) -> Option<usize>59 pub fn peek_front_len(&self) -> Option<usize> { 60 self.queue.front().map(|d| d.len()) 61 } 62 peek_front_bytes(&self, buf: &mut [u8], len: usize) -> Result<usize>63 pub fn peek_front_bytes(&self, buf: &mut [u8], len: usize) -> Result<usize> { 64 match self.queue.front() { 65 Some(d) => { 66 let len = std::cmp::min(len, d.len()); 67 if buf.len() < len { 68 return Err(Error::BufferTooShort); 69 } 70 71 buf[..len].copy_from_slice(&d[..len]); 72 Ok(len) 73 }, 74 75 None => Err(Error::Done), 76 } 77 } 78 pop(&mut self) -> Option<Vec<u8>>79 pub fn pop(&mut self) -> Option<Vec<u8>> { 80 if let Some(d) = self.queue.pop_front() { 81 self.queue_bytes_size = self.queue_bytes_size.saturating_sub(d.len()); 82 return Some(d); 83 } 84 85 None 86 } 87 has_pending(&self) -> bool88 pub fn has_pending(&self) -> bool { 89 !self.queue.is_empty() 90 } 91 purge<F: Fn(&[u8]) -> bool>(&mut self, f: F)92 pub fn purge<F: Fn(&[u8]) -> bool>(&mut self, f: F) { 93 self.queue.retain(|d| !f(d)); 94 self.queue_bytes_size = 95 self.queue.iter().fold(0, |total, d| total + d.len()); 96 } 97 is_full(&self) -> bool98 pub fn is_full(&self) -> bool { 99 self.queue.len() == self.queue_max_len 100 } 101 byte_size(&self) -> usize102 pub fn byte_size(&self) -> usize { 103 self.queue_bytes_size 104 } 105 } 106