Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
| 6 | #include <stdexcept> |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 7 | #include "../face.hpp" |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 8 | #include "../c/util/ndn_realloc.h" |
| 9 | #include "UdpTransport.hpp" |
| 10 | |
| 11 | using namespace std; |
| 12 | |
| 13 | namespace ndn { |
| 14 | |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 15 | void UdpTransport::connect(Face &face) |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 16 | { |
| 17 | ndn_Error error; |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 18 | if (error = ndn_UdpTransport_connect(&transport_, (char *)face.getHost(), face.getPort())) |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 19 | throw std::runtime_error(ndn_getErrorString(error)); |
| 20 | |
| 21 | // TODO: This belongs in the socket listener. |
| 22 | const unsigned int initialLength = 1000; |
| 23 | // Automatically cast ndn_ to (struct ndn_ElementListener *) |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 24 | ndn_BinaryXmlElementReader_init |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 25 | (&elementReader_, &face, (unsigned char *)malloc(initialLength), initialLength, ndn_realloc); |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 26 | |
| 27 | // TODO: Properly indicate connected status. |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 28 | face_ = &face; |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Jeff Thompson | 60eaae3 | 2013-07-30 15:46:19 -0700 | [diff] [blame] | 31 | void UdpTransport::send(const unsigned char *data, unsigned int dataLength) |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 32 | { |
| 33 | ndn_Error error; |
Jeff Thompson | 60eaae3 | 2013-07-30 15:46:19 -0700 | [diff] [blame] | 34 | if (error = ndn_UdpTransport_send(&transport_, (unsigned char *)data, dataLength)) |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 35 | throw std::runtime_error(ndn_getErrorString(error)); |
| 36 | } |
| 37 | |
| 38 | void UdpTransport::tempReceive() |
| 39 | { |
| 40 | try { |
| 41 | ndn_Error error; |
| 42 | unsigned char buffer[8000]; |
| 43 | unsigned int nBytes; |
| 44 | if (error = ndn_UdpTransport_receive(&transport_, buffer, sizeof(buffer), &nBytes)) |
| 45 | throw std::runtime_error(ndn_getErrorString(error)); |
| 46 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 47 | ndn_BinaryXmlElementReader_onReceivedData(&elementReader_, buffer, nBytes); |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -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 | } |
| 52 | } |
| 53 | |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 54 | void UdpTransport::close() |
| 55 | { |
| 56 | ndn_Error error; |
| 57 | if (error = ndn_UdpTransport_close(&transport_)) |
| 58 | throw std::runtime_error(ndn_getErrorString(error)); |
| 59 | } |
| 60 | |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 61 | } |