|
21 | 21 |
|
22 | 22 | #include <google/protobuf/message.h> // Message |
23 | 23 | #include "butil/iobuf.h" // butil::IOBuf |
24 | | -#include "json2pb/pb_to_json.h" |
25 | | -#include "json2pb/json_to_pb.h" |
| 24 | +#include "butil/logging.h" |
26 | 25 | #include "brpc/options.pb.h" // CompressType |
| 26 | +#include "brpc/nonreflectable_message.h" |
27 | 27 |
|
28 | 28 | namespace brpc { |
29 | 29 |
|
| 30 | +// Serializer can be used to implement custom serialization |
| 31 | +// before compression with user callback. |
| 32 | +class Serializer : public NonreflectableMessage<Serializer> { |
| 33 | +public: |
| 34 | + using Callback = std::function<bool(google::protobuf::io::ZeroCopyOutputStream*)>; |
| 35 | + |
| 36 | + Serializer() :Serializer(NULL) {} |
| 37 | + |
| 38 | + explicit Serializer(Callback callback) |
| 39 | + :_callback(std::move(callback)) { |
| 40 | + SharedCtor(); |
| 41 | + } |
| 42 | + |
| 43 | + ~Serializer() override { |
| 44 | + SharedDtor(); |
| 45 | + } |
| 46 | + |
| 47 | + Serializer(const Serializer& from) |
| 48 | + : NonreflectableMessage(from) { |
| 49 | + SharedCtor(); |
| 50 | + MergeFrom(from); |
| 51 | + } |
| 52 | + |
| 53 | + Serializer& operator=(const Serializer& from) { |
| 54 | + CopyFrom(from); |
| 55 | + return *this; |
| 56 | + } |
| 57 | + |
| 58 | + void Swap(Serializer* other) { |
| 59 | + if (other != this) { |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + void MergeFrom(const Serializer& from) override { |
| 64 | + CHECK_NE(&from, this); |
| 65 | + } |
| 66 | + |
| 67 | + // implements Message ---------------------------------------------- |
| 68 | + void Clear() override { |
| 69 | + _callback = nullptr; |
| 70 | + } |
| 71 | + size_t ByteSizeLong() const override { return 0; } |
| 72 | + int GetCachedSize() const PB_425_OVERRIDE { return ByteSize(); } |
| 73 | + |
| 74 | + ::google::protobuf::Metadata GetMetadata() const PB_527_OVERRIDE; |
| 75 | + |
| 76 | + // Converts the data into `output' for later compression. |
| 77 | + bool SerializeTo(google::protobuf::io::ZeroCopyOutputStream* output) const { |
| 78 | + if (!_callback) { |
| 79 | + LOG(WARNING) << "CompressCallback::SerializeTo() called without converter"; |
| 80 | + return false; |
| 81 | + } |
| 82 | + return _callback(output); |
| 83 | + } |
| 84 | + |
| 85 | + void SetCallback(Callback callback) { |
| 86 | + _callback = std::move(callback); |
| 87 | + } |
| 88 | + |
| 89 | +private: |
| 90 | + void SharedCtor() {} |
| 91 | + void SharedDtor() {} |
| 92 | + |
| 93 | + Callback _callback; |
| 94 | +}; |
| 95 | + |
| 96 | +// Deserializer can be used to implement custom deserialization |
| 97 | +// after decompression with user callback. |
| 98 | +class Deserializer : public NonreflectableMessage<Deserializer> { |
| 99 | +public: |
| 100 | +public: |
| 101 | + using Callback = std::function<bool(google::protobuf::io::ZeroCopyInputStream*)>; |
| 102 | + |
| 103 | + Deserializer() :Deserializer(NULL) {} |
| 104 | + |
| 105 | + explicit Deserializer(Callback callback) : _callback(std::move(callback)) { |
| 106 | + SharedCtor(); |
| 107 | + } |
| 108 | + |
| 109 | + ~Deserializer() override { |
| 110 | + SharedDtor(); |
| 111 | + } |
| 112 | + |
| 113 | + Deserializer(const Deserializer& from) |
| 114 | + : NonreflectableMessage(from) { |
| 115 | + SharedCtor(); |
| 116 | + MergeFrom(from); |
| 117 | + } |
| 118 | + |
| 119 | + Deserializer& operator=(const Deserializer& from) { |
| 120 | + CopyFrom(from); |
| 121 | + return *this; |
| 122 | + } |
| 123 | + |
| 124 | + void Swap(Deserializer* other) { |
| 125 | + if (other != this) { |
| 126 | + _callback.swap(other->_callback); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + void MergeFrom(const Deserializer& from) override { |
| 131 | + CHECK_NE(&from, this); |
| 132 | + _callback = from._callback; |
| 133 | + } |
| 134 | + |
| 135 | + // implements Message ---------------------------------------------- |
| 136 | + void Clear() override { _callback = nullptr; } |
| 137 | + size_t ByteSizeLong() const override { return 0; } |
| 138 | + int GetCachedSize() const PB_425_OVERRIDE { return ByteSize(); } |
| 139 | + |
| 140 | + ::google::protobuf::Metadata GetMetadata() const PB_527_OVERRIDE; |
| 141 | + |
| 142 | + // Converts the decompressed `input'. |
| 143 | + bool DeserializeFrom(google::protobuf::io::ZeroCopyInputStream* intput) const { |
| 144 | + if (!_callback) { |
| 145 | + LOG(WARNING) << "Deserializer::DeserializeFrom() called without callback"; |
| 146 | + return false; |
| 147 | + } |
| 148 | + return _callback(intput); |
| 149 | + } |
| 150 | + void SetCallback(Callback callback) { |
| 151 | + _callback = std::move(callback); |
| 152 | + } |
| 153 | + |
| 154 | +private: |
| 155 | + void SharedCtor() {} |
| 156 | + void SharedDtor() {} |
| 157 | + |
| 158 | + Callback _callback; |
| 159 | +}; |
| 160 | + |
30 | 161 | struct CompressHandler { |
31 | 162 | // Compress serialized `msg' into `buf'. |
32 | 163 | // Returns true on success, false otherwise |
33 | 164 | bool (*Compress)(const google::protobuf::Message& msg, butil::IOBuf* buf); |
34 | | - bool (*Compress2Json)(const google::protobuf::Message& msg, butil::IOBuf* buf, |
35 | | - const json2pb::Pb2JsonOptions& options); |
36 | | - bool (*Compress2ProtoJson)(const google::protobuf::Message& msg, butil::IOBuf* buf, |
37 | | - const json2pb::Pb2ProtoJsonOptions& options); |
38 | | - bool (*Compress2ProtoText)(const google::protobuf::Message& msg, butil::IOBuf* buf); |
39 | 165 |
|
40 | 166 | // Parse decompressed `data' as `msg'. |
41 | 167 | // Returns true on success, false otherwise |
42 | 168 | bool (*Decompress)(const butil::IOBuf& data, google::protobuf::Message* msg); |
43 | | - bool (*DecompressFromJson)(const butil::IOBuf& data, google::protobuf::Message* msg, |
44 | | - const json2pb::Json2PbOptions& options); |
45 | | - bool (*DecompressFromProtoJson)(const butil::IOBuf& data, google::protobuf::Message* msg, |
46 | | - const json2pb::ProtoJson2PbOptions& options); |
47 | | - bool (*DecompressFromProtoText)(const butil::IOBuf& data, google::protobuf::Message* msg); |
48 | 169 |
|
49 | 170 | // Name of the compression algorithm, must be string constant. |
50 | 171 | const char* name; |
|
0 commit comments