@@ -443,11 +443,18 @@ static void HandleBackupRequest(void* arg) {
443443 bthread_id_error (correlation_id, EBACKUPREQUEST);
444444}
445445
446- void Channel::CallMethod (const google::protobuf::MethodDescriptor* method,
447- google::protobuf::RpcController* controller_base,
448- const google::protobuf::Message* request,
449- google::protobuf::Message* response,
450- google::protobuf::Closure* done) {
446+ template <bool is_pb>
447+ void Channel::CallMethodInternal (const typename std::conditional<is_pb,
448+ google::protobuf::MethodDescriptor,
449+ brpc::flatbuffers::MethodDescriptor>::type* method,
450+ google::protobuf::RpcController* controller_base,
451+ const typename std::conditional<is_pb,
452+ google::protobuf::Message,
453+ brpc::flatbuffers::Message>::type* request,
454+ typename std::conditional<is_pb,
455+ google::protobuf::Message,
456+ brpc::flatbuffers::Message>::type* response,
457+ google::protobuf::Closure* done) {
451458 const int64_t start_send_real_us = butil::gettimeofday_us ();
452459 Controller* cntl = static_cast <Controller*>(controller_base);
453460 cntl->OnRPCBegin (start_send_real_us);
@@ -507,22 +514,38 @@ void Channel::CallMethod(const google::protobuf::MethodDescriptor* method,
507514 const int64_t start_send_us = butil::cpuwide_time_us ();
508515 std::string method_name;
509516 if (_get_method_name) {
510- method_name = butil::EnsureString (_get_method_name (method, cntl));
517+ if (is_pb) {
518+ auto pb_method = reinterpret_cast <const google::protobuf::MethodDescriptor*>(method);
519+ method_name = butil::EnsureString (_get_method_name (pb_method, cntl));
520+ } else {
521+ // FlatBuffers doesn't support _get_method_name yet
522+ method_name = " " ;
523+ }
524+
511525 } else if (method) {
512- method_name = butil::EnsureString (method->full_name ());
526+ if (is_pb) {
527+ auto pb_method = reinterpret_cast <const google::protobuf::MethodDescriptor*>(method);
528+ method_name = butil::EnsureString (pb_method->full_name ());
529+ } else {
530+ auto fb_method = reinterpret_cast <const brpc::flatbuffers::MethodDescriptor*>(method);
531+ method_name = butil::EnsureString (fb_method->full_name ());
532+ }
533+
513534 } else {
514535 const static std::string NULL_METHOD_STR = " null-method" ;
515536 method_name = NULL_METHOD_STR;
516537 }
517- std::shared_ptr<Span> span = Span::CreateClientSpan (
538+ if (!method_name.empty ()) {
539+ std::shared_ptr<Span> span = Span::CreateClientSpan (
518540 method_name, start_send_real_us - start_send_us);
519- if (span) {
520- ControllerPrivateAccessor accessor (cntl);
521- span->set_log_id (cntl->log_id ());
522- span->set_base_cid (correlation_id);
523- span->set_protocol (_options.protocol );
524- span->set_start_send_us (start_send_us);
525- accessor.set_span (span);
541+ if (span) {
542+ ControllerPrivateAccessor accessor (cntl);
543+ span->set_log_id (cntl->log_id ());
544+ span->set_base_cid (correlation_id);
545+ span->set_protocol (_options.protocol );
546+ span->set_start_send_us (start_send_us);
547+ accessor.set_span (span);
548+ }
526549 }
527550 }
528551 // Override some options if they haven't been set by Controller
@@ -541,11 +564,20 @@ void Channel::CallMethod(const google::protobuf::MethodDescriptor* method,
541564 if (cntl->connection_type () == CONNECTION_TYPE_UNKNOWN) {
542565 cntl->set_connection_type (_options.connection_type );
543566 }
544- cntl-> _response = response;
567+
545568 cntl->_done = done;
546569 cntl->_pack_request = _pack_request;
547- cntl->_method = method;
548570 cntl->_auth = _options.auth ;
571+ // Use reinterpret_cast to avoid template instantiation errors
572+ // The actual type is guaranteed by the is_pb parameter
573+ if (is_pb) {
574+ cntl->_method = reinterpret_cast <const google::protobuf::MethodDescriptor*>(method);
575+ cntl->_response = reinterpret_cast <google::protobuf::Message*>(response);
576+ } else {
577+ cntl->_fb_method = reinterpret_cast <const brpc::flatbuffers::MethodDescriptor*>(method);
578+ cntl->_fb_response = reinterpret_cast <brpc::flatbuffers::Message*>(response);
579+ cntl->set_use_flatbuffer ();
580+ }
549581
550582 if (SingleServer ()) {
551583 cntl->_single_server_id = _server_id;
@@ -629,6 +661,22 @@ void Channel::CallMethod(const google::protobuf::MethodDescriptor* method,
629661 }
630662}
631663
664+ void Channel::CallMethod (const google::protobuf::MethodDescriptor* method,
665+ google::protobuf::RpcController* controller_base,
666+ const google::protobuf::Message* request,
667+ google::protobuf::Message* response,
668+ google::protobuf::Closure* done) {
669+ CallMethodInternal<true >(method, controller_base, request, response, done);
670+ }
671+
672+ void Channel::FBCallMethod (const brpc::flatbuffers::MethodDescriptor* method,
673+ google::protobuf::RpcController* controller_base,
674+ const brpc::flatbuffers::Message* request,
675+ brpc::flatbuffers::Message* response,
676+ google::protobuf::Closure* done) {
677+ CallMethodInternal<false >(method, controller_base, request, response, done);
678+ }
679+
632680void Channel::Describe (std::ostream& os, const DescribeOptions& opt) const {
633681 os << " Channel[" ;
634682 if (SingleServer ()) {
@@ -658,4 +706,24 @@ int Channel::CheckHealth() {
658706 }
659707}
660708
709+ // CallMethodInternal instance for pb and fb
710+ template
711+ void Channel::CallMethodInternal<true >(
712+ const google::protobuf::MethodDescriptor* method,
713+ google::protobuf::RpcController* controller_base,
714+ const google::protobuf::Message* request,
715+ google::protobuf::Message* response,
716+ google::protobuf::Closure* done
717+ );
718+
719+ // CallMethodInternal instance for pb and fb
720+ template
721+ void Channel::CallMethodInternal<false >(
722+ const brpc::flatbuffers::MethodDescriptor* method,
723+ google::protobuf::RpcController* controller_base,
724+ const brpc::flatbuffers::Message* request,
725+ brpc::flatbuffers::Message* response,
726+ google::protobuf::Closure* done
727+ );
728+
661729} // namespace brpc
0 commit comments