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