use crate::*;
impl Default for ContextData {
#[inline(always)]
fn default() -> Self {
Self {
aborted: false,
socket: None,
request: Request::new(),
response: Response::default(),
client_addr: None,
attributes: HashMap::new(),
}
}
}
impl Default for Context {
#[inline(always)]
fn default() -> Self {
Self(arc_rwlock(ContextData::default()))
}
}
impl ContextData {
pub fn new(socket: ArcRwLockUdpSocket, request: Request, client_addr: SocketAddr) -> Self {
Self {
aborted: false,
socket: Some(socket),
request,
response: Response::default(),
client_addr: Some(client_addr),
attributes: HashMap::new(),
}
}
pub fn get_aborted(&self) -> bool {
self.aborted
}
pub fn set_aborted(&mut self, aborted: bool) {
self.aborted = aborted;
}
pub fn get_socket(&self) -> Option<ArcRwLockUdpSocket> {
self.socket.clone()
}
pub fn get_request(&self) -> &Request {
&self.request
}
pub fn get_response(&self) -> &Response {
&self.response
}
pub fn get_response_mut(&mut self) -> &mut Response {
&mut self.response
}
pub fn get_client_addr(&self) -> Option<SocketAddr> {
self.client_addr
}
pub fn get_attributes(&self) -> &ThreadSafeAttributeStore {
&self.attributes
}
pub fn get_attributes_mut(&mut self) -> &mut ThreadSafeAttributeStore {
&mut self.attributes
}
}
impl Context {
pub fn new(socket: &ArcRwLockUdpSocket, request: &Request, client_addr: SocketAddr) -> Self {
Self(arc_rwlock(ContextData::new(
socket.clone(),
request.clone(),
client_addr,
)))
}
async fn read(&self) -> RwLockReadGuard<'_, ContextData> {
self.0.read().await
}
async fn write(&self) -> RwLockWriteGuard<'_, ContextData> {
self.0.write().await
}
pub async fn get_aborted(&self) -> bool {
self.read().await.get_aborted()
}
pub async fn set_aborted(&self, aborted: bool) {
self.write().await.set_aborted(aborted);
}
pub async fn aborted(&self) {
self.set_aborted(true).await;
}
pub async fn cancel_aborted(&self) {
self.set_aborted(false).await;
}
pub async fn try_get_socket(&self) -> Option<ArcRwLockUdpSocket> {
self.read().await.get_socket()
}
pub async fn get_socket(&self) -> ArcRwLockUdpSocket {
self.try_get_socket().await.unwrap()
}
pub async fn get_request(&self) -> Request {
self.read().await.get_request().clone()
}
pub async fn get_response(&self) -> Response {
self.read().await.get_response().clone()
}
pub async fn try_get_client_addr(&self) -> Option<SocketAddr> {
self.read().await.get_client_addr()
}
pub async fn get_client_addr(&self) -> SocketAddr {
self.try_get_client_addr().await.unwrap()
}
pub async fn try_get_client_addr_string(&self) -> Option<String> {
self.try_get_client_addr()
.await
.map(|addr| addr.to_string())
}
pub async fn try_get_client_host(&self) -> Option<IpAddr> {
self.try_get_client_addr().await.map(|addr| addr.ip())
}
pub async fn try_get_client_port(&self) -> Option<u16> {
self.try_get_client_addr().await.map(|addr| addr.port())
}
pub async fn set_attribute<K, V>(&self, key: K, value: V) -> &Self
where
K: AsRef<str>,
V: AnySendSyncClone,
{
self.write()
.await
.get_attributes_mut()
.insert(key.as_ref().to_owned(), Arc::new(value));
self
}
pub async fn try_get_attribute<V, K>(&self, key: K) -> Option<V>
where
V: AnySendSyncClone,
K: AsRef<str>,
{
self.read()
.await
.get_attributes()
.get(key.as_ref())
.and_then(|arc| arc.downcast_ref::<V>())
.cloned()
}
pub async fn get_attribute<V, K>(&self, key: K) -> V
where
V: AnySendSyncClone,
K: AsRef<str>,
{
self.try_get_attribute(key).await.unwrap()
}
pub async fn remove_attribute<K>(&self, key: K) -> &Self
where
K: AsRef<str>,
{
self.write().await.get_attributes_mut().remove(key.as_ref());
self
}
pub async fn clear_attributes(&self) -> &Self {
self.write().await.get_attributes_mut().clear();
self
}
pub async fn send<T>(&self, data: T) -> ResponseResult
where
T: Into<ResponseData>,
{
let socket_opt: Option<ArcRwLockUdpSocket> = self.try_get_socket().await;
let addr_opt: Option<SocketAddr> = self.try_get_client_addr().await;
let response: Response = Response::from(data);
response.send(&socket_opt, &addr_opt).await
}
}