Skip to content

Commit 928eb38

Browse files
committed
Delegate on_logon logic to AwaitingLogon state
1 parent cbfb92b commit 928eb38

3 files changed

Lines changed: 29 additions & 14 deletions

File tree

crates/hotfix/src/session.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -339,20 +339,8 @@ where
339339
}
340340

341341
async fn on_logon(&mut self) -> Result<(), SessionOperationError> {
342-
if let SessionState::AwaitingLogon(AwaitingLogonState { writer, .. }) = &self.state {
343-
let writer = writer.clone();
344-
// happy logon flow, the session is now active
345-
self.apply_transition(TransitionResult::TransitionTo(SessionState::new_active(
346-
writer,
347-
self.ctx.config.heartbeat_interval,
348-
)))
349-
.await;
350-
self.ctx.application.on_logon().await;
351-
self.ctx.store.increment_target_seq_number().await?;
352-
} else {
353-
error!("received unexpected logon message");
354-
}
355-
342+
let transition = self.state.on_peer_logon(&mut self.ctx).await?;
343+
self.apply_transition(transition).await;
356344
Ok(())
357345
}
358346

crates/hotfix/src/session/state.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ impl SessionState {
6363
})
6464
}
6565

66+
/// Handles a Logon message from the peer. Only AwaitingLogon produces a
67+
/// transition — all other states log an error and stay.
68+
pub(crate) async fn on_peer_logon<A: Application, S: MessageStore>(
69+
&self,
70+
ctx: &mut SessionCtx<A, S>,
71+
) -> Result<TransitionResult, SessionOperationError> {
72+
match self {
73+
Self::AwaitingLogon(state) => state.on_peer_logon(ctx).await,
74+
_ => {
75+
error!("received unexpected logon message");
76+
Ok(TransitionResult::Stay)
77+
}
78+
}
79+
}
80+
6681
/// Returns the transition to apply when a Logout message is received from the peer.
6782
/// Each state determines its own reconnect policy and disconnect reason.
6883
pub(crate) fn on_peer_logout(&self) -> TransitionResult {

crates/hotfix/src/session/state/awaiting_logon.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ impl AwaitingLogonState {
3838
}
3939
}
4040

41+
pub(crate) async fn on_peer_logon<A: Application, S: MessageStore>(
42+
&self,
43+
ctx: &mut SessionCtx<A, S>,
44+
) -> Result<TransitionResult, SessionOperationError> {
45+
ctx.application.on_logon().await;
46+
ctx.store.increment_target_seq_number().await?;
47+
Ok(TransitionResult::TransitionTo(SessionState::new_active(
48+
self.writer.clone(),
49+
ctx.config.heartbeat_interval,
50+
)))
51+
}
52+
4153
pub(crate) async fn handle_verification_issue<A: Application, S: MessageStore>(
4254
&self,
4355
ctx: &mut SessionCtx<A, S>,

0 commit comments

Comments
 (0)