Merge "[webrtc] React to input socket disconnecting" am: 0d513659b3
Change-Id: I66a90bc332e60c90caa402026c8065feb20f9a88
diff --git a/host/frontend/gcastv2/libsource/InputSink.cpp b/host/frontend/gcastv2/libsource/InputSink.cpp
index e673358..ce9597d 100644
--- a/host/frontend/gcastv2/libsource/InputSink.cpp
+++ b/host/frontend/gcastv2/libsource/InputSink.cpp
@@ -123,6 +123,8 @@
makeFdNonblocking(s);
mClientFd = s;
+ mRunLoop->postSocketRecv(
+ mClientFd, makeSafeCallback(this, &InputSink::onSocketRecv));
}
}
@@ -151,6 +153,27 @@
}
}
+void InputSink::onSocketRecv() {
+ if (mClientFd < 0) return;
+
+ char buff[512];
+ auto n = recv(mClientFd, buff, sizeof(buff), 0 /* flags */);
+ if (n > 0) {
+ LOG(INFO) << "Discarding " << n << " bytes received from the input device.";
+ mRunLoop->postSocketRecv(
+ mClientFd, makeSafeCallback(this, &InputSink::onSocketRecv));
+ } else {
+ // Client disconnected
+ if (n < 0) {
+ auto errno_save = errno;
+ LOG(ERROR) << "Error receiving from socket: " << strerror(errno_save);
+ }
+ mRunLoop->cancelSocket(mClientFd);
+ close(mClientFd);
+ mClientFd = -1;
+ }
+}
+
void InputSink::onSocketSend() {
std::lock_guard autoLock(mLock);
diff --git a/host/frontend/gcastv2/libsource/include/source/InputSink.h b/host/frontend/gcastv2/libsource/include/source/InputSink.h
index ba5af91..38af13b 100644
--- a/host/frontend/gcastv2/libsource/include/source/InputSink.h
+++ b/host/frontend/gcastv2/libsource/include/source/InputSink.h
@@ -54,6 +54,7 @@
bool mWriteVirtioInput;
void onServerConnection();
+ void onSocketRecv();
void onSocketSend();
void sendRawEvents(const void* evt_buffer, size_t length);