UDPStreamer: poll data semaphore first, then non-blocking command poll
Reorder Execute() main stage so the background thread waits on dataSem (sleeping until the RT thread posts) before doing the socket select(). The socket poll is now non-blocking (timeout=0) since command latency bounded by UDPS_DATA_WAIT_MS is acceptable for CONNECT/DISCONNECT. Also force-remove old udpstreamer-webui binary in run.sh before rebuild so stale embedded assets are never served. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -57,8 +57,11 @@ static const uint32 UDPS_DEFAULT_MAX_PAYLOAD = 1400u;
|
||||
/** Minimum MaxPayloadSize: header + at least 1 byte of payload. */
|
||||
static const uint32 UDPS_MIN_PAYLOAD = static_cast<uint32>(sizeof(UDPSPacketHeader)) + 1u;
|
||||
|
||||
/** Server socket receive timeout in milliseconds. */
|
||||
static const uint32 UDPS_RECV_TIMEOUT_MS = 5u;
|
||||
/** Server socket receive timeout in milliseconds.
|
||||
* Set to 0 for a pure non-blocking poll so the send loop can keep pace with
|
||||
* the RT thread regardless of its frequency. Client commands (CONNECT /
|
||||
* DISCONNECT) are still caught on the very next loop iteration. */
|
||||
static const uint32 UDPS_RECV_TIMEOUT_MS = 0u;
|
||||
|
||||
/** EventSem wait timeout in milliseconds for the data loop. */
|
||||
static const uint32 UDPS_DATA_WAIT_MS = 10u;
|
||||
@@ -615,18 +618,24 @@ ErrorManagement::ErrorType UDPStreamer::Execute(ExecutionInfo &info) {
|
||||
}
|
||||
|
||||
if (info.GetStage() == ExecutionInfo::MainStage) {
|
||||
/* --- Poll server socket for incoming client commands ---
|
||||
* Use select() directly so we get a silent timeout with no log spam.
|
||||
* MARTe2's Read(buf, size, timeout) calls recvfrom() and logs an error
|
||||
* on every timeout (EAGAIN from SO_RCVTIMEO). */
|
||||
/* --- Wait for RT thread to post new data ---
|
||||
* ResetWait sleeps the background thread until the RT thread calls
|
||||
* Synchronise() and posts dataSem, or until the timeout expires.
|
||||
* Doing this FIRST means the thread spends nearly all its time here
|
||||
* instead of spinning on the non-blocking select() below.
|
||||
* Command latency is bounded by UDPS_DATA_WAIT_MS (acceptable for
|
||||
* CONNECT / DISCONNECT). */
|
||||
ErrorManagement::ErrorType waitErr =
|
||||
dataSem.ResetWait(TimeoutType(UDPS_DATA_WAIT_MS));
|
||||
bool dataReady = (waitErr == ErrorManagement::NoError);
|
||||
|
||||
/* --- Poll server socket for incoming client commands (non-blocking) --- */
|
||||
uint8 cmdBuf[256u];
|
||||
Handle sockFd = serverSocket.GetReadHandle();
|
||||
fd_set rfds;
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(static_cast<int>(sockFd), &rfds);
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = static_cast<long>(UDPS_RECV_TIMEOUT_MS) * 1000L;
|
||||
struct timeval tv = { 0, 0 }; /* non-blocking poll */
|
||||
int nReady = select(static_cast<int>(sockFd) + 1, &rfds, NULL_PTR(fd_set *), NULL_PTR(fd_set *), &tv);
|
||||
if (nReady > 0) {
|
||||
uint32 recvSize = static_cast<uint32>(sizeof(cmdBuf));
|
||||
@@ -636,12 +645,6 @@ ErrorManagement::ErrorType UDPStreamer::Execute(ExecutionInfo &info) {
|
||||
}
|
||||
}
|
||||
|
||||
/* --- Wait for RT thread to post new data --- */
|
||||
/* ResetWait atomically lowers the barrier then waits, preventing missed posts */
|
||||
ErrorManagement::ErrorType waitErr =
|
||||
dataSem.ResetWait(TimeoutType(UDPS_DATA_WAIT_MS));
|
||||
bool dataReady = (waitErr == ErrorManagement::NoError);
|
||||
|
||||
if (dataReady && clientConnected) {
|
||||
/* Copy readyBuffer → scratchBuffer under brief spinlock */
|
||||
uint64 ts = 0u;
|
||||
|
||||
@@ -61,7 +61,7 @@ WEBUI_DIR="${REPO_ROOT}/Client/WebUI"
|
||||
WEBUI_BIN="${WEBUI_DIR}/udpstreamer-webui"
|
||||
if [ "${START_WEBUI}" -eq 1 ] && [ ! -x "${WEBUI_BIN}" ]; then
|
||||
echo "==> Building WebUI..."
|
||||
(cd "${WEBUI_DIR}" && go build -o udpstreamer-webui ./...)
|
||||
(cd "${WEBUI_DIR}" && rm udpstreamer-webui && go build -o udpstreamer-webui ./...)
|
||||
echo "==> WebUI build done."
|
||||
fi
|
||||
|
||||
|
||||
Reference in New Issue
Block a user