JayBeams  0.1
Another project to have fun coding.
process_iostream_mlist.hpp
Go to the documentation of this file.
1 #ifndef jb_itch5_process_iostream_mlist_hpp
2 #define jb_itch5_process_iostream_mlist_hpp
3 
6 #include <jb/log.hpp>
7 
8 namespace jb {
9 namespace itch5 {
10 
11 /*
12  * Process an iostream of ITCH-5.0 given a list of expected messages.
13  *
14  * This function parses each ITCH-5.0 message in an iostream, then
15  * calls a handler for the message. The messages are allocated on the
16  * stack, and do not survice the call to the handler.
17  *
18  * If the iostream contains a message that is not on the list, the
19  * message is not parsed and the handle_unknown() member function of
20  * the handler can be ignored. This is convenient when you want to
21  * ignore the most common messages in the protocol, and only process a
22  * subset. It also decouples the list of messages from the processing.
23  *
24  * Please see @ref jb::itch5::message_handler_concept for a detailed
25  * description of the message_handler requirements.
26  */
27 template <typename message_handler, typename... message_types>
28 void process_iostream_mlist(std::istream& is, message_handler& handler) {
29  std::size_t msgoffset = 0;
30  for (std::uint64_t msgcnt = 0; is.good(); ++msgcnt) {
31  // We only use the side-effects of this call, particularly during
32  // testing.
33  (void)handler.now();
34  char blen[2];
35  is.read(blen, 2);
36  if (not is) {
37  if (not is.eof()) {
38  JB_LOG(error) << "reading length when msgcnt=" << msgcnt
39  << ", msgoffset=" << msgoffset;
40  }
41  return;
42  }
43  msgoffset += 2;
44 
45  constexpr std::size_t maxmsglen = 1L << 16;
46  std::size_t msglen = jb::itch5::decoder<true, std::uint16_t>::r(2, blen, 0);
47  char msgbuf[maxmsglen];
48  is.read(msgbuf, msglen);
49  auto recv_ts = handler.now();
51  handler, recv_ts, msgcnt, msgoffset, msgbuf, msglen);
52  msgoffset += msglen;
53  }
54 }
55 
56 } // namespace itch5
57 } // namespace jb
58 
59 #endif // jb_itch5_process_iostream_mlist_hpp
void process_iostream_mlist(std::istream &is, message_handler &handler)
static T r(std::size_t size, void const *msg, std::size_t offset)
Read a single message or field.
Process a buffer with a single message: parse it and call the handler.
#define JB_LOG(lvl)
Definition: log.hpp:70
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7