Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
| 6 | #ifndef NDN_TCPTRANSPORT_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 7 | #define NDN_TCPTRANSPORT_HPP |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 8 | |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 9 | #include "../c/transport/tcp-transport.h" |
| 10 | #include "../c/encoding/binary-xml-element-reader.h" |
| 11 | #include "transport.hpp" |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 12 | |
| 13 | namespace ndn { |
| 14 | |
| 15 | class TcpTransport : public Transport { |
| 16 | public: |
| 17 | TcpTransport() |
| 18 | { |
| 19 | ndn_TcpTransport_init(&transport_); |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 20 | face_ = 0; |
Jeff Thompson | a00f4eb | 2013-08-12 12:36:48 -0700 | [diff] [blame] | 21 | elementReader_.partialData.array = 0; |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Jeff Thompson | 2ed62fb | 2013-07-16 18:10:30 -0700 | [diff] [blame] | 24 | /** |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 25 | * Connect to the host specified in face. |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 26 | * @param face Not a shared_ptr because we assume that it will remain valid during the life of this Transport object. |
Jeff Thompson | 2ed62fb | 2013-07-16 18:10:30 -0700 | [diff] [blame] | 27 | */ |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 28 | virtual void connect(Face &face); |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 29 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Set data to the host |
| 32 | * @param data A pointer to the buffer of data to send. |
| 33 | * @param dataLength The number of bytes in data. |
| 34 | */ |
Jeff Thompson | b605b5d | 2013-07-30 15:12:56 -0700 | [diff] [blame] | 35 | virtual void send(const unsigned char *data, unsigned int dataLength); |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 36 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Process any data to receive. For each element received, call face.onReceivedElement. |
| 39 | * This is non-blocking and will silently time out after a brief period if there is no data to receive. |
| 40 | * You should repeatedly call this from an event loop. |
| 41 | * @throw This may throw an exception for reading data or in the callback for processing the data. If you |
| 42 | * call this from an main event loop, you may want to catch and log/disregard all exceptions. |
| 43 | */ |
| 44 | virtual void processEvents(); |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 45 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Close the connection to the host. |
| 48 | */ |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 49 | virtual void close(); |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 50 | |
Jeff Thompson | a00f4eb | 2013-08-12 12:36:48 -0700 | [diff] [blame] | 51 | ~TcpTransport(); |
| 52 | |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 53 | private: |
| 54 | struct ndn_TcpTransport transport_; |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 55 | Face *face_; |
Jeff Thompson | b002f90 | 2013-07-16 18:07:18 -0700 | [diff] [blame] | 56 | // TODO: This belongs in the socket listener. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 57 | ndn_BinaryXmlElementReader elementReader_; |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | } |
| 61 | |
| 62 | #endif |