Added Transport close method
diff --git a/ndn-cpp/c/errors.c b/ndn-cpp/c/errors.c
index f6dcfec..a5b0510 100644
--- a/ndn-cpp/c/errors.c
+++ b/ndn-cpp/c/errors.c
@@ -64,6 +64,8 @@
return "SocketTransport error in send";
case NDN_ERROR_SocketTransport_error_in_recv:
return "SocketTransport error in recv";
+ case NDN_ERROR_SocketTransport_error_in_close:
+ return "SocketTransport error in close";
default:
return "unrecognized ndn_Error code";
}
diff --git a/ndn-cpp/c/errors.h b/ndn-cpp/c/errors.h
index f8564d8..7400bf1 100644
--- a/ndn-cpp/c/errors.h
+++ b/ndn-cpp/c/errors.h
@@ -39,7 +39,8 @@
NDN_ERROR_SocketTransport_cannot_connect_to_socket,
NDN_ERROR_SocketTransport_socket_is_not_open,
NDN_ERROR_SocketTransport_error_in_send,
- NDN_ERROR_SocketTransport_error_in_recv
+ NDN_ERROR_SocketTransport_error_in_recv,
+ NDN_ERROR_SocketTransport_error_in_close
} ndn_Error;
/**
diff --git a/ndn-cpp/c/transport/SocketTransport.c b/ndn-cpp/c/transport/SocketTransport.c
index 187f0da..ee1f72a 100644
--- a/ndn-cpp/c/transport/SocketTransport.c
+++ b/ndn-cpp/c/transport/SocketTransport.c
@@ -98,3 +98,17 @@
return 0;
}
+
+ndn_Error ndn_SocketTransport_close(struct ndn_SocketTransport *self)
+{
+ if (self->socketDescriptor < 0)
+ // Already closed. Do nothing.
+ return 0;
+
+ if (close(self->socketDescriptor) != 0)
+ return NDN_ERROR_SocketTransport_error_in_close;
+
+ self->socketDescriptor = -1;
+
+ return 0;
+}
diff --git a/ndn-cpp/c/transport/SocketTransport.h b/ndn-cpp/c/transport/SocketTransport.h
index a5e89f7..6973d19 100644
--- a/ndn-cpp/c/transport/SocketTransport.h
+++ b/ndn-cpp/c/transport/SocketTransport.h
@@ -34,6 +34,8 @@
ndn_Error ndn_SocketTransport_receive
(struct ndn_SocketTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytes);
+ndn_Error ndn_SocketTransport_close(struct ndn_SocketTransport *self);
+
#ifdef __cplusplus
}
#endif
diff --git a/ndn-cpp/c/transport/TcpTransport.h b/ndn-cpp/c/transport/TcpTransport.h
index ec43806..6421d4b 100644
--- a/ndn-cpp/c/transport/TcpTransport.h
+++ b/ndn-cpp/c/transport/TcpTransport.h
@@ -39,6 +39,11 @@
return ndn_SocketTransport_receive(&self->base, buffer, bufferLength, nBytes);
}
+static inline ndn_Error ndn_TcpTransport_close(struct ndn_TcpTransport *self)
+{
+ return ndn_SocketTransport_close(&self->base);
+}
+
#ifdef __cplusplus
}
#endif
diff --git a/ndn-cpp/c/transport/UdpTransport.h b/ndn-cpp/c/transport/UdpTransport.h
index 262d5ec..ef113b2 100644
--- a/ndn-cpp/c/transport/UdpTransport.h
+++ b/ndn-cpp/c/transport/UdpTransport.h
@@ -39,6 +39,11 @@
return ndn_SocketTransport_receive(&self->base, buffer, bufferLength, nBytes);
}
+static inline ndn_Error ndn_UdpTransport_close(struct ndn_UdpTransport *self)
+{
+ return ndn_SocketTransport_close(&self->base);
+}
+
#ifdef __cplusplus
}
#endif