# HG changeset patch # User David Demelier # Date 1412102690 -7200 # Node ID 806dbb6011c798128a6cc5335706bd787be281c1 # Parent 9cfa6fbc9c03d6b68616f9e21198d12aa0c61bbe Socket: * Make copyable so it's easier to use with SocketListener * Remove domain(), type() and protocol() functions because accept() can not determine them diff -r 9cfa6fbc9c03 -r 806dbb6011c7 C++/Socket.cpp --- a/C++/Socket.cpp Mon Sep 29 12:57:43 2014 +0200 +++ b/C++/Socket.cpp Tue Sep 30 20:44:50 2014 +0200 @@ -25,9 +25,9 @@ * SocketError implementation * -------------------------------------------------------- */ -SocketError::SocketError(const std::string &error) +SocketError::SocketError(std::string error) { - m_error = error; + m_error = std::move(error); } const char *SocketError::what() const noexcept @@ -145,7 +145,7 @@ // Usually accept works only with SOCK_STREAM info = SocketAddress(address, addrlen); - return Socket(handle, address.ss_family, s.type(), s.protocol()); + return Socket(handle); } void Standard::listen(Socket &s, int max) @@ -213,68 +213,19 @@ * Socket code * -------------------------------------------------------- */ -Socket::Socket() - : m_domain(0) - , m_type(0) - , m_protocol(0) -{ -} - Socket::Socket(int domain, int type, int protocol) : m_interface(std::make_unique()) - , m_domain(domain) - , m_type(type) - , m_protocol(protocol) { m_handle = socket(domain, type, protocol); if (m_handle == INVALID_SOCKET) throw SocketError(syserror()); - - m_open = true; -} - -Socket::Socket(Handle handle, int domain, int type, int protocol) - : m_handle(handle) - , m_domain(domain) - , m_type(type) - , m_protocol(protocol) - , m_open(true) -{ } -Socket::Socket(Socket &&other) noexcept - : m_interface(std::move(other.m_interface)) - , m_handle(std::move(other.m_handle)) - , m_domain(other.m_domain) - , m_type(other.m_type) - , m_protocol(other.m_protocol) - , m_open(true) -{ - // invalidate other - other.m_handle = 0; - other.m_open = false; -} - -Socket &Socket::operator=(Socket &&other) noexcept +Socket::Socket(Handle handle) + : m_interface(std::make_shared()) + , m_handle(handle) { - m_interface = std::move(other.m_interface); - m_handle = std::move(other.m_handle); - m_domain = other.m_domain; - m_type = other.m_type; - m_protocol = other.m_protocol; - m_open = true; - - // invalidate other - other.m_handle = 0; - other.m_open = false; - - return *this; -} - -Socket::~Socket() -{ - close(); } Socket::Handle Socket::handle() const @@ -282,21 +233,6 @@ return m_handle; } -int Socket::domain() const -{ - return m_domain; -} - -int Socket::type() const -{ - return m_type; -} - -int Socket::protocol() const -{ - return m_protocol; -} - void Socket::set(int level, int name, const void *arg, unsigned argLen) { if (setsockopt(m_handle, level, name, (Socket::ConstArg)arg, argLen) == SOCKET_ERROR) diff -r 9cfa6fbc9c03 -r 806dbb6011c7 C++/Socket.h --- a/C++/Socket.h Mon Sep 29 12:57:43 2014 +0200 +++ b/C++/Socket.h Tue Sep 30 20:44:50 2014 +0200 @@ -62,9 +62,9 @@ std::string m_error; public: - SocketError(const std::string &error); + SocketError(std::string error); - const char * what(void) const noexcept override; + const char *what() const noexcept override; }; /** @@ -189,20 +189,11 @@ using Arg = void *; #endif - using Iface = std::unique_ptr; - -private: - // Be sure to not copy the socket - Socket(const Socket &) = delete; - Socket &operator=(const Socket &) = delete; + using Iface = std::shared_ptr; protected: Iface m_interface; //!< the interface - Handle m_handle; //!< the socket shared pointer - int m_domain; //!< the domain - int m_type; //!< the type - int m_protocol; //!< the protocol - bool m_open { false }; //!< socket is valid and open + Handle m_handle { 0 }; //!< the socket shared pointer public: /** @@ -225,7 +216,7 @@ /** * Default constructor. */ - Socket(); + Socket() = default; /** * Constructor to create a new socket. @@ -241,31 +232,13 @@ * Create a socket object with a already initialized socket. * * @param handle the handle - * @param domain the domain - * @param type the type - * @param protocol the protocol */ - Socket(Handle handle, int domain, int type, int protocol); - - /** - * Move constructor, transfers the socket handle. - * - * @param other the other - */ - Socket(Socket &&other) noexcept; - - /** - * Move operator, transfers the socket handle. - * - * @param other the other - * @return *this - */ - Socket &operator=(Socket &&) noexcept; + Socket(Handle handle); /** * Close the socket. */ - virtual ~Socket(); + virtual ~Socket() = default; /** * Get the socket. @@ -275,27 +248,6 @@ Handle handle() const; /** - * Get the domain. - * - * @return the domain - */ - int domain() const; - - /** - * Get the type of socket. - * - * @return the type - */ - int type() const; - - /** - * Get the protocol. - * - * @return the protocol - */ - int protocol() const; - - /** * Set an option for the socket. * * @param level the setting level @@ -326,10 +278,7 @@ */ inline void close() { - if (m_open) { - m_interface->close(*this); - m_open = false; - } + m_interface->close(*this); } /** diff -r 9cfa6fbc9c03 -r 806dbb6011c7 C++/SocketListener.cpp --- a/C++/SocketListener.cpp Mon Sep 29 12:57:43 2014 +0200 +++ b/C++/SocketListener.cpp Tue Sep 30 20:44:50 2014 +0200 @@ -30,7 +30,7 @@ m_sockets.reserve(count); } -void SocketListener::add(Socket &&s) +void SocketListener::add(Socket s) { m_sockets.push_back(std::move(s)); } diff -r 9cfa6fbc9c03 -r 806dbb6011c7 C++/SocketListener.h --- a/C++/SocketListener.h Mon Sep 29 12:57:43 2014 +0200 +++ b/C++/SocketListener.h Tue Sep 30 20:44:50 2014 +0200 @@ -55,7 +55,7 @@ * * @param s the socket */ - void add(Socket &&s); + void add(Socket s); /** * Remove a socket from the list. @@ -80,30 +80,14 @@ * Wait for an event in the socket list. If both s and us are set to 0 then * it waits indefinitely. * - * Taking ownership of the returned socket is undefined behaviour. - * * @param s the timeout in seconds * @param us the timeout in milliseconds * @see take - * @return the socket ready owner by the SocketListener + * @return the socket ready * @throw SocketError on error * @throw SocketTimeout on timeout */ Socket &select(int s = 0, int us = 0); - - /** - * Take back all sockets that listener own. - * - * @param func the function to call (void f(Socket &&s)) - */ - template - void take(Func func) - { - for (auto &x : m_sockets) - func(std::move(x)); - - clear(); - } }; #endif // !_SOCKET_LISTENER_H_