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