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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//! Client of one FIX session of a trading account.
//!
//! An account has two sessions, one for prices and one for trading; each is a connection of
//! its own and a [`FixClient`] of its own.
//!
//! ```no_run
//! use ctrader_connector::common::config::ConfigurationFix;
//! use ctrader_connector::fix::client::FixClient;
//! use ctrader_connector::fix::session::SessionKind;
//!
//! # async fn run() -> Result<(), ctrader_connector::common::errors::ConnectorError> {
//! let client = FixClient::connect(ConfigurationFix::from_env()?, SessionKind::Quote).await?;
//! client.logout().await?;
//! # Ok(())
//! # }
//! ```
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;
use tokio::sync::broadcast::error::RecvError;
use tokio::sync::{broadcast, mpsc, oneshot, watch};
use tokio::time::timeout;
use crate::common::config::ConfigurationFix;
use crate::common::errors::{ConnectorError, FixError, Interruption, TransportError};
use crate::common::time::Timestamp;
use crate::common::transport::{Connect, Plain, Tls};
use crate::fix::messages::Message;
use crate::fix::session::{Command, Event, SessionKind, start};
/// Cloneable client of one FIX session.
#[derive(Debug, Clone)]
pub struct FixClient {
commands: mpsc::Sender<Command>,
events: broadcast::Sender<Event>,
closed: watch::Receiver<bool>,
kind: SessionKind,
timeout: Duration,
counter: Arc<AtomicU64>,
}
impl FixClient {
/// Connects to the host named by the configuration and logs on to the session.
///
/// The connection runs over TLS unless the configuration asks for none.
///
/// # Errors
///
/// Returns [`ConnectorError::Transport`] when the connection cannot be established,
/// [`ConnectorError::Fix`] with [`FixError::LogonRefused`] when the server answers the
/// logon with a logout, and [`ConnectorError::Timeout`] when it does not answer in time.
pub async fn connect(
configuration: ConfigurationFix,
kind: SessionKind,
) -> Result<Self, ConnectorError> {
if configuration.encrypted() {
Self::connect_with(configuration, kind, Tls::new()?).await
} else {
Self::connect_with(configuration, kind, Plain).await
}
}
/// Connects through the given connector and logs on to the session.
///
/// # Errors
///
/// Returns the errors of [`FixClient::connect`].
pub async fn connect_with<C: Connect>(
configuration: ConfigurationFix,
kind: SessionKind,
connector: C,
) -> Result<Self, ConnectorError> {
let limit = configuration.timeout();
let started = start(connector, configuration, kind).await?;
Ok(Self {
commands: started.commands,
events: started.events,
closed: started.closed,
kind,
timeout: limit,
counter: Arc::new(AtomicU64::new(0)),
})
}
/// Returns which session the client serves.
#[must_use]
pub const fn kind(&self) -> SessionKind {
self.kind
}
/// Returns the longest wait for an answer of the server.
#[must_use]
pub const fn timeout(&self) -> Duration {
self.timeout
}
/// Subscribes to the events of the session from this moment on.
///
/// A reader that falls behind loses the oldest events and is told how many.
#[must_use]
pub fn events(&self) -> broadcast::Receiver<Event> {
self.events.subscribe()
}
/// Sends a message and returns the sequence number it was given.
///
/// The message goes out as it is, with one exception: a business message reject (j) is
/// never sent. It is refused here, before anything is written and without a sequence
/// number, because the server refuses one with a reject and closes the connection.
/// Every message of the application passes through this method, those of
/// [`FixClient::request`] and [`FixClient::request_once`] included.
///
/// # Errors
///
/// Returns [`ConnectorError::Fix`] with [`FixError::Unsendable`] for a business message
/// reject and with another [`FixError`] when the message cannot be encoded,
/// [`ConnectorError::Transport`] when the write fails, and [`ConnectorError::Closed`]
/// when the session has ended or is between two connections; the message was not sent
/// then.
pub async fn send(&self, message: Message) -> Result<u64, ConnectorError> {
if matches!(message, Message::BusinessReject(_)) {
return Err(FixError::Unsendable(message.msg_type().to_owned()).into());
}
let (reply, answer) = oneshot::channel();
self.commands
.send(Command::Send { message, reply })
.await
.map_err(|_| ConnectorError::Closed)?;
answer.await.map_err(|_| ConnectorError::Closed)?
}
/// Logs out and closes the connection; the session ends for every clone of the client.
///
/// # Errors
///
/// Returns [`ConnectorError::Timeout`] when the server does not confirm the logout in
/// time, [`ConnectorError::Transport`] when the connection fails first, and
/// [`ConnectorError::Closed`] when the session has ended already. The connection is
/// closed in every case. A logout between two connections ends the session without
/// an exchange and returns `Ok`.
pub async fn logout(&self) -> Result<(), ConnectorError> {
let (reply, answer) = oneshot::channel();
self.commands
.send(Command::Logout { reply })
.await
.map_err(|_| ConnectorError::Closed)?;
answer.await.map_err(|_| ConnectorError::Closed)?
}
/// Drops the connection on this side, which starts a reconnection as a loss would.
///
/// # Errors
///
/// Returns [`ConnectorError::Closed`] when the session has ended.
pub async fn force_reconnect(&self) -> Result<(), ConnectorError> {
let (reply, answer) = oneshot::channel();
self.commands
.send(Command::Reconnect { reply })
.await
.map_err(|_| ConnectorError::Closed)?;
answer.await.map_err(|_| ConnectorError::Closed)?
}
/// Waits until the session has ended.
pub async fn closed(&self) {
let mut closed = self.closed.clone();
let _ = closed.wait_for(|closed| *closed).await;
}
/// Sends a request and waits for the message `pick` accepts as its answer.
///
/// `pick` sees every message that arrives after the request was sent and returns the
/// outcome once it recognises the answer. `reference` is the identifier the request
/// carries, by which a refusal of the server names it; it may be empty.
///
/// # Errors
///
/// Returns [`ConnectorError::Fix`] with [`FixError::Rejected`] or
/// [`FixError::BusinessRejected`] when the server refuses the request,
/// [`ConnectorError::Timeout`] when no answer arrives in time,
/// [`ConnectorError::OutcomeUnknown`] when the server asks for the request again, which
/// the library never sends twice,
/// [`ConnectorError::Transport`] when the connection is lost first,
/// [`ConnectorError::Lagged`] when the reader fell behind and the answer may have been
/// skipped, and [`ConnectorError::Closed`] when the session has ended already or has no
/// connection at the moment, between [`Event::Disconnected`] and [`Event::Reconnected`].
pub async fn request<T>(
&self,
message: Message,
reference: &str,
pick: impl FnMut(&Message) -> Option<Result<T, ConnectorError>>,
) -> Result<T, ConnectorError> {
self.exchange(message, reference, false, pick).await
}
/// Sends a request that must not be sent twice, such as an order, and waits for its answer.
///
/// # Errors
///
/// Returns the errors of [`FixClient::request`], except that a wait cut short by the
/// deadline or by the loss of the session is reported as
/// [`ConnectorError::OutcomeUnknown`]: the server may have acted on the request.
pub async fn request_once<T>(
&self,
message: Message,
reference: &str,
pick: impl FnMut(&Message) -> Option<Result<T, ConnectorError>>,
) -> Result<T, ConnectorError> {
self.exchange(message, reference, true, pick).await
}
/// Returns an identifier no other request of this client carries.
#[must_use]
pub fn next_id(&self, prefix: &str) -> String {
let number = self.counter.fetch_add(1, Ordering::Relaxed);
format!("{prefix}{}n{number}", Timestamp::now().as_millis())
}
/// Sends a request and waits for what `pick` accepts as its answer.
///
/// A reject of the session level or of the application level that refers to the request,
/// by its sequence number or by `reference`, ends the wait with a typed error. For a
/// `critical` request, one that must not be sent twice, a wait cut short by the deadline
/// or by the loss of the connection is reported as [`ConnectorError::OutcomeUnknown`].
async fn exchange<T>(
&self,
message: Message,
reference: &str,
critical: bool,
mut pick: impl FnMut(&Message) -> Option<Result<T, ConnectorError>>,
) -> Result<T, ConnectorError> {
let interrupted = |interruption: Interruption| match (critical, interruption) {
(true, interruption) => ConnectorError::OutcomeUnknown(interruption),
(false, Interruption::Timeout(limit)) => ConnectorError::Timeout(limit),
(false, Interruption::Transport(error)) => ConnectorError::Transport(error),
(false, Interruption::Skipped) => ConnectorError::OutcomeUnknown(Interruption::Skipped),
};
let mut events = self.events.subscribe();
let seq = match self.send(message).await {
Err(ConnectorError::Transport(error)) => {
return Err(interrupted(Interruption::Transport(error)));
}
sent => sent?,
};
let limit = self.timeout;
let waited = timeout(limit, async {
loop {
let inbound = match events.recv().await {
Ok(Event::Message(inbound)) => inbound,
Ok(Event::Disconnected(error)) => {
return Err(interrupted(Interruption::Transport(error)));
}
Ok(Event::LoggedOut { .. } | Event::Ended) | Err(RecvError::Closed) => {
return Err(interrupted(Interruption::Transport(TransportError::Closed)));
}
Ok(Event::Skipped { from, to }) if (from..=to).contains(&seq) => {
return Err(ConnectorError::OutcomeUnknown(Interruption::Skipped));
}
Ok(_) => continue,
Err(RecvError::Lagged(skipped)) => {
return Err(ConnectorError::Lagged { skipped });
}
};
match &inbound.message {
Message::Reject(reject) if reject.ref_seq_num == seq => {
return Err(FixError::Rejected {
ref_seq_num: seq,
tag: reject.ref_tag_id,
reason: reject.reason,
text: reject.text.clone(),
}
.into());
}
Message::BusinessReject(reject)
if reject.ref_seq_num == Some(seq)
|| (!reference.is_empty()
&& reject.ref_id.as_deref() == Some(reference)) =>
{
return Err(FixError::BusinessRejected {
ref_id: reject.ref_id.clone(),
reason: reject.reason,
text: reject.text.clone(),
}
.into());
}
other => {
if let Some(result) = pick(other) {
return result;
}
}
}
}
})
.await;
waited.unwrap_or_else(|_| Err(interrupted(Interruption::Timeout(limit))))
}
}