Mercurial > code
changeset 466:a126c10d9321
Socket: rename Type to Protocol everywhere
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 04 Nov 2015 20:36:07 +0100 |
parents | 55b3ec952c53 |
children | 41766abe942e |
files | C++/modules/Socket/Sockets.h |
diffstat | 1 files changed, 88 insertions(+), 88 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/modules/Socket/Sockets.h Wed Nov 04 20:32:34 2015 +0100 +++ b/C++/modules/Socket/Sockets.h Wed Nov 04 20:36:07 2015 +0100 @@ -152,7 +152,7 @@ * The following types are defined differently between Unix and Windows. */ -/* {{{ Types */ +/* {{{ Protocols */ #if defined(_WIN32) @@ -568,10 +568,10 @@ * @class Socket * @brief Base socket class for socket operations. */ -template <typename Address, typename Type> +template <typename Address, typename Protocol> class Socket { private: - Type m_type; + Protocol m_proto; State m_state{State::Closed}; Action m_action{Action::None}; Condition m_condition{Condition::None}; @@ -597,8 +597,8 @@ * @post state is set to Open * @post handle is not set to Invalid */ - Socket(int domain, int type, int protocol, Type iface = Type{}) - : m_type(std::move(iface)) + Socket(int domain, int type, int protocol, Protocol iface = Protocol{}) + : m_proto(std::move(iface)) { #if !defined(SOCKET_NO_AUTO_INIT) init(); @@ -609,7 +609,7 @@ throw Error{Error::System, "socket"}; } - m_type.create(*this); + m_proto.create(*this); m_state = State::Open; assert(m_handle != Invalid); @@ -618,13 +618,13 @@ /** * This tries to create a socket. * - * Domain and type are determined by the Address and Type object. + * Domain and type are determined by the Address and Protocol object. * * @param address which type of address * @param type the type instance */ - explicit inline Socket(const Address &address = {}, Type type = Type{}) - : Socket{address.domain(), type.type(), 0, std::move(type)} + explicit inline Socket(const Address &address = {}, Protocol proto = Protocol{}) + : Socket{address.domain(), proto.type(), 0, std::move(proto)} { } @@ -637,8 +637,8 @@ * @post action is set to None * @post condition is set to None */ - explicit inline Socket(Handle handle, State state = State::Closed, Type type = Type{}) noexcept - : m_type(std::move(type)) + explicit inline Socket(Handle handle, State state = State::Closed, Protocol type = Protocol{}) noexcept + : m_proto(std::move(type)) , m_state{state} , m_handle{handle} { @@ -665,7 +665,7 @@ * @param other the other socket */ inline Socket(Socket &&other) noexcept - : m_type(std::move(other.m_type)) + : m_proto(std::move(other.m_proto)) , m_state{other.m_state} , m_action{other.m_action} , m_condition{other.m_condition} @@ -692,9 +692,9 @@ * @return the implementation * @warning use this function with care */ - inline const Type &type() const noexcept + inline const Protocol &protocol() const noexcept { - return m_type; + return m_proto; } /** @@ -702,9 +702,9 @@ * * @return the implementation */ - inline Type &type() noexcept + inline Protocol &protocol() noexcept { - return m_type; + return m_proto; } /** @@ -922,7 +922,7 @@ m_action = Action::None; m_condition = Condition::None; - m_type.connect(*this, address, length); + m_proto.connect(*this, address, length); assert((m_state == State::Connected && m_action == Action::None && m_condition == Condition::None) || (m_state == State::Connecting && m_action == Action::Connect && m_condition != Condition::None)); @@ -956,7 +956,7 @@ m_action = Action::None; m_condition = Condition::None; - m_type.connect(*this); + m_proto.connect(*this); assert((m_state == State::Connected && m_action == Action::None && m_condition == Condition::None) || (m_state == State::Connecting && m_action == Action::Connect && m_condition != Condition::None)); @@ -976,7 +976,7 @@ * @post the client socket state is either set to State::Accepting or State::Accepted * @post if the client state is set to State::Accepting, action and condition are defined */ - Socket<Address, Type> accept(Address *info) + Socket<Address, Protocol> accept(Address *info) { assert(m_state == State::Bound); @@ -986,7 +986,7 @@ sockaddr_storage storage; socklen_t length = sizeof (storage); - Socket<Address, Type> sc = m_type.accept(*this, reinterpret_cast<sockaddr *>(&storage), &length); + Socket<Address, Protocol> sc = m_proto.accept(*this, reinterpret_cast<sockaddr *>(&storage), &length); if (info) { *info = Address{&storage, length}; @@ -1022,7 +1022,7 @@ m_action = Action::None; m_condition = Condition::None; - m_type.accept(*this); + m_proto.accept(*this); assert( (m_state == State::Accepting && m_action == Action::Accept && m_condition != Condition::None) || @@ -1073,7 +1073,7 @@ m_action = Action::None; m_condition = Condition::None; - unsigned nbread = m_type.recv(*this, data, length); + unsigned nbread = m_proto.recv(*this, data, length); assert((m_action == Action::None && m_condition == Condition::None) || (m_action != Action::Receive && m_condition != Condition::None)); @@ -1117,7 +1117,7 @@ m_action = Action::None; m_condition = Condition::None; - unsigned nbsent = m_type.send(*this, data, length); + unsigned nbsent = m_proto.send(*this, data, length); assert((m_action == Action::None && m_condition == Condition::None) || (m_action != Action::Send && m_condition != Condition::None)); @@ -1150,7 +1150,7 @@ */ inline unsigned sendto(const void *data, unsigned length, const Address &address) { - return m_type.sendto(*this, data, length, address); + return m_proto.sendto(*this, data, length, address); } /** @@ -1179,7 +1179,7 @@ { Address dummy; - return m_type.recvfrom(*this, data, length, info == nullptr ? dummy : *info); + return m_proto.recvfrom(*this, data, length, info == nullptr ? dummy : *info); } /** @@ -1241,7 +1241,7 @@ Socket &operator=(Socket &&other) noexcept { m_handle = other.m_handle; - m_type = std::move(other.m_type); + m_proto = std::move(other.m_proto); m_state = other.m_state; m_action = other.m_action; m_condition = other.m_condition; @@ -1263,8 +1263,8 @@ * @param s2 the second socket * @return true if they equals */ -template <typename Address, typename Type> -bool operator==(const Socket<Address, Type> &s1, const Socket<Address, Type> &s2) +template <typename Address, typename Protocol> +bool operator==(const Socket<Address, Protocol> &s1, const Socket<Address, Protocol> &s2) { return s1.handle() == s2.handle(); } @@ -1276,8 +1276,8 @@ * @param s2 the second socket * @return true if they are different */ -template <typename Address, typename Type> -bool operator!=(const Socket<Address, Type> &s1, const Socket<Address, Type> &s2) +template <typename Address, typename Protocol> +bool operator!=(const Socket<Address, Protocol> &s1, const Socket<Address, Protocol> &s2) { return s1.handle() != s2.handle(); } @@ -1289,8 +1289,8 @@ * @param s2 the second socket * @return true if s1 < s2 */ -template <typename Address, typename Type> -bool operator<(const Socket<Address, Type> &s1, const Socket<Address, Type> &s2) +template <typename Address, typename Protocol> +bool operator<(const Socket<Address, Protocol> &s1, const Socket<Address, Protocol> &s2) { return s1.handle() < s2.handle(); } @@ -1302,8 +1302,8 @@ * @param s2 the second socket * @return true if s1 > s2 */ -template <typename Address, typename Type> -bool operator>(const Socket<Address, Type> &s1, const Socket<Address, Type> &s2) +template <typename Address, typename Protocol> +bool operator>(const Socket<Address, Protocol> &s1, const Socket<Address, Protocol> &s2) { return s1.handle() > s2.handle(); } @@ -1315,8 +1315,8 @@ * @param s2 the second socket * @return true if s1 <= s2 */ -template <typename Address, typename Type> -bool operator<=(const Socket<Address, Type> &s1, const Socket<Address, Type> &s2) +template <typename Address, typename Protocol> +bool operator<=(const Socket<Address, Protocol> &s1, const Socket<Address, Protocol> &s2) { return s1.handle() <= s2.handle(); } @@ -1328,8 +1328,8 @@ * @param s2 the second socket * @return true if s1 >= s2 */ -template <typename Address, typename Type> -bool operator>=(const Socket<Address, Type> &s1, const Socket<Address, Type> &s2) +template <typename Address, typename Protocol> +bool operator>=(const Socket<Address, Protocol> &s1, const Socket<Address, Protocol> &s2) { return s1.handle() >= s2.handle(); } @@ -1587,7 +1587,7 @@ /* }}} */ /* - * Predefined types + * Predefined protocols * ------------------------------------------------------------------ * * - Tcp, for standard stream connections, @@ -1595,7 +1595,7 @@ * - Tls, for secure stream connections. */ -/* {{{ Types */ +/* {{{ Protocols */ /* {{{ Tcp */ @@ -1631,8 +1631,8 @@ * @param address the address * @param length the length */ - template <typename Address, typename Type> - void connect(Socket<Address, Type> &sc, const sockaddr *address, socklen_t length) + template <typename Address, typename Protocol> + void connect(Socket<Address, Protocol> &sc, const sockaddr *address, socklen_t length) { if (::connect(sc.handle(), address, length) == Failure) { /* @@ -1669,8 +1669,8 @@ * @param sc the socket * @throw Error on errors */ - template <typename Address, typename Type> - void connect(Socket<Address, Type> &sc) + template <typename Address, typename Protocol> + void connect(Socket<Address, Protocol> &sc) { int error = sc.template get<int>(SOL_SOCKET, SO_ERROR); @@ -1689,8 +1689,8 @@ * @return the socket * @throw Error on errors */ - template <typename Address, typename Type> - Socket<Address, Type> accept(Socket<Address, Type> &sc, sockaddr *address, socklen_t *length) + template <typename Address, typename Protocol> + Socket<Address, Protocol> accept(Socket<Address, Protocol> &sc, sockaddr *address, socklen_t *length) { Handle handle = ::accept(sc.handle(), address, length); @@ -1698,14 +1698,14 @@ throw Error{Error::System, "accept"}; } - return Socket<Address, Type>{handle, State::Accepted}; + return Socket<Address, Protocol>{handle, State::Accepted}; } /** * Continue accept. */ - template <typename Address, typename Type> - inline void accept(Socket<Address, Type> &) noexcept + template <typename Address, typename Protocol> + inline void accept(Socket<Address, Protocol> &) noexcept { /* no-op */ } @@ -1954,8 +1954,8 @@ return msg == nullptr ? "" : msg; } - template <typename Address, typename Type> - inline void updateStates(Socket<Address, Type> &sc, State state, Action action, int code) + template <typename Address, typename Protocol> + inline void updateStates(Socket<Address, Protocol> &sc, State state, Action action, int code) { assert(code == SSL_ERROR_WANT_READ || code == SSL_ERROR_WANT_WRITE); @@ -1972,8 +1972,8 @@ /* * Continue the connect operation. */ - template <typename Address, typename Type> - void processConnect(Socket<Address, Type> &sc) + template <typename Address, typename Protocol> + void processConnect(Socket<Address, Protocol> &sc) { int ret = SSL_connect(m_ssl.get()); @@ -1993,8 +1993,8 @@ /* * Continue accept. */ - template <typename Address, typename Type> - void processAccept(Socket<Address, Type> &sc) + template <typename Address, typename Protocol> + void processAccept(Socket<Address, Protocol> &sc) { int ret = SSL_accept(m_ssl.get()); @@ -2082,8 +2082,8 @@ * @param address the address * @throw Error on errors */ - template <typename Address, typename Type> - void connect(Socket<Address, Type> &sc, const sockaddr *address, socklen_t length) + template <typename Address, typename Protocol> + void connect(Socket<Address, Protocol> &sc, const sockaddr *address, socklen_t length) { /* 1. Connect using raw TCP */ Tcp::connect(sc, address, length); @@ -2100,8 +2100,8 @@ * * @param sc the socket */ - template <typename Address, typename Type> - void connect(Socket<Address, Type> &sc) + template <typename Address, typename Protocol> + void connect(Socket<Address, Protocol> &sc) { /* 1. Be sure to complete standard connect before */ if (!m_tcpconnected) { @@ -2125,17 +2125,17 @@ Socket<Address, Tls> accept(Socket<Address, Tls> &sc, sockaddr *address, socklen_t *length) { Socket<Address, Tls> client = Tcp::accept(sc, address, length); - Tls &type = client.type(); + Tls &proto = client.protocol(); /* 1. Share the context */ - type.m_context = m_context; + proto.m_context = m_context; /* 2. Create new SSL instance */ - type.m_ssl = Ssl{SSL_new(m_context.get()), SSL_free}; - SSL_set_fd(type.m_ssl.get(), client.handle()); + proto.m_ssl = Ssl{SSL_new(m_context.get()), SSL_free}; + SSL_set_fd(proto.m_ssl.get(), client.handle()); /* 3. Try accept process on the **new** client */ - type.processAccept(client); + proto.processAccept(client); return client; } @@ -2146,8 +2146,8 @@ * @param sc the socket * @throw Error on errors */ - template <typename Address, typename Type> - inline void accept(Socket<Address, Type> &sc) + template <typename Address, typename Protocol> + inline void accept(Socket<Address, Protocol> &sc) { processAccept(sc); } @@ -2848,7 +2848,7 @@ * This object is created from StreamServer when a new client is connected, it is the higher * level object of sockets and completely asynchronous. */ -template <typename Address, typename Type> +template <typename Address, typename Protocol> class StreamConnection { public: using WriteHandler = Callback<>; @@ -2858,7 +2858,7 @@ WriteHandler m_onWrite; /* Sockets and output buffer */ - Socket<Address, Type> m_socket; + Socket<Address, Protocol> m_socket; std::string m_output; public: @@ -2867,7 +2867,7 @@ * * @param s the socket */ - StreamConnection(Socket<Address, Type> s) + StreamConnection(Socket<Address, Protocol> s) : m_socket{std::move(s)} { m_socket.setBlockMode(false); @@ -2879,7 +2879,7 @@ * @return the socket * @warning use with care */ - inline Socket<Address, Type> &socket() noexcept + inline Socket<Address, Protocol> &socket() noexcept { return m_socket; } @@ -2957,18 +2957,18 @@ * * This class is not thread safe and you must not call any of the functions from different threads. */ -template <typename Address, typename Type> +template <typename Address, typename Protocol> class StreamServer { public: - using ConnectionHandler = Callback<const std::shared_ptr<StreamConnection<Address, Type>> &>; - using DisconnectionHandler = Callback<const std::shared_ptr<StreamConnection<Address, Type>> &>; - using ReadHandler = Callback<const std::shared_ptr<StreamConnection<Address, Type>> &, const std::string &>; - using WriteHandler = Callback<const std::shared_ptr<StreamConnection<Address, Type>> &, const std::string &>; + using ConnectionHandler = Callback<const std::shared_ptr<StreamConnection<Address, Protocol>> &>; + using DisconnectionHandler = Callback<const std::shared_ptr<StreamConnection<Address, Protocol>> &>; + using ReadHandler = Callback<const std::shared_ptr<StreamConnection<Address, Protocol>> &, const std::string &>; + using WriteHandler = Callback<const std::shared_ptr<StreamConnection<Address, Protocol>> &, const std::string &>; using ErrorHandler = Callback<const Error &>; using TimeoutHandler = Callback<>; private: - using ClientMap = std::map<Handle, std::shared_ptr<StreamConnection<Address, Type>>>; + using ClientMap = std::map<Handle, std::shared_ptr<StreamConnection<Address, Protocol>>>; /* Signals */ ConnectionHandler m_onConnection; @@ -2979,14 +2979,14 @@ TimeoutHandler m_onTimeout; /* Sockets */ - Socket<Address, Type> m_master; + Socket<Address, Protocol> m_master; Listener<> m_listener; ClientMap m_clients; /* * Update flags depending on the required condition. */ - void updateFlags(std::shared_ptr<StreamConnection<Address, Type>> &client) + void updateFlags(std::shared_ptr<StreamConnection<Address, Protocol>> &client) { assert(client->socket().action() != Action::None); @@ -3003,7 +3003,7 @@ * Continue accept process. */ template <typename AcceptCall> - void processAccept(std::shared_ptr<StreamConnection<Address, Type>> &client, const AcceptCall &acceptFunc) + void processAccept(std::shared_ptr<StreamConnection<Address, Protocol>> &client, const AcceptCall &acceptFunc) { try { /* Do the accept */ @@ -3035,8 +3035,8 @@ void processInitialAccept() { // TODO: store address too. - std::shared_ptr<StreamConnection<Address, Type>> client = std::make_shared<StreamConnection<Address, Type>>(m_master.accept(nullptr)); - std::weak_ptr<StreamConnection<Address, Type>> ptr{client}; + std::shared_ptr<StreamConnection<Address, Protocol>> client = std::make_shared<StreamConnection<Address, Protocol>>(m_master.accept(nullptr)); + std::weak_ptr<StreamConnection<Address, Protocol>> ptr{client}; /* 1. Register output changed to update listener */ client->setWriteHandler([this, ptr] () { @@ -3061,7 +3061,7 @@ /* * Read or complete the read operation. */ - void processRead(std::shared_ptr<StreamConnection<Address, Type>> &client) + void processRead(std::shared_ptr<StreamConnection<Address, Protocol>> &client) { /* * Read because there is something to read or because the pending operation is @@ -3101,7 +3101,7 @@ /* * Flush the output buffer. */ - void processWrite(std::shared_ptr<StreamConnection<Address, Type>> &client) + void processWrite(std::shared_ptr<StreamConnection<Address, Protocol>> &client) { auto &output = client->output(); auto nsent = client->socket().send(output); @@ -3125,7 +3125,7 @@ } } - void processSync(std::shared_ptr<StreamConnection<Address, Type>> &client, Condition flags) + void processSync(std::shared_ptr<StreamConnection<Address, Protocol>> &client, Condition flags) { try { auto action = client->socket().action(); @@ -3151,7 +3151,7 @@ * @param max the max number to listen * @throw Error on errors */ - StreamServer(const Address &address, Type type = {}, int max = 128) + StreamServer(const Address &address, Protocol type = {}, int max = 128) : m_master{address, std::move(type)} { // TODO: m_onError @@ -3270,7 +3270,7 @@ * * This class is not thread safe and you must not call any of the functions from different threads. */ -template <typename Address, typename Type> +template <typename Address, typename Protocol> class StreamClient { public: /** @@ -3313,7 +3313,7 @@ TimeoutHandler m_onTimeout; /* Socket */ - Socket<Address, Type> m_socket; + Socket<Address, Protocol> m_socket; Listener<> m_listener; /* Output buffer */ @@ -3441,7 +3441,7 @@ * @param type the type (Tcp or Tls) * @throw net::Error on failures */ - StreamClient(const Address &address = {}, Type type = {}) + StreamClient(const Address &address = {}, Protocol type = {}) : m_socket{address, std::move(type)} { m_socket.setBlockMode(false);