blob: 6c2da6e113241e2cb60d7a347592af05c7cd12b9 [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 Thompson0aa754a2013-07-17 17:42:28 -070029static inline void ndn_SocketTransport_init(struct ndn_SocketTransport *self)
30{
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.
55 * @param self A pointer to the ndn_SocketTransport struct.
56 * @param receiveIsReady This will be set to 1 if data is ready, 0 if not.
57 * @return 0 for success, else an error code.
58 */
59ndn_Error ndn_SocketTransport_receiveIsReady(struct ndn_SocketTransport *self, int *receiveIsReady);
60
61/**
62 * Receive data from the socket. NOTE: This is a blocking call. You should first call ndn_SocketTransport_receiveIsReady
63 * to make sure there is data ready to receive.
64 * @param self A pointer to the ndn_SocketTransport struct.
65 * @param buffer A pointer to the buffer to receive the data.
66 * @param bufferLength The maximum length of buffer.
67 * @param nBytes Return the number of bytes received into buffer.
68 * @return 0 for success, else an error code.
69 */
Jeff Thompson0aa754a2013-07-17 17:42:28 -070070ndn_Error ndn_SocketTransport_receive
71 (struct ndn_SocketTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytes);
72
Jeff Thompson432c8be2013-08-09 16:16:08 -070073/**
74 * Close the socket.
75 * @param self A pointer to the ndn_SocketTransport struct.
76 * @return 0 for success, else an error code.
77 */
Jeff Thompson57963882013-08-05 16:01:25 -070078ndn_Error ndn_SocketTransport_close(struct ndn_SocketTransport *self);
79
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070080#ifdef __cplusplus
Jeff Thompson0aa754a2013-07-17 17:42:28 -070081}
82#endif
83
84#endif