Move functionality into ndn_SocketTransport base class.
diff --git a/ndn-cpp/c/transport/UdpTransport.c b/ndn-cpp/c/transport/SocketTransport.c
similarity index 63%
rename from ndn-cpp/c/transport/UdpTransport.c
rename to ndn-cpp/c/transport/SocketTransport.c
index 0dc5d8e..187f0da 100644
--- a/ndn-cpp/c/transport/UdpTransport.c
+++ b/ndn-cpp/c/transport/SocketTransport.c
@@ -3,7 +3,7 @@
* See COPYING for copyright and distribution information.
*/
-#include "UdpTransport.h"
+#include "SocketTransport.h"
#include <stdio.h>
#include <stdlib.h>
@@ -15,7 +15,7 @@
#include <arpa/inet.h>
#include "../util/ndn_memory.h"
-ndn_Error ndn_UdpTransport_connect(struct ndn_UdpTransport *self, char *host, unsigned short port)
+ndn_Error ndn_SocketTransport_connect(struct ndn_SocketTransport *self, ndn_SocketType socketType, char *host, unsigned short port)
{
if (self->socketDescriptor >= 0) {
close(self->socketDescriptor);
@@ -25,14 +25,19 @@
struct addrinfo hints;
ndn_memset((unsigned char *)&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_DGRAM;
+ if (socketType == SOCKET_TCP)
+ hints.ai_socktype = SOCK_STREAM;
+ else if (socketType == SOCKET_UDP)
+ hints.ai_socktype = SOCK_DGRAM;
+ else
+ return NDN_ERROR_unrecognized_ndn_SocketTransport;
char portString[10];
sprintf(portString, "%d", port);
struct addrinfo *serverInfo;
if (getaddrinfo(host, portString, &hints, &serverInfo) != 0)
- return NDN_ERROR_UdpTransport_error_in_getaddrinfo;
+ return NDN_ERROR_SocketTransport_error_in_getaddrinfo;
// loop through all the results and connect to the first we can
struct addrinfo *p;
@@ -51,7 +56,7 @@
if (p == NULL) {
freeaddrinfo(serverInfo);
- return NDN_ERROR_UdpTransport_cannot_connect_to_socket;
+ return NDN_ERROR_SocketTransport_cannot_connect_to_socket;
}
freeaddrinfo(serverInfo);
@@ -60,15 +65,15 @@
return 0;
}
-ndn_Error ndn_UdpTransport_send(struct ndn_UdpTransport *self, unsigned char *data, unsigned int dataLength)
+ndn_Error ndn_SocketTransport_send(struct ndn_SocketTransport *self, unsigned char *data, unsigned int dataLength)
{
if (self->socketDescriptor < 0)
- return NDN_ERROR_UdpTransport_socket_is_not_open;
+ return NDN_ERROR_SocketTransport_socket_is_not_open;
int nBytes;
while (1) {
if ((nBytes = send(self->socketDescriptor, data, dataLength, 0)) < 0)
- return NDN_ERROR_UdpTransport_error_in_send;
+ return NDN_ERROR_SocketTransport_error_in_send;
if (nBytes >= dataLength)
break;
@@ -79,15 +84,15 @@
return 0;
}
-ndn_Error ndn_UdpTransport_receive
-(struct ndn_UdpTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytesOut)
+ndn_Error ndn_SocketTransport_receive
+(struct ndn_SocketTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytesOut)
{
if (self->socketDescriptor < 0)
- return NDN_ERROR_UdpTransport_socket_is_not_open;
+ return NDN_ERROR_SocketTransport_socket_is_not_open;
int nBytes;
if ((nBytes = recv(self->socketDescriptor, buffer, bufferLength, 0)) == -1)
- return NDN_ERROR_UdpTransport_error_in_recv;
+ return NDN_ERROR_SocketTransport_error_in_recv;
*nBytesOut = (unsigned int)nBytes;
diff --git a/ndn-cpp/c/transport/SocketTransport.h b/ndn-cpp/c/transport/SocketTransport.h
new file mode 100644
index 0000000..a5e89f7
--- /dev/null
+++ b/ndn-cpp/c/transport/SocketTransport.h
@@ -0,0 +1,41 @@
+/**
+ * @author: Jeff Thompson
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NDN_SOCKETTRANSPORT_H
+#define NDN_SOCKETTRANSPORT_H
+
+#include <sys/socket.h>
+#include "../errors.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum {
+ SOCKET_TCP,
+ SOCKET_UDP
+} ndn_SocketType;
+
+struct ndn_SocketTransport {
+ int socketDescriptor; /**< -1 if not connected */
+};
+
+static inline void ndn_SocketTransport_init(struct ndn_SocketTransport *self)
+{
+ self->socketDescriptor = -1;
+}
+
+ndn_Error ndn_SocketTransport_connect(struct ndn_SocketTransport *self, ndn_SocketType socketType, char *host, unsigned short port);
+
+ndn_Error ndn_SocketTransport_send(struct ndn_SocketTransport *self, unsigned char *data, unsigned int dataLength);
+
+ndn_Error ndn_SocketTransport_receive
+ (struct ndn_SocketTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytes);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/ndn-cpp/c/transport/TcpTransport.c b/ndn-cpp/c/transport/TcpTransport.c
deleted file mode 100644
index ea44d86..0000000
--- a/ndn-cpp/c/transport/TcpTransport.c
+++ /dev/null
@@ -1,95 +0,0 @@
-/**
- * @author: Jeff Thompson
- * See COPYING for copyright and distribution information.
- */
-
-#include "TcpTransport.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <netdb.h>
-#include <sys/types.h>
-#include <netinet/in.h>
-#include <sys/socket.h>
-#include <arpa/inet.h>
-#include "../util/ndn_memory.h"
-
-ndn_Error ndn_TcpTransport_connect(struct ndn_TcpTransport *self, char *host, unsigned short port)
-{
- if (self->socketDescriptor >= 0) {
- close(self->socketDescriptor);
- self->socketDescriptor = -1;
- }
-
- struct addrinfo hints;
- ndn_memset((unsigned char *)&hints, 0, sizeof(hints));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
-
- char portString[10];
- sprintf(portString, "%d", port);
-
- struct addrinfo *serverInfo;
- if (getaddrinfo(host, portString, &hints, &serverInfo) != 0)
- return NDN_ERROR_TcpTransport_error_in_getaddrinfo;
-
- // loop through all the results and connect to the first we can
- struct addrinfo *p;
- int socketDescriptor;
- for(p = serverInfo; p != NULL; p = p->ai_next) {
- if ((socketDescriptor = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
- continue;
-
- if (connect(socketDescriptor, p->ai_addr, p->ai_addrlen) == -1) {
- close(socketDescriptor);
- continue;
- }
-
- break;
- }
-
- if (p == NULL) {
- freeaddrinfo(serverInfo);
- return NDN_ERROR_TcpTransport_cannot_connect_to_socket;
- }
-
- freeaddrinfo(serverInfo);
- self->socketDescriptor = socketDescriptor;
-
- return 0;
-}
-
-ndn_Error ndn_TcpTransport_send(struct ndn_TcpTransport *self, unsigned char *data, unsigned int dataLength)
-{
- if (self->socketDescriptor < 0)
- return NDN_ERROR_TcpTransport_socket_is_not_open;
-
- int nBytes;
- while (1) {
- if ((nBytes = send(self->socketDescriptor, data, dataLength, 0)) < 0)
- return NDN_ERROR_TcpTransport_error_in_send;
- if (nBytes >= dataLength)
- break;
-
- // Send more.
- dataLength -= nBytes;
- }
-
- return 0;
-}
-
-ndn_Error ndn_TcpTransport_receive
-(struct ndn_TcpTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytesOut)
-{
- if (self->socketDescriptor < 0)
- return NDN_ERROR_TcpTransport_socket_is_not_open;
-
- int nBytes;
- if ((nBytes = recv(self->socketDescriptor, buffer, bufferLength, 0)) == -1)
- return NDN_ERROR_TcpTransport_error_in_recv;
-
- *nBytesOut = (unsigned int)nBytes;
-
- return 0;
-}
diff --git a/ndn-cpp/c/transport/TcpTransport.h b/ndn-cpp/c/transport/TcpTransport.h
index 0a1742c..ec43806 100644
--- a/ndn-cpp/c/transport/TcpTransport.h
+++ b/ndn-cpp/c/transport/TcpTransport.h
@@ -8,27 +8,36 @@
#ifndef NDN_TCPTRANSPORT_H
#define NDN_TCPTRANSPORT_H
-#include "../errors.h"
+#include "SocketTransport.h"
#ifdef __cplusplus
extern "C" {
#endif
struct ndn_TcpTransport {
- int socketDescriptor; /**< -1 if not connected */
+ struct ndn_SocketTransport base;
};
static inline void ndn_TcpTransport_init(struct ndn_TcpTransport *self)
{
- self->socketDescriptor = -1;
+ ndn_SocketTransport_init(&self->base);
}
-ndn_Error ndn_TcpTransport_connect(struct ndn_TcpTransport *self, char *host, unsigned short port);
+static inline ndn_Error ndn_TcpTransport_connect(struct ndn_TcpTransport *self, char *host, unsigned short port)
+{
+ return ndn_SocketTransport_connect(&self->base, SOCKET_TCP, host, port);
+}
-ndn_Error ndn_TcpTransport_send(struct ndn_TcpTransport *self, unsigned char *data, unsigned int dataLength);
+static inline ndn_Error ndn_TcpTransport_send(struct ndn_TcpTransport *self, unsigned char *data, unsigned int dataLength)
+{
+ return ndn_SocketTransport_send(&self->base, data, dataLength);
+}
-ndn_Error ndn_TcpTransport_receive
- (struct ndn_TcpTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytes);
+static inline ndn_Error ndn_TcpTransport_receive
+ (struct ndn_TcpTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytes)
+{
+ return ndn_SocketTransport_receive(&self->base, buffer, bufferLength, nBytes);
+}
#ifdef __cplusplus
}
diff --git a/ndn-cpp/c/transport/UdpTransport.h b/ndn-cpp/c/transport/UdpTransport.h
index 924e4a7..262d5ec 100644
--- a/ndn-cpp/c/transport/UdpTransport.h
+++ b/ndn-cpp/c/transport/UdpTransport.h
@@ -1,32 +1,43 @@
-/**
- * @author: Jeff Thompson
- * See COPYING for copyright and distribution information.
+/*
+ * File: UdpTransport.h
+ * Author: jefft0
+ *
+ * Created on July 14, 2013, 4:15 PM
*/
#ifndef NDN_UDPTRANSPORT_H
#define NDN_UDPTRANSPORT_H
-#include "../errors.h"
+#include "SocketTransport.h"
#ifdef __cplusplus
extern "C" {
#endif
struct ndn_UdpTransport {
- int socketDescriptor; /**< -1 if not connected */
+ struct ndn_SocketTransport base;
};
static inline void ndn_UdpTransport_init(struct ndn_UdpTransport *self)
{
- self->socketDescriptor = -1;
+ ndn_SocketTransport_init(&self->base);
}
-ndn_Error ndn_UdpTransport_connect(struct ndn_UdpTransport *self, char *host, unsigned short port);
+static inline ndn_Error ndn_UdpTransport_connect(struct ndn_UdpTransport *self, char *host, unsigned short port)
+{
+ return ndn_SocketTransport_connect(&self->base, SOCKET_UDP, host, port);
+}
-ndn_Error ndn_UdpTransport_send(struct ndn_UdpTransport *self, unsigned char *data, unsigned int dataLength);
+static inline ndn_Error ndn_UdpTransport_send(struct ndn_UdpTransport *self, unsigned char *data, unsigned int dataLength)
+{
+ return ndn_SocketTransport_send(&self->base, data, dataLength);
+}
-ndn_Error ndn_UdpTransport_receive
- (struct ndn_UdpTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytes);
+static inline ndn_Error ndn_UdpTransport_receive
+ (struct ndn_UdpTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytes)
+{
+ return ndn_SocketTransport_receive(&self->base, buffer, bufferLength, nBytes);
+}
#ifdef __cplusplus
}