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