From a70adcc2811840f5ba6debe447ccd956897b8c46 Mon Sep 17 00:00:00 2001 From: Arthur LE MOIGNE Date: Mon, 13 Nov 2023 15:41:08 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Expose=20more=20function=20from=20S?= =?UTF-8?q?ocketAcceptor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- quickfix-bind/include/quickfix_bind.h | 7 +++ quickfix-bind/src/quickfix_bind.cpp | 69 +++++++++++++++++++++++++-- quickfix-ffi/src/lib.rs | 8 ++++ quickfix/src/socket_acceptor.rs | 36 +++++++++++++- 4 files changed, 114 insertions(+), 6 deletions(-) diff --git a/quickfix-bind/include/quickfix_bind.h b/quickfix-bind/include/quickfix_bind.h index e7ec9d8..2d28f1b 100644 --- a/quickfix-bind/include/quickfix_bind.h +++ b/quickfix-bind/include/quickfix_bind.h @@ -3,6 +3,9 @@ #include +#define ERRNO_INVAL -1 +#define ERRNO_EXCEPTION -2 + #ifdef __cplusplus extern "C" { @@ -42,7 +45,11 @@ extern "C" FixSocketAcceptor_t *FixSocketAcceptor_new(const FixApplication_t *application, const FixFileStoreFactory_t *storeFactory, const FixSessionSettings_t *settings, const FixFileLogFactory_t *logFactory); int FixSocketAcceptor_start(const FixSocketAcceptor_t *obj); + int FixSocketAcceptor_block(const FixSocketAcceptor_t *obj); + int FixSocketAcceptor_poll(const FixSocketAcceptor_t *obj); int FixSocketAcceptor_stop(const FixSocketAcceptor_t *obj); + int FixSocketAcceptor_isLoggedOn(const FixSocketAcceptor_t *obj); + int FixSocketAcceptor_isStopped(const FixSocketAcceptor_t *obj); void FixSocketAcceptor_delete(FixSocketAcceptor_t *obj); const char *FixSessionID_getBeginString(const FixSessionID_t *session); diff --git a/quickfix-bind/src/quickfix_bind.cpp b/quickfix-bind/src/quickfix_bind.cpp index c3dd93b..50f01d5 100644 --- a/quickfix-bind/src/quickfix_bind.cpp +++ b/quickfix-bind/src/quickfix_bind.cpp @@ -230,7 +230,7 @@ extern "C" int FixSocketAcceptor_start(const FixSocketAcceptor_t *obj) { - RETURN_VAL_IF_NULL(obj, -1); + RETURN_VAL_IF_NULL(obj, ERRNO_INVAL); auto fix_obj = (FIX::SocketAcceptor *)(obj); try @@ -239,14 +239,45 @@ extern "C" } catch (std::exception &ex) { - return -1; + return ERRNO_EXCEPTION; } return 0; } + int FixSocketAcceptor_block(const FixSocketAcceptor_t *obj) + { + RETURN_VAL_IF_NULL(obj, ERRNO_INVAL); + + auto fix_obj = (FIX::SocketAcceptor *)(obj); + try + { + fix_obj->block(); + } + catch (std::exception &ex) + { + return ERRNO_EXCEPTION; + } + return 0; + } + + int FixSocketAcceptor_poll(const FixSocketAcceptor_t *obj) + { + RETURN_VAL_IF_NULL(obj, ERRNO_INVAL); + + auto fix_obj = (FIX::SocketAcceptor *)(obj); + try + { + return fix_obj->poll() ? 1 : 0; + } + catch (std::exception &ex) + { + return ERRNO_EXCEPTION; + } + } + int FixSocketAcceptor_stop(const FixSocketAcceptor_t *obj) { - RETURN_VAL_IF_NULL(obj, -1); + RETURN_VAL_IF_NULL(obj, ERRNO_INVAL); auto fix_obj = (FIX::SocketAcceptor *)(obj); try @@ -255,11 +286,41 @@ extern "C" } catch (std::exception &ex) { - return -1; + return ERRNO_EXCEPTION; } return 0; } + int FixSocketAcceptor_isLoggedOn(const FixSocketAcceptor_t *obj) + { + RETURN_VAL_IF_NULL(obj, ERRNO_INVAL); + + auto fix_obj = (FIX::SocketAcceptor *)(obj); + try + { + return fix_obj->isLoggedOn() ? 1 : 0; + } + catch (std::exception &ex) + { + return ERRNO_EXCEPTION; + } + } + + int FixSocketAcceptor_isStopped(const FixSocketAcceptor_t *obj) + { + RETURN_VAL_IF_NULL(obj, ERRNO_INVAL); + + auto fix_obj = (FIX::SocketAcceptor *)(obj); + try + { + return fix_obj->isStopped() ? 1 : 0; + } + catch (std::exception &ex) + { + return ERRNO_EXCEPTION; + } + } + void FixSocketAcceptor_delete(FixSocketAcceptor_t *obj) { RETURN_IF_NULL(obj); diff --git a/quickfix-ffi/src/lib.rs b/quickfix-ffi/src/lib.rs index e086b08..2a7b524 100644 --- a/quickfix-ffi/src/lib.rs +++ b/quickfix-ffi/src/lib.rs @@ -73,7 +73,15 @@ extern "C" { #[must_use] pub fn FixSocketAcceptor_start(obj: FixSocketAcceptor_t) -> ffi::c_int; #[must_use] + pub fn FixSocketAcceptor_block(obj: FixSocketAcceptor_t) -> ffi::c_int; + #[must_use] + pub fn FixSocketAcceptor_poll(obj: FixSocketAcceptor_t) -> ffi::c_int; + #[must_use] pub fn FixSocketAcceptor_stop(obj: FixSocketAcceptor_t) -> ffi::c_int; + #[must_use] + pub fn FixSocketAcceptor_isLoggedOn(obj: FixSocketAcceptor_t) -> ffi::c_int; + #[must_use] + pub fn FixSocketAcceptor_isStopped(obj: FixSocketAcceptor_t) -> ffi::c_int; pub fn FixSocketAcceptor_delete(obj: FixSocketAcceptor_t); pub fn FixSessionID_getBeginString(obj: FixSessionID_t) -> Option>; diff --git a/quickfix/src/socket_acceptor.rs b/quickfix/src/socket_acceptor.rs index a82789a..68950a5 100644 --- a/quickfix/src/socket_acceptor.rs +++ b/quickfix/src/socket_acceptor.rs @@ -1,8 +1,9 @@ use std::marker::PhantomData; use quickfix_ffi::{ - FixSocketAcceptor_delete, FixSocketAcceptor_new, FixSocketAcceptor_start, - FixSocketAcceptor_stop, FixSocketAcceptor_t, + FixSocketAcceptor_block, FixSocketAcceptor_delete, FixSocketAcceptor_isLoggedOn, + FixSocketAcceptor_isStopped, FixSocketAcceptor_new, FixSocketAcceptor_poll, + FixSocketAcceptor_start, FixSocketAcceptor_stop, FixSocketAcceptor_t, }; use crate::{ @@ -46,12 +47,43 @@ impl<'a, C: ApplicationCallback> SocketAcceptor<'a, C> { } } + pub fn block(&self) -> Result<(), QuickFixError> { + match unsafe { FixSocketAcceptor_block(self.inner) } { + 0 => Ok(()), + code => Err(QuickFixError::InvalidFunctionReturnCode(code)), + } + } + + pub fn poll(&self) -> Result { + match unsafe { FixSocketAcceptor_poll(self.inner) } { + 1 => Ok(true), + 0 => Ok(false), + code => Err(QuickFixError::InvalidFunctionReturnCode(code)), + } + } + pub fn stop(&self) -> Result<(), QuickFixError> { match unsafe { FixSocketAcceptor_stop(self.inner) } { 0 => Ok(()), code => Err(QuickFixError::InvalidFunctionReturnCode(code)), } } + + pub fn is_logged_on(&self) -> Result { + match unsafe { FixSocketAcceptor_isLoggedOn(self.inner) } { + 1 => Ok(true), + 0 => Ok(false), + code => Err(QuickFixError::InvalidFunctionReturnCode(code)), + } + } + + pub fn is_stopped(&self) -> Result { + match unsafe { FixSocketAcceptor_isStopped(self.inner) } { + 1 => Ok(true), + 0 => Ok(false), + code => Err(QuickFixError::InvalidFunctionReturnCode(code)), + } + } } impl Drop for SocketAcceptor<'_, C> {