Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
| 8 | #ifndef NDN_TCPTRANSPORT_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 9 | #define NDN_TCPTRANSPORT_HPP |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 10 | |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 11 | #include <string> |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 12 | #include "../common.hpp" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 13 | #include "transport.hpp" |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 14 | |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 15 | struct ndn_TcpTransport; |
| 16 | struct ndn_BinaryXmlElementReader; |
| 17 | |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 18 | namespace ndn { |
| 19 | |
| 20 | class TcpTransport : public Transport { |
| 21 | public: |
Alexander Afanasyev | 0b688dc | 2013-12-18 16:43:37 -0800 | [diff] [blame] | 22 | TcpTransport(const char *host, unsigned short port = 6363); |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 23 | |
Jeff Thompson | 2ed62fb | 2013-07-16 18:10:30 -0700 | [diff] [blame] | 24 | /** |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 25 | * Connect according to the info in ConnectionInfo, and processEvents() will use elementListener. |
| 26 | * @param connectionInfo A reference to a TcpTransport::ConnectionInfo. |
| 27 | * @param elementListener Not a shared_ptr because we assume that it will remain valid during the life of this object. |
Jeff Thompson | 2ed62fb | 2013-07-16 18:10:30 -0700 | [diff] [blame] | 28 | */ |
Alexander Afanasyev | 0b688dc | 2013-12-18 16:43:37 -0800 | [diff] [blame] | 29 | virtual void connect(ElementListener& elementListener); |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 30 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Set data to the host |
| 33 | * @param data A pointer to the buffer of data to send. |
| 34 | * @param dataLength The number of bytes in data. |
| 35 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 36 | virtual void send(const uint8_t *data, size_t dataLength); |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 37 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 38 | /** |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 39 | * Process any data to receive. For each element received, call elementListener.onReceivedElement. |
Jeff Thompson | c7e0744 | 2013-08-19 15:25:43 -0700 | [diff] [blame] | 40 | * This is non-blocking and will return immediately if there is no data to receive. |
| 41 | * You should normally not call this directly since it is called by Face.processEvents. |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 42 | * @throw This may throw an exception for reading data or in the callback for processing the data. If you |
| 43 | * call this from an main event loop, you may want to catch and log/disregard all exceptions. |
| 44 | */ |
| 45 | virtual void processEvents(); |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 46 | |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 47 | virtual bool getIsConnected(); |
| 48 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Close the connection to the host. |
| 51 | */ |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 52 | virtual void close(); |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 53 | |
Jeff Thompson | a00f4eb | 2013-08-12 12:36:48 -0700 | [diff] [blame] | 54 | ~TcpTransport(); |
| 55 | |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 56 | private: |
Alexander Afanasyev | 0b688dc | 2013-12-18 16:43:37 -0800 | [diff] [blame] | 57 | std::string host_; |
| 58 | unsigned short port_; |
| 59 | |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 60 | bool isConnected_; |
Alexander Afanasyev | 0b688dc | 2013-12-18 16:43:37 -0800 | [diff] [blame] | 61 | ptr_lib::shared_ptr<struct ndn_TcpTransport> transport_; |
Jeff Thompson | b002f90 | 2013-07-16 18:07:18 -0700 | [diff] [blame] | 62 | // TODO: This belongs in the socket listener. |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 63 | ptr_lib::shared_ptr<struct ndn_BinaryXmlElementReader> elementReader_; |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | } |
| 67 | |
| 68 | #endif |