blob: 9c9969845229b30cd2c99db1cad01aeeebbb7670 [file] [log] [blame]
Jeff Thompson0aa754a2013-07-17 17:42:28 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 */
5
6#ifndef NDN_SOCKETTRANSPORT_H
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07007#define NDN_SOCKETTRANSPORT_H
Jeff Thompson0aa754a2013-07-17 17:42:28 -07008
9#include <sys/socket.h>
10#include "../errors.h"
11
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070012#ifdef __cplusplus
Jeff Thompson0aa754a2013-07-17 17:42:28 -070013extern "C" {
14#endif
15
16typedef enum {
17 SOCKET_TCP,
18 SOCKET_UDP
19} ndn_SocketType;
20
21struct ndn_SocketTransport {
22 int socketDescriptor; /**< -1 if not connected */
23};
24
Jeff Thompson432c8be2013-08-09 16:16:08 -070025/**
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 Thompsond1427fb2013-08-29 17:20:32 -070029static inline void ndn_SocketTransport_initialize(struct ndn_SocketTransport *self)
Jeff Thompson0aa754a2013-07-17 17:42:28 -070030{
31 self->socketDescriptor = -1;
32}
33
Jeff Thompson432c8be2013-08-09 16:16:08 -070034/**
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 Thompson0aa754a2013-07-17 17:42:28 -070042ndn_Error ndn_SocketTransport_connect(struct ndn_SocketTransport *self, ndn_SocketType socketType, char *host, unsigned short port);
43
Jeff Thompson432c8be2013-08-09 16:16:08 -070044/**
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 Thompson0aa754a2013-07-17 17:42:28 -070051ndn_Error ndn_SocketTransport_send(struct ndn_SocketTransport *self, unsigned char *data, unsigned int dataLength);
52
Jeff Thompson432c8be2013-08-09 16:16:08 -070053/**
54 * Check if there is data ready on the socket to be received with ndn_SocketTransport_receive.
Jeff Thompsonc7e07442013-08-19 15:25:43 -070055 * This does not block, and returns immediately.
Jeff Thompson432c8be2013-08-09 16:16:08 -070056 * @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 */
60ndn_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 Thompson0aa754a2013-07-17 17:42:28 -070071ndn_Error ndn_SocketTransport_receive
72 (struct ndn_SocketTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytes);
73
Jeff Thompson432c8be2013-08-09 16:16:08 -070074/**
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 Thompson57963882013-08-05 16:01:25 -070079ndn_Error ndn_SocketTransport_close(struct ndn_SocketTransport *self);
80
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070081#ifdef __cplusplus
Jeff Thompson0aa754a2013-07-17 17:42:28 -070082}
83#endif
84
85#endif