1 #ifndef jmmcg_core_socket_wrapper_asio_hpp 2 #define jmmcg_core_socket_wrapper_asio_hpp 3 4 /****************************************************************************** 5 ** $Header: svn+ssh://jmmcg@svn.code.sf.net/p/libjmmcg/code/trunk/libjmmcg/core/socket_wrapper_asio.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 <boost/asio.hpp> 25 26 namespace jmmcg { namespace socket { namespace asio { 27 28 /// A simple TCP/IP socket wrapper using boost::asio. 29 class socket_wrapper { 30 public: 31 using socket_t=boost::asio::ip::tcp::socket; 32 33 /// Wrap a TCP socket using the TCP/IP protocol. 34 /** 35 \param io_service The I/O service that underlies the socket. 36 */ 37 explicit socket_wrapper(boost::asio::io_service &io_service); 38 39 void connect(boost::asio::ip::tcp::endpoint const &endpoint); 40 41 /// Write the whole message to the socket in one go. 42 /** 43 \param message The message to write, that must be as-if a POD. 44 */ 45 template< 46 class MsgT ///< The type of the message to write. 47 > 48 void write(MsgT const &message); 49 50 /// Write the whole message to the socket in one go. 51 /** 52 \param message The message to write, that must be as-if a POD. 53 */ 54 template<class V, std::size_t N> 55 void write(std::array<V, N> const &message); 56 57 /// Read the whole message from the socket in one go. 58 /** 59 \param dest The message will be placed into this buffer, which may be grown to accommodate the message. 60 */ 61 template< 62 class MsgT ///< The type of the message to read, that must be as-if a POD. 63 > 64 void read(MsgT &dest); 65 /// Read the whole message from the socket in one go. 66 /** 67 \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. 68 */ 69 template<class V, std::size_t SrcSz> void 70 read(V (& dest)[SrcSz]); 71 72 template<class MsgDetails, class V, std::size_t N> bool 73 read(std::array<V, N> &buff) noexcept(false); 74 75 bool is_open() const { 76 return socket_.is_open(); 77 } 78 79 void close() { 80 socket_.close(); 81 } 82 83 socket_t &socket() { 84 return socket_; 85 } 86 87 void set_options(std::size_t, std::size_t max_message_size); 88 89 std::string to_string() const noexcept(false); 90 91 private: 92 socket_t socket_; 93 boost::system::error_code io_error; 94 }; 95 96 inline std::ostream & 97 operator<<(std::ostream &os, socket_wrapper const &ec) noexcept(false); 98 99 } } } 100 101 #include "socket_wrapper_asio_impl.hpp" 102 103 #endif