From afb84524d722e2d5801fbe98bd13a42c8f094f53 Mon Sep 17 00:00:00 2001 From: Mazet Laurent Date: Fri, 6 Jun 2025 16:52:35 +0200 Subject: [PATCH] add timeout in MOREP receive function --- morep.c | 24 ++++++++++++++++++++++++ morep.h | 21 +++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/morep.c b/morep.c index a009b61..b86a6bd 100644 --- a/morep.c +++ b/morep.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -525,4 +526,27 @@ int MOREP_Close (int index) return 0; } +int MOREP_Receive_timeout (int index, uint8_t *msgtype, uint8_t *buffer, int *len, int ms) +{ + + VERBOSE (morep, TRACE, PRINTF ("MOREP_Receive_timeout\n")); + + if (MOREP_list[index]->used) { + return -1; + } + int fid = MOREP_list[index]->sock; + + fd_set rfds; + struct timeval tv = { 0, ms }; + FD_ZERO (&rfds); + FD_SET (fid, &rfds); + + int retval = select (fid + 1, &rfds, NULL, NULL, &tv); + if (retval != 1) { + return retval; + } + + return MOREP_Receive (index, msgtype, buffer, len); +} + /* vim: set ts=4 sw=4 si et: */ diff --git a/morep.h b/morep.h index d2fca44..0b883f8 100644 --- a/morep.h +++ b/morep.h @@ -96,6 +96,27 @@ int MOREP_Send (int index, uint8_t msgtype, uint8_t *buffer, int len); int MOREP_Receive (int index, uint8_t *msgtype, uint8_t *buffer, int *len); +/** + @ingroup MOREP + + Received a message over an existing MOREP connection with a timeout. + + @param index MOREP descriptor retuned by MOREP_Connect() + @param msgtype a pointer to retrieve the user defined message type + identifier, transported transparently + @param buffer a pointer to the reception buffer (pre-allocated) + @param len a pointer to retrieve the length of the received message + @param ms exit after x microseconds if no data is received + + Be carreful that recevied message can be as large as 16 * (ETHERSIZE - 4) - 1 bytes + + @return sequence number of the received message + + @see MOREP_Connect() +*/ + +int MOREP_Receive_timeout (int index, uint8_t *msgtype, uint8_t *buffer, int *len, int ms); + __END_DECLS #endif /* __MOREP_H__ */ -- 2.30.2