aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/protobuf/ybusbuf.h
blob: b01338f9866f2f31612c5d86b2a7a90e8396cae7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#pragma once

#include <library/cpp/messagebus/ybus.h>

#include <google/protobuf/descriptor.h>
#include <google/protobuf/message.h>

#include <util/generic/cast.h>
#include <util/generic/vector.h>
#include <util/stream/mem.h>

#include <array>

namespace NBus {
    using TBusBufferRecord = ::google::protobuf::Message; 

    template <class TBufferMessage> 
    class TBusBufferMessagePtr; 
    template <class TBufferMessage> 
    class TBusBufferMessageAutoPtr; 

    class TBusBufferBase: public TBusMessage { 
    public: 
        TBusBufferBase(int type) 
            : TBusMessage((ui16)type) 
        { 
        } 
        TBusBufferBase(ECreateUninitialized) 
            : TBusMessage(MESSAGE_CREATE_UNINITIALIZED) 
        { 
        } 

        ui16 GetType() const { 
            return GetHeader()->Type; 
        } 

        virtual TBusBufferRecord* GetRecord() const = 0; 
        virtual TBusBufferBase* New() = 0; 
    }; 

    /////////////////////////////////////////////////////////////////// 
    /// \brief Template for all messages that have protobuf description 

    /// @param TBufferRecord is record described in .proto file with namespace 
    /// @param MessageFile is offset for .proto file message ids 

    /// \attention If you want one protocol NBus::TBusBufferProtocol to handle 
    /// messageges described in different .proto files, make sure that they have 
    /// unique values for MessageFile 

    template <class TBufferRecord, int MType> 
    class TBusBufferMessage: public TBusBufferBase { 
    public: 
        static const int MessageType = MType; 

        typedef TBusBufferMessagePtr<TBusBufferMessage<TBufferRecord, MType>> TPtr; 
        typedef TBusBufferMessageAutoPtr<TBusBufferMessage<TBufferRecord, MType>> TAutoPtr; 
 
    public: 
        typedef TBufferRecord RecordType; 
        TBufferRecord Record; 
 
    public: 
        TBusBufferMessage() 
            : TBusBufferBase(MessageType) 
        { 
        } 
        TBusBufferMessage(ECreateUninitialized) 
            : TBusBufferBase(MESSAGE_CREATE_UNINITIALIZED) 
        { 
        } 
        explicit TBusBufferMessage(const TBufferRecord& record) 
            : TBusBufferBase(MessageType) 
            , Record(record) 
        { 
        } 
        explicit TBusBufferMessage(TBufferRecord&& record) 
            : TBusBufferBase(MessageType) 
            , Record(std::move(record)) 
        { 
        } 
 
    public: 
        TBusBufferRecord* GetRecord() const override { 
            return (TBusBufferRecord*)&Record; 
        } 
        TBusBufferBase* New() override { 
            return new TBusBufferMessage<TBufferRecord, MessageType>(); 
        } 
    }; 

    template <class TSelf, class TBufferMessage> 
    class TBusBufferMessagePtrBase { 
    public: 
        typedef typename TBufferMessage::RecordType RecordType; 

    private: 
        TSelf* GetSelf() { 
            return static_cast<TSelf*>(this); 
        } 
        const TSelf* GetSelf() const { 
            return static_cast<const TSelf*>(this); 
        } 

    public: 
        RecordType* operator->() { 
            Y_ASSERT(GetSelf()->Get()); 
            return &(GetSelf()->Get()->Record); 
        } 
        const RecordType* operator->() const { 
            Y_ASSERT(GetSelf()->Get()); 
            return &(GetSelf()->Get()->Record); 
        } 
        RecordType& operator*() { 
            Y_ASSERT(GetSelf()->Get()); 
            return GetSelf()->Get()->Record; 
        } 
        const RecordType& operator*() const { 
            Y_ASSERT(GetSelf()->Get()); 
            return GetSelf()->Get()->Record; 
        } 

        TBusHeader* GetHeader() { 
            return GetSelf()->Get()->GetHeader(); 
        } 
        const TBusHeader* GetHeader() const { 
            return GetSelf()->Get()->GetHeader(); 
        } 
    }; 

    template <class TBufferMessage> 
    class TBusBufferMessagePtr: public TBusBufferMessagePtrBase<TBusBufferMessagePtr<TBufferMessage>, TBufferMessage> { 
    protected: 
        TBufferMessage* Holder; 

    public: 
        TBusBufferMessagePtr(TBufferMessage* mess) 
            : Holder(mess) 
        { 
        } 
        static TBusBufferMessagePtr<TBufferMessage> DynamicCast(TBusMessage* message) { 
            return dynamic_cast<TBufferMessage*>(message); 
        } 
        TBufferMessage* Get() { 
            return Holder; 
        } 
        const TBufferMessage* Get() const { 
            return Holder; 
        } 

        operator TBufferMessage*() { 
            return Holder; 
        } 
        operator const TBufferMessage*() const { 
            return Holder; 
        } 

        operator TAutoPtr<TBusMessage>() { 
            TAutoPtr<TBusMessage> r(Holder); 
            Holder = 0; 
            return r; 
        } 
        operator TBusMessageAutoPtr() { 
            TBusMessageAutoPtr r(Holder); 
            Holder = nullptr; 
            return r; 
        } 
    }; 

    template <class TBufferMessage> 
    class TBusBufferMessageAutoPtr: public TBusBufferMessagePtrBase<TBusBufferMessageAutoPtr<TBufferMessage>, TBufferMessage> { 
    public: 
        TAutoPtr<TBufferMessage> AutoPtr; 

    public: 
        TBusBufferMessageAutoPtr() { 
        } 
        TBusBufferMessageAutoPtr(TBufferMessage* message) 
            : AutoPtr(message) 
        { 
        } 

        TBufferMessage* Get() { 
            return AutoPtr.Get(); 
        } 
        const TBufferMessage* Get() const { 
            return AutoPtr.Get(); 
        } 

        TBufferMessage* Release() const { 
            return AutoPtr.Release(); 
        } 

        operator TAutoPtr<TBusMessage>() { 
            return AutoPtr.Release(); 
        } 
        operator TBusMessageAutoPtr() { 
            return AutoPtr.Release(); 
        } 
    }; 

    ///////////////////////////////////////////// 
    /// \brief Generic protocol object for messages descibed with protobuf 

    /// \attention If you mix messages in the same protocol from more than 
    /// .proto file make sure that they have different MessageFile parameter 
    ///  in the NBus::TBusBufferMessage template 

    class TBusBufferProtocol: public TBusProtocol { 
    private: 
        TVector<TBusBufferBase*> Types; 
        std::array<ui32, ((1 << 16) >> 5)> TypeMask; 

        TBusBufferBase* FindType(int type); 
        bool IsRegisteredType(unsigned type); 

    public: 
        TBusBufferProtocol(TBusService name, int port); 

        ~TBusBufferProtocol() override; 
 
        /// register all the message that this protocol should handle 
        void RegisterType(TAutoPtr<TBusBufferBase> mess); 
 
        TArrayRef<TBusBufferBase* const> GetTypes() const; 

        /// serialized protocol specific data into TBusData 
        void Serialize(const TBusMessage* mess, TBuffer& data) override; 

        TAutoPtr<TBusMessage> Deserialize(ui16 messageType, TArrayRef<const char> payload) override; 
    }; 

}