libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
socket_client_manager_impl.hpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2015 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE { namespace socket {
20 
21 namespace asio {
22 
23 template<class LkT>
24 template<class ConnPol> inline
25 client_manager<LkT>::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)
26 : socket_(io_context) {
27  conn_pol.operator()(
28  [this](boost::asio::ip::tcp::endpoint const &endpoint) {
29  socket_.connect(endpoint);
30  }
31  );
32  socket_.set_options(min_message_size, max_message_size, timeout, priority, incoming_cpu);
33  assert(socket_.is_open());
34 }
35 
36 template<class LkT>
37 template<class MsgT> inline void
38 client_manager<LkT>::write(MsgT const &message) {
39  socket_.write(message);
40 }
41 
42 template<class LkT>
43 template<class MsgT> inline void
44 client_manager<LkT>::read(MsgT &dest) {
45  socket_.read(dest);
46 }
47 
48 template<class LkT>
49 template<class V, std::size_t SrcSz> inline void
50 client_manager<LkT>::read(V (& dest)[SrcSz]) {
51  socket_.read(dest);
52 }
53 
54 template<class LkT>
55 inline void
57  io_context.stop();
58 }
59 
60 template<class LkT>
61 inline std::string
62 client_manager<LkT>::to_string() const noexcept(false) {
63  std::ostringstream ss;
64  ss<<socket_;
65  return ss.str();
66 }
67 
68 template<class LkT>
69 inline std::ostream &
70 operator<<(std::ostream &os, client_manager<LkT> const &ec) noexcept(false) {
71  os<<ec.to_string();
72  return os;
73 }
74 
75 }
76 
77 namespace glibc {
78 
79 template<class LkT>
80 template<class ConnPol> inline
81 client_manager<LkT>::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) {
82  conn_pol.operator()(
83  [this](boost::asio::ip::tcp::endpoint const &endpoint) {
84  socket_.connect(endpoint.address().to_string().c_str(), endpoint.port());
85  }
86  );
87  socket_.set_options(min_message_size, max_message_size, timeout, priority, incoming_cpu);
88 }
89 
90 template<class LkT>
91 template<class MsgT> inline void
92 client_manager<LkT>::write(MsgT const &message) {
93  socket_.write(message);
94 }
95 
96 template<class LkT>
97 template<class MsgT> inline void
98 client_manager<LkT>::read(MsgT &dest) {
99  [[maybe_unused]] const bool ret=socket_.read(dest);
100  assert(!ret);
101 }
102 
103 template<class LkT>
104 template<class V, std::size_t SrcSz> inline void
105 client_manager<LkT>::read(V (& dest)[SrcSz]) {
106  [[maybe_unused]] const bool ret=socket_.read(dest);
107  assert(!ret);
108 }
109 
110 template<class LkT>
111 inline void
112 client_manager<LkT>::stop() noexcept(true) {
113 }
114 
115 template<class LkT>
116 inline std::string
117 client_manager<LkT>::to_string() const noexcept(false) {
118  std::ostringstream ss;
119  ss<<socket_;
120  return ss.str();
121 }
122 
123 template<class LkT>
124 inline std::ostream &
125 operator<<(std::ostream &os, client_manager<LkT> const &ec) noexcept(false) {
126  os<<ec.to_string();
127  return os;
128 }
129 
130 }
131 
132 } } }