OGLplus  (0.59.0) a C++ wrapper for rendering APIs

blobs.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_MESSAGE_BUS_BLOBS_HPP
10 #define EAGINE_MESSAGE_BUS_BLOBS_HPP
11 
12 #include "../callable_ref.hpp"
13 #include "../double_buffer.hpp"
14 #include "../main_ctx_object.hpp"
15 #include "../memory/buffer_pool.hpp"
16 #include "../memory/split_block.hpp"
17 #include "../timeout.hpp"
18 #include "../valid_if/positive.hpp"
19 #include "message.hpp"
20 #include <cstdint>
21 #include <vector>
22 
23 namespace eagine::msgbus {
24 //------------------------------------------------------------------------------
25 struct pending_blob {
26  message_id msg_id{};
28  identifier_t target_id{0U};
29  std::uint64_t blob_id{0U};
30  memory::buffer blob{};
31  memory::const_split_block current{};
32  // TODO: recycle the done parts vectors?
33  double_buffer<std::vector<std::tuple<span_size_t, span_size_t>>>
34  done_parts{};
35  timeout max_time{};
37 
38  void init();
39  auto done_size() const noexcept -> span_size_t;
40  auto total_size() const noexcept -> span_size_t {
41  return blob.size();
42  }
43 
44  auto age() const noexcept -> message_age {
45  return std::chrono::duration_cast<message_age>(max_time.elapsed_time());
46  }
47 
48  auto is_complete() const noexcept -> bool;
49  auto merge_fragment(span_size_t offset, memory::const_block) -> bool;
50 };
51 //------------------------------------------------------------------------------
52 class blob_manipulator : main_ctx_object {
53 public:
54  blob_manipulator(main_ctx_parent parent)
55  : main_ctx_object{EAGINE_ID(BlobManipl), parent} {}
56 
57  auto max_blob_size() const noexcept -> valid_if_positive<span_size_t> {
58  return {span_size(_max_blob_size)};
59  }
60 
61  auto message_size(const pending_blob&, span_size_t max_message_size)
62  const noexcept -> span_size_t;
63 
64  auto cleanup() -> bool;
65 
66  void push_outgoing(
67  message_id msg_id,
68  identifier_t source_id,
69  identifier_t target_id,
71  std::chrono::seconds max_time,
72  message_priority priority);
73 
74  auto push_incoming_fragment(
75  message_id msg_id,
76  identifier_t source_id,
77  identifier_t blob_id,
78  std::int64_t offset,
79  std::int64_t total,
80  memory::const_block fragment,
81  message_priority priority) -> bool;
82 
83  using filter_function = callable_ref<bool(message_id)>;
84 
85  auto process_incoming(filter_function, const message_view&) -> bool;
86 
87  using fetch_handler =
88  callable_ref<bool(message_id, message_age, const message_view&)>;
89 
90  auto fetch_all(fetch_handler) -> span_size_t;
91 
92  using send_handler = callable_ref<bool(message_id, const message_view&)>;
93 
94  auto has_outgoing() const noexcept -> bool {
95  return !_outgoing.empty();
96  }
97  auto process_outgoing(send_handler, span_size_t max_data_size) -> bool;
98 
99 private:
100  std::int64_t _max_blob_size{16 * 1024 * 1024};
101  std::uint64_t _blob_id_sequence{0};
102  memory::buffer _scratch_buffer{};
103  memory::buffer_pool _buffers{};
104  std::vector<pending_blob> _outgoing{};
105  std::vector<pending_blob> _incoming{};
106 
107  auto _scratch_block(span_size_t size) -> memory::block;
108 };
109 //------------------------------------------------------------------------------
110 } // namespace eagine::msgbus
111 
112 #if !EAGINE_LINK_LIBRARY || defined(EAGINE_IMPLEMENTING_LIBRARY)
113 #include <eagine/message_bus/blobs.inl>
114 #endif
115 #endif // EAGINE_MESSAGE_BUS_BLOBS_HPP
const main_ctx_object_parent_info & main_ctx_parent
Alias for main_ctx_object_parent_info parameter type.
Definition: main_ctx_fwd.hpp:24
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
valid_if< T, valid_if_positive_policy< T > > valid_if_positive
Specialization of valid_if, for values valid if positive.
Definition: positive.hpp:44
#define EAGINE_ID(NAME)
Macro for constructing instances of eagine::identifier.
Definition: identifier.hpp:353
static constexpr auto span_size(T v) noexcept
Converts argument to span size type.
Definition: types.hpp:59
basic_split_block< true > const_split_block
Alias for const split blocks.
Definition: split_block.hpp:29
basic_block< true > const_block
Alias for const byte memory span.
Definition: block.hpp:32
basic_callable_ref< Sig, is_noexcept_function_v< Sig > > callable_ref
Alias for callable object references.
Definition: callable_ref.hpp:191
basic_block< false > block
Alias for non-const byte memory span.
Definition: block.hpp:27
@ source_id
The source has been verified.
message_priority
Message priority enumeration.
Definition: message.hpp:58
Message bus code is placed in this namespace.
Definition: eagine.hpp:58
@ normal
Normal, default message priority.
std::uint64_t identifier_t
The underlying integer type for eagine::identifier.
Definition: identifier_t.hpp:19
@ message_id
The message type id has been verified.
std::chrono::duration< float > message_age
Alias for message age type.
Definition: message.hpp:54

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).