bus: utility to process messages in infinite loop

This change introduces a simple function for synchronous DBus servers to
start listening to incoming requests and run forever.

This can avoid duplicate while loops in server codes across repos.

Please see the following change for context.
[1] https://gerrit.openbmc.org/c/openbmc/phosphor-user-manager/+/56843

Tested: the calculator server unit test passed.

Signed-off-by: Nan Zhou <nanzhoumails@gmail.com>
Change-Id: I1b6485869527e938c429c035a0551ec4dbb36c99
diff --git a/example/calculator-server.cpp b/example/calculator-server.cpp
index c31fc19..e6959b3 100644
--- a/example/calculator-server.cpp
+++ b/example/calculator-server.cpp
@@ -71,11 +71,7 @@
     Calculator c1{b, path};
 
     // Handle dbus processing forever.
-    while (1)
-    {
-        b.process_discard(); // discard any unhandled messages
-        b.wait();
-    }
+    b.process_loop();
 
     return 0;
 }
diff --git a/include/sdbusplus/bus.hpp b/include/sdbusplus/bus.hpp
index ab80e85..a2b3fb9 100644
--- a/include/sdbusplus/bus.hpp
+++ b/include/sdbusplus/bus.hpp
@@ -197,6 +197,18 @@
         return r > 0;
     }
 
+    /** @brief Process waiting dbus messages or signals forever, discarding
+     * unhandled.
+     */
+    void process_loop()
+    {
+        while (true)
+        {
+            process_discard();
+            wait();
+        }
+    }
+
     /** @brief Claim a service name on the dbus.
      *
      *  @param[in] service - The service name to claim.