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