blob: fe81b42c2863520cc9c606303a3309cc172f56e6 [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#include <stdexcept>
Jeff Thompson0cb7aee2013-07-16 16:18:06 -07007#include "../NDN.hpp"
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07008#include "TcpTransport.hpp"
9
10using namespace std;
11
12namespace ndn {
13
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070014void TcpTransport::connect(NDN &ndn)
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070015{
16 ndn_Error error;
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070017 if (error = ndn_TcpTransport_connect(&transport_, (char *)ndn.getHost(), ndn.getPort()))
18 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompsonb002f902013-07-16 18:07:18 -070019
20 // TODO: This belongs in the socket listener.
21 // Automatically cast ndn_ to (struct ndn_ElementListener *)
22 ndn_BinaryXMLElementReader_init(&elementReader_, &ndn);
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070023
24 // TODO: Properly indicate connected status.
25 ndn_ = &ndn;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070026}
27
28void 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 Thompson0cb7aee2013-07-16 16:18:06 -070035void TcpTransport::tempReceive()
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070036{
Jeff Thompsonb002f902013-07-16 18:07:18 -070037 try {
Jeff Thompson7ed97c72013-07-16 17:56:41 -070038 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 Thompsonb002f902013-07-16 18:07:18 -070044 ndn_BinaryXMLElementReader_onReceivedData(&elementReader_, buffer, nBytes);
Jeff Thompson7ed97c72013-07-16 17:56:41 -070045 } 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 Thompsonfcf347d2013-07-15 11:30:44 -070049}
50
51}