changeset 173:18ad49172e6c

Update documentation and add Unix sockets
author David Demelier <markand@malikania.fr>
date Thu, 12 Sep 2013 11:23:08 +0200
parents a61cddaf7547
children 9f22bd478f21
files C++/Socket.h C++/SocketAddress.cpp C++/SocketAddress.h
diffstat 3 files changed, 67 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/C++/Socket.h	Wed Sep 11 15:27:00 2013 +0200
+++ b/C++/Socket.h	Thu Sep 12 11:23:08 2013 +0200
@@ -118,10 +118,10 @@
 	 *
 	 * @param domain the domain
 	 * @param type the type
-	 * @param protocol the optional protocol
+	 * @param protocol the protocol
 	 * @throw SocketError on error
 	 */
-	Socket(int domain, int type, int protocol = 0);
+	Socket(int domain, int type, int protocol);
 
 	/**
 	 * Constructor with a socket already opened.
--- a/C++/SocketAddress.cpp	Wed Sep 11 15:27:00 2013 +0200
+++ b/C++/SocketAddress.cpp	Thu Sep 12 11:23:08 2013 +0200
@@ -16,6 +16,10 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <cerrno>
+#include <cstdio>
+#include <cstring>
+
 #include "SocketAddress.h"
 
 /* --------------------------------------------------------
@@ -81,6 +85,34 @@
 }
 
 /* --------------------------------------------------------
+ * AddressUnix implementation
+ * -------------------------------------------------------- */
+
+#if !defined(_WIN32)
+
+#include <sys/un.h>
+
+AddressUnix::AddressUnix(const std::string &path, bool rm)
+{
+	sockaddr_un *sun = (sockaddr_un *)&m_addr;
+
+	// Silently remove the file even if it fails
+	if (rm)
+		::remove(path.c_str());
+
+	// Copy the path
+	memset(sun->sun_path, 0, sizeof (sun->sun_path));
+	strncpy(sun->sun_path, path.c_str(), sizeof (sun->sun_path) - 1);
+
+	// Set the parameters
+	sun->sun_family = AF_UNIX;
+	sun->sun_len = SUN_LEN(sun);
+	m_addrlen = SUN_LEN(sun);
+}
+
+#endif // _WIN32
+
+/* --------------------------------------------------------
  * SocketAddress implementation
  * -------------------------------------------------------- */
 
--- a/C++/SocketAddress.h	Wed Sep 11 15:27:00 2013 +0200
+++ b/C++/SocketAddress.h	Thu Sep 12 11:23:08 2013 +0200
@@ -21,6 +21,17 @@
 
 #include "Socket.h"
 
+/**
+ * @class SocketAddress
+ * @brief base class for socket addresses
+ *
+ * This class is mostly used to bind, connect or getting information
+ * on socket clients.
+ *
+ * @see BindAddressIP
+ * @see ConnectAddressIP
+ * @see AddressUnix
+ */
 class SocketAddress
 {
 protected:
@@ -112,4 +123,26 @@
 	ConnectAddressIP(const std::string &host, unsigned port, int family, int type = SOCK_STREAM);
 };
 
+#if !defined(_WIN32)
+
+/**
+ * @class AddressUnix
+ * @brief unix family sockets
+ *
+ * Create an address to a specific path. Only available on Unix.
+ */
+class AddressUnix : public SocketAddress
+{
+public:
+	/**
+	 * Construct an address to a path.
+	 *
+	 * @param path the path
+	 * @param rm remove the file before (default: false)
+	 */
+	AddressUnix(const std::string &path, bool rm = false);
+};
+
+#endif // ! !_WIN32
+
 #endif // !_SOCKET_ADDRESS_H_