libflute
Receiver.h
Go to the documentation of this file.
1 // libflute - FLUTE/ALC library
2 //
3 // Copyright (C) 2021 Klaus Kühnhammer (Österreichische Rundfunksender GmbH & Co KG)
4 //
5 // Licensed under the License terms and conditions for use, reproduction, and
6 // distribution of 5G-MAG software (the “License”). You may not use this file
7 // except in compliance with the License. You may obtain a copy of the License at
8 // https://www.5g-mag.com/reference-tools. Unless required by applicable law or
9 // agreed to in writing, software distributed under the License is distributed on
10 // an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 // or implied.
12 //
13 // See the License for the specific language governing permissions and limitations
14 // under the License.
15 //
16 #pragma once
17 #include <boost/asio.hpp>
18 #include <boost/bind/bind.hpp>
19 #include <atomic>
20 #include <memory>
21 #include <string>
22 #include <map>
23 #include <mutex>
24 #include "File.h"
25 #include "FileDeliveryTable.h"
26 
27 namespace LibFlute {
31  class Receiver {
32  public:
39  typedef std::function<void(std::shared_ptr<LibFlute::File>)> completion_callback_t;
51  Receiver( const std::string& iface, const std::string& address,
52  short port, uint64_t tsi,
53  boost::asio::io_context& io_context,
54  const std::string& source_address = "");
55 
65  virtual ~Receiver();
66 
73  void enable_ipsec( uint32_t spi, const std::string& aes_key);
74 
80  std::vector<std::shared_ptr<LibFlute::File>> file_list();
81 
85  void remove_expired_files(unsigned max_age);
86 
90  void remove_file_with_content_location(const std::string& cl);
91 
97  void register_completion_callback(completion_callback_t cb) { _completion_cb = cb; };
98 
99  void stop() { _running = false; }
100  private:
101 
102  void handle_receive_from(const boost::system::error_code& error,
103  size_t bytes_recvd);
104  void arm_receive();
105  boost::asio::ip::udp::socket _socket;
106  boost::asio::ip::udp::endpoint _sender_endpoint;
107 
108  // Must hold the largest UDP datagram that can actually arrive: with the
109  // Compact No-Code FEC scheme, a packet is a 4-byte SBN+ID header plus one
110  // full encoding symbol, and FEC-OTI-Encoding-Symbol-Length is a per-session
111  // configuration value with no fixed small upper bound (e.g. large symbols
112  // sized close to the path MTU, or, as over loopback/jumbo-capable links,
113  // sized close to the max IPv4 UDP payload). A too-small buffer here doesn't
114  // error out -- recvfrom() on a datagram socket silently truncates to
115  // whatever fits, so every symbol beyond that size is completed with
116  // whatever partial prefix arrived, and the truncation is invisible until a
117  // Content-MD5 check (if present in the FDT) catches the corruption. 65536
118  // covers the maximum possible IPv4 UDP payload (65507 bytes) with margin.
119  enum { max_length = 65536 };
120  char _data[max_length];
121  uint64_t _tsi;
122  std::unique_ptr<LibFlute::FileDeliveryTable> _fdt;
123  // FDT instance currently being reassembled at TOI 0 (0xFFFFFFFF = none).
124  // Used to discard a partial FDT object when a newer instance starts
125  // arriving, so two instances never splice into one corrupt buffer.
126  uint32_t _fdt_in_progress_instance_id = 0xFFFFFFFF;
127  std::map<uint64_t, std::shared_ptr<LibFlute::File>> _files;
128  std::mutex _files_mutex;
129  std::string _mcast_address;
130 
131  completion_callback_t _completion_cb = nullptr;
132 
133  bool _running = true;
134 
135  // See ~Receiver()'s comment. Copied into each async_receive_from completion handler;
136  // outlives `this` if the receiver is destroyed while a read is in flight.
137  std::shared_ptr<std::atomic<bool>> _alive = std::make_shared<std::atomic<bool>>(true);
138  };
139 };
FLUTE receiver class.
Definition: Receiver.h:31
void register_completion_callback(completion_callback_t cb)
Register a callback for file reception notifications.
Definition: Receiver.h:97
Receiver(const std::string &iface, const std::string &address, short port, uint64_t tsi, boost::asio::io_context &io_context, const std::string &source_address="")
Default constructor.
Definition: Receiver.cpp:68
void remove_file_with_content_location(const std::string &cl)
Remove a file from the list that matches the passed content location.
Definition: Receiver.cpp:351
virtual ~Receiver()
Destructor.
Definition: Receiver.cpp:178
std::vector< std::shared_ptr< LibFlute::File > > file_list()
List all current files.
Definition: Receiver.cpp:328
void enable_ipsec(uint32_t spi, const std::string &aes_key)
Enable IPSEC ESP decryption of FLUTE payloads.
Definition: Receiver.cpp:194
void remove_expired_files(unsigned max_age)
Remove files from the list that are older than max_age seconds.
Definition: Receiver.cpp:337
std::function< void(std::shared_ptr< LibFlute::File >)> completion_callback_t
Definition of a file reception completion callback function that can be registered through ::register...
Definition: Receiver.h:39