blob: 3f87a51f2e7df5e607fa21f066817f940a22ff52 [file] [log] [blame]
Jeff Thompson7850d782013-07-14 17:59:14 -07001/*
2 * File: TcpTransport.h
3 * Author: jefft0
4 *
5 * Created on July 14, 2013, 4:15 PM
6 */
7
8#ifndef NDN_TCPTRANSPORT_H
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07009#define NDN_TCPTRANSPORT_H
Jeff Thompson7850d782013-07-14 17:59:14 -070010
Jeff Thompson53412192013-08-06 13:35:50 -070011#include "socket-transport.h"
Jeff Thompson0d567da2013-07-14 22:10:21 -070012
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070013#ifdef __cplusplus
Jeff Thompson7850d782013-07-14 17:59:14 -070014extern "C" {
15#endif
16
17struct ndn_TcpTransport {
Jeff Thompson0aa754a2013-07-17 17:42:28 -070018 struct ndn_SocketTransport base;
Jeff Thompson7850d782013-07-14 17:59:14 -070019};
20
Jeff Thompson432c8be2013-08-09 16:16:08 -070021/**
22 * Initialize the ndn_TcpTransport struct with default values for no connection yet.
23 * @param self A pointer to the ndn_TcpTransport struct.
24 */
Jeff Thompsond1427fb2013-08-29 17:20:32 -070025static inline void ndn_TcpTransport_initialize(struct ndn_TcpTransport *self)
Jeff Thompson7850d782013-07-14 17:59:14 -070026{
Jeff Thompsond1427fb2013-08-29 17:20:32 -070027 ndn_SocketTransport_initialize(&self->base);
Jeff Thompson7850d782013-07-14 17:59:14 -070028}
29
Jeff Thompson432c8be2013-08-09 16:16:08 -070030/**
31 * Connect with TCP to the host:port.
32 * @param self A pointer to the ndn_TcpTransport struct.
33 * @param host The host to connect to.
34 * @param port The port to connect to.
35 * @return 0 for success, else an error code.
36 */
Jeff Thompson0aa754a2013-07-17 17:42:28 -070037static inline ndn_Error ndn_TcpTransport_connect(struct ndn_TcpTransport *self, char *host, unsigned short port)
38{
39 return ndn_SocketTransport_connect(&self->base, SOCKET_TCP, host, port);
40}
Jeff Thompson0d567da2013-07-14 22:10:21 -070041
Jeff Thompson432c8be2013-08-09 16:16:08 -070042/**
43 * Send data to the socket.
44 * @param self A pointer to the ndn_TcpTransport struct.
45 * @param data A pointer to the buffer of data to send.
46 * @param dataLength The number of bytes in data.
47 * @return 0 for success, else an error code.
48 */
Jeff Thompson0aa754a2013-07-17 17:42:28 -070049static inline ndn_Error ndn_TcpTransport_send(struct ndn_TcpTransport *self, unsigned char *data, unsigned int dataLength)
50{
51 return ndn_SocketTransport_send(&self->base, data, dataLength);
52}
Jeff Thompson0d567da2013-07-14 22:10:21 -070053
Jeff Thompson432c8be2013-08-09 16:16:08 -070054/**
55 * Check if there is data ready on the socket to be received with ndn_TcpTransport_receive.
Jeff Thompsonc7e07442013-08-19 15:25:43 -070056 * This does not block, and returns immediately.
Jeff Thompson432c8be2013-08-09 16:16:08 -070057 * @param self A pointer to the ndn_TcpTransport struct.
58 * @param receiveIsReady This will be set to 1 if data is ready, 0 if not.
59 * @return 0 for success, else an error code.
60 */
61static inline ndn_Error ndn_TcpTransport_receiveIsReady(struct ndn_TcpTransport *self, int *receiveIsReady)
62{
63 return ndn_SocketTransport_receiveIsReady(&self->base, receiveIsReady);
64}
65
66/**
67 * Receive data from the socket. NOTE: This is a blocking call. You should first call ndn_SocketTransport_receiveIsReady
68 * to make sure there is data ready to receive.
69 * @param self A pointer to the ndn_TcpTransport struct.
70 * @param buffer A pointer to the buffer to receive the data.
71 * @param bufferLength The maximum length of buffer.
72 * @param nBytes Return the number of bytes received into buffer.
73 * @return 0 for success, else an error code.
74 */
Jeff Thompson0aa754a2013-07-17 17:42:28 -070075static inline ndn_Error ndn_TcpTransport_receive
76 (struct ndn_TcpTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytes)
77{
78 return ndn_SocketTransport_receive(&self->base, buffer, bufferLength, nBytes);
79}
Jeff Thompson0d567da2013-07-14 22:10:21 -070080
Jeff Thompson432c8be2013-08-09 16:16:08 -070081/**
82 * Close the socket.
83 * @param self A pointer to the ndn_TcpTransport struct.
84 * @return 0 for success, else an error code.
85 */
Jeff Thompson57963882013-08-05 16:01:25 -070086static inline ndn_Error ndn_TcpTransport_close(struct ndn_TcpTransport *self)
87{
88 return ndn_SocketTransport_close(&self->base);
89}
90
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070091#ifdef __cplusplus
Jeff Thompson7850d782013-07-14 17:59:14 -070092}
93#endif
94
95#endif