1 #ifndef jmmcg_core_socket_client_manager_hpp 2 #define jmmcg_core_socket_client_manager_hpp 3 4 /****************************************************************************** 5 ** $Header: svn+ssh://jmmcg@svn.code.sf.net/p/libjmmcg/code/trunk/libjmmcg/core/socket_client_manager.hpp 2055 2017-05-13 19:35:47Z jmmcg $ 6 ** 7 ** Copyright (C) 2015 by J.M.McGuiness, coder@hussar.me.uk 8 ** 9 ** This library is free software; you can redistribute it and/or 10 ** modify it under the terms of the GNU Lesser General Public 11 ** License as published by the Free Software Foundation; either 12 ** version 2.1 of the License, or (at your option) any later version. 13 ** 14 ** This library is distributed in the hope that it will be useful, 15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 ** Lesser General Public License for more details. 18 ** 19 ** You should have received a copy of the GNU Lesser General Public 20 ** License along with this library; if not, write to the Free Software 21 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 */ 23 24 #include "socket_wrapper.hpp" 25 26 namespace jmmcg { namespace socket { 27 28 namespace asio { 29 30 /// A simple TCP/IP socket wrapper using boost::asio. 31 class client_manager { 32 public: 33 using socket_t=socket_wrapper; 34 35 /// Create a new connection to the specified TCP socket using the TCP/IP protocol. 36 /** 37 \param max_message_size The largest size of message that shall be sent or received using the socket. 38 \param conn_pol The connection policy that may be applied to attempt to connect to the specified endpoint. 39 */ 40 template<class ConnPol> 41 client_manager(std::size_t min_message_size, std::size_t max_message_size, ConnPol const &conn_pol); 42 43 /// Write the whole message to the socket in one go. 44 /** 45 \param message The message to write, that must be as-if a POD. 46 */ 47 template< 48 class MsgT ///< The type of the message to write. 49 > 50 void write(MsgT const &message); 51 52 /// Read the whole message from the socket in one go. 53 /** 54 \param dest The message will be placed into this buffer, which may be grown to accommodate the message. 55 */ 56 template< 57 class MsgT ///< The type of the message to read, that must be as-if a POD. 58 > 59 void read(MsgT &dest); 60 /// Read the whole message from the socket in one go. 61 /** 62 \param dest The message will be placed into this stack-based buffer, which must be sufficiently large to accommodate the message read, otherwise UB will result. 63 */ 64 template<class V, std::size_t SrcSz> void 65 read(V (& dest)[SrcSz]); 66 67 void stop(); 68 69 socket_t &socket() noexcept(true) { 70 return socket_; 71 } 72 73 std::string to_string() const noexcept(false); 74 75 private: 76 boost::asio::io_service io_service; 77 socket_t socket_; 78 }; 79 80 inline std::ostream & 81 operator<<(std::ostream &os, client_manager const &ec) noexcept(false); 82 83 } 84 85 namespace glibc { 86 87 /// A simple TCP/IP socket wrapper using the glibc POSIX API. 88 class client_manager { 89 public: 90 using socket_t=client::wrapper; 91 92 /// Create a new connection to the specified TCP socket using the TCP/IP protocol. 93 /** 94 \param max_message_size The largest size of message that shall be sent or received using the socket. 95 \param conn_pol The connection policy that may be applied to attempt to connect to the specified endpoint. 96 */ 97 template<class ConnPol> 98 client_manager(std::size_t min_message_size, std::size_t max_message_size, ConnPol const &conn_pol); 99 100 /// Write the whole message to the socket in one go. 101 /** 102 \param message The message to write, that must be as-if a POD. 103 */ 104 template< 105 class MsgT ///< The type of the message to write. 106 > 107 void write(MsgT const &message); 108 109 /// Read the whole message from the socket in one go. 110 /** 111 \param dest The message will be placed into this buffer, which may be grown to accommodate the message. 112 */ 113 template< 114 class MsgT ///< The type of the message to read, that must be as-if a POD. 115 > 116 void read(MsgT &dest); 117 /// Read the whole message from the socket in one go. 118 /** 119 \param dest The message will be placed into this stack-based buffer, which must be sufficiently large to accommodate the message read, otherwise UB will result. 120 */ 121 template<class V, std::size_t SrcSz> void 122 read(V (& dest)[SrcSz]); 123 124 socket_t &socket() { 125 return socket_; 126 } 127 128 void stop() noexcept(true); 129 130 std::string to_string() const noexcept(false); 131 132 private: 133 socket_t socket_; 134 }; 135 136 inline std::ostream & 137 operator<<(std::ostream &os, client_manager const &ec) noexcept(false); 138 139 } 140 } } 141 142 #include "socket_client_manager_impl.hpp" 143 144 #endif