add timeout in MOREP receive function
authorMazet Laurent <laurent.mazet@thalesgroup.com>
Fri, 6 Jun 2025 14:52:35 +0000 (16:52 +0200)
committerMazet Laurent <laurent.mazet@thalesgroup.com>
Fri, 6 Jun 2025 14:52:35 +0000 (16:52 +0200)
morep.c
morep.h

diff --git a/morep.c b/morep.c
index a009b61c42bc8469e689f4f0c2d3aa508bd21811..b86a6bd6b25be51411e0b7e3ce7471a3114fa2b8 100644 (file)
--- a/morep.c
+++ b/morep.c
@@ -18,6 +18,7 @@
 #include <netinet/in.h>
 #include <netinet/ip.h>
 #include <sys/ioctl.h>
+#include <sys/select.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 
@@ -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 d2fca44df748645082c4f606093180f06d273b31..0b883f83642f9461d93e72f62b116d33485590ef 100644 (file)
--- 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__ */