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