blob: 85e297e0f2ee80c880eaf104fb562b6679230dab [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>
Jeff Thompson25b4e612013-10-10 16:03:24 -070011#include <ndn-cpp/c/common.h>
Jeff Thompson0aa754a2013-07-17 17:42:28 -070012#include "../errors.h"
13
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070014#ifdef __cplusplus
Jeff Thompson0aa754a2013-07-17 17:42:28 -070015extern "C" {
16#endif
17
18typedef enum {
19 SOCKET_TCP,
20 SOCKET_UDP
21} ndn_SocketType;
22
23struct ndn_SocketTransport {
24 int socketDescriptor; /**< -1 if not connected */
25};
26
Jeff Thompson432c8be2013-08-09 16:16:08 -070027/**
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 Thompsond1427fb2013-08-29 17:20:32 -070031static inline void ndn_SocketTransport_initialize(struct ndn_SocketTransport *self)
Jeff Thompson0aa754a2013-07-17 17:42:28 -070032{
33 self->socketDescriptor = -1;
34}
35
Jeff Thompson432c8be2013-08-09 16:16:08 -070036/**
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 Thompson0aa754a2013-07-17 17:42:28 -070044ndn_Error ndn_SocketTransport_connect(struct ndn_SocketTransport *self, ndn_SocketType socketType, char *host, unsigned short port);
45
Jeff Thompson432c8be2013-08-09 16:16:08 -070046/**
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 Thompson97223af2013-09-24 17:01:27 -070053ndn_Error ndn_SocketTransport_send(struct ndn_SocketTransport *self, uint8_t *data, size_t dataLength);
Jeff Thompson0aa754a2013-07-17 17:42:28 -070054
Jeff Thompson432c8be2013-08-09 16:16:08 -070055/**
56 * Check if there is data ready on the socket to be received with ndn_SocketTransport_receive.
Jeff Thompsonc7e07442013-08-19 15:25:43 -070057 * This does not block, and returns immediately.
Jeff Thompson432c8be2013-08-09 16:16:08 -070058 * @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 */
62ndn_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 Thompson0aa754a2013-07-17 17:42:28 -070073ndn_Error ndn_SocketTransport_receive
Jeff Thompson97223af2013-09-24 17:01:27 -070074 (struct ndn_SocketTransport *self, uint8_t *buffer, size_t bufferLength, size_t *nBytes);
Jeff Thompson0aa754a2013-07-17 17:42:28 -070075
Jeff Thompson432c8be2013-08-09 16:16:08 -070076/**
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 Thompson57963882013-08-05 16:01:25 -070081ndn_Error ndn_SocketTransport_close(struct ndn_SocketTransport *self);
82
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070083#ifdef __cplusplus
Jeff Thompson0aa754a2013-07-17 17:42:28 -070084}
85#endif
86
87#endif