hmbdc
simplify-high-performance-messaging-programming
Sender.hpp
1 #if 0
2 #include "hmbdc/Copyright.hpp"
3 #pragma once
4 #include "hmbdc/app/rmcast/SendTransportEngine.hpp"
5 #include "hmbdc/MetaUtils.hpp"
6 
7 namespace hmbdc { namespace app { namespace rmcast {
8 struct NetContext;
9 
10 namespace sender_detail {
11 using namespace std;
12 /**
13  * @brief fascade class for sending network messages
14  */
15 struct Sender {
16  using ptr = std::shared_ptr<Sender>;
17 private:
18  friend struct hmbdc::app::rmcast::NetContext;
19  Sender(SendTransportEngine::ptr transport, Topic const& t)
20  : transport_(transport)
21  , topic_(t) {
22  }
23 
24 
25 public:
26  /**
27  * @brief send a message's first bytes
28  * @details the bytes length could be larger than Message, but needs to
29  * be able to fit in the tansport buffer the sender is associated
30  * with - exception thrown otherwise
31  *
32  * @param msg the message to send
33  * @tparam Message Type
34  * @param len just send the first len bytes of this message
35  * @tparam T integral type
36  */
37  template <typename Message, typename T
38  , typename Enabled = typename std::enable_if<std::is_integral<T>::value, void>::type>
39  void send(Message&& msg, T len) {
40  using raw = typename decay<Message>::type;
41  static_assert(std::is_trivially_destructible<raw>::value, "cannot send message with dtor");
42  transport_->queueBytes(
43  topic_, raw::typeTag, &msg, static_cast<size_t>(len));
44  }
45 
46  /**
47  * @brief send a batch of message asynchronizely
48  * @details although batching could implicitly happen
49  * further performance gain could be achieved by sending more msg in
50  * a batch here
51  *
52  * @param m Message
53  * @tparam Message Type
54  */
55  template <typename... Messages>
56  void send(Messages&&... msgs) {
57  transport_->queue(topic_, std::forward<Messages>(msgs)...);
58  }
59 
60  /**
61  * @brief try to send a batch of message asynchronizely
62  * @details this call does not block and it is transactional - all or none is queued
63  *
64  *
65  * @param msgs messages
66  * @tparam Messages message types
67  * @return true if the messages are queued successfully in bufffer
68  */
69  template <typename... Messages>
70  bool trySend(Messages&&... msgs) {
71  return transport_->tryQueue(topic_, std::forward<Messages>(msgs)...);
72  }
73 
74  /**
75  * @brief send a message asynchronizely - avoiding Message copying
76  * by directly constructing the message in the buffer
77  *
78  * @param args Message's ctor args to construct the Message in the buffer
79  * @tparam Message Type
80  * @tparam typename ... Args args type
81  */
82  template <typename Message, typename ... Args>
83  void sendInPlace(Args&&... args) {
84  transport_->template queueInPlace<Message>(topic_, std::forward<Args>(args)...);
85  }
86 
87  /**
88  * @brief send a message asynchronizely by providing message
89  * in tag and bytes
90  * @details for runtime typed usage when the message type can only be decided
91  * at runtime
92  *
93  * @param tag message tag
94  * @param bytes message byte starting address
95  * @param len byte length of the above
96  */
97  void sendBytes(uint16_t tag, void const* bytes, size_t len) {
98  transport_->queueBytes(topic_, tag, bytes, len);
99  }
100 
101  /**
102  * @brief send a memory attachment message asynchronizely by providing constructed message
103  * in tag and bytes
104  * @details The message needs to have hasMemoryAttachment data (attachment and len) filled up already.
105  * it is for runtime typed usage when the hasMemoryAttachment message type can only be decided
106  * at runtime
107  *
108  * @param tag message tag
109  * @param attachment message bytes starting address
110  * @param len byte length of the above - not counting the attachment size
111  */
112  void sendBytes(uint16_t tag, hasMemoryAttachment const* att, size_t len) {
113  transport_->queueBytes(topic_, tag, att, len);
114  }
115 private:
116  SendTransport::ptr transport_;
117  Topic topic_;
118 };
119 }
120 
121 using Sender = sender_detail::Sender;
122 }}}
123 #endif
Definition: TypedString.hpp:84
Definition: Base.hpp:12