blob: c9f0d55c383ee12a85c1bbb32919d32467f10686 [file] [log] [blame]
Jeff Thompson0aa754a2013-07-17 17:42:28 -07001/*
2 * File: UdpTransport.h
3 * Author: jefft0
4 *
5 * Created on July 14, 2013, 4:15 PM
Jeff Thompsonbc53c522013-07-17 17:11:48 -07006 */
7
8#ifndef NDN_UDPTRANSPORT_H
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07009#define NDN_UDPTRANSPORT_H
Jeff Thompsonbc53c522013-07-17 17:11:48 -070010
Jeff Thompson53412192013-08-06 13:35:50 -070011#include "socket-transport.h"
Jeff Thompsonbc53c522013-07-17 17:11:48 -070012
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070013#ifdef __cplusplus
Jeff Thompsonbc53c522013-07-17 17:11:48 -070014extern "C" {
15#endif
16
17struct ndn_UdpTransport {
Jeff Thompson0aa754a2013-07-17 17:42:28 -070018 struct ndn_SocketTransport base;
Jeff Thompsonbc53c522013-07-17 17:11:48 -070019};
20
Jeff Thompson432c8be2013-08-09 16:16:08 -070021/**
22 * Initialize the ndn_UdpTransport struct with default values for no connection yet.
23 * @param self A pointer to the ndn_UdpTransport struct.
24 */
Jeff Thompsonbc53c522013-07-17 17:11:48 -070025static inline void ndn_UdpTransport_init(struct ndn_UdpTransport *self)
26{
Jeff Thompson0aa754a2013-07-17 17:42:28 -070027 ndn_SocketTransport_init(&self->base);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070028}
29
Jeff Thompson432c8be2013-08-09 16:16:08 -070030/**
31 * Connect with UDP to the host:port.
32 * @param self A pointer to the ndn_UdpTransport 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_UdpTransport_connect(struct ndn_UdpTransport *self, char *host, unsigned short port)
38{
39 return ndn_SocketTransport_connect(&self->base, SOCKET_UDP, host, port);
40}
Jeff Thompsonbc53c522013-07-17 17:11:48 -070041
Jeff Thompson432c8be2013-08-09 16:16:08 -070042/**
43 * Send data to the socket.
44 * @param self A pointer to the ndn_UdpTransport 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_UdpTransport_send(struct ndn_UdpTransport *self, unsigned char *data, unsigned int dataLength)
50{
51 return ndn_SocketTransport_send(&self->base, data, dataLength);
52}
Jeff Thompsonbc53c522013-07-17 17:11:48 -070053
Jeff Thompson432c8be2013-08-09 16:16:08 -070054/**
55 * Check if there is data ready on the socket to be received with ndn_UdpTransport_receive.
56 * @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
75 (struct ndn_UdpTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytes)
76{
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