-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support rpc protobuf message factory interface
- Loading branch information
1 parent
64ce760
commit e20d804
Showing
7 changed files
with
256 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#include "brpc/rpc_pb_message_factory.h" | ||
#include "butil/object_pool.h" | ||
|
||
namespace brpc { | ||
|
||
struct DefaultRpcPBMessages : public RpcPBMessages { | ||
DefaultRpcPBMessages() : request(NULL), response(NULL) {} | ||
::google::protobuf::Message* Request() override { return request; } | ||
::google::protobuf::Message* Response() override { return response; } | ||
|
||
::google::protobuf::Message* request; | ||
::google::protobuf::Message* response; | ||
}; | ||
|
||
class DefaultRpcPBMessageFactory : public RpcPBMessageFactory { | ||
public: | ||
RpcPBMessages* Get(const ::google::protobuf::Service& service, | ||
const ::google::protobuf::MethodDescriptor& method) override { | ||
auto messages = butil::get_object<DefaultRpcPBMessages>(); | ||
messages->request = service.GetRequestPrototype(&method).New(); | ||
messages->response = service.GetResponsePrototype(&method).New(); | ||
return messages; | ||
} | ||
|
||
void Return(RpcPBMessages* messages) override { | ||
auto default_messages = static_cast<DefaultRpcPBMessages*>(messages); | ||
delete default_messages->request; | ||
delete default_messages->response; | ||
default_messages->request = NULL; | ||
default_messages->response = NULL; | ||
butil::return_object(default_messages); | ||
} | ||
}; | ||
|
||
RpcPBMessageFactory* GetDefaultRpcPBMessageFactory() { | ||
static DefaultRpcPBMessageFactory factory; | ||
return &factory; | ||
} | ||
|
||
|
||
} // namespace brpc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#ifndef BRPC_RPC_PB_MESSAGE_FACTORY_H | ||
#define BRPC_RPC_PB_MESSAGE_FACTORY_H | ||
|
||
#include <google/protobuf/service.h> | ||
#include <google/protobuf/descriptor.h> | ||
#include <google/protobuf/message.h> | ||
|
||
namespace brpc { | ||
|
||
// Inherit this class to customize rpc protobuf messages, | ||
// include request and response. | ||
class RpcPBMessages { | ||
public: | ||
virtual ~RpcPBMessages() = default; | ||
virtual google::protobuf::Message* Request() = 0; | ||
virtual google::protobuf::Message* Response() = 0; | ||
}; | ||
|
||
// Factory to manage `RpcPBMessages'. | ||
class RpcPBMessageFactory { | ||
public: | ||
virtual ~RpcPBMessageFactory() = default; | ||
virtual RpcPBMessages* Get(const ::google::protobuf::Service& service, | ||
const ::google::protobuf::MethodDescriptor& method) = 0; | ||
virtual void Return(RpcPBMessages* protobuf_message) = 0; | ||
}; | ||
|
||
// Get the `RpcPBMessageFactory' used by `Server'. | ||
RpcPBMessageFactory* GetDefaultRpcPBMessageFactory(); | ||
|
||
} // namespace brpc | ||
|
||
#endif //BRPC_RPC_PB_MESSAGE_FACTORY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.