1 // Copyright (C) 2019 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
2 // This Source Code Form is subject to the terms of the Mozilla Public
3 // License, v. 2.0. If a copy of the MPL was not distributed with this
4 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 
6 #ifndef VSOMEIP_V3_TP_REASSEMBLER_HPP_
7 #define VSOMEIP_V3_TP_REASSEMBLER_HPP_
8 
9 #include <cstdint>
10 #include <map>
11 #include <mutex>
12 #include <memory>
13 
14 #include <boost/asio/ip/address.hpp>
15 #include <boost/asio/io_service.hpp>
16 #include <boost/asio/steady_timer.hpp>
17 
18 #include <vsomeip/primitive_types.hpp>
19 
20 #include "tp_message.hpp"
21 
22 
23 namespace vsomeip_v3 {
24 namespace tp {
25 
26 class tp_reassembler : public std::enable_shared_from_this<tp_reassembler> {
27 public:
28     tp_reassembler(std::uint32_t _max_message_size, boost::asio::io_service &_io);
29     /**
30      * @return Returns a pair consisting of a bool and a message_buffer_t. The
31      * value of the bool is set to true if the pair contains a finished message
32      */
33     std::pair<bool, message_buffer_t> process_tp_message(
34             const byte_t* const _data, std::uint32_t _data_size,
35             const boost::asio::ip::address& _address, std::uint16_t _port);
36     bool cleanup_unfinished_messages();
37     void stop();
38 
39 private:
40     void cleanup_timer_start(bool _force);
41     void cleanup_timer_start_unlocked(bool _force);
42     void cleanup_timer_cbk(const boost::system::error_code _error);
43 
44 private:
45     const std::uint32_t max_message_size_;
46     std::mutex cleanup_timer_mutex_;
47     bool cleanup_timer_running_;
48     boost::asio::steady_timer cleanup_timer_;
49 
50     std::mutex mutex_;
51     std::map<boost::asio::ip::address, std::map<std::uint16_t,
52         std::map<std::uint64_t, std::pair<session_t, tp_message>>>> tp_messages_;
53 };
54 
55 } // namespace tp
56 } // namespace vsomeip_v3
57 
58 #endif // VSOMEIP_V3_TP_REASSEMBLER_HPP_
59