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
diff --git a/ndn-cpp/transport/TcpTransport.cpp b/ndn-cpp/transport/TcpTransport.cpp
index a6a6663..bf5180a 100644
--- a/ndn-cpp/transport/TcpTransport.cpp
+++ b/ndn-cpp/transport/TcpTransport.cpp
@@ -51,4 +51,11 @@
}
}
+void TcpTransport::close()
+{
+ ndn_Error error;
+ if (error = ndn_TcpTransport_close(&transport_))
+ throw std::runtime_error(ndn_getErrorString(error));
+}
+
}
diff --git a/ndn-cpp/transport/TcpTransport.hpp b/ndn-cpp/transport/TcpTransport.hpp
index 88e61fb..da8b103 100644
--- a/ndn-cpp/transport/TcpTransport.hpp
+++ b/ndn-cpp/transport/TcpTransport.hpp
@@ -29,6 +29,8 @@
virtual void send(const unsigned char *data, unsigned int dataLength);
virtual void tempReceive();
+
+ virtual void close();
private:
struct ndn_TcpTransport transport_;
diff --git a/ndn-cpp/transport/Transport.cpp b/ndn-cpp/transport/Transport.cpp
index e0aab03..1e63649 100644
--- a/ndn-cpp/transport/Transport.cpp
+++ b/ndn-cpp/transport/Transport.cpp
@@ -19,5 +19,9 @@
{
throw logic_error("unimplemented");
}
+
+void Transport::close()
+{
+}
}
diff --git a/ndn-cpp/transport/Transport.hpp b/ndn-cpp/transport/Transport.hpp
index af1a2c0..dedfffd 100644
--- a/ndn-cpp/transport/Transport.hpp
+++ b/ndn-cpp/transport/Transport.hpp
@@ -25,6 +25,11 @@
{
send(&data[0], data.size());
}
+
+ /**
+ * Close the connection. This base class implementation does nothing, but your derived class can override.
+ */
+ virtual void close();
};
}
diff --git a/ndn-cpp/transport/UdpTransport.cpp b/ndn-cpp/transport/UdpTransport.cpp
index 52b62a5..5fc4a71 100644
--- a/ndn-cpp/transport/UdpTransport.cpp
+++ b/ndn-cpp/transport/UdpTransport.cpp
@@ -51,4 +51,11 @@
}
}
+void UdpTransport::close()
+{
+ ndn_Error error;
+ if (error = ndn_UdpTransport_close(&transport_))
+ throw std::runtime_error(ndn_getErrorString(error));
+}
+
}
diff --git a/ndn-cpp/transport/UdpTransport.hpp b/ndn-cpp/transport/UdpTransport.hpp
index 781effd..9043e5c 100644
--- a/ndn-cpp/transport/UdpTransport.hpp
+++ b/ndn-cpp/transport/UdpTransport.hpp
@@ -30,6 +30,8 @@
virtual void tempReceive();
+ virtual void close();
+
private:
struct ndn_UdpTransport transport_;
Face *face_;