Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NDN_SOCKETTRANSPORT_H |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 8 | #define NDN_SOCKETTRANSPORT_H |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 9 | |
| 10 | #include <sys/socket.h> |
| 11 | #include "../errors.h" |
| 12 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 13 | #ifdef __cplusplus |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 14 | extern "C" { |
| 15 | #endif |
| 16 | |
| 17 | typedef enum { |
| 18 | SOCKET_TCP, |
| 19 | SOCKET_UDP |
| 20 | } ndn_SocketType; |
| 21 | |
| 22 | struct ndn_SocketTransport { |
| 23 | int socketDescriptor; /**< -1 if not connected */ |
| 24 | }; |
| 25 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Initialize the ndn_SocketTransport struct with default values for no connection yet. |
| 28 | * @param self A pointer to the ndn_SocketTransport struct. |
| 29 | */ |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 30 | static inline void ndn_SocketTransport_initialize(struct ndn_SocketTransport *self) |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 31 | { |
| 32 | self->socketDescriptor = -1; |
| 33 | } |
| 34 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Connect with TCP or UDP to the host:port. |
| 37 | * @param self A pointer to the ndn_SocketTransport struct. |
| 38 | * @param socketType SOCKET_TCP or SOCKET_UDP. |
| 39 | * @param host The host to connect to. |
| 40 | * @param port The port to connect to. |
| 41 | * @return 0 for success, else an error code. |
| 42 | */ |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 43 | ndn_Error ndn_SocketTransport_connect(struct ndn_SocketTransport *self, ndn_SocketType socketType, char *host, unsigned short port); |
| 44 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Send data to the socket. |
| 47 | * @param self A pointer to the ndn_SocketTransport struct. |
| 48 | * @param data A pointer to the buffer of data to send. |
| 49 | * @param dataLength The number of bytes in data. |
| 50 | * @return 0 for success, else an error code. |
| 51 | */ |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 52 | ndn_Error ndn_SocketTransport_send(struct ndn_SocketTransport *self, unsigned char *data, unsigned int dataLength); |
| 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_SocketTransport_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_SocketTransport 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 | ndn_Error ndn_SocketTransport_receiveIsReady(struct ndn_SocketTransport *self, int *receiveIsReady); |
| 62 | |
| 63 | /** |
| 64 | * Receive data from the socket. NOTE: This is a blocking call. You should first call ndn_SocketTransport_receiveIsReady |
| 65 | * to make sure there is data ready to receive. |
| 66 | * @param self A pointer to the ndn_SocketTransport struct. |
| 67 | * @param buffer A pointer to the buffer to receive the data. |
| 68 | * @param bufferLength The maximum length of buffer. |
| 69 | * @param nBytes Return the number of bytes received into buffer. |
| 70 | * @return 0 for success, else an error code. |
| 71 | */ |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 72 | ndn_Error ndn_SocketTransport_receive |
| 73 | (struct ndn_SocketTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytes); |
| 74 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 75 | /** |
| 76 | * Close the socket. |
| 77 | * @param self A pointer to the ndn_SocketTransport struct. |
| 78 | * @return 0 for success, else an error code. |
| 79 | */ |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 80 | ndn_Error ndn_SocketTransport_close(struct ndn_SocketTransport *self); |
| 81 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 82 | #ifdef __cplusplus |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 83 | } |
| 84 | #endif |
| 85 | |
| 86 | #endif |