blob: 41906006dd0218a4822bfa1743d9475bc2dfdd33 [file] [log] [blame]
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 */
5
6#ifndef NDN_TCPTRANSPORT_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07007#define NDN_TCPTRANSPORT_HPP
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07008
Jeff Thompson53412192013-08-06 13:35:50 -07009#include "../c/transport/tcp-transport.h"
10#include "../c/encoding/binary-xml-element-reader.h"
11#include "transport.hpp"
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070012
13namespace ndn {
14
15class TcpTransport : public Transport {
16public:
17 TcpTransport()
Jeff Thompsona4056972013-08-22 11:52:21 -070018 : node_(0), isConnected_(false)
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070019 {
20 ndn_TcpTransport_init(&transport_);
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070021 elementReader_.partialData.array = 0;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070022 }
23
Jeff Thompson2ed62fb2013-07-16 18:10:30 -070024 /**
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070025 * Connect to the host specified in node.
26 * @param node Not a shared_ptr because we assume that it will remain valid during the life of this Transport object.
Jeff Thompson2ed62fb2013-07-16 18:10:30 -070027 */
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070028 virtual void connect(Node &node);
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070029
Jeff Thompson432c8be2013-08-09 16:16:08 -070030 /**
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 Thompsonb605b5d2013-07-30 15:12:56 -070035 virtual void send(const unsigned char *data, unsigned int dataLength);
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070036
Jeff Thompson432c8be2013-08-09 16:16:08 -070037 /**
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070038 * Process any data to receive. For each element received, call node.onReceivedElement.
Jeff Thompsonc7e07442013-08-19 15:25:43 -070039 * This is non-blocking and will return immediately if there is no data to receive.
40 * You should normally not call this directly since it is called by Face.processEvents.
Jeff Thompson432c8be2013-08-09 16:16:08 -070041 * @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 Thompson57963882013-08-05 16:01:25 -070045
Jeff Thompsona4056972013-08-22 11:52:21 -070046 virtual bool getIsConnected();
47
Jeff Thompson432c8be2013-08-09 16:16:08 -070048 /**
49 * Close the connection to the host.
50 */
Jeff Thompson57963882013-08-05 16:01:25 -070051 virtual void close();
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070052
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070053 ~TcpTransport();
54
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070055private:
56 struct ndn_TcpTransport transport_;
Jeff Thompsona4056972013-08-22 11:52:21 -070057 bool isConnected_;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070058 Node *node_;
Jeff Thompsonb002f902013-07-16 18:07:18 -070059 // TODO: This belongs in the socket listener.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070060 ndn_BinaryXmlElementReader elementReader_;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070061};
62
63}
64
65#endif