blob: 750cc5671a0c5a23455d23d466442fe99358ab92 [file] [log] [blame]
Jeff Thompson0aa754a2013-07-17 17:42:28 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson0aa754a2013-07-17 17:42:28 -07004 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NDN_SOCKETTRANSPORT_H
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07008#define NDN_SOCKETTRANSPORT_H
Jeff Thompson0aa754a2013-07-17 17:42:28 -07009
10#include <sys/socket.h>
11#include "../errors.h"
12
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070013#ifdef __cplusplus
Jeff Thompson0aa754a2013-07-17 17:42:28 -070014extern "C" {
15#endif
16
17typedef enum {
18 SOCKET_TCP,
19 SOCKET_UDP
20} ndn_SocketType;
21
22struct ndn_SocketTransport {
23 int socketDescriptor; /**< -1 if not connected */
24};
25
Jeff Thompson432c8be2013-08-09 16:16:08 -070026/**
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 Thompsond1427fb2013-08-29 17:20:32 -070030static inline void ndn_SocketTransport_initialize(struct ndn_SocketTransport *self)
Jeff Thompson0aa754a2013-07-17 17:42:28 -070031{
32 self->socketDescriptor = -1;
33}
34
Jeff Thompson432c8be2013-08-09 16:16:08 -070035/**
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 Thompson0aa754a2013-07-17 17:42:28 -070043ndn_Error ndn_SocketTransport_connect(struct ndn_SocketTransport *self, ndn_SocketType socketType, char *host, unsigned short port);
44
Jeff Thompson432c8be2013-08-09 16:16:08 -070045/**
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 Thompson0aa754a2013-07-17 17:42:28 -070052ndn_Error ndn_SocketTransport_send(struct ndn_SocketTransport *self, unsigned char *data, unsigned int dataLength);
53
Jeff Thompson432c8be2013-08-09 16:16:08 -070054/**
55 * Check if there is data ready on the socket to be received with ndn_SocketTransport_receive.
Jeff Thompsonc7e07442013-08-19 15:25:43 -070056 * This does not block, and returns immediately.
Jeff Thompson432c8be2013-08-09 16:16:08 -070057 * @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 */
61ndn_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 Thompson0aa754a2013-07-17 17:42:28 -070072ndn_Error ndn_SocketTransport_receive
73 (struct ndn_SocketTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytes);
74
Jeff Thompson432c8be2013-08-09 16:16:08 -070075/**
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 Thompson57963882013-08-05 16:01:25 -070080ndn_Error ndn_SocketTransport_close(struct ndn_SocketTransport *self);
81
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070082#ifdef __cplusplus
Jeff Thompson0aa754a2013-07-17 17:42:28 -070083}
84#endif
85
86#endif