face+transport: Cleanup and preparation for implementation of fully async Face operations
Change-Id: I7816b6a9c99a0cf4825459b9652372d6585a5191
diff --git a/include/ndn-cpp/transport/transport.hpp b/include/ndn-cpp/transport/transport.hpp
index 80d41b2..8bfa355 100644
--- a/include/ndn-cpp/transport/transport.hpp
+++ b/include/ndn-cpp/transport/transport.hpp
@@ -8,58 +8,88 @@
#ifndef NDN_TRANSPORT_HPP
#define NDN_TRANSPORT_HPP
+#include <ndn-cpp/common.hpp>
+
#include <vector>
+#include <boost/asio.hpp>
namespace ndn {
-class ElementListener;
-
class Transport {
public:
+ typedef ptr_lib::function<void (const Block &wire)> ReceiveCallback;
+
+ inline
+ Transport();
+
+ inline virtual
+ ~Transport();
+
/**
* Connect according to the info in ConnectionInfo, and processEvents() will use elementListener.
* @param connectionInfo A reference to an object of a subclass of ConnectionInfo.
- * @param elementListener Not a shared_ptr because we assume that it will remain valid during the life of this object.
+ */
+ inline virtual void
+ connect(boost::asio::io_service &io_service, const ReceiveCallback &receiveCallback);
+
+ /**
+ * Close the connection.
*/
virtual void
- connect(ElementListener& elementListener) = 0;
-
+ close() = 0;
+
/**
* Set data to the host
* @param data A pointer to the buffer of data to send.
* @param dataLength The number of bytes in data.
*/
virtual void
- send(const uint8_t *data, size_t dataLength) = 0;
-
- inline void
- send(const std::vector<uint8_t>& data)
- {
- send(&data[0], data.size());
- }
-
- /**
- * Process any data to receive. For each element received, call elementListener.onReceivedElement.
- * This is non-blocking and will return immediately if there is no data to receive.
- * You should normally not call this directly since it is called by Face.processEvents.
- * @throw This may throw an exception for reading data or in the callback for processing the data. If you
- * call this from an main event loop, you may want to catch and log/disregard all exceptions.
- */
- virtual void
- processEvents() = 0;
+ send(const Block &wire) = 0;
- virtual bool
- getIsConnected() = 0;
+ inline bool
+ isConnected();
+
+protected:
+ inline void
+ receive(const Block &wire);
- /**
- * Close the connection. This base class implementation does nothing, but your derived class can override.
- */
- virtual void
- close();
-
- virtual ~Transport();
+protected:
+ boost::asio::io_service *ioService_;
+ bool isConnected_;
+ ReceiveCallback receiveCallback_;
};
+inline
+Transport::Transport()
+ : ioService_(0)
+ , isConnected_(false)
+{
+}
+
+inline
+Transport::~Transport()
+{
+}
+
+inline void
+Transport::connect(boost::asio::io_service &ioService, const ReceiveCallback &receiveCallback)
+{
+ ioService_ = &ioService;
+ receiveCallback_ = receiveCallback;
+}
+
+inline bool
+Transport::isConnected()
+{
+ return isConnected_;
+}
+
+inline void
+Transport::receive(const Block &wire)
+{
+ receiveCallback_(wire);
+}
+
}
#endif