Browse Source
`attach_stream` uses `send` under the hood which will process messages in order. This will guarantee that the `taker_cfd::Actor` processes the messages in the order they come in on the wire. That allows us to assume that we will always get the `Accept` message before the first `Setup` message.fix-bad-api-calls
Thomas Eizinger
3 years ago
3 changed files with 49 additions and 102 deletions
@ -1,52 +0,0 @@ |
|||
use crate::model::cfd::Origin; |
|||
use crate::wire::JsonCodec; |
|||
use crate::{taker_cfd, wire}; |
|||
use futures::{Future, StreamExt}; |
|||
use tokio::net::tcp::OwnedReadHalf; |
|||
use tokio_util::codec::FramedRead; |
|||
use xtra::prelude::*; |
|||
|
|||
pub fn new(read: OwnedReadHalf, cfd_actor: Address<taker_cfd::Actor>) -> impl Future<Output = ()> { |
|||
let mut messages = FramedRead::new(read, JsonCodec::new()); |
|||
|
|||
async move { |
|||
while let Some(message) = messages.next().await { |
|||
match message { |
|||
Ok(wire::MakerToTaker::CurrentOrder(mut order)) => { |
|||
if let Some(order) = order.as_mut() { |
|||
order.origin = Origin::Theirs; |
|||
} |
|||
cfd_actor |
|||
.do_send_async(taker_cfd::NewOrder(order)) |
|||
.await |
|||
.expect("actor to be always available"); |
|||
} |
|||
Ok(wire::MakerToTaker::ConfirmOrder(order_id)) => { |
|||
// TODO: This naming is not well aligned.
|
|||
cfd_actor |
|||
.do_send_async(taker_cfd::OrderAccepted(order_id)) |
|||
.await |
|||
.expect("actor to be always available"); |
|||
} |
|||
Ok(wire::MakerToTaker::RejectOrder(order_id)) => { |
|||
cfd_actor |
|||
.do_send_async(taker_cfd::OrderRejected(order_id)) |
|||
.await |
|||
.expect("actor to be always available"); |
|||
} |
|||
Ok(wire::MakerToTaker::InvalidOrderId(_)) => { |
|||
todo!() |
|||
} |
|||
Ok(wire::MakerToTaker::Protocol(msg)) => { |
|||
cfd_actor |
|||
.do_send_async(taker_cfd::IncProtocolMsg(msg)) |
|||
.await |
|||
.expect("actor to be always available"); |
|||
} |
|||
Err(error) => { |
|||
tracing::error!("Error in reading message: {}", error); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue