blob: 20d106bf8d129683bce97f15ea250c405f589afb [file] [log] [blame]
Jeff Thompson0aa754a2013-07-17 17:42:28 -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 Thompsonbc53c522013-07-17 17:11:48 -07005 */
6
7#ifndef NDN_UDPTRANSPORT_H
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07008#define NDN_UDPTRANSPORT_H
Jeff Thompsonbc53c522013-07-17 17:11:48 -07009
Jeff Thompson53412192013-08-06 13:35:50 -070010#include "socket-transport.h"
Jeff Thompsonbc53c522013-07-17 17:11:48 -070011
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070012#ifdef __cplusplus
Jeff Thompsonbc53c522013-07-17 17:11:48 -070013extern "C" {
14#endif
15
16struct ndn_UdpTransport {
Jeff Thompson0aa754a2013-07-17 17:42:28 -070017 struct ndn_SocketTransport base;
Jeff Thompsonbc53c522013-07-17 17:11:48 -070018};
19
Jeff Thompson432c8be2013-08-09 16:16:08 -070020/**
21 * Initialize the ndn_UdpTransport struct with default values for no connection yet.
22 * @param self A pointer to the ndn_UdpTransport struct.
23 */
Jeff Thompsond1427fb2013-08-29 17:20:32 -070024static inline void ndn_UdpTransport_initialize(struct ndn_UdpTransport *self)
Jeff Thompsonbc53c522013-07-17 17:11:48 -070025{
Jeff Thompsond1427fb2013-08-29 17:20:32 -070026 ndn_SocketTransport_initialize(&self->base);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070027}
28
Jeff Thompson432c8be2013-08-09 16:16:08 -070029/**
30 * Connect with UDP to the host:port.
31 * @param self A pointer to the ndn_UdpTransport 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_UdpTransport_connect(struct ndn_UdpTransport *self, char *host, unsigned short port)
37{
38 return ndn_SocketTransport_connect(&self->base, SOCKET_UDP, host, port);
39}
Jeff Thompsonbc53c522013-07-17 17:11:48 -070040
Jeff Thompson432c8be2013-08-09 16:16:08 -070041/**
42 * Send data to the socket.
43 * @param self A pointer to the ndn_UdpTransport 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 Thompson97223af2013-09-24 17:01:27 -070048static inline ndn_Error ndn_UdpTransport_send(struct ndn_UdpTransport *self, uint8_t *data, size_t dataLength)
Jeff Thompson0aa754a2013-07-17 17:42:28 -070049{
50 return ndn_SocketTransport_send(&self->base, data, dataLength);
51}
Jeff Thompsonbc53c522013-07-17 17:11:48 -070052
Jeff Thompson432c8be2013-08-09 16:16:08 -070053/**
54 * Check if there is data ready on the socket to be received with ndn_UdpTransport_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_UdpTransport 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_UdpTransport_receiveIsReady(struct ndn_UdpTransport *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_UdpTransport 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_UdpTransport_receive
Jeff Thompson97223af2013-09-24 17:01:27 -070075 (struct ndn_UdpTransport *self, uint8_t *buffer, size_t bufferLength, size_t *nBytes)
Jeff Thompson0aa754a2013-07-17 17:42:28 -070076{
77 return ndn_SocketTransport_receive(&self->base, buffer, bufferLength, nBytes);
78}
Jeff Thompsonbc53c522013-07-17 17:11:48 -070079
Jeff Thompson432c8be2013-08-09 16:16:08 -070080/**
81 * Close the socket.
82 * @param self A pointer to the ndn_UdpTransport struct.
83 * @return 0 for success, else an error code.
84 */
Jeff Thompson57963882013-08-05 16:01:25 -070085static inline ndn_Error ndn_UdpTransport_close(struct ndn_UdpTransport *self)
86{
87 return ndn_SocketTransport_close(&self->base);
88}
89
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070090#ifdef __cplusplus
Jeff Thompsonbc53c522013-07-17 17:11:48 -070091}
92#endif
93
94#endif