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