JayBeams  0.1
Another project to have fun coding.
mold_udp_channel.hpp
Go to the documentation of this file.
1 #ifndef jb_itch5_mold_udp_channel_hpp
2 #define jb_itch5_mold_udp_channel_hpp
3 
4 #include <boost/asio/io_service.hpp>
5 #include <boost/asio/ip/udp.hpp>
6 
7 #include <chrono>
8 #include <functional>
9 
10 namespace jb {
11 namespace itch5 {
12 
13 class udp_receiver_config;
14 
15 /**
16  * Create and manage a socket to receive MoldUDP64 packets.
17  *
18  * This class creates a socket to receive MoldUDP64 packets,
19  * registers with the Boost.ASIO IO service to be notified of new
20  * packets in the socket, and when new packets are received it breaks
21  * down the packet into ITCH-5.0 messages and invokes a handler for
22  * each one.
23  */
25 public:
26  /**
27  * A callback function type to process any received ITCH-5.0
28  * messages
29  *
30  * The parameters represent (in order)
31  * - When was the MoldUDP64 packet containing this message received
32  * - The sequence number for this particular message
33  * - The offset (in bytes) from the beginning of the MoldUDP64
34  * stream for this message
35  * - The message, including the ITCH-5.0 headers but excluding any
36  * MoldUDP64 headers.
37  * - The size of the message, in bytes.
38  */
39  typedef std::function<void(
40  std::chrono::steady_clock::time_point, std::uint64_t, std::size_t,
41  char const*, std::size_t)>
43 
44  /**
45  * Constructor, create a socket and register for IO notifications.
46  *
47  * @param handler the callback to invoke to process any ITCH-5.0
48  * messages received
49  * @param io the Boost.ASIO IO service to register with for IO
50  * notifications
51  * @param cfg the configuration for the UDP receiver.
52  */
54  boost::asio::io_service& io, buffer_handler const& handler,
55  udp_receiver_config const& cfg);
56 
57  /**
58  * Constructor, create a socket and register for IO notifications.
59  *
60  * @param handler the callback to invoke to process any ITCH-5.0
61  * messages received
62  * @param io the Boost.ASIO IO service to register with for IO
63  * notifications
64  * @param cfg the configuration for the UDP receiver.
65  */
67  boost::asio::io_service& io, buffer_handler&& handler,
68  udp_receiver_config const& cfg);
69 
70 private:
71  /**
72  * Refactor code to register (and reregister) for Boost.ASIO
73  * notifications
74  *
75  * In the Boost.ASIO framework all I/O callbacks fire only once. We
76  * must reset them to get the first I/O event, and after each one.
77  * This function refactors that code into a single place.
78  */
80 
81  /**
82  * The Boost.ASIO callback for I/O events
83  *
84  * @param ec contains the error code, if any, detected while trying
85  * to read the data
86  * @param bytes_received the number of bytes received, the actual
87  * bytes are in the buffer_ class member.
88  */
89  void
90  handle_received(boost::system::error_code const& ec, size_t bytes_received);
91 
92  /// Allow testing class access to the code ...
93  friend struct mold_udp_channel_tester;
94 
95 private:
96  // The callback handler
98 
99  // A UDP socket configured as per the constructor arguments
100  boost::asio::ip::udp::socket socket_;
101 
102  // The next sequence number expected from the MoldUDP64 stream
104 
105  // The offset (in bytes) since the beginning of the MoldUDP64
106  // stream, mostly for logging.
107  std::size_t message_offset_;
108 
109  // The maximum packet length expected (UDP is limited to 2^16 bytes)
110  static std::size_t const buflen = 1 << 16;
111 
112  // A buffer to read the data into, when handle_received() is called
113  // the data should already be in this location.
115 
116  // The UDP endpoint that sent the last received MoldUDP64 packet
117  boost::asio::ip::udp::endpoint sender_endpoint_;
118 };
119 
120 } // namespace itch5
121 } // namespace jb
122 
123 #endif // jb_itch5_mold_udp_channel_hpp
mold_udp_channel(boost::asio::io_service &io, buffer_handler const &handler, udp_receiver_config const &cfg)
Constructor, create a socket and register for IO notifications.
clock_type::time_point time_point
A convenience alias for clock_type::time_point.
static std::size_t const buflen
std::function< void(std::chrono::steady_clock::time_point, std::uint64_t, std::size_t, char const *, std::size_t)> buffer_handler
A callback function type to process any received ITCH-5.0 messages.
void handle_received(boost::system::error_code const &ec, size_t bytes_received)
The Boost.ASIO callback for I/O events.
void restart_async_receive_from()
Refactor code to register (and reregister) for Boost.ASIO notifications.
Break encapsulation in jb::itch5::mold_udp_channel for testing purposes.
boost::asio::ip::udp::endpoint sender_endpoint_
boost::asio::ip::udp::socket socket_
A configuration object for UDP receivers.
Create and manage a socket to receive MoldUDP64 packets.
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7