瀏覽代碼

LibCore: Add a Socket::set_idle() API that turns the notifiers on/off

When a socket's user doesn't need it to be active, but wants to keep it
open, the socket's notifiers should not be enabled to avoid hogging the
CPU with effectively useless notifications.
This API can be used to disable said notifiers until the user needs the
notifications.
Ali Mohammad Pur 3 年之前
父節點
當前提交
29acb7fcf8
共有 2 個文件被更改,包括 9 次插入0 次删除
  1. 8 0
      Userland/Libraries/LibCore/Socket.cpp
  2. 1 0
      Userland/Libraries/LibCore/Socket.h

+ 8 - 0
Userland/Libraries/LibCore/Socket.cpp

@@ -218,6 +218,14 @@ bool Socket::close()
     return IODevice::close();
 }
 
+void Socket::set_idle(bool idle)
+{
+    if (m_read_notifier)
+        m_read_notifier->set_enabled(!idle);
+    if (m_notifier)
+        m_notifier->set_enabled(!idle);
+}
+
 void Socket::ensure_read_notifier()
 {
     VERIFY(m_connected);

+ 1 - 0
Userland/Libraries/LibCore/Socket.h

@@ -43,6 +43,7 @@ public:
     int destination_port() const { return m_destination_port; }
 
     virtual bool close() override;
+    virtual void set_idle(bool);
 
     Function<void()> on_connected;
     Function<void()> on_error;