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