daemon: use asio::defer() instead of asio::post() where appropriate

Change-Id: I24d0fab6499d6430dd500ca157daa87f48960d15
diff --git a/daemon/face/datagram-transport.hpp b/daemon/face/datagram-transport.hpp
index 51f06a0..cbef9ba 100644
--- a/daemon/face/datagram-transport.hpp
+++ b/daemon/face/datagram-transport.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2022,  Regents of the University of California,
+ * Copyright (c) 2014-2023,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -32,6 +32,8 @@
 
 #include <array>
 
+#include <boost/asio/defer.hpp>
+
 namespace nfd::face {
 
 struct Unicast {};
@@ -149,7 +151,7 @@
 
   // Ensure that the Transport stays alive at least until
   // all pending handlers are dispatched
-  getGlobalIoService().post([this] {
+  boost::asio::defer(getGlobalIoService(), [this] {
     this->setState(TransportState::CLOSED);
   });
 }
diff --git a/daemon/face/ethernet-transport.cpp b/daemon/face/ethernet-transport.cpp
index a1c23f2..a76b0b6 100644
--- a/daemon/face/ethernet-transport.cpp
+++ b/daemon/face/ethernet-transport.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2022,  Regents of the University of California,
+ * Copyright (c) 2014-2023,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -29,6 +29,7 @@
 
 #include <pcap/pcap.h>
 
+#include <boost/asio/defer.hpp>
 #include <boost/endian/conversion.hpp>
 
 namespace nfd::face {
@@ -87,7 +88,7 @@
 
   // Ensure that the Transport stays alive at least
   // until all pending handlers are dispatched
-  getGlobalIoService().post([this] {
+  boost::asio::defer(getGlobalIoService(), [this] {
     this->setState(TransportState::CLOSED);
   });
 }
diff --git a/daemon/face/internal-transport.cpp b/daemon/face/internal-transport.cpp
index 2eeab51..a61f227 100644
--- a/daemon/face/internal-transport.cpp
+++ b/daemon/face/internal-transport.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2022,  Regents of the University of California,
+ * Copyright (c) 2014-2023,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -26,6 +26,8 @@
 #include "internal-transport.hpp"
 #include "common/global.hpp"
 
+#include <boost/asio/post.hpp>
+
 namespace nfd::face {
 
 NFD_LOG_MEMBER_INIT(InternalForwarderTransport, InternalForwarderTransport);
@@ -47,7 +49,7 @@
 void
 InternalForwarderTransport::receivePacket(const Block& packet)
 {
-  getGlobalIoService().post([this, packet] {
+  boost::asio::post(getGlobalIoService(), [this, packet] {
     NFD_LOG_FACE_TRACE("Received: " << packet.size() << " bytes");
     receive(packet);
   });
@@ -105,7 +107,7 @@
 void
 InternalClientTransport::receivePacket(const Block& packet)
 {
-  getGlobalIoService().post([this, packet] {
+  boost::asio::post(getGlobalIoService(), [this, packet] {
     NFD_LOG_TRACE("Received: " << packet.size() << " bytes");
     if (m_receiveCallback) {
       m_receiveCallback(packet);
diff --git a/daemon/face/stream-transport.hpp b/daemon/face/stream-transport.hpp
index b6ad18b..c8b0ac3 100644
--- a/daemon/face/stream-transport.hpp
+++ b/daemon/face/stream-transport.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2022,  Regents of the University of California,
+ * Copyright (c) 2014-2023,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -32,6 +32,7 @@
 
 #include <queue>
 
+#include <boost/asio/defer.hpp>
 #include <boost/asio/write.hpp>
 
 namespace nfd::face {
@@ -149,7 +150,7 @@
 
   // Ensure that the Transport stays alive at least until
   // all pending handlers are dispatched
-  getGlobalIoService().post([this] { deferredClose(); });
+  boost::asio::defer(getGlobalIoService(), [this] { deferredClose(); });
 
   // Some bug or feature of Boost.Asio (see https://redmine.named-data.net/issues/1856):
   //
diff --git a/daemon/face/tcp-transport.cpp b/daemon/face/tcp-transport.cpp
index 45bc952..15c0769 100644
--- a/daemon/face/tcp-transport.cpp
+++ b/daemon/face/tcp-transport.cpp
@@ -26,6 +26,8 @@
 #include "tcp-transport.hpp"
 #include "common/global.hpp"
 
+#include <boost/asio/defer.hpp>
+
 #if defined(__linux__)
 #include <linux/sockios.h>
 #include <sys/ioctl.h>
@@ -105,7 +107,7 @@
     m_socket.cancel(ec);
 
     // do this asynchronously because there could be some callbacks still pending
-    getGlobalIoService().post([this] { reconnect(); });
+    boost::asio::defer(getGlobalIoService(), [this] { reconnect(); });
   }
   else {
     StreamTransport::handleError(error);
@@ -175,7 +177,7 @@
                MAX_RECONNECT_DELAY);
 
   // do this asynchronously because there could be some callbacks still pending
-  getGlobalIoService().post([this] { reconnect(); });
+  boost::asio::defer(getGlobalIoService(), [this] { reconnect(); });
 }
 
 void
diff --git a/daemon/fw/face-table.cpp b/daemon/fw/face-table.cpp
index fe3a6ab..9a2fe4c 100644
--- a/daemon/fw/face-table.cpp
+++ b/daemon/fw/face-table.cpp
@@ -28,6 +28,8 @@
 #include "common/logger.hpp"
 #include "face/channel.hpp"
 
+#include <boost/asio/defer.hpp>
+
 namespace nfd {
 
 NFD_LOG_INIT(FaceTable);
@@ -100,7 +102,7 @@
                " local=" << face->getLocalUri());
 
   // defer Face deallocation, so that Transport isn't deallocated during afterStateChange signal
-  getGlobalIoService().post([face] {});
+  boost::asio::defer(getGlobalIoService(), [face] {});
 }
 
 FaceTable::ForwardRange
diff --git a/daemon/mgmt/rib-manager.cpp b/daemon/mgmt/rib-manager.cpp
index 4943672..7149680 100644
--- a/daemon/mgmt/rib-manager.cpp
+++ b/daemon/mgmt/rib-manager.cpp
@@ -30,6 +30,8 @@
 #include "rib/rib.hpp"
 #include "table/fib.hpp"
 
+#include <boost/asio/defer.hpp>
+
 #include <ndn-cxx/lp/tags.hpp>
 #include <ndn-cxx/mgmt/nfd/rib-entry.hpp>
 #include <ndn-cxx/mgmt/nfd/status-dataset.hpp>
@@ -446,11 +448,12 @@
 {
   NFD_LOG_DEBUG("Checking for invalid face registrations");
 
-  std::set<uint64_t> activeFaceIds;
+  std::set<uint64_t> activeIds;
   for (const auto& faceStatus : activeFaces) {
-    activeFaceIds.insert(faceStatus.getFaceId());
+    activeIds.insert(faceStatus.getFaceId());
   }
-  getGlobalIoService().post([=] { m_rib.beginRemoveFailedFaces(activeFaceIds); });
+  boost::asio::defer(getGlobalIoService(),
+                     [this, active = std::move(activeIds)] { m_rib.beginRemoveFailedFaces(active); });
 
   // Reschedule the check for future clean up
   scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
@@ -463,7 +466,8 @@
 
   if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
     NFD_LOG_DEBUG("Received notification for destroyed FaceId " << notification.getFaceId());
-    getGlobalIoService().post([this, id = notification.getFaceId()] { m_rib.beginRemoveFace(id); });
+    boost::asio::defer(getGlobalIoService(),
+                       [this, id = notification.getFaceId()] { m_rib.beginRemoveFace(id); });
   }
 }
 
diff --git a/tests/daemon/face/tcp-channel-fixture.hpp b/tests/daemon/face/tcp-channel-fixture.hpp
index b6eb99c..557eeb8 100644
--- a/tests/daemon/face/tcp-channel-fixture.hpp
+++ b/tests/daemon/face/tcp-channel-fixture.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2022,  Regents of the University of California,
+ * Copyright (c) 2014-2023,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -31,6 +31,8 @@
 
 #include "channel-fixture.hpp"
 
+#include <boost/asio/defer.hpp>
+
 namespace nfd::tests {
 
 using face::TcpChannel;
@@ -57,7 +59,7 @@
   void
   connect(TcpChannel& channel) final
   {
-    g_io.post([&] {
+    boost::asio::defer(g_io, [&] {
       channel.connect(listenerEp, {},
         [this] (const shared_ptr<Face>& newFace) {
           BOOST_REQUIRE(newFace != nullptr);
diff --git a/tests/daemon/face/udp-channel-fixture.hpp b/tests/daemon/face/udp-channel-fixture.hpp
index a314a8c..63caf9a 100644
--- a/tests/daemon/face/udp-channel-fixture.hpp
+++ b/tests/daemon/face/udp-channel-fixture.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2022,  Regents of the University of California,
+ * Copyright (c) 2014-2023,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -31,6 +31,8 @@
 
 #include "channel-fixture.hpp"
 
+#include <boost/asio/defer.hpp>
+
 namespace nfd::tests {
 
 using face::UdpChannel;
@@ -52,7 +54,7 @@
   void
   connect(UdpChannel& channel) final
   {
-    g_io.post([&] {
+    boost::asio::defer(g_io, [&] {
       channel.connect(listenerEp, {},
         [this] (const shared_ptr<Face>& newFace) {
           BOOST_REQUIRE(newFace != nullptr);
diff --git a/tests/daemon/mgmt/rib-manager.t.cpp b/tests/daemon/mgmt/rib-manager.t.cpp
index 8ae96ae..0b407f6 100644
--- a/tests/daemon/mgmt/rib-manager.t.cpp
+++ b/tests/daemon/mgmt/rib-manager.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2022,  Regents of the University of California,
+ * Copyright (c) 2014-2023,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -185,7 +185,7 @@
       data->setContent(resp.wireEncode());
       m_keyChain.sign(*data, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_SHA256));
 
-      m_face.getIoService().post([this, data] { m_face.receive(*data); });
+      boost::asio::post(m_face.getIoService(), [this, data] { m_face.receive(*data); });
     };
 
     const Name commandPrefix("/localhost/nfd/fib/add-nexthop");
diff --git a/tests/daemon/rib/fib-updates-common.hpp b/tests/daemon/rib/fib-updates-common.hpp
index 33b549d..15e623f 100644
--- a/tests/daemon/rib/fib-updates-common.hpp
+++ b/tests/daemon/rib/fib-updates-common.hpp
@@ -33,6 +33,8 @@
 #include "tests/daemon/global-io-fixture.hpp"
 #include "tests/daemon/rib/create-route.hpp"
 
+#include <boost/asio/defer.hpp>
+
 #include <ndn-cxx/util/dummy-client-face.hpp>
 
 namespace nfd::tests {
@@ -80,7 +82,7 @@
              uint32_t nTimeouts)
   {
     updates.push_back(update);
-    getGlobalIoService().post([=] {
+    boost::asio::defer(getGlobalIoService(), [=] {
       if (mockSuccess) {
         onUpdateSuccess(update, onSuccess, onFailure);
       }
diff --git a/tests/tools/mock-nfd-mgmt-fixture.hpp b/tests/tools/mock-nfd-mgmt-fixture.hpp
index ede5568..eed9846 100644
--- a/tests/tools/mock-nfd-mgmt-fixture.hpp
+++ b/tests/tools/mock-nfd-mgmt-fixture.hpp
@@ -34,6 +34,7 @@
 #include <ndn-cxx/mgmt/nfd/control-response.hpp>
 #include <ndn-cxx/util/dummy-client-face.hpp>
 
+#include <boost/asio/defer.hpp>
 #include <boost/concept/assert.hpp>
 
 namespace nfd::tests {
@@ -52,7 +53,7 @@
   {
     face.onSendInterest.connect([this] (const Interest& interest) {
       if (processInterest) {
-        m_io.post([=] { processInterest(interest); });
+        boost::asio::defer(m_io, [=] { processInterest(interest); });
       }
     });
   }