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