use crate::wire::{self, EncryptedJsonCodec}; use futures::SinkExt; use serde::Serialize; use snow::TransportState; use std::fmt; use std::sync::{Arc, Mutex}; use tokio::io::AsyncWriteExt; use tokio::net::tcp::OwnedWriteHalf; use tokio_util::codec::FramedWrite; use xtra::{Handler, Message}; pub struct Actor { write: FramedWrite>, } impl Actor { pub fn new(write: OwnedWriteHalf, transport_state: Arc>) -> Self { Self { write: FramedWrite::new(write, EncryptedJsonCodec::new(transport_state)), } } pub async fn shutdown(self) { let _ = self.write.into_inner().shutdown().await; } } #[async_trait::async_trait] impl Handler for Actor where T: Message + Serialize + fmt::Display + Sync, { async fn handle(&mut self, message: T, ctx: &mut xtra::Context) { let message_name = message.to_string(); // send consumes the message, avoid a clone just in case it errors by getting the name here tracing::trace!("Sending '{}'", message_name); if let Err(e) = self.write.send(message).await { tracing::error!("Failed to write message {} to socket: {}", message_name, e); ctx.stop(); } } } impl xtra::Actor for Actor {} impl xtra::Message for wire::MakerToTaker { type Result = (); } impl xtra::Message for wire::TakerToMaker { type Result = (); }