transport: Serialize socket write operations

Change-Id: Ieccaf0ccbd6ee8bd08b1eee77d781edd47b2cbb2
Refs: #1769, #1775
diff --git a/src/face.cpp b/src/face.cpp
index 6f13570..28482ba 100644
--- a/src/face.cpp
+++ b/src/face.cpp
@@ -165,19 +165,18 @@
 void
 Face::put(const Data& data)
 {
-  if (!m_transport->isConnected())
-    m_transport->connect(*m_ioService,
-                         bind(&Face::onReceiveElement, this, _1));
+  shared_ptr<const Data> dataPtr;
+  try {
+    dataPtr = data.shared_from_this();
+  }
+  catch (const bad_weak_ptr& e) {
+    std::cerr << "Face::put WARNING: the supplied Data should be created using make_shared<Data>()"
+              << std::endl;
+    dataPtr = make_shared<Data>(data);
+  }
 
-  if (!data.getLocalControlHeader().empty(false, true))
-    {
-      m_transport->send(data.getLocalControlHeader().wireEncode(data, false, true),
-                        data.wireEncode());
-    }
-  else
-    {
-      m_transport->send(data.wireEncode());
-    }
+  // If the same ioService thread, dispatch directly calls the method
+  m_ioService->dispatch(bind(&Impl::asyncPutData, m_impl, dataPtr));
 }
 
 void
@@ -186,8 +185,6 @@
   m_ioService->post(bind(&Impl::asyncRemovePendingInterest, m_impl, pendingInterestId));
 }
 
-
-
 const RegisteredPrefixId*
 Face::setInterestFilter(const InterestFilter& interestFilter,
                         const OnInterest& onInterest,