1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use std::cmp::Ordering;
use std::num::NonZeroU64;
/// A tracker for seq. numbers inside a FIX session.
#[derive(Debug, Copy, Clone)]
pub struct SeqNumbers {
/// Next expected inbound message sequence number.
pub next_inbound: u64,
/// Next outbound message sequence number.
pub next_outbound: u64,
}
impl SeqNumbers {
/// Creates a new tracker starting from `inbound` and `outbound`.
pub fn new(inbound: NonZeroU64, outbound: NonZeroU64) -> Self {
Self { next_inbound: inbound.get(), next_outbound: outbound.get() }
}
/// Returns the expected seq. number of the next inbound message.
pub fn next_inbound(&self) -> u64 {
self.next_inbound
}
/// Returns the expected seq. number of the next outbound message.
pub fn next_outbound(&self) -> u64 {
self.next_outbound
}
/// Increments the inbound sequence number counter.
pub fn incr_inbound(&mut self) {
self.next_inbound += 1;
}
/// Increments the outbound sequence number counter.
pub fn incr_outbound(&mut self) {
self.next_outbound += 1;
}
/// Validates an inbound sequence number against expected value.
pub fn validate_inbound(
&self,
inbound: u64,
) -> Result<(), SeqNumberError> {
match inbound.cmp(&self.next_inbound) {
Ordering::Equal => Ok(()),
Ordering::Less => Err(SeqNumberError::TooLow),
Ordering::Greater => Err(SeqNumberError::Recover),
}
}
}
impl Default for SeqNumbers {
/// Returns the default [`SeqNumbers`](SeqNumbers) at the start of a new FIX
/// session, i.e. both 1.
fn default() -> Self {
Self { next_inbound: 1, next_outbound: 1 }
}
}
#[derive(Debug, Clone)]
/// Errors that can occur during sequence number validation.
pub enum SeqNumberError {
/// Sequence number gap detected, resend request needed.
Recover,
/// Sequence number is lower than expected (duplicate).
TooLow,
/// Message is missing the sequence number field.
NoSeqNum,
}