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