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 | #include <stdexcept> |
Jeff Thompson | 0cb7aee | 2013-07-16 16:18:06 -0700 | [diff] [blame] | 7 | #include "../NDN.hpp" |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 8 | #include "TcpTransport.hpp" |
| 9 | |
| 10 | using namespace std; |
| 11 | |
| 12 | namespace ndn { |
| 13 | |
Jeff Thompson | 0cb7aee | 2013-07-16 16:18:06 -0700 | [diff] [blame] | 14 | void TcpTransport::connect(NDN &ndn) |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 15 | { |
| 16 | ndn_Error error; |
Jeff Thompson | 0cb7aee | 2013-07-16 16:18:06 -0700 | [diff] [blame] | 17 | if (error = ndn_TcpTransport_connect(&transport_, (char *)ndn.getHost(), ndn.getPort())) |
| 18 | throw std::runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | b002f90 | 2013-07-16 18:07:18 -0700 | [diff] [blame] | 19 | |
| 20 | // TODO: This belongs in the socket listener. |
| 21 | // Automatically cast ndn_ to (struct ndn_ElementListener *) |
| 22 | ndn_BinaryXMLElementReader_init(&elementReader_, &ndn); |
Jeff Thompson | 0cb7aee | 2013-07-16 16:18:06 -0700 | [diff] [blame] | 23 | |
| 24 | // TODO: Properly indicate connected status. |
| 25 | ndn_ = &ndn; |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void TcpTransport::send(unsigned char *data, unsigned int dataLength) |
| 29 | { |
| 30 | ndn_Error error; |
| 31 | if (error = ndn_TcpTransport_send(&transport_, data, dataLength)) |
| 32 | throw std::runtime_error(ndn_getErrorString(error)); |
| 33 | } |
| 34 | |
Jeff Thompson | 0cb7aee | 2013-07-16 16:18:06 -0700 | [diff] [blame] | 35 | void TcpTransport::tempReceive() |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 36 | { |
Jeff Thompson | b002f90 | 2013-07-16 18:07:18 -0700 | [diff] [blame] | 37 | try { |
Jeff Thompson | 7ed97c7 | 2013-07-16 17:56:41 -0700 | [diff] [blame] | 38 | ndn_Error error; |
| 39 | unsigned char buffer[8000]; |
| 40 | unsigned int nBytes; |
| 41 | if (error = ndn_TcpTransport_receive(&transport_, buffer, sizeof(buffer), &nBytes)) |
| 42 | throw std::runtime_error(ndn_getErrorString(error)); |
| 43 | |
Jeff Thompson | b002f90 | 2013-07-16 18:07:18 -0700 | [diff] [blame] | 44 | ndn_BinaryXMLElementReader_onReceivedData(&elementReader_, buffer, nBytes); |
Jeff Thompson | 7ed97c7 | 2013-07-16 17:56:41 -0700 | [diff] [blame] | 45 | } catch (...) { |
| 46 | // This function is called by the socket callback, so don't send an exception back to it. |
| 47 | // TODO: Log the exception? |
| 48 | } |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | } |