Skip to content

Commit 8d59910

Browse files
committed
Move handle_incorrect_comp_id to inbound module
1 parent 2c7dc40 commit 8d59910

2 files changed

Lines changed: 43 additions & 30 deletions

File tree

crates/hotfix/src/session.rs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::message::reject::Reject;
3232
use crate::message::resend_request::ResendRequest;
3333
use crate::message::sequence_reset::SequenceReset;
3434
use crate::message::test_request::TestRequest;
35-
use crate::message::verification_error::{CompIdType, MessageVerificationError};
35+
use crate::message::verification_error::MessageVerificationError;
3636
use crate::session::admin_request::AdminRequest;
3737
use crate::session::ctx::{SessionCtx, TransitionResult};
3838
use crate::session::error::SessionCreationError;
@@ -605,9 +605,9 @@ where
605605
}
606606
MessageVerificationError::IncorrectBeginString(begin_string) => {
607607
if let Some(writer) = self.state.get_writer() {
608-
let result = inbound::handle_incorrect_begin_string(
609-
&mut self.ctx, writer, begin_string,
610-
).await;
608+
let result =
609+
inbound::handle_incorrect_begin_string(&mut self.ctx, writer, begin_string)
610+
.await;
611611
self.apply_transition(result);
612612
}
613613
}
@@ -616,8 +616,17 @@ where
616616
comp_id_type,
617617
msg_seq_num,
618618
} => {
619-
self.handle_incorrect_comp_id(comp_id, comp_id_type, msg_seq_num)
619+
if let Some(writer) = self.state.get_writer() {
620+
let result = inbound::handle_incorrect_comp_id(
621+
&mut self.ctx,
622+
writer,
623+
comp_id,
624+
comp_id_type,
625+
msg_seq_num,
626+
)
620627
.await;
628+
self.apply_transition(result);
629+
}
621630
}
622631
MessageVerificationError::SendingTimeAccuracyIssue { msg_seq_num } => {
623632
if let Some(writer) = self.state.get_writer() {
@@ -669,28 +678,6 @@ where
669678
Ok(())
670679
}
671680

672-
673-
async fn handle_incorrect_comp_id(
674-
&mut self,
675-
received_comp_id: String,
676-
comp_id_type: CompIdType,
677-
msg_seq_num: u64,
678-
) {
679-
error!(
680-
"rejecting message with incorrect comp ID: {received_comp_id} (type: {comp_id_type:?})"
681-
);
682-
let reject = Reject::new(msg_seq_num)
683-
.session_reject_reason(SessionRejectReason::ValueIsIncorrect)
684-
.text(&format!("invalid comp ID {received_comp_id}"));
685-
if let Err(err) = self.send_message(reject).await {
686-
error!("failed to send reject message with invalid comp ID: {err}");
687-
};
688-
689-
self.state
690-
.logout_and_terminate(&mut self.ctx, "incorrect comp ID received")
691-
.await;
692-
}
693-
694681
async fn handle_sequence_number_too_low(
695682
&mut self,
696683
expected: u64,

crates/hotfix/src/session/inbound.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use crate::message::logout::Logout;
22
use crate::message::reject::Reject;
33
use crate::message::verification::verify_message;
4-
use crate::message::verification_error::MessageVerificationError;
4+
use crate::message::verification_error::{CompIdType, MessageVerificationError};
55
use crate::session::ctx::{SessionCtx, TransitionResult};
66
use crate::session::outbound;
77
use crate::session::state::SessionState;
88
use crate::transport::writer::WriterRef;
9-
use tracing::warn;
109
use hotfix_message::Part;
1110
use hotfix_message::message::Message;
1211
use hotfix_message::session_fields::{MSG_SEQ_NUM, SessionRejectReason};
1312
use hotfix_store::MessageStore;
1413
use tracing::error;
14+
use tracing::warn;
1515

1616
pub(crate) fn verify_message_with_ctx<A, S: MessageStore>(
1717
ctx: &SessionCtx<A, S>,
@@ -63,7 +63,33 @@ pub(crate) async fn handle_incorrect_begin_string<A, S: MessageStore>(
6363
Err(err) => warn!("failed to send logout for incorrect begin string: {err}"),
6464
}
6565
writer.disconnect().await;
66-
TransitionResult::TransitionTo(SessionState::new_disconnected(true, "incorrect begin string"))
66+
TransitionResult::TransitionTo(SessionState::new_disconnected(
67+
true,
68+
"incorrect begin string",
69+
))
70+
}
71+
72+
pub(crate) async fn handle_incorrect_comp_id<A, S: MessageStore>(
73+
ctx: &mut SessionCtx<A, S>,
74+
writer: &WriterRef,
75+
received_comp_id: String,
76+
comp_id_type: CompIdType,
77+
msg_seq_num: u64,
78+
) -> TransitionResult {
79+
error!("rejecting message with incorrect comp ID: {received_comp_id} (type: {comp_id_type:?})");
80+
let reject = Reject::new(msg_seq_num)
81+
.session_reject_reason(SessionRejectReason::ValueIsIncorrect)
82+
.text(&format!("invalid comp ID {received_comp_id}"));
83+
if let Err(err) = outbound::send_message(ctx, writer, reject).await {
84+
error!("failed to send reject message with invalid comp ID: {err}");
85+
}
86+
let logout = Logout::with_reason("incorrect comp ID received".to_string());
87+
match ctx.prepare_message(logout).await {
88+
Ok(prepared) => writer.send_raw_message(prepared.raw).await,
89+
Err(err) => warn!("failed to send logout for incorrect comp ID: {err}"),
90+
}
91+
writer.disconnect().await;
92+
TransitionResult::TransitionTo(SessionState::new_disconnected(true, "incorrect comp ID"))
6793
}
6894

6995
pub(crate) async fn handle_invalid_msg_type<A, S: MessageStore>(

0 commit comments

Comments
 (0)