use core::fmt::Debug;
use core::ops::Deref;
use iceoryx2_bb_elementary_traits::zero_copy_send::ZeroCopySend;
use iceoryx2_bb_posix::unique_system_id::UniqueSystemId;
use iceoryx2_cal::arc_sync_policy::ArcSyncPolicy;
use iceoryx2_cal::zero_copy_connection::ChannelId;
use crate::port::client::ClientSharedState;
use crate::port::details::chunk_details::ChunkDetails;
use crate::port::port_identifiers::UniqueServerId;
use crate::raw_sample::RawSample;
use crate::service;
pub struct Response<
Service: crate::service::Service,
ResponsePayload: Debug + ZeroCopySend + ?Sized,
ResponseHeader: Debug + ZeroCopySend,
> {
pub(crate) ptr: RawSample<
crate::service::header::request_response::ResponseHeader,
ResponseHeader,
ResponsePayload,
>,
pub(crate) client_shared_state: Service::ArcThreadSafetyPolicy<ClientSharedState<Service>>,
pub(crate) details: ChunkDetails,
pub(crate) channel_id: ChannelId,
}
unsafe impl<
Service: crate::service::Service,
ResponsePayload: Debug + ZeroCopySend + ?Sized,
ResponseHeader: Debug + ZeroCopySend,
> Send for Response<Service, ResponsePayload, ResponseHeader>
where
Service::ArcThreadSafetyPolicy<ClientSharedState<Service>>: Send + Sync,
{
}
impl<
Service: crate::service::Service,
ResponsePayload: Debug + ZeroCopySend + ?Sized,
ResponseHeader: Debug + ZeroCopySend,
> Drop for Response<Service, ResponsePayload, ResponseHeader>
{
fn drop(&mut self) {
self.client_shared_state
.lock()
.response_receiver
.release_offset(&self.details, self.channel_id);
}
}
impl<
Service: crate::service::Service,
ResponsePayload: Debug + ZeroCopySend + ?Sized,
ResponseHeader: Debug + ZeroCopySend,
> Debug for Response<Service, ResponsePayload, ResponseHeader>
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"Response<{}, {}, {}> {{ ptr: {:?} }}",
core::any::type_name::<Service>(),
core::any::type_name::<ResponsePayload>(),
core::any::type_name::<ResponseHeader>(),
self.ptr
)
}
}
impl<
Service: crate::service::Service,
ResponsePayload: Debug + ZeroCopySend + ?Sized,
ResponseHeader: Debug + ZeroCopySend,
> Deref for Response<Service, ResponsePayload, ResponseHeader>
{
type Target = ResponsePayload;
fn deref(&self) -> &Self::Target {
self.ptr.as_payload_ref()
}
}
impl<
Service: crate::service::Service,
ResponsePayload: Debug + ZeroCopySend + ?Sized,
ResponseHeader: Debug + ZeroCopySend,
> Response<Service, ResponsePayload, ResponseHeader>
{
pub fn header(&self) -> &service::header::request_response::ResponseHeader {
self.ptr.as_header_ref()
}
pub fn user_header(&self) -> &ResponseHeader {
self.ptr.as_user_header_ref()
}
pub fn payload(&self) -> &ResponsePayload {
self.ptr.as_payload_ref()
}
pub fn origin(&self) -> UniqueServerId {
UniqueServerId(UniqueSystemId::from(self.details.origin))
}
}