changeset 446:8396fd66e57a

Socket: use non-owning pointer for optional addresses
author David Demelier <markand@malikania.fr>
date Mon, 26 Oct 2015 21:32:17 +0100
parents f5e62f6c1475
children 828d3dc89f2d
files C++/modules/Socket/Sockets.h C++/tests/Socket/main.cpp
diffstat 2 files changed, 10 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/C++/modules/Socket/Sockets.h	Mon Oct 26 19:29:34 2015 +0100
+++ b/C++/modules/Socket/Sockets.h	Mon Oct 26 21:32:17 2015 +0100
@@ -623,26 +623,15 @@
 	/**
 	 * Accept a pending connection.
 	 *
-	 * @param info the client address
+	 * @param info the address where to store client's information (optional)
 	 * @return the new socket
 	 * @throw Error on errors
 	 */
-	inline Socket<Address, Type> accept(Address &info)
-	{
-		return m_type.accept(*this, info);
-	}
-
-	/**
-	 * Overloaded function without information.
-	 *
-	 * @return the new socket
-	 * @throw Error on errors
-	 */
-	inline Socket<Address, Type> accept()
+	inline Socket<Address, Type> accept(Address *info = nullptr)
 	{
 		Address dummy;
 
-		return accept(dummy);
+		return m_type.accept(*this, info == nullptr ? dummy : *info);
 	}
 
 	/**
@@ -747,9 +736,11 @@
 	 * @return the number of bytes received
 	 * @throw Error on error
 	 */
-	unsigned recvfrom(void *data, unsigned length, Address &info)
+	inline unsigned recvfrom(void *data, unsigned length, Address *info = nullptr)
 	{
-		return m_type.recvfrom(*this, data, length, info);
+		Address dummy;
+
+		return m_type.recvfrom(*this, data, length, info == nullptr ? dummy : *info);
 	}
 
 	/**
@@ -760,7 +751,7 @@
 	 * @return the string
 	 * @throw Error on error
 	 */
-	inline std::string recvfrom(unsigned count, Address &info)
+	inline std::string recvfrom(unsigned count, Address *info = nullptr)
 	{
 		std::string result;
 
@@ -772,20 +763,6 @@
 	}
 
 	/**
-	 * Overloaded function.
-	 *
-	 * @param count the number of bytes to read
-	 * @return the string
-	 * @throw Error on errors
-	 */
-	inline std::string recvfrom(unsigned count)
-	{
-		Address dummy;
-
-		return recvfrom(count, dummy);
-	}
-
-	/**
 	 * Close the socket.
 	 *
 	 * Automatically called from the destructor.
--- a/C++/tests/Socket/main.cpp	Mon Oct 26 19:29:34 2015 +0100
+++ b/C++/tests/Socket/main.cpp	Mon Oct 26 21:32:17 2015 +0100
@@ -131,7 +131,7 @@
 		Ipv4 info{"*", 16000};
 
 		m_server.bind(info);
-		auto msg = m_server.recvfrom(512, client);
+		auto msg = m_server.recvfrom(512, &client);
 
 		ASSERT_EQ("hello world", msg);
 
@@ -146,7 +146,7 @@
 
 		m_client.sendto("hello world", info);
 
-		ASSERT_EQ("hello world", m_client.recvfrom(512, info));
+		ASSERT_EQ("hello world", m_client.recvfrom(512, &info));
 
 		m_client.close();
 	});