changeset 175:73d8f140bc86

Add some getters
author David Demelier <markand@malikania.fr>
date Thu, 12 Sep 2013 13:36:12 +0200
parents 9f22bd478f21
children ba62ab219fc4
files C++/Socket.cpp C++/Socket.h
diffstat 2 files changed, 55 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/C++/Socket.cpp	Thu Sep 12 11:44:34 2013 +0200
+++ b/C++/Socket.cpp	Thu Sep 12 13:36:12 2013 +0200
@@ -92,10 +92,16 @@
 }
 
 Socket::Socket()
+	: m_domain(0)
+	, m_type(0)
+	, m_protocol(0)
 {
 }
 
 Socket::Socket(int domain, int type, int protocol)
+	: m_domain(domain)
+	, m_type(type)
+	, m_protocol(protocol)
 {
 	m_socket = socket(domain, type, protocol);
 
@@ -103,20 +109,26 @@
 		throw SocketError(getLastSysError());
 }
 
-Socket::Socket(Socket::Type sock)
-	: m_socket(sock)
-{
-}
-
-Socket::~Socket()
-{
-}
-
 Socket::Type Socket::getSocket() const
 {
 	return m_socket;
 }
 
+int Socket::getDomain() const
+{
+	return m_domain;
+}
+
+int Socket::getType() const
+{
+	return m_type;
+}
+
+int Socket::getProtocol() const
+{
+	return m_protocol;
+}
+
 void Socket::set(int level, int name, const void *arg, unsigned argLen)
 {
 	if (setsockopt(m_socket, level, name, (Socket::ConstArg)arg, argLen) == SOCKET_ERROR)
@@ -173,15 +185,19 @@
 
 Socket Socket::accept(SocketAddress &info)
 {
-	Socket::Type sock;
+	Socket s;
 
 	info.m_addrlen = sizeof (sockaddr_storage);
-	sock = ::accept(m_socket, (sockaddr *)&info.m_addr, &info.m_addrlen);
+	s.m_socket = ::accept(m_socket, (sockaddr *)&info.m_addr, &info.m_addrlen);
 
-	if (sock == INVALID_SOCKET)
+	// Usually accept works only with SOCK_STREAM
+	s.m_domain	= info.m_addr.ss_family;
+	s.m_type	= SOCK_STREAM;
+
+	if (s.m_socket == INVALID_SOCKET)
 		throw SocketError(getLastSysError());
 
-	return Socket(sock);
+	return s;
 }
 
 void Socket::listen(int max)
@@ -265,7 +281,7 @@
 	return s1.getSocket() == s2.getSocket();
 }
 
-bool operator<(const Socket &s, const Socket &s2)
+bool operator<(const Socket &s1, const Socket &s2)
 {
 	return s1.getSocket() < s2.getSocket();
 }
--- a/C++/Socket.h	Thu Sep 12 11:44:34 2013 +0200
+++ b/C++/Socket.h	Thu Sep 12 13:36:12 2013 +0200
@@ -87,8 +87,11 @@
 	typedef void *		Arg;
 #endif
 
-protected:
+private:
 	Socket::Type m_socket;
+	int m_domain;
+	int m_type;
+	int m_protocol;
 
 public:
 	/**
@@ -124,18 +127,6 @@
 	Socket(int domain, int type, int protocol);
 
 	/**
-	 * Constructor with a socket already opened.
-	 *
-	 * @param sock the socket
-	 */
-	Socket(Socket::Type sock);
-
-	/**
-	 * Default destructor.
-	 */
-	virtual ~Socket();
-
-	/**
 	 * Get the socket.
 	 *
 	 * @return the socket
@@ -143,6 +134,27 @@
 	Type getSocket() const;
 
 	/**
+	 * Get the domain.
+	 *
+	 * @return the domain
+	 */
+	int getDomain() const;
+
+	/**
+	 * Get the type of socket.
+	 *
+	 * @return the type
+	 */
+	int getType() const;
+
+	/**
+	 * Get the protocol.
+	 *
+	 * @return the protocol
+	 */
+	int getProtocol() const;
+
+	/**
 	 * Set an option for the socket.
 	 *
 	 * @param level the setting level