Mercurial > code
diff C++/Socket.cpp @ 247:806dbb6011c7
Socket:
* Make copyable so it's easier to use with SocketListener
* Remove domain(), type() and protocol() functions because accept() can not determine them
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 30 Sep 2014 20:44:50 +0200 |
parents | 9cfa6fbc9c03 |
children | 4ad3c85ab73e |
line wrap: on
line diff
--- 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<Standard>()) - , 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<Standard>()) + , 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)