use crate::*;
impl Default for ContextData {
#[inline(always)]
fn default() -> Self {
Self {
aborted: false,
closed: false,
stream: None,
request: Request::new(),
response: Response::default(),
attributes: HashMap::new(),
}
}
}
impl ContextData {
#[inline(always)]
pub fn new() -> Self {
Self::default()
}
}
impl Default for Context {
#[inline(always)]
fn default() -> Self {
Self(Arc::new(RwLock::new(ContextData::default())))
}
}
impl From<ContextData> for Context {
#[inline(always)]
fn from(data: ContextData) -> Self {
Self(Arc::new(RwLock::new(data)))
}
}
impl From<ArcRwLockStream> for Context {
#[inline(always)]
fn from(stream: ArcRwLockStream) -> Self {
let data: ContextData = ContextData {
stream: Some(stream),
..Default::default()
};
Self::from(data)
}
}
impl Context {
#[inline(always)]
pub fn new() -> Self {
Self::default()
}
pub(crate) async fn read(&self) -> ArcRwLockReadGuard<'_, ContextData> {
self.0.read().await
}
pub(crate) async fn write(&self) -> ArcRwLockWriteGuard<'_, ContextData> {
self.0.write().await
}
pub async fn is_aborted(&self) -> bool {
self.read().await.aborted
}
pub async fn set_aborted(&self, aborted: bool) -> &Self {
self.write().await.aborted = aborted;
self
}
pub async fn abort(&self) -> &Self {
self.set_aborted(true).await
}
pub async fn cancel_abort(&self) -> &Self {
self.set_aborted(false).await
}
pub async fn is_closed(&self) -> bool {
self.read().await.closed
}
pub async fn set_closed(&self, closed: bool) -> &Self {
self.write().await.closed = closed;
self
}
pub async fn close(&self) -> &Self {
self.set_closed(true).await
}
pub async fn open(&self) -> &Self {
self.set_closed(false).await
}
pub async fn is_terminated(&self) -> bool {
self.is_aborted().await || self.is_closed().await
}
pub async fn try_get_stream(&self) -> Option<ArcRwLockStream> {
self.read().await.stream.clone()
}
pub async fn get_stream(&self) -> ArcRwLockStream {
self.try_get_stream().await.unwrap()
}
pub async fn set_stream(&self, stream: ArcRwLockStream) -> &Self {
self.write().await.stream = Some(stream);
self
}
pub async fn get_request(&self) -> Request {
self.read().await.request.clone()
}
pub async fn set_request(&self, request: Request) -> &Self {
self.write().await.request = request;
self
}
pub async fn get_response(&self) -> Response {
self.read().await.response.clone()
}
pub async fn set_response(&self, response: Response) -> &Self {
self.write().await.response = response;
self
}
pub async fn try_get_socket_addr(&self) -> OptionSocketAddr {
if let Some(stream) = self.try_get_stream().await {
return stream.try_get_peer_addr().await;
}
None
}
pub async fn get_socket_addr(&self) -> SocketAddr {
self.try_get_socket_addr().await.unwrap()
}
pub async fn try_get_socket_addr_string(&self) -> Option<String> {
self.try_get_socket_addr()
.await
.map(|addr| addr.to_string())
}
pub async fn get_socket_addr_string(&self) -> String {
self.get_socket_addr().await.to_string()
}
pub async fn try_get_socket_host(&self) -> OptionSocketHost {
self.try_get_socket_addr().await.map(|addr| addr.ip())
}
pub async fn get_socket_host(&self) -> std::net::IpAddr {
self.try_get_socket_host().await.unwrap()
}
pub async fn try_get_socket_port(&self) -> OptionSocketPort {
self.try_get_socket_addr().await.map(|addr| addr.port())
}
pub async fn get_socket_port(&self) -> u16 {
self.try_get_socket_port().await.unwrap()
}
pub async fn set_data<K, V>(&self, key: K, value: V) -> &Self
where
K: Into<String>,
V: Any + Send + Sync + Clone,
{
self.write()
.await
.attributes
.insert(key.into(), Arc::new(value));
self
}
pub async fn try_get_data<V, K>(&self, key: K) -> Option<V>
where
V: Any + Send + Sync + Clone,
K: AsRef<str>,
{
self.read()
.await
.attributes
.get(key.as_ref())
.and_then(|arc| arc.downcast_ref::<V>())
.cloned()
}
pub async fn get_data_value<V, K>(&self, key: K) -> V
where
V: Any + Send + Sync + Clone,
K: AsRef<str>,
{
self.try_get_data(key).await.unwrap()
}
pub async fn remove_data<K>(&self, key: K) -> &Self
where
K: AsRef<str>,
{
self.write().await.attributes.remove(key.as_ref());
self
}
pub async fn clear_data(&self) -> &Self {
self.write().await.attributes.clear();
self
}
pub async fn try_send<D>(&self, data: D) -> ResponseResult
where
D: AsRef<[u8]>,
{
if self.is_terminated().await {
return Err(ResponseError::Terminated);
}
if let Some(stream) = self.try_get_stream().await {
return stream.try_send(data).await;
}
Err(ResponseError::NotFoundStream)
}
pub async fn send<D>(&self, data: D)
where
D: AsRef<[u8]>,
{
self.try_send(data).await.unwrap();
}
pub async fn try_flush(&self) -> ResponseResult {
if self.is_terminated().await {
return Err(ResponseError::Terminated);
}
if let Some(stream) = self.try_get_stream().await {
return stream.try_flush().await;
}
Err(ResponseError::NotFoundStream)
}
pub async fn flush(&self) {
self.try_flush().await.unwrap();
}
pub async fn try_shutdown(&self) -> ResponseResult {
if let Some(stream) = self.try_get_stream().await {
let result: ResponseResult = stream.try_shutdown().await;
self.close().await;
return result;
}
Err(ResponseError::NotFoundStream)
}
pub async fn shutdown(&self) {
self.try_shutdown().await.unwrap();
}
}