#[cfg(test)]
use tokio::sync::oneshot;
use tokio::{net::TcpStream, runtime::Handle};
use crate::{
client::{ClientError, WireframeClient},
rewind_stream::RewindStream,
serializer::Serializer,
};
#[cfg(test)]
fn signal_close_completion(completion_sender: Option<oneshot::Sender<()>>) {
if let Some(sender) = completion_sender {
sender.send(()).unwrap_or_default();
}
}
pub(crate) struct ManagedClientConnection<S, C>
where
S: Serializer + Send + Sync + 'static,
C: Send + 'static,
{
client: Option<WireframeClient<S, RewindStream<TcpStream>, C>>,
is_broken: bool,
#[cfg(test)]
close_completion: Option<oneshot::Sender<()>>,
}
impl<S, C> ManagedClientConnection<S, C>
where
S: Serializer + Send + Sync + 'static,
C: Send + 'static,
{
pub(crate) const fn new(client: WireframeClient<S, RewindStream<TcpStream>, C>) -> Self {
Self {
client: Some(client),
is_broken: false,
#[cfg(test)]
close_completion: None,
}
}
#[cfg(test)]
pub(crate) fn notify_after_close(&mut self, sender: oneshot::Sender<()>) {
self.close_completion = Some(sender);
}
pub(crate) const fn mark_broken(&mut self) { self.is_broken = true; }
pub(crate) const fn is_broken(&self) -> bool { self.is_broken }
pub(crate) fn client_mut(
&mut self,
) -> Result<&mut WireframeClient<S, RewindStream<TcpStream>, C>, ClientError> {
self.client.as_mut().ok_or_else(ClientError::disconnected)
}
}
impl<S, C> Drop for ManagedClientConnection<S, C>
where
S: Serializer + Send + Sync + 'static,
C: Send + 'static,
{
fn drop(&mut self) {
let Some(client) = self.client.take() else {
return;
};
if let Ok(handle) = Handle::try_current() {
#[cfg(test)]
let close_completion = self.close_completion.take();
handle.spawn(async move {
client.close().await;
#[cfg(test)]
signal_close_completion(close_completion);
});
return;
}
if let Ok(runtime) = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
{
runtime.block_on(client.close());
#[cfg(test)]
signal_close_completion(self.close_completion.take());
}
}
}