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