Mercurial > code
changeset 277:b544a599e08e
Socket: remove enum class
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 23 Oct 2014 17:59:14 +0200 |
parents | 05f0a3e09cbf |
children | adcae2bde2f0 |
files | C++/SocketListener.cpp C++/SocketListener.h C++/Tests/Sockets/main.cpp |
diffstat | 3 files changed, 106 insertions(+), 143 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/SocketListener.cpp Thu Oct 23 17:08:13 2014 +0200 +++ b/C++/SocketListener.cpp Thu Oct 23 17:59:14 2014 +0200 @@ -38,7 +38,7 @@ private: std::set<Socket> m_rsockets; std::set<Socket> m_wsockets; - std::map<Socket::Handle, std::tuple<Socket, SocketDirection>> m_lookup; + std::map<Socket::Handle, std::tuple<Socket, int>> m_lookup; fd_set m_readset; fd_set m_writeset; @@ -47,8 +47,8 @@ public: SelectMethod(); - void add(Socket &&s, SocketDirection direction) override; - void remove(const Socket &s, SocketDirection direction) override; + void add(Socket s, int direction) override; + void remove(const Socket &s, int direction) override; void list(const SocketListener::MapFunc &func) override; void clear() override; unsigned size() const override; @@ -62,18 +62,18 @@ FD_ZERO(&m_writeset); } -void SelectMethod::add(Socket &&s, SocketDirection direction) +void SelectMethod::add(Socket s, int direction) { if (m_lookup.count(s.handle()) > 0) std::get<1>(m_lookup[s.handle()]) |= direction; else m_lookup[s.handle()] = std::make_tuple(s, direction); - if ((direction & SocketDirection::Read) == SocketDirection::Read) { + if (direction & Read) { m_rsockets.insert(s); FD_SET(s.handle(), &m_readset); } - if ((direction & SocketDirection::Write) == SocketDirection::Write) { + if (direction & Write) { m_wsockets.insert(s); FD_SET(s.handle(), &m_writeset); } @@ -82,18 +82,18 @@ m_max = s.handle(); } -void SelectMethod::remove(const Socket &s, SocketDirection direction) +void SelectMethod::remove(const Socket &s, int direction) { std::get<1>(m_lookup[s.handle()]) &= ~(direction); if (static_cast<int>(std::get<1>(m_lookup[s.handle()])) == 0) m_lookup.erase(s.handle()); - if ((direction & SocketDirection::Read) == SocketDirection::Read) { + if (direction & Read) { m_rsockets.erase(s.handle()); FD_CLR(s.handle(), &m_readset); } - if ((direction & SocketDirection::Write) == SocketDirection::Write) { + if (direction & Write) { m_wsockets.erase(s.handle()); FD_CLR(s.handle(), &m_writeset); } @@ -157,10 +157,10 @@ std::vector<SocketStatus> sockets; for (auto &c : m_lookup) if (FD_ISSET(c.first, &m_readset)) - sockets.push_back({ std::get<0>(c.second), SocketDirection::Read }); + sockets.push_back({ std::get<0>(c.second), Read }); for (auto &c : m_lookup) if (FD_ISSET(c.first, &m_writeset)) - sockets.push_back({ std::get<0>(c.second), SocketDirection::Write }); + sockets.push_back({ std::get<0>(c.second), Write }); return sockets; } @@ -185,33 +185,33 @@ std::vector<pollfd> m_fds; std::map<Socket::Handle, Socket> m_lookup; - inline short topoll(SocketDirection direction) + inline short topoll(int direction) { short result(0); - if ((direction & SocketDirection::Read) == SocketDirection::Read) + if (direction & Read) result |= POLLIN; - if ((direction & SocketDirection::Write) == SocketDirection::Write) + if (direction & Write) result |= POLLOUT; return result; } - inline SocketDirection todirection(short event) + inline int todirection(short event) { - SocketDirection direction = static_cast<SocketDirection>(0); + int direction{}; if (event & POLLIN) - direction |= SocketDirection::Read; + direction |= Read; if (event & POLLOUT) - direction |= SocketDirection::Write; + direction |= Write; return direction; } public: - void add(Socket &&s, SocketDirection direction) override; - void remove(const Socket &s, SocketDirection direction) override; + void add(Socket s, int direction) override; + void remove(const Socket &s, int direction) override; void list(const SocketListener::MapFunc &func) override; void clear() override; unsigned size() const override; @@ -219,7 +219,7 @@ std::vector<SocketStatus> selectMultiple(int ms) override; }; -void PollMethod::add(Socket &&s, SocketDirection direction) +void PollMethod::add(Socket s, int direction) { auto it = std::find_if(m_fds.begin(), m_fds.end(), [&] (const auto &pfd) { return pfd.fd == s.handle(); }); @@ -232,7 +232,7 @@ } } -void PollMethod::remove(const Socket &s, SocketDirection direction) +void PollMethod::remove(const Socket &s, int direction) { for (auto i = m_fds.begin(); i != m_fds.end();) { if (i->fd == s) { @@ -305,10 +305,10 @@ * Socket listener * -------------------------------------------------------- */ -SocketListener::SocketListener(SocketMethod method) +SocketListener::SocketListener(int method) { #if defined(SOCKET_LISTENER_HAVE_POLL) - if (method == SocketMethod::Poll) + if (method == Poll) m_interface = std::make_unique<PollMethod>(); else #endif
--- a/C++/SocketListener.h Thu Oct 23 17:08:13 2014 +0200 +++ b/C++/SocketListener.h Thu Oct 23 17:59:14 2014 +0200 @@ -39,52 +39,11 @@ * * Bitmask that can be set to both reading and writing. */ -enum class SocketDirection { +enum SocketDirection { Read = (1 << 0), //!< only for receive Write = (1 << 1) //!< only for sending }; -inline SocketDirection operator&(SocketDirection x, SocketDirection y) -{ - return static_cast<SocketDirection>(static_cast<int>(x) & static_cast<int>(y)); -} - -inline SocketDirection operator|(SocketDirection x, SocketDirection y) -{ - return static_cast<SocketDirection>(static_cast<int>(x) | static_cast<int>(y)); -} - -inline SocketDirection operator^(SocketDirection x, SocketDirection y) -{ - return static_cast<SocketDirection>(static_cast<int>(x) ^ static_cast<int>(y)); -} - -inline SocketDirection operator~(SocketDirection x) -{ - return static_cast<SocketDirection>(~static_cast<int>(x)); -} - -inline SocketDirection &operator&=(SocketDirection &x, SocketDirection y) -{ - x = x & y; - - return x; -} - -inline SocketDirection &operator|=(SocketDirection &x, SocketDirection y) -{ - x = x | y; - - return x; -} - -inline SocketDirection &operator^=(SocketDirection &x, SocketDirection y) -{ - x = x ^ y; - - return x; -} - /** * @enum SocketMethod * @brief The SocketMethod enum @@ -92,7 +51,7 @@ * Select the method of polling. It is only a preferred method, for example if you * request for poll but it is not available, select will be used. */ -enum class SocketMethod { +enum SocketMethod { Select, //!< select(2) method, fallback Poll //!< poll(2), everywhere possible }; @@ -105,8 +64,8 @@ * direction. */ struct SocketStatus { - Socket socket; //!< which socket is ready - SocketDirection direction; //!< the direction + Socket socket; //!< which socket is ready + int direction; //!< the direction }; /** @@ -120,7 +79,7 @@ /** * @brief Function for listing all sockets */ - using MapFunc = std::function<void (Socket &, SocketDirection)>; + using MapFunc = std::function<void (Socket &, int)>; #if defined(SOCKET_LISTENER_HAVE_POLL) static constexpr const SocketMethod PreferredMethod = SocketMethod::Poll; @@ -152,7 +111,7 @@ * @param s the socket * @param direction the direction */ - virtual void add(Socket &&s, SocketDirection direction) = 0; + virtual void add(Socket s, int direction) = 0; /** * Remove a socket with a specified direction. @@ -160,7 +119,7 @@ * @param s the socket * @param direction the direction */ - virtual void remove(const Socket &s, SocketDirection direction) = 0; + virtual void remove(const Socket &s, int direction) = 0; /** * Remove all sockets. @@ -216,7 +175,7 @@ * * @param method the preferred method */ - SocketListener(SocketMethod method = SocketMethod::Poll); + SocketListener(int method = Poll); /** * Add a socket to listen to. @@ -224,7 +183,7 @@ * @param s the socket * @param direction the direction */ - inline void add(Socket s, SocketDirection direction) + inline void add(Socket s, int direction) { m_interface->add(std::move(s), direction); } @@ -235,7 +194,7 @@ * @param s the socket * @param direction the direction */ - inline void remove(const Socket &s, SocketDirection direction) + inline void remove(const Socket &s, int direction) { m_interface->remove(s, direction); }
--- a/C++/Tests/Sockets/main.cpp Thu Oct 23 17:08:13 2014 +0200 +++ b/C++/Tests/Sockets/main.cpp Thu Oct 23 17:59:14 2014 +0200 @@ -68,8 +68,8 @@ s2 = { AF_INET, SOCK_STREAM, 0 }; SocketListener listener(SocketMethod::Select); - listener.add(s, SocketDirection::Read); - listener.add(s2, SocketDirection::Read); + listener.add(s, Read); + listener.add(s2, Read); ASSERT_EQ(2UL, listener.size()); } catch (const std::exception &ex) { @@ -89,10 +89,10 @@ s2 = { AF_INET, SOCK_STREAM, 0 }; SocketListener listener(SocketMethod::Select); - listener.add(s, SocketDirection::Read); - listener.add(s2, SocketDirection::Read); - listener.remove(s, SocketDirection::Read); - listener.remove(s2, SocketDirection::Read); + listener.add(s, Read); + listener.add(s2, Read); + listener.remove(s, Read); + listener.remove(s2, Read); ASSERT_EQ(0UL, listener.size()); } catch (const std::exception &ex) { @@ -116,26 +116,26 @@ s2 = { AF_INET, SOCK_STREAM, 0 }; SocketListener listener(SocketMethod::Select); - listener.add(s, SocketDirection::Read | SocketDirection::Write); - listener.add(s2, SocketDirection::Read | SocketDirection::Write); + listener.add(s, Read | Write); + listener.add(s2, Read | Write); - listener.list([&] (Socket &si, SocketDirection dir) { + listener.list([&] (Socket &si, int dir) { ASSERT_TRUE(si == s || si == s2); ASSERT_EQ(0x03, static_cast<int>(dir)); }); - listener.remove(s, SocketDirection::Write); - listener.remove(s2, SocketDirection::Write); + listener.remove(s, Write); + listener.remove(s2, Write); ASSERT_EQ(2UL, listener.size()); - listener.list([&] (Socket &si, SocketDirection dir) { + listener.list([&] (Socket &si, int dir) { ASSERT_TRUE(si == s || si == s2); - ASSERT_EQ(SocketDirection::Read, dir); + ASSERT_EQ(Read, dir); }); - listener.remove(s, SocketDirection::Read); - listener.remove(s2, SocketDirection::Read); + listener.remove(s, Read); + listener.remove(s2, Read); ASSERT_EQ(0UL, listener.size()); } catch (const std::exception &ex) { @@ -154,27 +154,27 @@ s = { AF_INET, SOCK_STREAM, 0 }; SocketListener listener(SocketMethod::Select); - listener.add(s, SocketDirection::Read); + listener.add(s, Read); ASSERT_EQ(1UL, listener.size()); - listener.list([&] (const Socket &si, SocketDirection dir) { + listener.list([&] (const Socket &si, int dir) { ASSERT_TRUE(si == s); - ASSERT_EQ(SocketDirection::Read, dir); + ASSERT_EQ(Read, dir); }); - listener.add(s, SocketDirection::Write); + listener.add(s, Write); ASSERT_EQ(1UL, listener.size()); - listener.list([&] (const Socket &si, SocketDirection dir) { + listener.list([&] (const Socket &si, int dir) { ASSERT_TRUE(si == s); ASSERT_EQ(0x03, static_cast<int>(dir)); }); // Oops, added the same - listener.add(s, SocketDirection::Read); - listener.add(s, SocketDirection::Write); - listener.add(s, SocketDirection::Read | SocketDirection::Write); + listener.add(s, Read); + listener.add(s, Write); + listener.add(s, Read | Write); ASSERT_EQ(1UL, listener.size()); - listener.list([&] (const Socket &si, SocketDirection dir) { + listener.list([&] (const Socket &si, int dir) { ASSERT_TRUE(si == s); ASSERT_EQ(0x03, static_cast<int>(dir)); }); @@ -198,8 +198,8 @@ s2 = { AF_INET, SOCK_STREAM, 0 }; SocketListener listener(SocketMethod::Poll); - listener.add(s, SocketDirection::Read); - listener.add(s2, SocketDirection::Read); + listener.add(s, Read); + listener.add(s2, Read); ASSERT_EQ(2UL, listener.size()); } catch (const std::exception &ex) { @@ -219,10 +219,10 @@ s2 = { AF_INET, SOCK_STREAM, 0 }; SocketListener listener(SocketMethod::Poll); - listener.add(s, SocketDirection::Read); - listener.add(s2, SocketDirection::Read); - listener.remove(s, SocketDirection::Read); - listener.remove(s2, SocketDirection::Read); + listener.add(s, Read); + listener.add(s2, Read); + listener.remove(s, Read); + listener.remove(s2, Read); ASSERT_EQ(0UL, listener.size()); } catch (const std::exception &ex) { @@ -242,26 +242,26 @@ s2 = { AF_INET, SOCK_STREAM, 0 }; SocketListener listener(SocketMethod::Poll); - listener.add(s, SocketDirection::Read | SocketDirection::Write); - listener.add(s2, SocketDirection::Read | SocketDirection::Write); + listener.add(s, Read | Write); + listener.add(s2, Read | Write); - listener.list([&] (Socket &si, SocketDirection dir) { + listener.list([&] (Socket &si, int dir) { ASSERT_TRUE(si == s || si == s2); ASSERT_EQ(0x03, static_cast<int>(dir)); }); - listener.remove(s, SocketDirection::Write); - listener.remove(s2, SocketDirection::Write); + listener.remove(s, Write); + listener.remove(s2, Write); ASSERT_EQ(2UL, listener.size()); - listener.list([&] (Socket &si, SocketDirection dir) { + listener.list([&] (Socket &si, int dir) { ASSERT_TRUE(si == s || si == s2); - ASSERT_EQ(SocketDirection::Read, dir); + ASSERT_EQ(Read, dir); }); - listener.remove(s, SocketDirection::Read); - listener.remove(s2, SocketDirection::Read); + listener.remove(s, Read); + listener.remove(s2, Read); ASSERT_EQ(0UL, listener.size()); } catch (const std::exception &ex) { @@ -280,27 +280,27 @@ s = { AF_INET, SOCK_STREAM, 0 }; SocketListener listener(SocketMethod::Poll); - listener.add(s, SocketDirection::Read); + listener.add(s, Read); ASSERT_EQ(1UL, listener.size()); - listener.list([&] (const Socket &si, SocketDirection dir) { + listener.list([&] (const Socket &si, int dir) { ASSERT_TRUE(si == s); - ASSERT_EQ(SocketDirection::Read, dir); + ASSERT_EQ(Read, dir); }); - listener.add(s, SocketDirection::Write); + listener.add(s, Write); ASSERT_EQ(1UL, listener.size()); - listener.list([&] (const Socket &si, SocketDirection dir) { + listener.list([&] (const Socket &si, int dir) { ASSERT_TRUE(si == s); ASSERT_EQ(0x03, static_cast<int>(dir)); }); // Oops, added the same - listener.add(s, SocketDirection::Read); - listener.add(s, SocketDirection::Write); - listener.add(s, SocketDirection::Read | SocketDirection::Write); + listener.add(s, Read); + listener.add(s, Write); + listener.add(s, Read | Write); ASSERT_EQ(1UL, listener.size()); - listener.list([&] (const Socket &si, SocketDirection dir) { + listener.list([&] (const Socket &si, int dir) { ASSERT_TRUE(si == s); ASSERT_EQ(0x03, static_cast<int>(dir)); }); @@ -328,11 +328,11 @@ s.bind(Internet{"*", 10000, AF_INET}); s.listen(8); - listener.add(s, SocketDirection::Read); + listener.add(s, Read); auto client = listener.select(10s); - ASSERT_TRUE(client.direction == SocketDirection::Read); + ASSERT_TRUE(client.direction == Read); ASSERT_TRUE(client.socket == s); } catch (const std::exception &ex) { std::cerr << "warning: " << ex.what() << std::endl; @@ -374,20 +374,20 @@ s.listen(8); // Read for master - listener.add(s, SocketDirection::Read); + listener.add(s, Read); auto result = listener.select(10s); - ASSERT_TRUE(result.direction == SocketDirection::Read); + ASSERT_TRUE(result.direction == Read); ASSERT_TRUE(result.socket == s); // Wait for client auto client = s.accept(); - listener.add(client, SocketDirection::Read); + listener.add(client, Read); result = listener.select(10s); - ASSERT_TRUE(result.direction == SocketDirection::Read); + ASSERT_TRUE(result.direction == Read); ASSERT_TRUE(result.socket == client); char data[512]; @@ -439,13 +439,13 @@ server.set(SOL_SOCKET, SO_REUSEADDR, 1); server.bind(Internet{"*", 10000, AF_INET}); server.listen(10); - listener.add(server, SocketDirection::Read); + listener.add(server, Read); while (!finished) { auto s = listener.select(60s).socket; if (s == server) { - listener.add(s.accept(), SocketDirection::Read); + listener.add(s.accept(), Read); } else { char data[512]; auto nb = s.recv(data, sizeof (data) - 1); @@ -487,7 +487,7 @@ client.connect(Internet{"localhost", 10000, AF_INET}); client.blockMode(false); - listener.add(client, SocketDirection::Write); + listener.add(client, Write); while (data.size() > 0) { auto s = listener.select(30s).socket; @@ -526,15 +526,15 @@ master.bind(Internet{"*", 10000, AF_INET}); master.listen(8); - masterListener.add(master, SocketDirection::Read); + masterListener.add(master, Read); while (clientListener.size() != 3) { - masterListener.select(500ms); - clientListener.add(master.accept(), SocketDirection::Write); + masterListener.select(3s); + clientListener.add(master.accept(), Write); } // Now do the test of writing - auto result = clientListener.selectMultiple(1s); + auto result = clientListener.selectMultiple(3s); ASSERT_EQ(3UL, result.size()); clientListener.list([] (auto s, auto) { @@ -555,9 +555,11 @@ s2 = { AF_INET, SOCK_STREAM, 0 }; s3 = { AF_INET, SOCK_STREAM, 0 }; - s1.connect(Internet("localhost", 10000, AF_INET)); - s2.connect(Internet("localhost", 10000, AF_INET)); - s3.connect(Internet("localhost", 10000, AF_INET)); + std::this_thread::sleep_for(1s); + + s1.connect(Internet{"localhost", 10000, AF_INET}); + s2.connect(Internet{"localhost", 10000, AF_INET}); + s3.connect(Internet{"localhost", 10000, AF_INET}); } catch (const std::exception &ex) { std::cerr << "warning: " << ex.what() << std::endl; } @@ -590,15 +592,15 @@ master.bind(Internet{"*", 10000, AF_INET}); master.listen(8); - masterListener.add(master, SocketDirection::Read); + masterListener.add(master, Read); while (clientListener.size() != 3) { - masterListener.select(1s); - clientListener.add(master.accept(), SocketDirection::Write); + masterListener.select(3s); + clientListener.add(master.accept(), Write); } // Now do the test of writing - auto result = clientListener.selectMultiple(500ms); + auto result = clientListener.selectMultiple(3s); ASSERT_EQ(3UL, result.size()); clientListener.list([] (auto s, auto) { @@ -619,6 +621,8 @@ s2 = { AF_INET, SOCK_STREAM, 0 }; s3 = { AF_INET, SOCK_STREAM, 0 }; + std::this_thread::sleep_for(1s); + s1.connect(Internet("localhost", 10000, AF_INET)); s2.connect(Internet("localhost", 10000, AF_INET)); s3.connect(Internet("localhost", 10000, AF_INET));