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