libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
msg_processor.hpp
Go to the documentation of this file.
1 #ifndef LIBJMMCG_CORE_MSG_PROCESSOR_HPP
2 #define LIBJMMCG_CORE_MSG_PROCESSOR_HPP
3 
4 /******************************************************************************
5 ** Copyright © 2016 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 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE { namespace socket {
23 
24 /// Read a message from the source socket, process it according to the supplied rules and write the resultant message(s) to the destination socket.
25 template<class ProcessingRules>
27 public:
28  /// The processing rules for the particular server instance.
29  /**
30  These rules specify how an input message should be transformed to one, or more, response messages,
31  */
32  using proc_rules_t=ProcessingRules;
33  using msg_details_t=typename proc_rules_t::src_msg_details_t;
34 
35  /// The buffer into which the incoming messages are received.
36  /**
37  This should be per-thread, on the stack of the thread, suitably aligned, possibly to 64-bits. For maximum performance it should be a reference to the internal buffer of the network stack or even the hardware buffer in the card (assuming suitable kernel modules).
38  It is not recommended to use GCC <=v4.9.* due to sub-optimal code-generation in std::memcpy() (see the performance graphs of libjmmcg::stack_string.
39  */
40  using msg_buffer_t=typename msg_details_t::msg_buffer_t;
41 
42  explicit msg_processor(proc_rules_t const &proc_rules);
43 
44  /// Read a message from the source socket, process it and write the resultant message(s) to the destination socket.
45  /**
46  Note that this call will block until sufficient data has been read from the source socket to comprise a complete source message.
47 
48  Throws: an exception if there has been a socket error (the source socket being cleanly closed is not an error).
49 
50  \param src_skt The socket that is source of the messages.
51  \param dest_skt The socket that is destination of the messages, may be the same as src_skt.
52  \return True if the source socket has been closed, false otherwise.
53  */
54  template<class SktT, class LatencyTimestamps>
55  typename std::enable_if<std::is_class<typename LatencyTimestamps::period>::value, bool>::type
56  read_and_process_a_msg(SktT &src_skt, SktT &dest_skt, LatencyTimestamps &ts) noexcept(false);
57  /// Read a message from the source socket, process it and write the resultant message(s) to the destination socket, if it is valid, otherwise write back to the src_skt.
58  /**
59  Note that this call will block until sufficient data has been read from the source socket to comprise a complete source message.
60 
61  Throws: an exception if there has been a socket error (the source socket being cleanly closed is not an error).
62 
63  \param src_skt The socket that is source of the messages.
64  \param dest_skt The socket that is destination of the messages, may be the same as src_skt.
65  \return True if the source socket has been closed, false otherwise.
66  */
67  template<class SktT, class ClientCxn, class LatencyTimestamps>
68  typename std::enable_if<std::is_class<typename LatencyTimestamps::period>::value, bool>::type
69  read_and_process_a_msg(SktT &src_skt, ClientCxn &dest_cxn, LatencyTimestamps &ts) noexcept(false);
70 
71  std::string to_string() const noexcept(false);
72 
73 private:
74  proc_rules_t processing_rules;
75 
76  /// Read a message from the source socket into the destination buffer
77  /**
78  Synchronously blocks until the entire message is read into the buffer.
79 
80  \param src_skt The connected socket from which to read the message.
81  \param buff Destination buffer for the message, which must be large enough.
82  \return False if the operation failed, otherwise true if succeeded.
83  */
84  template<class SktT>
85  bool read_msg_into_buff(SktT &src_skt, msg_buffer_t &buff) noexcept(false);
86 };
87 
88 template<class ProcessingRules> inline std::ostream &
89 operator<<(std::ostream &os, msg_processor<ProcessingRules> const &ec) noexcept(false);
90 
91 } } }
92 
94 
95 #endif