Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * File: UdpTransport.h |
| 3 | * Author: jefft0 |
| 4 | * |
| 5 | * Created on July 14, 2013, 4:15 PM |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef NDN_UDPTRANSPORT_H |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 9 | #define NDN_UDPTRANSPORT_H |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 10 | |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 11 | #include "socket-transport.h" |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 12 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 13 | #ifdef __cplusplus |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 14 | extern "C" { |
| 15 | #endif |
| 16 | |
| 17 | struct ndn_UdpTransport { |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 18 | struct ndn_SocketTransport base; |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 19 | }; |
| 20 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 21 | /** |
| 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 Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 25 | static inline void ndn_UdpTransport_initialize(struct ndn_UdpTransport *self) |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 26 | { |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 27 | ndn_SocketTransport_initialize(&self->base); |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 30 | /** |
| 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 Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 37 | static 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 Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 41 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 42 | /** |
| 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 Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 49 | static 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 Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 53 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Check if there is data ready on the socket to be received with ndn_UdpTransport_receive. |
Jeff Thompson | c7e0744 | 2013-08-19 15:25:43 -0700 | [diff] [blame] | 56 | * This does not block, and returns immediately. |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 57 | * @param self A pointer to the ndn_UdpTransport 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 | */ |
| 61 | static inline ndn_Error ndn_UdpTransport_receiveIsReady(struct ndn_UdpTransport *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_UdpTransport 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 Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 75 | static inline ndn_Error ndn_UdpTransport_receive |
| 76 | (struct ndn_UdpTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytes) |
| 77 | { |
| 78 | return ndn_SocketTransport_receive(&self->base, buffer, bufferLength, nBytes); |
| 79 | } |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 80 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Close the socket. |
| 83 | * @param self A pointer to the ndn_UdpTransport struct. |
| 84 | * @return 0 for success, else an error code. |
| 85 | */ |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 86 | static inline ndn_Error ndn_UdpTransport_close(struct ndn_UdpTransport *self) |
| 87 | { |
| 88 | return ndn_SocketTransport_close(&self->base); |
| 89 | } |
| 90 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 91 | #ifdef __cplusplus |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 92 | } |
| 93 | #endif |
| 94 | |
| 95 | #endif |