-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
41 lines (37 loc) · 1.05 KB
/
utils.cpp
File metadata and controls
41 lines (37 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "utils.hpp"
#include <poll.h>
#include <errno.h>
#include <cstring>
#include "logger.cpp"
POOL_STATE waitForRead(int fd, uint64_t timeoutMs)
{
struct pollfd pfd;
pfd.fd = fd;
pfd.events = POLLIN;
pfd.revents = 0;
int ret = poll(&pfd, 1, timeoutMs);
if (ret < 0) {
if (errno == EINTR) return INTR;
LOG_ERROR("Poll error: " + std::string(strerror(errno)));
return FAIL;
} else if (ret > 0 && (pfd.revents & POLLIN)) {
return SUCCESS;
} else {
return TIMEOUT;
}
}
POOL_STATE waitForReadWithRetry(int fd, uint64_t timeoutMs, uint32_t maxRetries)
{
uint8_t retries = 0;
POOL_STATE pollState;
while (retries < maxRetries)
{
pollState = waitForRead(fd, timeoutMs);
if (pollState == SUCCESS) return SUCCESS;
if (pollState == INTR) continue;
if (pollState == FAIL) return FAIL;
retries++;
}
LOG_ERROR("[WAIT-FOR-READ-RETRY] All retry attempts exhausted. Total attempts: " + std::to_string(maxRetries));
return TIMEOUT;
}