|
|
|
@ -37,6 +37,8 @@ |
|
|
|
|
#include <grpc++/server.h> |
|
|
|
|
#include <grpc/support/cpu.h> |
|
|
|
|
#include <grpc/support/log.h> |
|
|
|
|
|
|
|
|
|
#include "include/grpc/support/useful.h" |
|
|
|
|
#include "src/cpp/server/thread_pool_interface.h" |
|
|
|
|
|
|
|
|
|
namespace grpc { |
|
|
|
@ -52,12 +54,18 @@ static void do_plugin_list_init(void) { |
|
|
|
|
|
|
|
|
|
ServerBuilder::ServerBuilder() |
|
|
|
|
: max_message_size_(-1), generic_service_(nullptr) { |
|
|
|
|
grpc_compression_options_init(&compression_options_); |
|
|
|
|
gpr_once_init(&once_init_plugin_list, do_plugin_list_init); |
|
|
|
|
for (auto factory : (*g_plugin_factory_list)) { |
|
|
|
|
std::unique_ptr<ServerBuilderPlugin> plugin = factory(); |
|
|
|
|
plugins_[plugin->name()] = std::move(plugin); |
|
|
|
|
} |
|
|
|
|
// all compression algorithms enabled by default.
|
|
|
|
|
enabled_compression_algorithms_bitset_ = |
|
|
|
|
(1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; |
|
|
|
|
memset(&maybe_default_compression_level_, 0, |
|
|
|
|
sizeof(maybe_default_compression_level_)); |
|
|
|
|
memset(&maybe_default_compression_algorithm_, 0, |
|
|
|
|
sizeof(maybe_default_compression_algorithm_)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::unique_ptr<ServerCompletionQueue> ServerBuilder::AddCompletionQueue() { |
|
|
|
@ -66,35 +74,65 @@ std::unique_ptr<ServerCompletionQueue> ServerBuilder::AddCompletionQueue() { |
|
|
|
|
return std::unique_ptr<ServerCompletionQueue>(cq); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ServerBuilder::RegisterService(Service* service) { |
|
|
|
|
ServerBuilder& ServerBuilder::RegisterService(Service* service) { |
|
|
|
|
services_.emplace_back(new NamedService(service)); |
|
|
|
|
return *this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ServerBuilder::RegisterService(const grpc::string& addr, |
|
|
|
|
Service* service) { |
|
|
|
|
ServerBuilder& ServerBuilder::RegisterService(const grpc::string& addr, |
|
|
|
|
Service* service) { |
|
|
|
|
services_.emplace_back(new NamedService(addr, service)); |
|
|
|
|
return *this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ServerBuilder::RegisterAsyncGenericService(AsyncGenericService* service) { |
|
|
|
|
ServerBuilder& ServerBuilder::RegisterAsyncGenericService( |
|
|
|
|
AsyncGenericService* service) { |
|
|
|
|
if (generic_service_) { |
|
|
|
|
gpr_log(GPR_ERROR, |
|
|
|
|
"Adding multiple AsyncGenericService is unsupported for now. " |
|
|
|
|
"Dropping the service %p", |
|
|
|
|
service); |
|
|
|
|
return; |
|
|
|
|
} else { |
|
|
|
|
generic_service_ = service; |
|
|
|
|
} |
|
|
|
|
generic_service_ = service; |
|
|
|
|
return *this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ServerBuilder::SetOption(std::unique_ptr<ServerBuilderOption> option) { |
|
|
|
|
ServerBuilder& ServerBuilder::SetOption( |
|
|
|
|
std::unique_ptr<ServerBuilderOption> option) { |
|
|
|
|
options_.push_back(std::move(option)); |
|
|
|
|
return *this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ServerBuilder& ServerBuilder::SetCompressionAlgorithmSupportStatus( |
|
|
|
|
grpc_compression_algorithm algorithm, bool enabled) { |
|
|
|
|
if (enabled) { |
|
|
|
|
GPR_BITSET(&enabled_compression_algorithms_bitset_, algorithm); |
|
|
|
|
} else { |
|
|
|
|
GPR_BITCLEAR(&enabled_compression_algorithms_bitset_, algorithm); |
|
|
|
|
} |
|
|
|
|
return *this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ServerBuilder::AddListeningPort(const grpc::string& addr, |
|
|
|
|
std::shared_ptr<ServerCredentials> creds, |
|
|
|
|
int* selected_port) { |
|
|
|
|
ServerBuilder& ServerBuilder::SetDefaultCompressionLevel( |
|
|
|
|
grpc_compression_level level) { |
|
|
|
|
maybe_default_compression_level_.level = level; |
|
|
|
|
return *this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ServerBuilder& ServerBuilder::SetDefaultCompressionAlgorithm( |
|
|
|
|
grpc_compression_algorithm algorithm) { |
|
|
|
|
maybe_default_compression_algorithm_.is_set = true; |
|
|
|
|
maybe_default_compression_algorithm_.algorithm = algorithm; |
|
|
|
|
return *this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ServerBuilder& ServerBuilder::AddListeningPort( |
|
|
|
|
const grpc::string& addr, std::shared_ptr<ServerCredentials> creds, |
|
|
|
|
int* selected_port) { |
|
|
|
|
Port port = {addr, creds, selected_port}; |
|
|
|
|
ports_.push_back(port); |
|
|
|
|
return *this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::unique_ptr<Server> ServerBuilder::BuildAndStart() { |
|
|
|
@ -124,13 +162,14 @@ std::unique_ptr<Server> ServerBuilder::BuildAndStart() { |
|
|
|
|
args.SetInt(GRPC_ARG_MAX_MESSAGE_LENGTH, max_message_size_); |
|
|
|
|
} |
|
|
|
|
args.SetInt(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET, |
|
|
|
|
compression_options_.enabled_algorithms_bitset); |
|
|
|
|
if (compression_options_.default_level.is_set) { |
|
|
|
|
enabled_compression_algorithms_bitset_); |
|
|
|
|
if (maybe_default_compression_level_.is_set) { |
|
|
|
|
args.SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_LEVEL, |
|
|
|
|
compression_options_.default_level.level); |
|
|
|
|
} else if (compression_options_.default_algorithm.is_set) { |
|
|
|
|
maybe_default_compression_level_.level); |
|
|
|
|
} |
|
|
|
|
if (maybe_default_compression_algorithm_.is_set) { |
|
|
|
|
args.SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM, |
|
|
|
|
compression_options_.default_algorithm.algorithm); |
|
|
|
|
maybe_default_compression_algorithm_.algorithm); |
|
|
|
|
} |
|
|
|
|
std::unique_ptr<Server> server( |
|
|
|
|
new Server(thread_pool.release(), true, max_message_size_, &args)); |
|
|
|
|