Skip to content

Commit

Permalink
✨ Add FixSession_send
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurlm committed Oct 6, 2024
1 parent be5c308 commit 9980466
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions quickfix-ffi/quickfix-bind/include/quickfix_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ int8_t FixSession_sendToTarget(FixMessage_t *msg, const FixSessionID_t *session_
FixSession_t *FixSession_lookup(const FixSessionID_t *session_id);
int8_t FixSession_logout(FixSession_t *session);
int8_t FixSession_isLoggedOn(FixSession_t *session);
int8_t FixSession_send(FixSession_t *session, FixMessage_t *msg);

#ifdef __cplusplus
}
Expand Down
5 changes: 5 additions & 0 deletions quickfix-ffi/quickfix-bind/src/quickfix_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1007,5 +1007,10 @@ int8_t FixSession_isLoggedOn(FixSession_t *session) {
CATCH_OR_RETURN_ERRNO({ return session->isLoggedOn(); });
}

int8_t FixSession_send(FixSession_t *session, FixMessage_t *msg) {
RETURN_VAL_IF_NULL(session, ERRNO_INVAL);
CATCH_OR_RETURN_ERRNO({ return session->send(*msg); });
}

} // namespace FIX
} // extern C
2 changes: 2 additions & 0 deletions quickfix-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,6 @@ extern "C" {
pub fn FixSession_logout(session: FixSession_t) -> i8;
#[must_use]
pub fn FixSession_isLoggedOn(session: FixSession_t) -> i8;
#[must_use]
pub fn FixSession_send(session: FixSession_t, msg: FixMessage_t) -> i8;
}
9 changes: 7 additions & 2 deletions quickfix/src/session.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::fmt;

use quickfix_ffi::{
FixSession_isLoggedOn, FixSession_logout, FixSession_lookup, FixSession_sendToTarget,
FixSession_t,
FixSession_isLoggedOn, FixSession_logout, FixSession_lookup, FixSession_send,
FixSession_sendToTarget, FixSession_t,
};

use crate::{
Expand Down Expand Up @@ -44,6 +44,11 @@ impl Session {
pub fn is_logged_on(&mut self) -> Result<bool, QuickFixError> {
ffi_code_to_bool(unsafe { FixSession_isLoggedOn(self.0) })
}

/// Send message using current session.
pub fn send(&mut self, msg: Message) -> Result<bool, QuickFixError> {
ffi_code_to_bool(unsafe { FixSession_send(self.0, msg.0) })
}
}

impl fmt::Debug for Session {
Expand Down
13 changes: 12 additions & 1 deletion quickfix/tests/test_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,18 @@ fn test_session_login_logout() -> Result<(), QuickFixError> {
}

// Lookup session
let mut session = unsafe { Session::lookup(&ServerType::Receiver.session_id()) }.unwrap();
let mut session = unsafe { Session::lookup(&ServerType::Sender.session_id()) }.unwrap();

// Send a message
assert_eq!(sender.user_msg_count(), MsgCounter { sent: 0, recv: 0 });
assert_eq!(receiver.user_msg_count(), MsgCounter { sent: 0, recv: 0 });

let news = build_news("Hello", &[])?;
assert!(session.send(news).unwrap());
thread::sleep(Duration::from_millis(50));

assert_eq!(sender.user_msg_count(), MsgCounter { sent: 1, recv: 0 });
assert_eq!(receiver.user_msg_count(), MsgCounter { sent: 0, recv: 1 });

// Play with session state
assert!(session.is_logged_on().unwrap());
Expand Down

0 comments on commit 9980466

Please sign in to comment.