main: Fixup non-blocking reads
Sometimes, we can get the notification of a read that ends up returning
EAGAIN or EWOULDBLOCK. We need to handle this as a non-error case as
this is normal for non-blocking file descriptors.
We can also optimize the routine by using edge triggering and reading
out all available post codes until we get an EAGAIN.
Change-Id: I16585cf70620ae8cd54ef284a0fe357401957f42
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/main.cpp b/main.cpp
index daec6c4..1b65964 100644
--- a/main.cpp
+++ b/main.cpp
@@ -53,14 +53,8 @@
uint32_t revents, PostReporter* reporter)
{
uint64_t code = 0;
- ssize_t readb = read(postFd, &code, codeSize);
- if (readb < 0)
- {
- /* Read failure. */
- s.get_event().exit(1);
- return;
- }
- if (readb > 0)
+ ssize_t readb;
+ while ((readb = read(postFd, &code, codeSize)) > 0)
{
code = le64toh(code);
// HACK: Always send property changed signal even for the same code
@@ -68,7 +62,27 @@
// first value.
reporter->value(~code, true);
reporter->value(code);
+
+ // read depends on old data being cleared since it doens't always read
+ // the full code size
+ code = 0;
}
+
+ if (readb < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
+ {
+ return;
+ }
+
+ /* Read failure. */
+ if (readb == 0)
+ {
+ fprintf(stderr, "Unexpected EOF reading postcode\n");
+ }
+ else
+ {
+ fprintf(stderr, "Failed to read postcode: %s\n", strerror(errno));
+ }
+ s.get_event().exit(1);
}
/*
@@ -152,7 +166,7 @@
{
sdeventplus::Event event = sdeventplus::Event::get_default();
sdeventplus::source::IO reporterSource(
- event, postFd, EPOLLIN,
+ event, postFd, EPOLLIN | EPOLLET,
std::bind(PostCodeEventHandler, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3, &reporter));
// Enable bus to handle incoming IO and bus events