Skip to content

Commit fcab23a

Browse files
committed
Move handle_sequence_number_too_low to inbound module
1 parent 8d59910 commit fcab23a

2 files changed

Lines changed: 36 additions & 23 deletions

File tree

crates/hotfix/src/session.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -596,8 +596,17 @@ where
596596
actual,
597597
possible_duplicate,
598598
} => {
599-
self.handle_sequence_number_too_low(expected, actual, possible_duplicate)
599+
if let Some(writer) = self.state.get_writer() {
600+
let result = inbound::handle_sequence_number_too_low(
601+
&mut self.ctx,
602+
writer,
603+
expected,
604+
actual,
605+
possible_duplicate,
606+
)
600607
.await;
608+
self.apply_transition(result);
609+
}
601610
}
602611
MessageVerificationError::SeqNumberTooHigh { expected, actual } => {
603612
self.handle_sequence_number_too_high(expected, actual)
@@ -678,28 +687,6 @@ where
678687
Ok(())
679688
}
680689

681-
async fn handle_sequence_number_too_low(
682-
&mut self,
683-
expected: u64,
684-
actual: u64,
685-
possible_duplicate: bool,
686-
) {
687-
if possible_duplicate {
688-
warn!(
689-
"sequence number too low (expected {expected}, actual {actual}, but counterparty indicated it's poss duplicate, ignoring"
690-
);
691-
return;
692-
}
693-
error!(
694-
"we expected {expected} sequence number, but target sent lower ({actual}), terminating..."
695-
);
696-
let reason = format!("sequence number too low (actual {actual}, expected {expected})");
697-
self.state
698-
.logout_and_terminate(&mut self.ctx, &reason)
699-
.await;
700-
self.state = SessionState::new_disconnected(false, &reason);
701-
}
702-
703690
async fn handle_sequence_number_too_high(
704691
&mut self,
705692
expected: u64,

crates/hotfix/src/session/inbound.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,32 @@ pub(crate) async fn handle_incorrect_comp_id<A, S: MessageStore>(
9292
TransitionResult::TransitionTo(SessionState::new_disconnected(true, "incorrect comp ID"))
9393
}
9494

95+
pub(crate) async fn handle_sequence_number_too_low<A, S: MessageStore>(
96+
ctx: &mut SessionCtx<A, S>,
97+
writer: &WriterRef,
98+
expected: u64,
99+
actual: u64,
100+
possible_duplicate: bool,
101+
) -> TransitionResult {
102+
if possible_duplicate {
103+
warn!(
104+
"sequence number too low (expected {expected}, actual {actual}, but counterparty indicated it's poss duplicate, ignoring"
105+
);
106+
return TransitionResult::Stay;
107+
}
108+
error!(
109+
"we expected {expected} sequence number, but target sent lower ({actual}), terminating..."
110+
);
111+
let reason = format!("sequence number too low (actual {actual}, expected {expected})");
112+
let logout = Logout::with_reason(reason.clone());
113+
match ctx.prepare_message(logout).await {
114+
Ok(prepared) => writer.send_raw_message(prepared.raw).await,
115+
Err(err) => warn!("failed to send logout for sequence number too low: {err}"),
116+
}
117+
writer.disconnect().await;
118+
TransitionResult::TransitionTo(SessionState::new_disconnected(false, &reason))
119+
}
120+
95121
pub(crate) async fn handle_invalid_msg_type<A, S: MessageStore>(
96122
ctx: &mut SessionCtx<A, S>,
97123
writer: &WriterRef,

0 commit comments

Comments
 (0)