2021-12-28 02:37:13 +00:00
|
|
|
use std::{cell::RefCell, ops, rc::Rc};
|
2018-11-16 19:28:07 -08:00
|
|
|
|
2019-03-27 10:38:01 -07:00
|
|
|
use bitflags::bitflags;
|
|
|
|
|
2018-11-18 18:17:38 -08:00
|
|
|
/// Represents various types of connection
|
2022-07-23 17:26:48 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
2018-11-18 18:17:38 -08:00
|
|
|
pub enum ConnectionType {
|
2022-01-19 02:09:25 +00:00
|
|
|
/// Close connection after response.
|
2018-11-18 18:17:38 -08:00
|
|
|
Close,
|
2021-02-12 00:15:25 +00:00
|
|
|
|
2022-01-19 02:09:25 +00:00
|
|
|
/// Keep connection alive after response.
|
2018-11-18 18:17:38 -08:00
|
|
|
KeepAlive,
|
2021-02-12 00:15:25 +00:00
|
|
|
|
2022-01-19 02:09:25 +00:00
|
|
|
/// Connection is upgraded to different type.
|
2018-11-18 18:17:38 -08:00
|
|
|
Upgrade,
|
|
|
|
}
|
|
|
|
|
2019-03-27 10:38:01 -07:00
|
|
|
bitflags! {
|
|
|
|
pub(crate) struct Flags: u8 {
|
|
|
|
const CLOSE = 0b0000_0001;
|
|
|
|
const KEEP_ALIVE = 0b0000_0010;
|
|
|
|
const UPGRADE = 0b0000_0100;
|
2019-04-05 16:46:44 -07:00
|
|
|
const EXPECT = 0b0000_1000;
|
|
|
|
const NO_CHUNKING = 0b0001_0000;
|
2019-04-25 01:48:49 +08:00
|
|
|
const CAMEL_CASE = 0b0010_0000;
|
2019-03-27 10:38:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 19:28:07 -08:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub trait Head: Default + 'static {
|
|
|
|
fn clear(&mut self);
|
|
|
|
|
2021-01-09 23:40:20 +08:00
|
|
|
fn with_pool<F, R>(f: F) -> R
|
|
|
|
where
|
|
|
|
F: FnOnce(&MessagePool<Self>) -> R;
|
2018-11-16 19:28:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Message<T: Head> {
|
2021-04-09 18:07:10 +01:00
|
|
|
/// Rc here should not be cloned by anyone.
|
|
|
|
/// It's used to reuse allocation of T and no shared ownership is allowed.
|
2019-02-09 10:33:49 -08:00
|
|
|
head: Rc<T>,
|
2018-11-16 19:28:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Head> Message<T> {
|
2019-02-07 21:16:46 -08:00
|
|
|
/// Get new message from the pool of objects
|
2021-12-22 07:16:07 +00:00
|
|
|
#[allow(clippy::new_without_default)]
|
2019-02-07 21:16:46 -08:00
|
|
|
pub fn new() -> Self {
|
2021-06-26 18:33:43 +04:00
|
|
|
T::with_pool(MessagePool::get_message)
|
2019-02-07 21:16:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 02:37:13 +00:00
|
|
|
impl<T: Head> ops::Deref for Message<T> {
|
2019-02-07 21:16:46 -08:00
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2021-08-13 18:49:58 +01:00
|
|
|
self.head.as_ref()
|
2019-02-07 21:16:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 02:37:13 +00:00
|
|
|
impl<T: Head> ops::DerefMut for Message<T> {
|
2019-02-07 21:16:46 -08:00
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
2019-02-09 10:33:49 -08:00
|
|
|
Rc::get_mut(&mut self.head).expect("Multiple copies exist")
|
2019-02-07 21:16:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Head> Drop for Message<T> {
|
|
|
|
fn drop(&mut self) {
|
2021-02-07 12:19:10 -08:00
|
|
|
T::with_pool(|p| p.release(self.head.clone()))
|
2018-11-16 19:28:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 02:09:25 +00:00
|
|
|
/// Generic `Head` object pool.
|
2018-11-16 19:28:07 -08:00
|
|
|
#[doc(hidden)]
|
2019-04-07 23:06:21 -07:00
|
|
|
pub struct MessagePool<T: Head>(RefCell<Vec<Rc<T>>>);
|
2018-11-16 19:28:07 -08:00
|
|
|
|
|
|
|
impl<T: Head> MessagePool<T> {
|
2021-12-19 17:05:27 +00:00
|
|
|
pub(crate) fn create() -> MessagePool<T> {
|
2021-01-09 23:40:20 +08:00
|
|
|
MessagePool(RefCell::new(Vec::with_capacity(128)))
|
2018-11-16 19:28:07 -08:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:16:46 -08:00
|
|
|
/// Get message from the pool
|
|
|
|
#[inline]
|
2021-01-09 23:40:20 +08:00
|
|
|
fn get_message(&self) -> Message<T> {
|
2019-04-07 23:06:21 -07:00
|
|
|
if let Some(mut msg) = self.0.borrow_mut().pop() {
|
2021-01-04 21:03:46 +08:00
|
|
|
// Message is put in pool only when it's the last copy.
|
|
|
|
// which means it's guaranteed to be unique when popped out.
|
|
|
|
Rc::get_mut(&mut msg)
|
|
|
|
.expect("Multiple copies exist")
|
|
|
|
.clear();
|
2019-04-06 15:02:02 -07:00
|
|
|
Message { head: msg }
|
2019-02-07 21:16:46 -08:00
|
|
|
} else {
|
|
|
|
Message {
|
2019-02-09 10:33:49 -08:00
|
|
|
head: Rc::new(T::default()),
|
2019-02-07 21:16:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 19:28:07 -08:00
|
|
|
#[inline]
|
2021-12-19 17:05:27 +00:00
|
|
|
/// Release message instance
|
2019-02-09 10:33:49 -08:00
|
|
|
fn release(&self, msg: Rc<T>) {
|
2021-12-19 17:05:27 +00:00
|
|
|
let pool = &mut self.0.borrow_mut();
|
|
|
|
if pool.len() < 128 {
|
|
|
|
pool.push(msg);
|
2019-04-06 15:02:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|