commit 8c0b135b85265185738e9066dc1f29b0c3f83364
parent e6b15db4f21b160d3f9c2a928471fad3a4f4d896
Author: Alexandre BIQUE <bique.alexandre@gmail.com>
Date: Fri, 30 Jul 2021 18:17:57 +0200
Implement poll based sync io
Diffstat:
2 files changed, 33 insertions(+), 3 deletions(-)
diff --git a/examples/io/remote-channel.cc b/examples/io/remote-channel.cc
@@ -1,5 +1,6 @@
#ifdef __unix__
# include <errno.h>
+# include <poll.h>
# include <unistd.h>
#endif
@@ -62,7 +63,7 @@ namespace clap {
auto nbytes = ::write(socket_, buffer.readData(), avail);
if (nbytes == -1) {
if (errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR) {
- evControl_.modifyFd(CLAP_FD_READ | CLAP_FD_WRITE);
+ modifyFd(CLAP_FD_READ | CLAP_FD_WRITE);
return;
}
@@ -78,7 +79,7 @@ namespace clap {
outputBuffers_.pop();
}
- evControl_.modifyFd(CLAP_FD_READ);
+ modifyFd(CLAP_FD_READ);
}
void RemoteChannel::close() {
@@ -149,5 +150,31 @@ namespace clap {
return true;
}
- void RemoteChannel::runOnce() {}
+ void RemoteChannel::modifyFd(clap_fd_flags flags)
+ {
+ if (flags == ioFlags_)
+ return;
+
+ ioFlags_ = flags;
+ evControl_.modifyFd(flags);
+ }
+
+ void RemoteChannel::runOnce() {
+#ifdef __unix__
+ pollfd pfd;
+ pfd.fd = socket_;
+ pfd.events = POLLIN | (ioFlags_ & CLAP_FD_WRITE ? POLLOUT : 0);
+ pfd.revents = 0;
+
+ int ret = ::poll(&pfd, 1, 0);
+ if (ret < 1)
+ // TODO error handling
+ return;
+
+ if (pfd.revents & POLLOUT)
+ onWrite();
+ if (pfd.revents & POLLIN)
+ onRead();
+#endif
+ }
} // namespace clap
\ No newline at end of file
diff --git a/examples/io/remote-channel.hh b/examples/io/remote-channel.hh
@@ -80,6 +80,8 @@ namespace clap {
void processInput();
+ void modifyFd(clap_fd_flags flags);
+
const bool cookieHalf_;
uint32_t nextCookie_ = 0;
@@ -87,6 +89,7 @@ namespace clap {
std::unordered_map<uint32_t /* cookie */, const MessageHandler &> syncHandlers_;
EventControl &evControl_;
clap_fd socket_;
+ clap_fd_flags ioFlags_ = 0;
ReadBuffer inputBuffer_;
std::queue<WriteBuffer> outputBuffers_;