Skip to content

Commit

Permalink
Set a timeout for blocking UDP send
Browse files Browse the repository at this point in the history
  • Loading branch information
dkulp committed Dec 6, 2024
1 parent 46655bc commit 02d5935
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/channeloutput/UDPOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,13 +449,18 @@ int UDPOutput::SendMessages(unsigned int socketKey, SendSocketInfo* socketInfo,

int outputCount = 0;
if (blockingOutput) {
int errorCount = 0;
for (int x = 0; x < msgCount; x++) {
ssize_t s = sendmsg(sendSocket, &msgs[x].msg_hdr, 0);
if (s != -1) {
errorCount = 0;
++outputCount;
} else if (errorCount) {
return outputCount;
} else {
// didn't send, we'll yield and re-send
// didn't send, we'll yield once and re-send
--x;
++errorCount;
std::this_thread::yield();
}
}
Expand Down Expand Up @@ -548,8 +553,8 @@ void UDPOutput::BackgroundOutputWork() {
i.socketInfo->errCount++;

// failed to send all messages or it took more than 100ms to send them
LogErr(VB_CHANNELOUT, "sendmmsg() failed for UDP output (key: %X output count: %d/%d time: %u ms errCount: %d) with error: %d %s\n",
i.id,
LogErr(VB_CHANNELOUT, "%s() failed for UDP output (key: %X output count: %d/%d time: %u ms errCount: %d) with error: %d %s\n",
blockingOutput ? "sendmsg" : "sendmmsg", i.id,
outputCount, i.msgs.size(), diff, i.socketInfo->errCount,
errno,
strerror(errno));
Expand Down Expand Up @@ -788,6 +793,13 @@ int UDPOutput::createSocket(int port, bool broadCast) {
bufSize = 4096;
setsockopt(sendSocket, SOL_SOCKET, SO_RCVBUF, &bufSize, sizeof(bufSize));

if (blockingOutput) {
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 1000; // 1ms timeout
setsockopt(sendSocket, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof timeout);
}

if (broadCast) {
int broadcast = 1;
if (setsockopt(sendSocket, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) < 0) {
Expand Down

0 comments on commit 02d5935

Please sign in to comment.