Browse Source
This removes a level of indirection for actually sending messages which simplifies the overall handling of the connection and unifies how heartbeats are sent to the taker. This is important because we don't want heartbeats to be handled in a special way as we use them to detect issues with the connection. May help with https://github.com/itchysats/itchysats/issues/759.update-blockstream-electrum-server-url
5 changed files with 149 additions and 122 deletions
@ -1,52 +0,0 @@ |
|||
use crate::wire; |
|||
use futures::stream::SplitSink; |
|||
use futures::SinkExt; |
|||
use serde::Serialize; |
|||
use std::fmt; |
|||
use tokio::net::TcpStream; |
|||
use tokio_util::codec::Framed; |
|||
use xtra::Handler; |
|||
use xtra::Message; |
|||
|
|||
pub struct Actor<D, E> { |
|||
write: SplitSink<Framed<TcpStream, wire::EncryptedJsonCodec<D, E>>, E>, |
|||
} |
|||
|
|||
impl<D, E> Actor<D, E> { |
|||
pub fn new(write: SplitSink<Framed<TcpStream, wire::EncryptedJsonCodec<D, E>>, E>) -> Self { |
|||
Self { write } |
|||
} |
|||
} |
|||
|
|||
#[async_trait::async_trait] |
|||
impl<D, E> Handler<E> for Actor<D, E> |
|||
where |
|||
D: Send + Sync + 'static, |
|||
E: Message<Result = ()> + Serialize + fmt::Display + Sync, |
|||
{ |
|||
async fn handle(&mut self, message: E, ctx: &mut xtra::Context<Self>) { |
|||
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<D, E> xtra::Actor for Actor<D, E> |
|||
where |
|||
D: 'static + Send, |
|||
E: 'static + Send, |
|||
{ |
|||
} |
|||
|
|||
impl xtra::Message for wire::MakerToTaker { |
|||
type Result = (); |
|||
} |
|||
|
|||
impl xtra::Message for wire::TakerToMaker { |
|||
type Result = (); |
|||
} |
Loading…
Reference in new issue