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/tcpcast/SendTransportEngine.hpp"
4 #include "hmbdc/app/RequestReply.hpp"
5 #include "hmbdc/MetaUtils.hpp"
6 
7 namespace hmbdc { namespace app { namespace tcpcast {
8 
9 /**
10  * @brief fascade class for sending network messages
11  */
12 struct Sender
13 : RequestReplySender<Sender> {
14  using ptr = std::shared_ptr<Sender>;
15 private:
16  friend struct NetContext;
17  Sender(SendTransportEngine::ptr transport, comm::Topic const& t)
18  : transport_(transport)
19  , topic_(t) {
20  }
21 
22 
23 public:
24  /**
25  * @brief send a message's first bytes
26  * @details the bytes length could be larger than Message, but needs to
27  * be able to fit in the tansport buffer the sender is associated
28  * with - exception thrown otherwise
29  *
30  * @param msg the message to send
31  * @tparam Message Type
32  * @param len just send the first len bytes of this message
33  * @tparam T integral type
34  */
35  template <typename Message, typename T
36  , typename Enabled = typename std::enable_if<std::is_integral<T>::value, void>::type>
37  void send(Message&& msg, T len) {
38  using namespace std;
39  using raw = typename decay<Message>::type;
40  static_assert(std::is_trivially_destructible<raw>::value, "cannot send message with dtor");
41  static_assert(!is_base_of<hasMemoryAttachment, raw>::value
43  , "hasMemoryAttachment has to the first base for Message");
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 try to send a batch of message asynchronizely
64  * @details this call does not block and it is transactional - all or none is queued
65  *
66  *
67  * @param msgs messages
68  * @tparam Messages message types
69  * @return true if the messages are queued successfully in bufffer
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 private:
103  SendTransport::ptr transport_;
104  comm::Topic topic_;
105 };
106 }}}
bool trySend(Messages &&...msgs)
try to send a batch of message asynchronizely
Definition: Sender.hpp:72
void send(Messages &&...msgs)
send a batch of message asynchronizely
Definition: Sender.hpp:58
fascade class for sending network messages
Definition: Sender.hpp:12
topic as in the publish / subscribe communication paradigm
Definition: Topic.hpp:14
synchronous request reply interface for the Network Sender
Definition: RequestReply.hpp:166
Definition: MetaUtils.hpp:36
a singleton that holding tcpcast resources
Definition: NetContext.hpp:44
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
void send(Message &&msg, T len)
send a message&#39;s first bytes
Definition: Sender.hpp:37
void sendInPlace(Args &&...args)
send a message asynchronizely - avoiding Message copying by directly constructing the message in the ...
Definition: Sender.hpp:85