MCPcopy Create free account
hub / github.com/apache/brpc / SerializeRpcMessage

Function SerializeRpcMessage

src/brpc/policy/baidu_rpc_protocol.cpp:148–216  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

146}
147
148bool SerializeRpcMessage(const google::protobuf::Message& message,
149 Controller& cntl, ContentType content_type,
150 CompressType compress_type, ChecksumType checksum_type,
151 butil::IOBuf* buf) {
152 auto serialize = [&](Serializer& serializer) -> bool {
153 bool ok;
154 if (COMPRESS_TYPE_NONE == compress_type) {
155 butil::IOBufAsZeroCopyOutputStream stream(buf);
156 ok = serializer.SerializeTo(&stream);
157 } else {
158 const CompressHandler* handler = FindCompressHandler(compress_type);
159 if (NULL == handler) {
160 return false;
161 }
162 ok = handler->Compress(serializer, buf);
163 }
164 ChecksumIn checksum_in{buf, &cntl};
165 ComputeDataChecksum(checksum_in, checksum_type);
166 return ok;
167 };
168
169 if (CONTENT_TYPE_PB == content_type) {
170 Serializer serializer([&message](google::protobuf::io::ZeroCopyOutputStream* output) -> bool {
171 return message.SerializeToZeroCopyStream(output);
172 });
173 return serialize(serializer);
174 } else if (CONTENT_TYPE_JSON == content_type) {
175 Serializer serializer([&message, &cntl](google::protobuf::io::ZeroCopyOutputStream* output) -> bool {
176 json2pb::Pb2JsonOptions options;
177 options.bytes_to_base64 = cntl.has_pb_bytes_to_base64();
178 options.jsonify_empty_array = cntl.has_pb_jsonify_empty_array();
179 options.always_print_primitive_fields = cntl.has_always_print_primitive_fields();
180 options.single_repeated_to_array = cntl.has_pb_single_repeated_to_array();
181 options.enum_option = FLAGS_pb_enum_as_number
182 ? json2pb::OUTPUT_ENUM_BY_NUMBER
183 : json2pb::OUTPUT_ENUM_BY_NAME;
184 std::string error;
185 bool ok = json2pb::ProtoMessageToJson(message, output, options, &error);
186 if (!ok) {
187 LOG(INFO) << "Fail to serialize message="
188 << message.GetDescriptor()->full_name()
189 << " to json :" << error;
190 }
191 return ok;
192 });
193 return serialize(serializer);
194 } else if (CONTENT_TYPE_PROTO_JSON == content_type) {
195 Serializer serializer([&message, &cntl](google::protobuf::io::ZeroCopyOutputStream* output) -> bool {
196 json2pb::Pb2ProtoJsonOptions options;
197 options.always_print_enums_as_ints = FLAGS_pb_enum_as_number;
198 AlwaysPrintPrimitiveFields(options) = cntl.has_always_print_primitive_fields();
199 std::string error;
200 bool ok = json2pb::ProtoMessageToProtoJson(message, output, options, &error);
201 if (!ok) {
202 LOG(INFO) << "Fail to serialize message="
203 << message.GetDescriptor()->full_name()
204 << " to proto-json :" << error;
205 }

Callers 4

ProcessRpcRequestMethod · 0.85
TestGenericCallFunction · 0.85
SerializeResponseFunction · 0.85
SerializeRpcRequestFunction · 0.85

Calls 11

FindCompressHandlerFunction · 0.85
ComputeDataChecksumFunction · 0.85
ProtoMessageToJsonFunction · 0.70
ProtoMessageToProtoJsonFunction · 0.70
PrintFunction · 0.50
SerializeToMethod · 0.45
GetDescriptorMethod · 0.45

Tested by 2

ProcessRpcRequestMethod · 0.68
TestGenericCallFunction · 0.68