Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -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> |
| 4 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef NDN_TCPTRANSPORT_H |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 8 | #define NDN_TCPTRANSPORT_H |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 9 | |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 10 | #include "socket-transport.h" |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 11 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 12 | #ifdef __cplusplus |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 13 | extern "C" { |
| 14 | #endif |
| 15 | |
| 16 | struct ndn_TcpTransport { |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 17 | struct ndn_SocketTransport base; |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 18 | }; |
| 19 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 20 | /** |
| 21 | * Initialize the ndn_TcpTransport struct with default values for no connection yet. |
| 22 | * @param self A pointer to the ndn_TcpTransport struct. |
| 23 | */ |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 24 | static inline void ndn_TcpTransport_initialize(struct ndn_TcpTransport *self) |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 25 | { |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 26 | ndn_SocketTransport_initialize(&self->base); |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Connect with TCP to the host:port. |
| 31 | * @param self A pointer to the ndn_TcpTransport struct. |
| 32 | * @param host The host to connect to. |
| 33 | * @param port The port to connect to. |
| 34 | * @return 0 for success, else an error code. |
| 35 | */ |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 36 | static inline ndn_Error ndn_TcpTransport_connect(struct ndn_TcpTransport *self, char *host, unsigned short port) |
| 37 | { |
| 38 | return ndn_SocketTransport_connect(&self->base, SOCKET_TCP, host, port); |
| 39 | } |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 40 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Send data to the socket. |
| 43 | * @param self A pointer to the ndn_TcpTransport struct. |
| 44 | * @param data A pointer to the buffer of data to send. |
| 45 | * @param dataLength The number of bytes in data. |
| 46 | * @return 0 for success, else an error code. |
| 47 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 48 | static inline ndn_Error ndn_TcpTransport_send(struct ndn_TcpTransport *self, uint8_t *data, size_t dataLength) |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 49 | { |
| 50 | return ndn_SocketTransport_send(&self->base, data, dataLength); |
| 51 | } |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 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_TcpTransport_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_TcpTransport 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 | static inline ndn_Error ndn_TcpTransport_receiveIsReady(struct ndn_TcpTransport *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_TcpTransport 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 Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 74 | static inline ndn_Error ndn_TcpTransport_receive |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 75 | (struct ndn_TcpTransport *self, uint8_t *buffer, size_t bufferLength, size_t *nBytes) |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 76 | { |
| 77 | return ndn_SocketTransport_receive(&self->base, buffer, bufferLength, nBytes); |
| 78 | } |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 79 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Close the socket. |
| 82 | * @param self A pointer to the ndn_TcpTransport struct. |
| 83 | * @return 0 for success, else an error code. |
| 84 | */ |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 85 | static inline ndn_Error ndn_TcpTransport_close(struct ndn_TcpTransport *self) |
| 86 | { |
| 87 | return ndn_SocketTransport_close(&self->base); |
| 88 | } |
| 89 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 90 | #ifdef __cplusplus |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 91 | } |
| 92 | #endif |
| 93 | |
| 94 | #endif |