blob: e31e127305bfece381b0137dedaed8ed3eea99e5 [file] [log] [blame]
Jeff Thompson7850d782013-07-14 17:59:14 -07001/*
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
4 * See COPYING for copyright and distribution information.
Jeff Thompson7850d782013-07-14 17:59:14 -07005 */
6
7#ifndef NDN_TCPTRANSPORT_H
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07008#define NDN_TCPTRANSPORT_H
Jeff Thompson7850d782013-07-14 17:59:14 -07009
Jeff Thompson53412192013-08-06 13:35:50 -070010#include "socket-transport.h"
Jeff Thompson0d567da2013-07-14 22:10:21 -070011
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070012#ifdef __cplusplus
Jeff Thompson7850d782013-07-14 17:59:14 -070013extern "C" {
14#endif
15
16struct ndn_TcpTransport {
Jeff Thompson0aa754a2013-07-17 17:42:28 -070017 struct ndn_SocketTransport base;
Jeff Thompson7850d782013-07-14 17:59:14 -070018};
19
Jeff Thompson432c8be2013-08-09 16:16:08 -070020/**
21 * Initialize the ndn_TcpTransport struct with default values for no connection yet.
22 * @param self A pointer to the ndn_TcpTransport struct.
23 */
Jeff Thompsond1427fb2013-08-29 17:20:32 -070024static inline void ndn_TcpTransport_initialize(struct ndn_TcpTransport *self)
Jeff Thompson7850d782013-07-14 17:59:14 -070025{
Jeff Thompsond1427fb2013-08-29 17:20:32 -070026 ndn_SocketTransport_initialize(&self->base);
Jeff Thompson7850d782013-07-14 17:59:14 -070027}
28
Jeff Thompson432c8be2013-08-09 16:16:08 -070029/**
30 * Connect with TCP to the host:port.
31 * @param self A pointer to the ndn_TcpTransport struct.
32 * @param host The host to connect to.
33 * @param port The port to connect to.
34 * @return 0 for success, else an error code.
35 */
Jeff Thompson0aa754a2013-07-17 17:42:28 -070036static inline ndn_Error ndn_TcpTransport_connect(struct ndn_TcpTransport *self, char *host, unsigned short port)
37{
38 return ndn_SocketTransport_connect(&self->base, SOCKET_TCP, host, port);
39}
Jeff Thompson0d567da2013-07-14 22:10:21 -070040
Jeff Thompson432c8be2013-08-09 16:16:08 -070041/**
42 * Send data to the socket.
43 * @param self A pointer to the ndn_TcpTransport struct.
44 * @param data A pointer to the buffer of data to send.
45 * @param dataLength The number of bytes in data.
46 * @return 0 for success, else an error code.
47 */
Jeff Thompson0aa754a2013-07-17 17:42:28 -070048static inline ndn_Error ndn_TcpTransport_send(struct ndn_TcpTransport *self, unsigned char *data, unsigned int dataLength)
49{
50 return ndn_SocketTransport_send(&self->base, data, dataLength);
51}
Jeff Thompson0d567da2013-07-14 22:10:21 -070052
Jeff Thompson432c8be2013-08-09 16:16:08 -070053/**
54 * Check if there is data ready on the socket to be received with ndn_TcpTransport_receive.
Jeff Thompsonc7e07442013-08-19 15:25:43 -070055 * This does not block, and returns immediately.
Jeff Thompson432c8be2013-08-09 16:16:08 -070056 * @param self A pointer to the ndn_TcpTransport struct.
57 * @param receiveIsReady This will be set to 1 if data is ready, 0 if not.
58 * @return 0 for success, else an error code.
59 */
60static inline ndn_Error ndn_TcpTransport_receiveIsReady(struct ndn_TcpTransport *self, int *receiveIsReady)
61{
62 return ndn_SocketTransport_receiveIsReady(&self->base, receiveIsReady);
63}
64
65/**
66 * Receive data from the socket. NOTE: This is a blocking call. You should first call ndn_SocketTransport_receiveIsReady
67 * to make sure there is data ready to receive.
68 * @param self A pointer to the ndn_TcpTransport struct.
69 * @param buffer A pointer to the buffer to receive the data.
70 * @param bufferLength The maximum length of buffer.
71 * @param nBytes Return the number of bytes received into buffer.
72 * @return 0 for success, else an error code.
73 */
Jeff Thompson0aa754a2013-07-17 17:42:28 -070074static inline ndn_Error ndn_TcpTransport_receive
75 (struct ndn_TcpTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytes)
76{
77 return ndn_SocketTransport_receive(&self->base, buffer, bufferLength, nBytes);
78}
Jeff Thompson0d567da2013-07-14 22:10:21 -070079
Jeff Thompson432c8be2013-08-09 16:16:08 -070080/**
81 * Close the socket.
82 * @param self A pointer to the ndn_TcpTransport struct.
83 * @return 0 for success, else an error code.
84 */
Jeff Thompson57963882013-08-05 16:01:25 -070085static inline ndn_Error ndn_TcpTransport_close(struct ndn_TcpTransport *self)
86{
87 return ndn_SocketTransport_close(&self->base);
88}
89
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070090#ifdef __cplusplus
Jeff Thompson7850d782013-07-14 17:59:14 -070091}
92#endif
93
94#endif