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 class NetContext;
15  Sender(SendTransportEngine::ptr transport, 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 = enable_if<is_integral<T>::value>>
34  void send(Message&& msg, T len) {
35  using raw = typename remove_reference<Message>::type;
36  transport_->queueBytes(
37  topic_, raw::typeTag, &msg, static_cast<size_t>(len));
38  }
39 
40  /**
41  * @brief send a batch of message asynchronizely
42  * @details although batching could implicitly happen
43  * further performance gain could be achieved by sending more msg in
44  * a batch here
45  *
46  * @param msgs messages
47  * @tparam Messages message types
48  */
49  template <typename... Messages>
50  void send(Messages&&... msgs) {
51  transport_->queue(topic_, std::forward<Messages>(msgs)...);
52  }
53 
54  /**
55  * @brief try to send a batch of message asynchronizely, return false if queue is full
56  * @details this call does not block and is transactional - all or none is queued
57  *
58  * @param msgs messages
59  * @tparam Messages message types
60  * @return true if messages are queued successfully
61  */
62  template <typename... Messages>
63  bool trySend(Messages&&... msgs) {
64  return transport_->tryQueue(topic_, std::forward<Messages>(msgs)...);
65  }
66 
67  /**
68  * @brief send a message asynchronizely - avoiding Message copying
69  * by directly constructing the message in the buffer
70  *
71  * @param args Message's ctor args to construct the Message in the buffer
72  * @tparam Message Type
73  * @tparam typename ... Args args type
74  */
75  template <typename Message, typename ... Args>
76  void sendInPlace(Args&&... args) {
77  transport_->template queueInPlace<Message>(topic_, std::forward<Args>(args)...);
78  }
79 
80  /**
81  * @brief send a message asynchronizely by providing message
82  * in tag and bytes
83  * @details for runtime typed usage when the message type can only be decided
84  * at runtime
85  *
86  * @param tag message tag
87  * @param bytes message byte starting address
88  * @param len byte length of the above
89  */
90  void sendBytes(uint16_t tag, void const* bytes, size_t len) {
91  transport_->queueBytes(topic_, tag, bytes, len);
92  }
93 private:
94  SendTransportEngine::ptr transport_;
95  Topic topic_;
96 };
97 }}}
void sendInPlace(Args &&...args)
send a message asynchronizely - avoiding Message copying by directly constructing the message in the ...
Definition: Sender.hpp:76
topic as in the publish / subscribe communication paradigm
Definition: Topic.hpp:14
fascade class for sending network messages
Definition: Sender.hpp:10
bool trySend(Messages &&...msgs)
try to send a batch of message asynchronizely, return false if queue is full
Definition: Sender.hpp:63
void send(Messages &&...msgs)
send a batch of message asynchronizely
Definition: Sender.hpp:50
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:90
Definition: NetContext.hpp:26
Definition: Client.hpp:11