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