ybusbuf.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #pragma once
  2. #include <library/cpp/messagebus/ybus.h>
  3. #include <google/protobuf/descriptor.h>
  4. #include <google/protobuf/message.h>
  5. #include <util/generic/cast.h>
  6. #include <util/generic/vector.h>
  7. #include <util/stream/mem.h>
  8. #include <array>
  9. namespace NBus {
  10. using TBusBufferRecord = ::google::protobuf::Message;
  11. template <class TBufferMessage>
  12. class TBusBufferMessagePtr;
  13. template <class TBufferMessage>
  14. class TBusBufferMessageAutoPtr;
  15. class TBusBufferBase: public TBusMessage {
  16. public:
  17. TBusBufferBase(int type)
  18. : TBusMessage((ui16)type)
  19. {
  20. }
  21. TBusBufferBase(ECreateUninitialized)
  22. : TBusMessage(MESSAGE_CREATE_UNINITIALIZED)
  23. {
  24. }
  25. ui16 GetType() const {
  26. return GetHeader()->Type;
  27. }
  28. virtual TBusBufferRecord* GetRecord() const = 0;
  29. virtual TBusBufferBase* New() = 0;
  30. };
  31. ///////////////////////////////////////////////////////////////////
  32. /// \brief Template for all messages that have protobuf description
  33. /// @param TBufferRecord is record described in .proto file with namespace
  34. /// @param MessageFile is offset for .proto file message ids
  35. /// \attention If you want one protocol NBus::TBusBufferProtocol to handle
  36. /// messageges described in different .proto files, make sure that they have
  37. /// unique values for MessageFile
  38. template <class TBufferRecord, int MType>
  39. class TBusBufferMessage: public TBusBufferBase {
  40. public:
  41. static const int MessageType = MType;
  42. typedef TBusBufferMessagePtr<TBusBufferMessage<TBufferRecord, MType>> TPtr;
  43. typedef TBusBufferMessageAutoPtr<TBusBufferMessage<TBufferRecord, MType>> TAutoPtr;
  44. public:
  45. typedef TBufferRecord RecordType;
  46. TBufferRecord Record;
  47. public:
  48. TBusBufferMessage()
  49. : TBusBufferBase(MessageType)
  50. {
  51. }
  52. TBusBufferMessage(ECreateUninitialized)
  53. : TBusBufferBase(MESSAGE_CREATE_UNINITIALIZED)
  54. {
  55. }
  56. explicit TBusBufferMessage(const TBufferRecord& record)
  57. : TBusBufferBase(MessageType)
  58. , Record(record)
  59. {
  60. }
  61. explicit TBusBufferMessage(TBufferRecord&& record)
  62. : TBusBufferBase(MessageType)
  63. , Record(std::move(record))
  64. {
  65. }
  66. public:
  67. TBusBufferRecord* GetRecord() const override {
  68. return (TBusBufferRecord*)&Record;
  69. }
  70. TBusBufferBase* New() override {
  71. return new TBusBufferMessage<TBufferRecord, MessageType>();
  72. }
  73. };
  74. template <class TSelf, class TBufferMessage>
  75. class TBusBufferMessagePtrBase {
  76. public:
  77. typedef typename TBufferMessage::RecordType RecordType;
  78. private:
  79. TSelf* GetSelf() {
  80. return static_cast<TSelf*>(this);
  81. }
  82. const TSelf* GetSelf() const {
  83. return static_cast<const TSelf*>(this);
  84. }
  85. public:
  86. RecordType* operator->() {
  87. Y_ASSERT(GetSelf()->Get());
  88. return &(GetSelf()->Get()->Record);
  89. }
  90. const RecordType* operator->() const {
  91. Y_ASSERT(GetSelf()->Get());
  92. return &(GetSelf()->Get()->Record);
  93. }
  94. RecordType& operator*() {
  95. Y_ASSERT(GetSelf()->Get());
  96. return GetSelf()->Get()->Record;
  97. }
  98. const RecordType& operator*() const {
  99. Y_ASSERT(GetSelf()->Get());
  100. return GetSelf()->Get()->Record;
  101. }
  102. TBusHeader* GetHeader() {
  103. return GetSelf()->Get()->GetHeader();
  104. }
  105. const TBusHeader* GetHeader() const {
  106. return GetSelf()->Get()->GetHeader();
  107. }
  108. };
  109. template <class TBufferMessage>
  110. class TBusBufferMessagePtr: public TBusBufferMessagePtrBase<TBusBufferMessagePtr<TBufferMessage>, TBufferMessage> {
  111. protected:
  112. TBufferMessage* Holder;
  113. public:
  114. TBusBufferMessagePtr(TBufferMessage* mess)
  115. : Holder(mess)
  116. {
  117. }
  118. static TBusBufferMessagePtr<TBufferMessage> DynamicCast(TBusMessage* message) {
  119. return dynamic_cast<TBufferMessage*>(message);
  120. }
  121. TBufferMessage* Get() {
  122. return Holder;
  123. }
  124. const TBufferMessage* Get() const {
  125. return Holder;
  126. }
  127. operator TBufferMessage*() {
  128. return Holder;
  129. }
  130. operator const TBufferMessage*() const {
  131. return Holder;
  132. }
  133. operator TAutoPtr<TBusMessage>() {
  134. TAutoPtr<TBusMessage> r(Holder);
  135. Holder = 0;
  136. return r;
  137. }
  138. operator TBusMessageAutoPtr() {
  139. TBusMessageAutoPtr r(Holder);
  140. Holder = nullptr;
  141. return r;
  142. }
  143. };
  144. template <class TBufferMessage>
  145. class TBusBufferMessageAutoPtr: public TBusBufferMessagePtrBase<TBusBufferMessageAutoPtr<TBufferMessage>, TBufferMessage> {
  146. public:
  147. TAutoPtr<TBufferMessage> AutoPtr;
  148. public:
  149. TBusBufferMessageAutoPtr() {
  150. }
  151. TBusBufferMessageAutoPtr(TBufferMessage* message)
  152. : AutoPtr(message)
  153. {
  154. }
  155. TBufferMessage* Get() {
  156. return AutoPtr.Get();
  157. }
  158. const TBufferMessage* Get() const {
  159. return AutoPtr.Get();
  160. }
  161. TBufferMessage* Release() const {
  162. return AutoPtr.Release();
  163. }
  164. operator TAutoPtr<TBusMessage>() {
  165. return AutoPtr.Release();
  166. }
  167. operator TBusMessageAutoPtr() {
  168. return AutoPtr.Release();
  169. }
  170. };
  171. /////////////////////////////////////////////
  172. /// \brief Generic protocol object for messages descibed with protobuf
  173. /// \attention If you mix messages in the same protocol from more than
  174. /// .proto file make sure that they have different MessageFile parameter
  175. /// in the NBus::TBusBufferMessage template
  176. class TBusBufferProtocol: public TBusProtocol {
  177. private:
  178. TVector<TBusBufferBase*> Types;
  179. std::array<ui32, ((1 << 16) >> 5)> TypeMask;
  180. TBusBufferBase* FindType(int type);
  181. bool IsRegisteredType(unsigned type);
  182. public:
  183. TBusBufferProtocol(TBusService name, int port);
  184. ~TBusBufferProtocol() override;
  185. /// register all the message that this protocol should handle
  186. void RegisterType(TAutoPtr<TBusBufferBase> mess);
  187. TArrayRef<TBusBufferBase* const> GetTypes() const;
  188. /// serialized protocol specific data into TBusData
  189. void Serialize(const TBusMessage* mess, TBuffer& data) override;
  190. TAutoPtr<TBusMessage> Deserialize(ui16 messageType, TArrayRef<const char> payload) override;
  191. };
  192. }