Mercurial > code
changeset 250:b686a09fb9c6
Merge
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 01 Oct 2014 14:39:24 +0200 |
parents | 3b4ae8feca1c (current diff) 806dbb6011c7 (diff) |
children | 0b7566e27eaa |
files | C++/Tests/Directory/TestDirectory.cpp C++/Tests/Directory/TestDirectory.h C++/Tests/Directory/main.cpp C++/Tests/DynLib/TestDynLib.cpp C++/Tests/DynLib/TestDynLib.h C++/Tests/Hash/TestHash.cpp C++/Tests/Hash/TestHash.h C++/Tests/Hash/main.cpp C++/Tests/Luae/TestLuae.cpp C++/Tests/Luae/TestLuae.h C++/Tests/Luae/TestLuaeClass.cpp C++/Tests/Luae/TestLuaeClass.h C++/Tests/Luae/main-class.cpp C++/Tests/Luae/main.cpp C++/Tests/Pack/TestPack.cpp C++/Tests/Pack/TestPack.h C++/Tests/Pack/main.cpp C++/Tests/Parser/TestParser.cpp C++/Tests/Parser/TestParser.h C++/Tests/Parser/main.cpp |
diffstat | 6 files changed, 52 insertions(+), 150 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/Socket.cpp Wed Oct 01 14:38:25 2014 +0200 +++ b/C++/Socket.cpp Wed Oct 01 14:39:24 2014 +0200 @@ -16,7 +16,6 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include <cerrno> #include <cstring> #include "Socket.h" @@ -26,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 @@ -115,7 +114,6 @@ auto &sa = addr.address(); auto addrlen = addr.length(); - if (::bind(s.handle(), (sockaddr *)&sa, addrlen) == SOCKET_ERROR) throw SocketError(Socket::syserror()); } @@ -147,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) @@ -215,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 @@ -284,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)
--- a/C++/Socket.h Wed Oct 01 14:38:25 2014 +0200 +++ b/C++/Socket.h Wed Oct 01 14:39:24 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<SocketInterface>; - -private: - // Be sure to not copy the socket - Socket(const Socket &) = delete; - Socket &operator=(const Socket &) = delete; + using Iface = std::shared_ptr<SocketInterface>; 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); } /**
--- a/C++/SocketAddress.cpp Wed Oct 01 14:38:25 2014 +0200 +++ b/C++/SocketAddress.cpp Wed Oct 01 14:39:24 2014 +0200 @@ -16,6 +16,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include <algorithm> #include <cstring> #include "Socket.h" @@ -111,3 +112,19 @@ { return m_addrlen; } + +bool operator<(const SocketAddress &s1, const SocketAddress &s2) +{ + const auto &array1 = reinterpret_cast<const unsigned char *>(&s1.address()); + const auto &array2 = reinterpret_cast<const unsigned char *>(&s2.address()); + + return std::lexicographical_compare(array1, array1 + s1.length(), array2, array2 + s2.length()); +} + +bool operator==(const SocketAddress &s1, const SocketAddress &s2) +{ + const auto &array1 = reinterpret_cast<const unsigned char *>(&s1.address()); + const auto &array2 = reinterpret_cast<const unsigned char *>(&s2.address()); + + return std::equal(array1, array1 + s1.length(), array2, array2 + s2.length()); +}
--- a/C++/SocketAddress.h Wed Oct 01 14:38:25 2014 +0200 +++ b/C++/SocketAddress.h Wed Oct 01 14:39:24 2014 +0200 @@ -66,6 +66,24 @@ * @return the address */ const sockaddr_storage &address() const; + + /** + * Compare the addresses. The check is lexicographical. + * + * @param s1 the first address + * @param s2 the second address + * @return true if s1 is less than s2 + */ + friend bool operator<(const SocketAddress &s1, const SocketAddress &s2); + + /** + * Compare the addresses. + * + * @param s1 the first address + * @param s2 the second address + * @return true if s1 == s2 + */ + friend bool operator==(const SocketAddress &s1, const SocketAddress &s2); }; namespace address {
--- a/C++/SocketListener.cpp Wed Oct 01 14:38:25 2014 +0200 +++ b/C++/SocketListener.cpp Wed Oct 01 14:39:24 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)); }
--- a/C++/SocketListener.h Wed Oct 01 14:38:25 2014 +0200 +++ b/C++/SocketListener.h Wed Oct 01 14:39:24 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 <typename Func> - void take(Func func) - { - for (auto &x : m_sockets) - func(std::move(x)); - - clear(); - } }; #endif // !_SOCKET_LISTENER_H_