face: Remove reliance on exceptions during TLV decoding

Change-Id: I8e96cc9369a5702805d3856ce70dfbe64834e1df
Refs: #1379
diff --git a/daemon/face/datagram-face.hpp b/daemon/face/datagram-face.hpp
index 8050164..cbda459 100644
--- a/daemon/face/datagram-face.hpp
+++ b/daemon/face/datagram-face.hpp
@@ -233,37 +233,37 @@
                 << ",endpoint:" << m_socket->local_endpoint()
                 << "] Received: " << nBytesReceived << " bytes");
 
-  /// @todo Eliminate reliance on exceptions in this path
-  try {
-    Block element(buffer, nBytesReceived);
+  Block element;
+  bool isOk = Block::fromBuffer(buffer, nBytesReceived, element);
+  if (!isOk)
+    {
+      NFD_LOG_WARN("[id:" << this->getId()
+                   << ",endpoint:" << m_socket->local_endpoint()
+                   << "] Failed to parse incoming packet");
+      // This message won't extend the face lifetime
+      return;
+    }
 
-    if (element.size() != nBytesReceived)
-      {
-        NFD_LOG_WARN("[id:" << this->getId()
-                     << ",endpoint:" << m_socket->local_endpoint()
-                     << "] Received datagram size and decoded "
-                     << "element size don't match");
-        // This message won't extend the face lifetime
-        return;
-      }
-    if (!this->decodeAndDispatchInput(element))
-      {
-        NFD_LOG_WARN("[id:" << this->getId()
-                     << ",endpoint:" << m_socket->local_endpoint()
-                     << "] Received unrecognized block of type ["
-                     << element.type() << "]");
-        // ignore unknown packet and proceed
-        // This message won't extend the face lifetime
-        return;
-      }
-  }
-  catch(const tlv::Error& e) {
-    NFD_LOG_WARN("[id:" << this->getId()
-                 << ",endpoint:" << m_socket->local_endpoint()
-                 << "] Received input is invalid");
-    // This message won't extend the face lifetime
-    return;
-  }
+  if (element.size() != nBytesReceived)
+    {
+      NFD_LOG_WARN("[id:" << this->getId()
+                   << ",endpoint:" << m_socket->local_endpoint()
+                   << "] Received datagram size and decoded "
+                   << "element size don't match");
+      // This message won't extend the face lifetime
+      return;
+    }
+
+  if (!this->decodeAndDispatchInput(element))
+    {
+      NFD_LOG_WARN("[id:" << this->getId()
+                   << ",endpoint:" << m_socket->local_endpoint()
+                   << "] Received unrecognized block of type ["
+                   << element.type() << "]");
+      // This message won't extend the face lifetime
+      return;
+    }
+
   m_hasBeenUsedRecently = true;
 }
 
diff --git a/daemon/face/ethernet-face.cpp b/daemon/face/ethernet-face.cpp
index 7d87c0f..e619724 100644
--- a/daemon/face/ethernet-face.cpp
+++ b/daemon/face/ethernet-face.cpp
@@ -213,22 +213,24 @@
       NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName
                     << "] Received: " << length << " bytes");
 
-      /// \todo Eliminate reliance on exceptions in this path
-      try {
-        /// \todo Reserve space in front and at the back
-        ///       of the underlying buffer
-        ndn::Block element(packet, length);
-        if (!decodeAndDispatchInput(element))
-          {
-            NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
-                         << "] Received unrecognized block of type " << element.type());
-            // ignore unknown packet
-          }
-      }
-      catch (const tlv::Error&) {
-        NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
-                     << "] Received block is invalid or too large to process");
-      }
+      /// \todo Reserve space in front and at the back
+      ///       of the underlying buffer
+      Block element;
+      bool isOk = Block::fromBuffer(packet, length, element);
+      if (isOk)
+        {
+          if (!decodeAndDispatchInput(element))
+            {
+              NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
+                           << "] Received unrecognized block of type " << element.type());
+              // ignore unknown packet
+            }
+        }
+      else
+        {
+          NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
+                       << "] Received block is invalid or too large to process");
+        }
     }
 
   m_socket->async_read_some(boost::asio::null_buffers(),
diff --git a/daemon/face/stream-face.hpp b/daemon/face/stream-face.hpp
index f6c008a..a1aa7c1 100644
--- a/daemon/face/stream-face.hpp
+++ b/daemon/face/stream-face.hpp
@@ -279,38 +279,40 @@
   // do magic
 
   std::size_t offset = 0;
-  /// @todo Eliminate reliance on exceptions in this path
-  try {
-    while(m_inputBufferSize - offset > 0)
-      {
-        Block element(m_inputBuffer + offset, m_inputBufferSize - offset);
-        offset += element.size();
 
-        BOOST_ASSERT(offset <= m_inputBufferSize);
+  bool isOk = true;
+  Block element;
+  while(m_inputBufferSize - offset > 0)
+    {
+      isOk = Block::fromBuffer(m_inputBuffer + offset, m_inputBufferSize - offset, element);
+      if (!isOk)
+        break;
 
-        if (!this->decodeAndDispatchInput(element))
-          {
-            NFD_LOG_WARN("[id:" << this->getId()
-                         << ",endpoint:" << m_socket->local_endpoint()
-                         << "] Received unrecognized block of type ["
-                         << element.type() << "]");
-            // ignore unknown packet and proceed
-          }
-      }
-  }
-  catch(const tlv::Error& e) {
-    if (m_inputBufferSize == MAX_NDN_PACKET_SIZE && offset == 0)
-      {
-        NFD_LOG_WARN("[id:" << this->getId()
-                     << ",endpoint:" << m_socket->local_endpoint()
-                     << "] Received input is invalid or too large to process, "
-                     << "closing down the face");
+      offset += element.size();
 
-        closeSocket();
-        this->onFail("Received input is invalid or too large to process, closing down the face");
-        return;
-      }
-  }
+      BOOST_ASSERT(offset <= m_inputBufferSize);
+
+      if (!this->decodeAndDispatchInput(element))
+        {
+          NFD_LOG_WARN("[id:" << this->getId()
+                       << ",endpoint:" << m_socket->local_endpoint()
+                       << "] Received unrecognized block of type ["
+                       << element.type() << "]");
+          // ignore unknown packet and proceed
+        }
+    }
+  if (!isOk && m_inputBufferSize == MAX_NDN_PACKET_SIZE && offset == 0)
+    {
+      NFD_LOG_WARN("[id:" << this->getId()
+                   << ",endpoint:" << m_socket->local_endpoint()
+                   << "] Failed to parse incoming packet or it is too large to process, "
+                   << "closing down the face");
+
+      closeSocket();
+      this->onFail("Failed to parse incoming packet or it is too large to process, "
+                   "closing down the face");
+      return;
+    }
 
   if (offset > 0)
     {