mirror of https://github.com/grpc/grpc.git
commit
218e290074
188 changed files with 4508 additions and 816 deletions
@ -1,16 +1,8 @@ |
||||
load("//third_party/py:python_configure.bzl", "python_configure") |
||||
load("@io_bazel_rules_python//python:pip.bzl", "pip_repositories") |
||||
load("@grpc_python_dependencies//:requirements.bzl", "pip_install") |
||||
load("@org_pubref_rules_protobuf//python:rules.bzl", "py_proto_repositories") |
||||
|
||||
def grpc_python_deps(): |
||||
# TODO(https://github.com/grpc/grpc/issues/18256): Remove conditional. |
||||
if hasattr(native, "http_archive"): |
||||
python_configure(name = "local_config_python") |
||||
pip_repositories() |
||||
pip_install() |
||||
py_proto_repositories() |
||||
else: |
||||
print("Building Python gRPC with bazel 23.0+ is disabled pending " + |
||||
"resolution of https://github.com/grpc/grpc/issues/18256.") |
||||
|
||||
python_configure(name = "local_config_python") |
||||
pip_repositories() |
||||
pip_install() |
||||
|
@ -0,0 +1,84 @@ |
||||
"""Utility functions for generating protobuf code.""" |
||||
|
||||
_PROTO_EXTENSION = ".proto" |
||||
|
||||
def get_proto_root(workspace_root): |
||||
"""Gets the root protobuf directory. |
||||
|
||||
Args: |
||||
workspace_root: context.label.workspace_root |
||||
|
||||
Returns: |
||||
The directory relative to which generated include paths should be. |
||||
""" |
||||
if workspace_root: |
||||
return "/{}".format(workspace_root) |
||||
else: |
||||
return "" |
||||
|
||||
def _strip_proto_extension(proto_filename): |
||||
if not proto_filename.endswith(_PROTO_EXTENSION): |
||||
fail('"{}" does not end with "{}"'.format( |
||||
proto_filename, |
||||
_PROTO_EXTENSION, |
||||
)) |
||||
return proto_filename[:-len(_PROTO_EXTENSION)] |
||||
|
||||
def proto_path_to_generated_filename(proto_path, fmt_str): |
||||
"""Calculates the name of a generated file for a protobuf path. |
||||
|
||||
For example, "examples/protos/helloworld.proto" might map to |
||||
"helloworld.pb.h". |
||||
|
||||
Args: |
||||
proto_path: The path to the .proto file. |
||||
fmt_str: A format string used to calculate the generated filename. For |
||||
example, "{}.pb.h" might be used to calculate a C++ header filename. |
||||
|
||||
Returns: |
||||
The generated filename. |
||||
""" |
||||
return fmt_str.format(_strip_proto_extension(proto_path)) |
||||
|
||||
def _get_include_directory(include): |
||||
directory = include.path |
||||
if directory.startswith("external"): |
||||
external_separator = directory.find("/") |
||||
repository_separator = directory.find("/", external_separator + 1) |
||||
return directory[:repository_separator] |
||||
else: |
||||
return "." |
||||
|
||||
def get_include_protoc_args(includes): |
||||
"""Returns protoc args that imports protos relative to their import root. |
||||
|
||||
Args: |
||||
includes: A list of included proto files. |
||||
|
||||
Returns: |
||||
A list of arguments to be passed to protoc. For example, ["--proto_path=."]. |
||||
""" |
||||
return [ |
||||
"--proto_path={}".format(_get_include_directory(include)) |
||||
for include in includes |
||||
] |
||||
|
||||
def get_plugin_args(plugin, flags, dir_out, generate_mocks): |
||||
"""Returns arguments configuring protoc to use a plugin for a language. |
||||
|
||||
Args: |
||||
plugin: An executable file to run as the protoc plugin. |
||||
flags: The plugin flags to be passed to protoc. |
||||
dir_out: The output directory for the plugin. |
||||
generate_mocks: A bool indicating whether to generate mocks. |
||||
|
||||
Returns: |
||||
A list of protoc arguments configuring the plugin. |
||||
""" |
||||
augmented_flags = list(flags) |
||||
if generate_mocks: |
||||
augmented_flags.append("generate_mock_code=true") |
||||
return [ |
||||
"--plugin=protoc-gen-PLUGIN=" + plugin.path, |
||||
"--PLUGIN_out=" + ",".join(augmented_flags) + ":" + dir_out, |
||||
] |
@ -0,0 +1,203 @@ |
||||
"""Generates and compiles Python gRPC stubs from proto_library rules.""" |
||||
|
||||
load("@grpc_python_dependencies//:requirements.bzl", "requirement") |
||||
load( |
||||
"//bazel:protobuf.bzl", |
||||
"get_include_protoc_args", |
||||
"get_plugin_args", |
||||
"get_proto_root", |
||||
"proto_path_to_generated_filename", |
||||
) |
||||
|
||||
_GENERATED_PROTO_FORMAT = "{}_pb2.py" |
||||
_GENERATED_GRPC_PROTO_FORMAT = "{}_pb2_grpc.py" |
||||
|
||||
def _get_staged_proto_file(context, source_file): |
||||
if source_file.dirname == context.label.package: |
||||
return source_file |
||||
else: |
||||
copied_proto = context.actions.declare_file(source_file.basename) |
||||
context.actions.run_shell( |
||||
inputs = [source_file], |
||||
outputs = [copied_proto], |
||||
command = "cp {} {}".format(source_file.path, copied_proto.path), |
||||
mnemonic = "CopySourceProto", |
||||
) |
||||
return copied_proto |
||||
|
||||
def _generate_py_impl(context): |
||||
protos = [] |
||||
for src in context.attr.deps: |
||||
for file in src.proto.direct_sources: |
||||
protos.append(_get_staged_proto_file(context, file)) |
||||
includes = [ |
||||
file |
||||
for src in context.attr.deps |
||||
for file in src.proto.transitive_imports |
||||
] |
||||
proto_root = get_proto_root(context.label.workspace_root) |
||||
format_str = (_GENERATED_GRPC_PROTO_FORMAT if context.executable.plugin else _GENERATED_PROTO_FORMAT) |
||||
out_files = [ |
||||
context.actions.declare_file( |
||||
proto_path_to_generated_filename( |
||||
proto.basename, |
||||
format_str, |
||||
), |
||||
) |
||||
for proto in protos |
||||
] |
||||
|
||||
arguments = [] |
||||
tools = [context.executable._protoc] |
||||
if context.executable.plugin: |
||||
arguments += get_plugin_args( |
||||
context.executable.plugin, |
||||
context.attr.flags, |
||||
context.genfiles_dir.path, |
||||
False, |
||||
) |
||||
tools += [context.executable.plugin] |
||||
else: |
||||
arguments += [ |
||||
"--python_out={}:{}".format( |
||||
",".join(context.attr.flags), |
||||
context.genfiles_dir.path, |
||||
), |
||||
] |
||||
|
||||
arguments += get_include_protoc_args(includes) |
||||
arguments += [ |
||||
"--proto_path={}".format(context.genfiles_dir.path) |
||||
for proto in protos |
||||
] |
||||
for proto in protos: |
||||
massaged_path = proto.path |
||||
if massaged_path.startswith(context.genfiles_dir.path): |
||||
massaged_path = proto.path[len(context.genfiles_dir.path) + 1:] |
||||
arguments.append(massaged_path) |
||||
|
||||
well_known_proto_files = [] |
||||
if context.attr.well_known_protos: |
||||
well_known_proto_directory = context.attr.well_known_protos.files.to_list( |
||||
)[0].dirname |
||||
|
||||
arguments += ["-I{}".format(well_known_proto_directory + "/../..")] |
||||
well_known_proto_files = context.attr.well_known_protos.files.to_list() |
||||
|
||||
context.actions.run( |
||||
inputs = protos + includes + well_known_proto_files, |
||||
tools = tools, |
||||
outputs = out_files, |
||||
executable = context.executable._protoc, |
||||
arguments = arguments, |
||||
mnemonic = "ProtocInvocation", |
||||
) |
||||
return struct(files = depset(out_files)) |
||||
|
||||
__generate_py = rule( |
||||
attrs = { |
||||
"deps": attr.label_list( |
||||
mandatory = True, |
||||
allow_empty = False, |
||||
providers = ["proto"], |
||||
), |
||||
"plugin": attr.label( |
||||
executable = True, |
||||
providers = ["files_to_run"], |
||||
cfg = "host", |
||||
), |
||||
"flags": attr.string_list( |
||||
mandatory = False, |
||||
allow_empty = True, |
||||
), |
||||
"well_known_protos": attr.label(mandatory = False), |
||||
"_protoc": attr.label( |
||||
default = Label("//external:protocol_compiler"), |
||||
executable = True, |
||||
cfg = "host", |
||||
), |
||||
}, |
||||
output_to_genfiles = True, |
||||
implementation = _generate_py_impl, |
||||
) |
||||
|
||||
def _generate_py(well_known_protos, **kwargs): |
||||
if well_known_protos: |
||||
__generate_py( |
||||
well_known_protos = "@com_google_protobuf//:well_known_protos", |
||||
**kwargs |
||||
) |
||||
else: |
||||
__generate_py(**kwargs) |
||||
|
||||
_WELL_KNOWN_PROTO_LIBS = [ |
||||
"@com_google_protobuf//:any_proto", |
||||
"@com_google_protobuf//:api_proto", |
||||
"@com_google_protobuf//:compiler_plugin_proto", |
||||
"@com_google_protobuf//:descriptor_proto", |
||||
"@com_google_protobuf//:duration_proto", |
||||
"@com_google_protobuf//:empty_proto", |
||||
"@com_google_protobuf//:field_mask_proto", |
||||
"@com_google_protobuf//:source_context_proto", |
||||
"@com_google_protobuf//:struct_proto", |
||||
"@com_google_protobuf//:timestamp_proto", |
||||
"@com_google_protobuf//:type_proto", |
||||
"@com_google_protobuf//:wrappers_proto", |
||||
] |
||||
|
||||
def py_proto_library( |
||||
name, |
||||
deps, |
||||
well_known_protos = True, |
||||
proto_only = False, |
||||
**kwargs): |
||||
"""Generate python code for a protobuf. |
||||
|
||||
Args: |
||||
name: The name of the target. |
||||
deps: A list of dependencies. Must contain a single element. |
||||
well_known_protos: A bool indicating whether or not to include well-known |
||||
protos. |
||||
proto_only: A bool indicating whether to generate vanilla protobuf code |
||||
or to also generate gRPC code. |
||||
""" |
||||
if len(deps) > 1: |
||||
fail("The supported length of 'deps' is 1.") |
||||
|
||||
codegen_target = "_{}_codegen".format(name) |
||||
codegen_grpc_target = "_{}_grpc_codegen".format(name) |
||||
|
||||
well_known_proto_rules = _WELL_KNOWN_PROTO_LIBS if well_known_protos else [] |
||||
|
||||
_generate_py( |
||||
name = codegen_target, |
||||
deps = deps, |
||||
well_known_protos = well_known_protos, |
||||
**kwargs |
||||
) |
||||
|
||||
if not proto_only: |
||||
_generate_py( |
||||
name = codegen_grpc_target, |
||||
deps = deps, |
||||
plugin = "//:grpc_python_plugin", |
||||
well_known_protos = well_known_protos, |
||||
**kwargs |
||||
) |
||||
|
||||
native.py_library( |
||||
name = name, |
||||
srcs = [ |
||||
":{}".format(codegen_grpc_target), |
||||
":{}".format(codegen_target), |
||||
], |
||||
deps = [requirement("protobuf")], |
||||
**kwargs |
||||
) |
||||
else: |
||||
native.py_library( |
||||
name = name, |
||||
srcs = [":{}".format(codegen_target), ":{}".format(codegen_target)], |
||||
deps = [requirement("protobuf")], |
||||
**kwargs |
||||
) |
@ -0,0 +1,179 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* Licensed 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 <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/iomgr/port.h" |
||||
#if GRPC_ARES == 1 && defined(GRPC_UV) |
||||
|
||||
#include <ares.h> |
||||
#include <uv.h> |
||||
|
||||
#include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.h" |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/time.h> |
||||
#include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/iomgr/combiner.h" |
||||
|
||||
namespace grpc_core { |
||||
|
||||
void ares_uv_poll_cb(uv_poll_t* handle, int status, int events); |
||||
|
||||
void ares_uv_poll_close_cb(uv_handle_t* handle) { Delete(handle); } |
||||
|
||||
class GrpcPolledFdLibuv : public GrpcPolledFd { |
||||
public: |
||||
GrpcPolledFdLibuv(ares_socket_t as, grpc_combiner* combiner) |
||||
: as_(as), combiner_(combiner) { |
||||
gpr_asprintf(&name_, "c-ares socket: %" PRIdPTR, (intptr_t)as); |
||||
handle_ = New<uv_poll_t>(); |
||||
uv_poll_init_socket(uv_default_loop(), handle_, as); |
||||
handle_->data = this; |
||||
GRPC_COMBINER_REF(combiner_, "libuv ares event driver"); |
||||
} |
||||
|
||||
~GrpcPolledFdLibuv() { |
||||
gpr_free(name_); |
||||
GRPC_COMBINER_UNREF(combiner_, "libuv ares event driver"); |
||||
} |
||||
|
||||
void RegisterForOnReadableLocked(grpc_closure* read_closure) override { |
||||
GPR_ASSERT(read_closure_ == nullptr); |
||||
GPR_ASSERT((poll_events_ & UV_READABLE) == 0); |
||||
read_closure_ = read_closure; |
||||
poll_events_ |= UV_READABLE; |
||||
uv_poll_start(handle_, poll_events_, ares_uv_poll_cb); |
||||
} |
||||
|
||||
void RegisterForOnWriteableLocked(grpc_closure* write_closure) override { |
||||
GPR_ASSERT(write_closure_ == nullptr); |
||||
GPR_ASSERT((poll_events_ & UV_WRITABLE) == 0); |
||||
write_closure_ = write_closure; |
||||
poll_events_ |= UV_WRITABLE; |
||||
uv_poll_start(handle_, poll_events_, ares_uv_poll_cb); |
||||
} |
||||
|
||||
bool IsFdStillReadableLocked() override { |
||||
/* uv_poll_t is based on poll, which is level triggered. So, if cares
|
||||
* leaves some data unread, the event will trigger again. */ |
||||
return false; |
||||
} |
||||
|
||||
void ShutdownInternalLocked(grpc_error* error) { |
||||
uv_poll_stop(handle_); |
||||
uv_close(reinterpret_cast<uv_handle_t*>(handle_), ares_uv_poll_close_cb); |
||||
if (read_closure_ != nullptr) { |
||||
GRPC_CLOSURE_SCHED(read_closure_, GRPC_ERROR_CANCELLED); |
||||
} |
||||
if (write_closure_ != nullptr) { |
||||
GRPC_CLOSURE_SCHED(write_closure_, GRPC_ERROR_CANCELLED); |
||||
} |
||||
} |
||||
|
||||
void ShutdownLocked(grpc_error* error) override { |
||||
if (grpc_core::ExecCtx::Get() == nullptr) { |
||||
grpc_core::ExecCtx exec_ctx; |
||||
ShutdownInternalLocked(error); |
||||
} else { |
||||
ShutdownInternalLocked(error); |
||||
} |
||||
} |
||||
|
||||
ares_socket_t GetWrappedAresSocketLocked() override { return as_; } |
||||
|
||||
const char* GetName() override { return name_; } |
||||
|
||||
char* name_; |
||||
ares_socket_t as_; |
||||
uv_poll_t* handle_; |
||||
grpc_closure* read_closure_ = nullptr; |
||||
grpc_closure* write_closure_ = nullptr; |
||||
int poll_events_ = 0; |
||||
grpc_combiner* combiner_; |
||||
}; |
||||
|
||||
struct AresUvPollCbArg { |
||||
AresUvPollCbArg(uv_poll_t* handle, int status, int events) |
||||
: handle(handle), status(status), events(events) {} |
||||
|
||||
uv_poll_t* handle; |
||||
int status; |
||||
int events; |
||||
}; |
||||
|
||||
static void ares_uv_poll_cb_locked(void* arg, grpc_error* error) { |
||||
grpc_core::UniquePtr<AresUvPollCbArg> arg_struct( |
||||
reinterpret_cast<AresUvPollCbArg*>(arg)); |
||||
uv_poll_t* handle = arg_struct->handle; |
||||
int status = arg_struct->status; |
||||
int events = arg_struct->events; |
||||
GrpcPolledFdLibuv* polled_fd = |
||||
reinterpret_cast<GrpcPolledFdLibuv*>(handle->data); |
||||
if (status < 0) { |
||||
error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("cares polling error"); |
||||
error = |
||||
grpc_error_set_str(error, GRPC_ERROR_STR_OS_ERROR, |
||||
grpc_slice_from_static_string(uv_strerror(status))); |
||||
} |
||||
if (events & UV_READABLE) { |
||||
GPR_ASSERT(polled_fd->read_closure_ != nullptr); |
||||
GRPC_CLOSURE_SCHED(polled_fd->read_closure_, error); |
||||
polled_fd->read_closure_ = nullptr; |
||||
polled_fd->poll_events_ &= ~UV_READABLE; |
||||
} |
||||
if (events & UV_WRITABLE) { |
||||
GPR_ASSERT(polled_fd->write_closure_ != nullptr); |
||||
GRPC_CLOSURE_SCHED(polled_fd->write_closure_, error); |
||||
polled_fd->write_closure_ = nullptr; |
||||
polled_fd->poll_events_ &= ~UV_WRITABLE; |
||||
} |
||||
uv_poll_start(handle, polled_fd->poll_events_, ares_uv_poll_cb); |
||||
} |
||||
|
||||
void ares_uv_poll_cb(uv_poll_t* handle, int status, int events) { |
||||
grpc_core::ExecCtx exec_ctx; |
||||
GrpcPolledFdLibuv* polled_fd = |
||||
reinterpret_cast<GrpcPolledFdLibuv*>(handle->data); |
||||
AresUvPollCbArg* arg = New<AresUvPollCbArg>(handle, status, events); |
||||
GRPC_CLOSURE_SCHED( |
||||
GRPC_CLOSURE_CREATE(ares_uv_poll_cb_locked, arg, |
||||
grpc_combiner_scheduler(polled_fd->combiner_)), |
||||
GRPC_ERROR_NONE); |
||||
} |
||||
|
||||
class GrpcPolledFdFactoryLibuv : public GrpcPolledFdFactory { |
||||
public: |
||||
GrpcPolledFd* NewGrpcPolledFdLocked(ares_socket_t as, |
||||
grpc_pollset_set* driver_pollset_set, |
||||
grpc_combiner* combiner) override { |
||||
return New<GrpcPolledFdLibuv>(as, combiner); |
||||
} |
||||
|
||||
void ConfigureAresChannelLocked(ares_channel channel) override {} |
||||
}; |
||||
|
||||
UniquePtr<GrpcPolledFdFactory> NewGrpcPolledFdFactory(grpc_combiner* combiner) { |
||||
return UniquePtr<GrpcPolledFdFactory>(New<GrpcPolledFdFactoryLibuv>()); |
||||
} |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
#endif /* GRPC_ARES == 1 && defined(GRPC_UV) */ |
@ -0,0 +1,52 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2016 gRPC authors. |
||||
* |
||||
* Licensed 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 <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/iomgr/port.h" |
||||
#if GRPC_ARES == 1 && defined(GRPC_UV) |
||||
|
||||
#include <grpc/support/string_util.h> |
||||
|
||||
#include "src/core/ext/filters/client_channel/parse_address.h" |
||||
#include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h" |
||||
#include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_libuv_windows.h" |
||||
#include "src/core/ext/filters/client_channel/server_address.h" |
||||
#include "src/core/lib/gpr/host_port.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
|
||||
bool grpc_ares_query_ipv6() { |
||||
/* The libuv grpc code currently does not have the code to probe for this,
|
||||
* so we assume for now that IPv6 is always available in contexts where this |
||||
* code will be used. */ |
||||
return true; |
||||
} |
||||
|
||||
bool grpc_ares_maybe_resolve_localhost_manually_locked( |
||||
const char* name, const char* default_port, |
||||
grpc_core::UniquePtr<grpc_core::ServerAddressList>* addrs) { |
||||
char* host = nullptr; |
||||
char* port = nullptr; |
||||
bool out = inner_maybe_resolve_localhost_manually_locked(name, default_port, |
||||
addrs, &host, &port); |
||||
gpr_free(host); |
||||
gpr_free(port); |
||||
return out; |
||||
} |
||||
|
||||
#endif /* GRPC_ARES == 1 && defined(GRPC_UV) */ |
@ -0,0 +1,83 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* Licensed 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 <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/iomgr/port.h" |
||||
#if GRPC_ARES == 1 && (defined(GRPC_UV) || defined(GPR_WINDOWS)) |
||||
|
||||
#include <grpc/support/string_util.h> |
||||
|
||||
#include "src/core/ext/filters/client_channel/parse_address.h" |
||||
#include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h" |
||||
#include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_libuv_windows.h" |
||||
#include "src/core/ext/filters/client_channel/server_address.h" |
||||
#include "src/core/lib/gpr/host_port.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
|
||||
bool inner_maybe_resolve_localhost_manually_locked( |
||||
const char* name, const char* default_port, |
||||
grpc_core::UniquePtr<grpc_core::ServerAddressList>* addrs, char** host, |
||||
char** port) { |
||||
gpr_split_host_port(name, host, port); |
||||
if (*host == nullptr) { |
||||
gpr_log(GPR_ERROR, |
||||
"Failed to parse %s into host:port during manual localhost " |
||||
"resolution check.", |
||||
name); |
||||
return false; |
||||
} |
||||
if (*port == nullptr) { |
||||
if (default_port == nullptr) { |
||||
gpr_log(GPR_ERROR, |
||||
"No port or default port for %s during manual localhost " |
||||
"resolution check.", |
||||
name); |
||||
return false; |
||||
} |
||||
*port = gpr_strdup(default_port); |
||||
} |
||||
if (gpr_stricmp(*host, "localhost") == 0) { |
||||
GPR_ASSERT(*addrs == nullptr); |
||||
*addrs = grpc_core::MakeUnique<grpc_core::ServerAddressList>(); |
||||
uint16_t numeric_port = grpc_strhtons(*port); |
||||
// Append the ipv6 loopback address.
|
||||
struct sockaddr_in6 ipv6_loopback_addr; |
||||
memset(&ipv6_loopback_addr, 0, sizeof(ipv6_loopback_addr)); |
||||
((char*)&ipv6_loopback_addr.sin6_addr)[15] = 1; |
||||
ipv6_loopback_addr.sin6_family = AF_INET6; |
||||
ipv6_loopback_addr.sin6_port = numeric_port; |
||||
(*addrs)->emplace_back(&ipv6_loopback_addr, sizeof(ipv6_loopback_addr), |
||||
nullptr /* args */); |
||||
// Append the ipv4 loopback address.
|
||||
struct sockaddr_in ipv4_loopback_addr; |
||||
memset(&ipv4_loopback_addr, 0, sizeof(ipv4_loopback_addr)); |
||||
((char*)&ipv4_loopback_addr.sin_addr)[0] = 0x7f; |
||||
((char*)&ipv4_loopback_addr.sin_addr)[3] = 0x01; |
||||
ipv4_loopback_addr.sin_family = AF_INET; |
||||
ipv4_loopback_addr.sin_port = numeric_port; |
||||
(*addrs)->emplace_back(&ipv4_loopback_addr, sizeof(ipv4_loopback_addr), |
||||
nullptr /* args */); |
||||
// Let the address sorter figure out which one should be tried first.
|
||||
grpc_cares_wrapper_address_sorting_sort(addrs->get()); |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
#endif /* GRPC_ARES == 1 && (defined(GRPC_UV) || defined(GPR_WINDOWS)) */ |
@ -0,0 +1,34 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* Licensed 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 GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVER_DNS_C_ARES_GRPC_ARES_WRAPPER_LIBUV_WINDOWS_H |
||||
#define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVER_DNS_C_ARES_GRPC_ARES_WRAPPER_LIBUV_WINDOWS_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include <grpc/support/string_util.h> |
||||
|
||||
#include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h" |
||||
|
||||
bool inner_maybe_resolve_localhost_manually_locked( |
||||
const char* name, const char* default_port, |
||||
grpc_core::UniquePtr<grpc_core::ServerAddressList>* addrs, char** host, |
||||
char** port); |
||||
|
||||
#endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVER_DNS_C_ARES_GRPC_ARES_WRAPPER_LIBUV_WINDOWS_H \ |
||||
*/ |
@ -0,0 +1,87 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* Licensed 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 GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_H |
||||
#define GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include <stdint.h> |
||||
|
||||
// --------------------------------------------------------------------
|
||||
// How to use global configuration variables:
|
||||
//
|
||||
// Defining config variables of a specified type:
|
||||
// GPR_GLOBAL_CONFIG_DEFINE_*TYPE*(name, default_value, help);
|
||||
//
|
||||
// Supported TYPEs: BOOL, INT32, STRING
|
||||
//
|
||||
// It's recommended to use lowercase letters for 'name' like
|
||||
// regular variables. The builtin configuration system uses
|
||||
// environment variable and the name is converted to uppercase
|
||||
// when looking up the value. For example,
|
||||
// GPR_GLOBAL_CONFIG_DEFINE(grpc_latency) looks up the value with the
|
||||
// name, "GRPC_LATENCY".
|
||||
//
|
||||
// The variable initially has the specified 'default_value'
|
||||
// which must be an expression convertible to 'Type'.
|
||||
// 'default_value' may be evaluated 0 or more times,
|
||||
// and at an unspecified time; keep it
|
||||
// simple and usually free of side-effects.
|
||||
//
|
||||
// GPR_GLOBAL_CONFIG_DEFINE_*TYPE* should not be called in a C++ header.
|
||||
// It should be called at the top-level (outside any namespaces)
|
||||
// in a .cc file.
|
||||
//
|
||||
// Getting the variables:
|
||||
// GPR_GLOBAL_CONFIG_GET(name)
|
||||
//
|
||||
// If error happens during getting variables, error messages will
|
||||
// be logged and default value will be returned.
|
||||
//
|
||||
// Setting the variables with new value:
|
||||
// GPR_GLOBAL_CONFIG_SET(name, new_value)
|
||||
//
|
||||
// Declaring config variables for other modules to access:
|
||||
// GPR_GLOBAL_CONFIG_DECLARE_*TYPE*(name)
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// How to customize the global configuration system:
|
||||
//
|
||||
// How to read and write configuration value can be customized.
|
||||
// Builtin system uses environment variables but it can be extended to
|
||||
// support command-line flag, file, etc.
|
||||
//
|
||||
// To customize it, following macros should be redefined.
|
||||
//
|
||||
// GPR_GLOBAL_CONFIG_DEFINE_BOOL
|
||||
// GPR_GLOBAL_CONFIG_DEFINE_INT32
|
||||
// GPR_GLOBAL_CONFIG_DEFINE_STRING
|
||||
//
|
||||
// These macros should define functions for getting and setting variable.
|
||||
// For example, GPR_GLOBAL_CONFIG_DEFINE_BOOL(test, ...) would define two
|
||||
// functions.
|
||||
//
|
||||
// bool gpr_global_config_get_test();
|
||||
// void gpr_global_config_set_test(bool value);
|
||||
|
||||
#include "src/core/lib/gprpp/global_config_env.h" |
||||
|
||||
#include "src/core/lib/gprpp/global_config_custom.h" |
||||
|
||||
#endif /* GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_H */ |
@ -0,0 +1,29 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* Licensed 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 GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_CUSTOM_H |
||||
#define GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_CUSTOM_H |
||||
|
||||
// This is a placeholder for custom global configuration implementaion.
|
||||
// To use the custom one, please define following macros here.
|
||||
//
|
||||
// GPR_GLOBAL_CONFIG_DEFINE_BOOL
|
||||
// GPR_GLOBAL_CONFIG_DEFINE_INT32
|
||||
// GPR_GLOBAL_CONFIG_DEFINE_STRING
|
||||
|
||||
#endif /* GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_CUSTOM_H */ |
@ -0,0 +1,135 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* Licensed 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 <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/gprpp/global_config_env.h" |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
|
||||
#include "src/core/lib/gpr/env.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
|
||||
#include <ctype.h> |
||||
#include <string.h> |
||||
|
||||
namespace grpc_core { |
||||
|
||||
namespace { |
||||
|
||||
void DefaultGlobalConfigEnvErrorFunction(const char* error_message) { |
||||
gpr_log(GPR_ERROR, "%s", error_message); |
||||
} |
||||
|
||||
GlobalConfigEnvErrorFunctionType g_global_config_env_error_func = |
||||
DefaultGlobalConfigEnvErrorFunction; |
||||
|
||||
void LogParsingError(const char* name, const char* value) { |
||||
char* error_message; |
||||
gpr_asprintf(&error_message, |
||||
"Illegal value '%s' specified for environment variable '%s'", |
||||
value, name); |
||||
(*g_global_config_env_error_func)(error_message); |
||||
gpr_free(error_message); |
||||
} |
||||
|
||||
} // namespace
|
||||
|
||||
void SetGlobalConfigEnvErrorFunction(GlobalConfigEnvErrorFunctionType func) { |
||||
g_global_config_env_error_func = func; |
||||
} |
||||
|
||||
UniquePtr<char> GlobalConfigEnv::GetValue() { |
||||
return UniquePtr<char>(gpr_getenv(GetName())); |
||||
} |
||||
|
||||
void GlobalConfigEnv::SetValue(const char* value) { |
||||
gpr_setenv(GetName(), value); |
||||
} |
||||
|
||||
void GlobalConfigEnv::Unset() { gpr_unsetenv(GetName()); } |
||||
|
||||
char* GlobalConfigEnv::GetName() { |
||||
// This makes sure that name_ is in a canonical form having uppercase
|
||||
// letters. This is okay to be called serveral times.
|
||||
for (char* c = name_; *c != 0; ++c) { |
||||
*c = toupper(*c); |
||||
} |
||||
return name_; |
||||
} |
||||
static_assert(std::is_trivially_destructible<GlobalConfigEnvBool>::value, |
||||
"GlobalConfigEnvBool needs to be trivially destructible."); |
||||
|
||||
bool GlobalConfigEnvBool::Get() { |
||||
UniquePtr<char> str = GetValue(); |
||||
if (str == nullptr) { |
||||
return default_value_; |
||||
} |
||||
// parsing given value string.
|
||||
bool result = false; |
||||
if (!gpr_parse_bool_value(str.get(), &result)) { |
||||
LogParsingError(GetName(), str.get()); |
||||
result = default_value_; |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
void GlobalConfigEnvBool::Set(bool value) { |
||||
SetValue(value ? "true" : "false"); |
||||
} |
||||
|
||||
static_assert(std::is_trivially_destructible<GlobalConfigEnvInt32>::value, |
||||
"GlobalConfigEnvInt32 needs to be trivially destructible."); |
||||
|
||||
int32_t GlobalConfigEnvInt32::Get() { |
||||
UniquePtr<char> str = GetValue(); |
||||
if (str == nullptr) { |
||||
return default_value_; |
||||
} |
||||
// parsing given value string.
|
||||
char* end = str.get(); |
||||
long result = strtol(str.get(), &end, 10); |
||||
if (*end != 0) { |
||||
LogParsingError(GetName(), str.get()); |
||||
result = default_value_; |
||||
} |
||||
return static_cast<int32_t>(result); |
||||
} |
||||
|
||||
void GlobalConfigEnvInt32::Set(int32_t value) { |
||||
char buffer[GPR_LTOA_MIN_BUFSIZE]; |
||||
gpr_ltoa(value, buffer); |
||||
SetValue(buffer); |
||||
} |
||||
|
||||
static_assert(std::is_trivially_destructible<GlobalConfigEnvString>::value, |
||||
"GlobalConfigEnvString needs to be trivially destructible."); |
||||
|
||||
UniquePtr<char> GlobalConfigEnvString::Get() { |
||||
UniquePtr<char> str = GetValue(); |
||||
if (str == nullptr) { |
||||
return UniquePtr<char>(gpr_strdup(default_value_)); |
||||
} |
||||
return str; |
||||
} |
||||
|
||||
void GlobalConfigEnvString::Set(const char* value) { SetValue(value); } |
||||
|
||||
} // namespace grpc_core
|
@ -0,0 +1,131 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* Licensed 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 GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_ENV_H |
||||
#define GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_ENV_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/gprpp/global_config_generic.h" |
||||
#include "src/core/lib/gprpp/memory.h" |
||||
|
||||
namespace grpc_core { |
||||
|
||||
typedef void (*GlobalConfigEnvErrorFunctionType)(const char* error_message); |
||||
|
||||
/*
|
||||
* Set global_config_env_error_function which is called when config system |
||||
* encounters errors such as parsing error. What the default function does |
||||
* is logging error message. |
||||
*/ |
||||
void SetGlobalConfigEnvErrorFunction(GlobalConfigEnvErrorFunctionType func); |
||||
|
||||
// Base class for all classes to access environment variables.
|
||||
class GlobalConfigEnv { |
||||
protected: |
||||
// `name` should be writable and alive after constructor is called.
|
||||
constexpr explicit GlobalConfigEnv(char* name) : name_(name) {} |
||||
|
||||
public: |
||||
// Returns the value of `name` variable.
|
||||
UniquePtr<char> GetValue(); |
||||
|
||||
// Sets the value of `name` variable.
|
||||
void SetValue(const char* value); |
||||
|
||||
// Unsets `name` variable.
|
||||
void Unset(); |
||||
|
||||
protected: |
||||
char* GetName(); |
||||
|
||||
private: |
||||
char* name_; |
||||
}; |
||||
|
||||
class GlobalConfigEnvBool : public GlobalConfigEnv { |
||||
public: |
||||
constexpr GlobalConfigEnvBool(char* name, bool default_value) |
||||
: GlobalConfigEnv(name), default_value_(default_value) {} |
||||
|
||||
bool Get(); |
||||
void Set(bool value); |
||||
|
||||
private: |
||||
bool default_value_; |
||||
}; |
||||
|
||||
class GlobalConfigEnvInt32 : public GlobalConfigEnv { |
||||
public: |
||||
constexpr GlobalConfigEnvInt32(char* name, int32_t default_value) |
||||
: GlobalConfigEnv(name), default_value_(default_value) {} |
||||
|
||||
int32_t Get(); |
||||
void Set(int32_t value); |
||||
|
||||
private: |
||||
int32_t default_value_; |
||||
}; |
||||
|
||||
class GlobalConfigEnvString : public GlobalConfigEnv { |
||||
public: |
||||
constexpr GlobalConfigEnvString(char* name, const char* default_value) |
||||
: GlobalConfigEnv(name), default_value_(default_value) {} |
||||
|
||||
UniquePtr<char> Get(); |
||||
void Set(const char* value); |
||||
|
||||
private: |
||||
const char* default_value_; |
||||
}; |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
// Macros for defining global config instances using environment variables.
|
||||
// This defines a GlobalConfig*Type* instance with arguments for
|
||||
// mutable variable name and default value.
|
||||
// Mutable name (g_env_str_##name) is here for having an array
|
||||
// for the canonical name without dynamic allocation.
|
||||
// `help` argument is ignored for this implementation.
|
||||
|
||||
#define GPR_GLOBAL_CONFIG_DEFINE_BOOL(name, default_value, help) \ |
||||
static char g_env_str_##name[] = #name; \
|
||||
static ::grpc_core::GlobalConfigEnvBool g_env_##name(g_env_str_##name, \
|
||||
default_value); \
|
||||
bool gpr_global_config_get_##name() { return g_env_##name.Get(); } \
|
||||
void gpr_global_config_set_##name(bool value) { g_env_##name.Set(value); } |
||||
|
||||
#define GPR_GLOBAL_CONFIG_DEFINE_INT32(name, default_value, help) \ |
||||
static char g_env_str_##name[] = #name; \
|
||||
static ::grpc_core::GlobalConfigEnvInt32 g_env_##name(g_env_str_##name, \
|
||||
default_value); \
|
||||
int32_t gpr_global_config_get_##name() { return g_env_##name.Get(); } \
|
||||
void gpr_global_config_set_##name(int32_t value) { g_env_##name.Set(value); } |
||||
|
||||
#define GPR_GLOBAL_CONFIG_DEFINE_STRING(name, default_value, help) \ |
||||
static char g_env_str_##name[] = #name; \
|
||||
static ::grpc_core::GlobalConfigEnvString g_env_##name(g_env_str_##name, \
|
||||
default_value); \
|
||||
::grpc_core::UniquePtr<char> gpr_global_config_get_##name() { \
|
||||
return g_env_##name.Get(); \
|
||||
} \
|
||||
void gpr_global_config_set_##name(const char* value) { \
|
||||
g_env_##name.Set(value); \
|
||||
} |
||||
|
||||
#endif /* GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_ENV_H */ |
@ -0,0 +1,44 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* Licensed 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 GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_GENERIC_H |
||||
#define GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_GENERIC_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/gprpp/memory.h" |
||||
|
||||
#include <stdint.h> |
||||
|
||||
#define GPR_GLOBAL_CONFIG_GET(name) gpr_global_config_get_##name() |
||||
|
||||
#define GPR_GLOBAL_CONFIG_SET(name, value) gpr_global_config_set_##name(value) |
||||
|
||||
#define GPR_GLOBAL_CONFIG_DECLARE_BOOL(name) \ |
||||
extern bool gpr_global_config_get_##name(); \
|
||||
extern void gpr_global_config_set_##name(bool value) |
||||
|
||||
#define GPR_GLOBAL_CONFIG_DECLARE_INT32(name) \ |
||||
extern int32_t gpr_global_config_get_##name(); \
|
||||
extern void gpr_global_config_set_##name(int32_t value) |
||||
|
||||
#define GPR_GLOBAL_CONFIG_DECLARE_STRING(name) \ |
||||
extern grpc_core::UniquePtr<char> gpr_global_config_get_##name(); \
|
||||
extern void gpr_global_config_set_##name(const char* value) |
||||
|
||||
#endif /* GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_GENERIC_H */ |
@ -0,0 +1,240 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2019 The gRPC Authors |
||||
// |
||||
// Licensed 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. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
using NUnit.Framework; |
||||
|
||||
using System.Runtime.InteropServices; |
||||
|
||||
#if GRPC_CSHARP_SUPPORT_SYSTEM_MEMORY |
||||
using System.Buffers; |
||||
#endif |
||||
|
||||
namespace Grpc.Core.Internal.Tests |
||||
{ |
||||
public class DefaultDeserializationContextTest |
||||
{ |
||||
FakeBufferReaderManager fakeBufferReaderManager; |
||||
|
||||
[SetUp] |
||||
public void Init() |
||||
{ |
||||
fakeBufferReaderManager = new FakeBufferReaderManager(); |
||||
} |
||||
|
||||
[TearDown] |
||||
public void Cleanup() |
||||
{ |
||||
fakeBufferReaderManager.Dispose(); |
||||
} |
||||
|
||||
#if GRPC_CSHARP_SUPPORT_SYSTEM_MEMORY |
||||
[TestCase] |
||||
public void PayloadAsReadOnlySequence_ZeroSegmentPayload() |
||||
{ |
||||
var context = new DefaultDeserializationContext(); |
||||
context.Initialize(fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List<byte[]> {})); |
||||
|
||||
Assert.AreEqual(0, context.PayloadLength); |
||||
|
||||
var sequence = context.PayloadAsReadOnlySequence(); |
||||
|
||||
Assert.AreEqual(ReadOnlySequence<byte>.Empty, sequence); |
||||
Assert.IsTrue(sequence.IsEmpty); |
||||
Assert.IsTrue(sequence.IsSingleSegment); |
||||
} |
||||
|
||||
[TestCase(0)] |
||||
[TestCase(1)] |
||||
[TestCase(10)] |
||||
[TestCase(100)] |
||||
[TestCase(1000)] |
||||
public void PayloadAsReadOnlySequence_SingleSegmentPayload(int segmentLength) |
||||
{ |
||||
var origBuffer = GetTestBuffer(segmentLength); |
||||
var context = new DefaultDeserializationContext(); |
||||
context.Initialize(fakeBufferReaderManager.CreateSingleSegmentBufferReader(origBuffer)); |
||||
|
||||
Assert.AreEqual(origBuffer.Length, context.PayloadLength); |
||||
|
||||
var sequence = context.PayloadAsReadOnlySequence(); |
||||
|
||||
Assert.AreEqual(origBuffer.Length, sequence.Length); |
||||
Assert.AreEqual(origBuffer.Length, sequence.First.Length); |
||||
Assert.IsTrue(sequence.IsSingleSegment); |
||||
CollectionAssert.AreEqual(origBuffer, sequence.First.ToArray()); |
||||
} |
||||
|
||||
[TestCase(0, 5, 10)] |
||||
[TestCase(1, 1, 1)] |
||||
[TestCase(10, 100, 1000)] |
||||
[TestCase(100, 100, 10)] |
||||
[TestCase(1000, 1000, 1000)] |
||||
public void PayloadAsReadOnlySequence_MultiSegmentPayload(int segmentLen1, int segmentLen2, int segmentLen3) |
||||
{ |
||||
var origBuffer1 = GetTestBuffer(segmentLen1); |
||||
var origBuffer2 = GetTestBuffer(segmentLen2); |
||||
var origBuffer3 = GetTestBuffer(segmentLen3); |
||||
int totalLen = origBuffer1.Length + origBuffer2.Length + origBuffer3.Length; |
||||
|
||||
var context = new DefaultDeserializationContext(); |
||||
context.Initialize(fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List<byte[]> { origBuffer1, origBuffer2, origBuffer3 })); |
||||
|
||||
Assert.AreEqual(totalLen, context.PayloadLength); |
||||
|
||||
var sequence = context.PayloadAsReadOnlySequence(); |
||||
|
||||
Assert.AreEqual(totalLen, sequence.Length); |
||||
|
||||
var segmentEnumerator = sequence.GetEnumerator(); |
||||
|
||||
Assert.IsTrue(segmentEnumerator.MoveNext()); |
||||
CollectionAssert.AreEqual(origBuffer1, segmentEnumerator.Current.ToArray()); |
||||
|
||||
Assert.IsTrue(segmentEnumerator.MoveNext()); |
||||
CollectionAssert.AreEqual(origBuffer2, segmentEnumerator.Current.ToArray()); |
||||
|
||||
Assert.IsTrue(segmentEnumerator.MoveNext()); |
||||
CollectionAssert.AreEqual(origBuffer3, segmentEnumerator.Current.ToArray()); |
||||
|
||||
Assert.IsFalse(segmentEnumerator.MoveNext()); |
||||
} |
||||
#endif |
||||
|
||||
[TestCase] |
||||
public void NullPayloadNotAllowed() |
||||
{ |
||||
var context = new DefaultDeserializationContext(); |
||||
Assert.Throws(typeof(InvalidOperationException), () => context.Initialize(fakeBufferReaderManager.CreateNullPayloadBufferReader())); |
||||
} |
||||
|
||||
[TestCase] |
||||
public void PayloadAsNewByteBuffer_ZeroSegmentPayload() |
||||
{ |
||||
var context = new DefaultDeserializationContext(); |
||||
context.Initialize(fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List<byte[]> {})); |
||||
|
||||
Assert.AreEqual(0, context.PayloadLength); |
||||
|
||||
var payload = context.PayloadAsNewBuffer(); |
||||
Assert.AreEqual(0, payload.Length); |
||||
} |
||||
|
||||
[TestCase(0)] |
||||
[TestCase(1)] |
||||
[TestCase(10)] |
||||
[TestCase(100)] |
||||
[TestCase(1000)] |
||||
public void PayloadAsNewByteBuffer_SingleSegmentPayload(int segmentLength) |
||||
{ |
||||
var origBuffer = GetTestBuffer(segmentLength); |
||||
var context = new DefaultDeserializationContext(); |
||||
context.Initialize(fakeBufferReaderManager.CreateSingleSegmentBufferReader(origBuffer)); |
||||
|
||||
Assert.AreEqual(origBuffer.Length, context.PayloadLength); |
||||
|
||||
var payload = context.PayloadAsNewBuffer(); |
||||
CollectionAssert.AreEqual(origBuffer, payload); |
||||
} |
||||
|
||||
[TestCase(0, 5, 10)] |
||||
[TestCase(1, 1, 1)] |
||||
[TestCase(10, 100, 1000)] |
||||
[TestCase(100, 100, 10)] |
||||
[TestCase(1000, 1000, 1000)] |
||||
public void PayloadAsNewByteBuffer_MultiSegmentPayload(int segmentLen1, int segmentLen2, int segmentLen3) |
||||
{ |
||||
var origBuffer1 = GetTestBuffer(segmentLen1); |
||||
var origBuffer2 = GetTestBuffer(segmentLen2); |
||||
var origBuffer3 = GetTestBuffer(segmentLen3); |
||||
|
||||
var context = new DefaultDeserializationContext(); |
||||
context.Initialize(fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List<byte[]> { origBuffer1, origBuffer2, origBuffer3 })); |
||||
|
||||
var payload = context.PayloadAsNewBuffer(); |
||||
|
||||
var concatenatedOrigBuffers = new List<byte>(); |
||||
concatenatedOrigBuffers.AddRange(origBuffer1); |
||||
concatenatedOrigBuffers.AddRange(origBuffer2); |
||||
concatenatedOrigBuffers.AddRange(origBuffer3); |
||||
|
||||
Assert.AreEqual(concatenatedOrigBuffers.Count, context.PayloadLength); |
||||
Assert.AreEqual(concatenatedOrigBuffers.Count, payload.Length); |
||||
CollectionAssert.AreEqual(concatenatedOrigBuffers, payload); |
||||
} |
||||
|
||||
[TestCase] |
||||
public void GetPayloadMultipleTimesIsIllegal() |
||||
{ |
||||
var origBuffer = GetTestBuffer(100); |
||||
var context = new DefaultDeserializationContext(); |
||||
context.Initialize(fakeBufferReaderManager.CreateSingleSegmentBufferReader(origBuffer)); |
||||
|
||||
Assert.AreEqual(origBuffer.Length, context.PayloadLength); |
||||
|
||||
var payload = context.PayloadAsNewBuffer(); |
||||
CollectionAssert.AreEqual(origBuffer, payload); |
||||
|
||||
// Getting payload multiple times is illegal |
||||
Assert.Throws(typeof(InvalidOperationException), () => context.PayloadAsNewBuffer()); |
||||
#if GRPC_CSHARP_SUPPORT_SYSTEM_MEMORY |
||||
Assert.Throws(typeof(InvalidOperationException), () => context.PayloadAsReadOnlySequence()); |
||||
#endif |
||||
} |
||||
|
||||
[TestCase] |
||||
public void ResetContextAndReinitialize() |
||||
{ |
||||
var origBuffer = GetTestBuffer(100); |
||||
var context = new DefaultDeserializationContext(); |
||||
context.Initialize(fakeBufferReaderManager.CreateSingleSegmentBufferReader(origBuffer)); |
||||
|
||||
Assert.AreEqual(origBuffer.Length, context.PayloadLength); |
||||
|
||||
// Reset invalidates context |
||||
context.Reset(); |
||||
|
||||
Assert.AreEqual(0, context.PayloadLength); |
||||
Assert.Throws(typeof(NullReferenceException), () => context.PayloadAsNewBuffer()); |
||||
#if GRPC_CSHARP_SUPPORT_SYSTEM_MEMORY |
||||
Assert.Throws(typeof(NullReferenceException), () => context.PayloadAsReadOnlySequence()); |
||||
#endif |
||||
|
||||
// Previously reset context can be initialized again |
||||
var origBuffer2 = GetTestBuffer(50); |
||||
context.Initialize(fakeBufferReaderManager.CreateSingleSegmentBufferReader(origBuffer2)); |
||||
|
||||
Assert.AreEqual(origBuffer2.Length, context.PayloadLength); |
||||
CollectionAssert.AreEqual(origBuffer2, context.PayloadAsNewBuffer()); |
||||
} |
||||
|
||||
private byte[] GetTestBuffer(int length) |
||||
{ |
||||
var testBuffer = new byte[length]; |
||||
for (int i = 0; i < testBuffer.Length; i++) |
||||
{ |
||||
testBuffer[i] = (byte) i; |
||||
} |
||||
return testBuffer; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,118 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2018 The gRPC Authors |
||||
// |
||||
// Licensed 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. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Runtime.InteropServices; |
||||
using System.Threading.Tasks; |
||||
|
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
|
||||
namespace Grpc.Core.Internal.Tests |
||||
{ |
||||
// Creates instances of fake IBufferReader. All created instances will become invalid once Dispose is called. |
||||
internal class FakeBufferReaderManager : IDisposable |
||||
{ |
||||
List<GCHandle> pinnedHandles = new List<GCHandle>(); |
||||
bool disposed = false; |
||||
public IBufferReader CreateSingleSegmentBufferReader(byte[] data) |
||||
{ |
||||
return CreateMultiSegmentBufferReader(new List<byte[]> { data }); |
||||
} |
||||
|
||||
public IBufferReader CreateMultiSegmentBufferReader(IEnumerable<byte[]> dataSegments) |
||||
{ |
||||
GrpcPreconditions.CheckState(!disposed); |
||||
GrpcPreconditions.CheckNotNull(dataSegments); |
||||
var segments = new List<GCHandle>(); |
||||
foreach (var data in dataSegments) |
||||
{ |
||||
GrpcPreconditions.CheckNotNull(data); |
||||
segments.Add(GCHandle.Alloc(data, GCHandleType.Pinned)); |
||||
} |
||||
pinnedHandles.AddRange(segments); // all the allocated GCHandles will be freed on Dispose() |
||||
return new FakeBufferReader(segments); |
||||
} |
||||
|
||||
public IBufferReader CreateNullPayloadBufferReader() |
||||
{ |
||||
GrpcPreconditions.CheckState(!disposed); |
||||
return new FakeBufferReader(null); |
||||
} |
||||
|
||||
public void Dispose() |
||||
{ |
||||
if (!disposed) |
||||
{ |
||||
disposed = true; |
||||
for (int i = 0; i < pinnedHandles.Count; i++) |
||||
{ |
||||
pinnedHandles[i].Free(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private class FakeBufferReader : IBufferReader |
||||
{ |
||||
readonly List<GCHandle> bufferSegments; |
||||
readonly int? totalLength; |
||||
readonly IEnumerator<GCHandle> segmentEnumerator; |
||||
|
||||
public FakeBufferReader(List<GCHandle> bufferSegments) |
||||
{ |
||||
this.bufferSegments = bufferSegments; |
||||
this.totalLength = ComputeTotalLength(bufferSegments); |
||||
this.segmentEnumerator = bufferSegments?.GetEnumerator(); |
||||
} |
||||
|
||||
public int? TotalLength => totalLength; |
||||
|
||||
public bool TryGetNextSlice(out Slice slice) |
||||
{ |
||||
GrpcPreconditions.CheckNotNull(bufferSegments); |
||||
if (!segmentEnumerator.MoveNext()) |
||||
{ |
||||
slice = default(Slice); |
||||
return false; |
||||
} |
||||
|
||||
var segment = segmentEnumerator.Current; |
||||
int sliceLen = ((byte[]) segment.Target).Length; |
||||
slice = new Slice(segment.AddrOfPinnedObject(), sliceLen); |
||||
return true; |
||||
} |
||||
|
||||
static int? ComputeTotalLength(List<GCHandle> bufferSegments) |
||||
{ |
||||
if (bufferSegments == null) |
||||
{ |
||||
return null; |
||||
} |
||||
|
||||
int sum = 0; |
||||
foreach (var segment in bufferSegments) |
||||
{ |
||||
var data = (byte[]) segment.Target; |
||||
sum += data.Length; |
||||
} |
||||
return sum; |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,121 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2018 The gRPC Authors |
||||
// |
||||
// Licensed 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. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
using NUnit.Framework; |
||||
|
||||
namespace Grpc.Core.Internal.Tests |
||||
{ |
||||
public class FakeBufferReaderManagerTest |
||||
{ |
||||
FakeBufferReaderManager fakeBufferReaderManager; |
||||
|
||||
[SetUp] |
||||
public void Init() |
||||
{ |
||||
fakeBufferReaderManager = new FakeBufferReaderManager(); |
||||
} |
||||
|
||||
[TearDown] |
||||
public void Cleanup() |
||||
{ |
||||
fakeBufferReaderManager.Dispose(); |
||||
} |
||||
|
||||
[TestCase] |
||||
public void NullPayload() |
||||
{ |
||||
var fakeBufferReader = fakeBufferReaderManager.CreateNullPayloadBufferReader(); |
||||
Assert.IsFalse(fakeBufferReader.TotalLength.HasValue); |
||||
Assert.Throws(typeof(ArgumentNullException), () => fakeBufferReader.TryGetNextSlice(out Slice slice)); |
||||
} |
||||
[TestCase] |
||||
public void ZeroSegmentPayload() |
||||
{ |
||||
var fakeBufferReader = fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List<byte[]> {}); |
||||
Assert.AreEqual(0, fakeBufferReader.TotalLength.Value); |
||||
Assert.IsFalse(fakeBufferReader.TryGetNextSlice(out Slice slice)); |
||||
} |
||||
|
||||
[TestCase(0)] |
||||
[TestCase(1)] |
||||
[TestCase(10)] |
||||
[TestCase(30)] |
||||
[TestCase(100)] |
||||
[TestCase(1000)] |
||||
public void SingleSegmentPayload(int bufferLen) |
||||
{ |
||||
var origBuffer = GetTestBuffer(bufferLen); |
||||
var fakeBufferReader = fakeBufferReaderManager.CreateSingleSegmentBufferReader(origBuffer); |
||||
Assert.AreEqual(origBuffer.Length, fakeBufferReader.TotalLength.Value); |
||||
|
||||
Assert.IsTrue(fakeBufferReader.TryGetNextSlice(out Slice slice)); |
||||
AssertSliceDataEqual(origBuffer, slice); |
||||
|
||||
Assert.IsFalse(fakeBufferReader.TryGetNextSlice(out Slice slice2)); |
||||
} |
||||
|
||||
[TestCase(0, 5, 10)] |
||||
[TestCase(1, 1, 1)] |
||||
[TestCase(10, 100, 1000)] |
||||
[TestCase(100, 100, 10)] |
||||
[TestCase(1000, 1000, 1000)] |
||||
public void MultiSegmentPayload(int segmentLen1, int segmentLen2, int segmentLen3) |
||||
{ |
||||
var origBuffer1 = GetTestBuffer(segmentLen1); |
||||
var origBuffer2 = GetTestBuffer(segmentLen2); |
||||
var origBuffer3 = GetTestBuffer(segmentLen3); |
||||
var fakeBufferReader = fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List<byte[]> { origBuffer1, origBuffer2, origBuffer3 }); |
||||
|
||||
Assert.AreEqual(origBuffer1.Length + origBuffer2.Length + origBuffer3.Length, fakeBufferReader.TotalLength.Value); |
||||
|
||||
Assert.IsTrue(fakeBufferReader.TryGetNextSlice(out Slice slice1)); |
||||
AssertSliceDataEqual(origBuffer1, slice1); |
||||
|
||||
Assert.IsTrue(fakeBufferReader.TryGetNextSlice(out Slice slice2)); |
||||
AssertSliceDataEqual(origBuffer2, slice2); |
||||
|
||||
Assert.IsTrue(fakeBufferReader.TryGetNextSlice(out Slice slice3)); |
||||
AssertSliceDataEqual(origBuffer3, slice3); |
||||
|
||||
Assert.IsFalse(fakeBufferReader.TryGetNextSlice(out Slice slice4)); |
||||
} |
||||
|
||||
private void AssertSliceDataEqual(byte[] expected, Slice actual) |
||||
{ |
||||
var actualSliceData = new byte[actual.Length]; |
||||
actual.CopyTo(new ArraySegment<byte>(actualSliceData)); |
||||
CollectionAssert.AreEqual(expected, actualSliceData); |
||||
} |
||||
|
||||
// create a buffer of given size and fill it with some data |
||||
private byte[] GetTestBuffer(int length) |
||||
{ |
||||
var testBuffer = new byte[length]; |
||||
for (int i = 0; i < testBuffer.Length; i++) |
||||
{ |
||||
testBuffer[i] = (byte) i; |
||||
} |
||||
return testBuffer; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,151 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2018 The gRPC Authors |
||||
// |
||||
// Licensed 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. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
using NUnit.Framework; |
||||
|
||||
using System.Runtime.InteropServices; |
||||
|
||||
#if GRPC_CSHARP_SUPPORT_SYSTEM_MEMORY |
||||
using System.Buffers; |
||||
#endif |
||||
|
||||
namespace Grpc.Core.Internal.Tests |
||||
{ |
||||
// Converts IBufferReader into instances of ReadOnlySequence<byte> |
||||
// Objects representing the sequence segments are cached to decrease GC load. |
||||
public class ReusableSliceBufferTest |
||||
{ |
||||
FakeBufferReaderManager fakeBufferReaderManager; |
||||
|
||||
[SetUp] |
||||
public void Init() |
||||
{ |
||||
fakeBufferReaderManager = new FakeBufferReaderManager(); |
||||
} |
||||
|
||||
[TearDown] |
||||
public void Cleanup() |
||||
{ |
||||
fakeBufferReaderManager.Dispose(); |
||||
} |
||||
|
||||
#if GRPC_CSHARP_SUPPORT_SYSTEM_MEMORY |
||||
[TestCase] |
||||
public void NullPayload() |
||||
{ |
||||
var sliceBuffer = new ReusableSliceBuffer(); |
||||
Assert.Throws(typeof(ArgumentNullException), () => sliceBuffer.PopulateFrom(fakeBufferReaderManager.CreateNullPayloadBufferReader())); |
||||
} |
||||
|
||||
[TestCase] |
||||
public void ZeroSegmentPayload() |
||||
{ |
||||
var sliceBuffer = new ReusableSliceBuffer(); |
||||
var sequence = sliceBuffer.PopulateFrom(fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List<byte[]> {})); |
||||
|
||||
Assert.AreEqual(ReadOnlySequence<byte>.Empty, sequence); |
||||
Assert.IsTrue(sequence.IsEmpty); |
||||
Assert.IsTrue(sequence.IsSingleSegment); |
||||
} |
||||
|
||||
[TestCase] |
||||
public void SegmentsAreCached() |
||||
{ |
||||
var bufferSegments1 = Enumerable.Range(0, 100).Select((_) => GetTestBuffer(50)).ToList(); |
||||
var bufferSegments2 = Enumerable.Range(0, 100).Select((_) => GetTestBuffer(50)).ToList(); |
||||
|
||||
var sliceBuffer = new ReusableSliceBuffer(); |
||||
|
||||
var sequence1 = sliceBuffer.PopulateFrom(fakeBufferReaderManager.CreateMultiSegmentBufferReader(bufferSegments1)); |
||||
var memoryManagers1 = GetMemoryManagersForSequenceSegments(sequence1); |
||||
|
||||
sliceBuffer.Invalidate(); |
||||
|
||||
var sequence2 = sliceBuffer.PopulateFrom(fakeBufferReaderManager.CreateMultiSegmentBufferReader(bufferSegments2)); |
||||
var memoryManagers2 = GetMemoryManagersForSequenceSegments(sequence2); |
||||
|
||||
// check memory managers are identical objects (i.e. they've been reused) |
||||
CollectionAssert.AreEquivalent(memoryManagers1, memoryManagers2); |
||||
} |
||||
|
||||
[TestCase] |
||||
public void MultiSegmentPayload_LotsOfSegments() |
||||
{ |
||||
var bufferSegments = Enumerable.Range(0, ReusableSliceBuffer.MaxCachedSegments + 100).Select((_) => GetTestBuffer(10)).ToList(); |
||||
|
||||
var sliceBuffer = new ReusableSliceBuffer(); |
||||
var sequence = sliceBuffer.PopulateFrom(fakeBufferReaderManager.CreateMultiSegmentBufferReader(bufferSegments)); |
||||
|
||||
int index = 0; |
||||
foreach (var memory in sequence) |
||||
{ |
||||
CollectionAssert.AreEqual(bufferSegments[index], memory.ToArray()); |
||||
index ++; |
||||
} |
||||
} |
||||
|
||||
[TestCase] |
||||
public void InvalidateMakesSequenceUnusable() |
||||
{ |
||||
var origBuffer = GetTestBuffer(100); |
||||
|
||||
var sliceBuffer = new ReusableSliceBuffer(); |
||||
var sequence = sliceBuffer.PopulateFrom(fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List<byte[]> { origBuffer })); |
||||
|
||||
Assert.AreEqual(origBuffer.Length, sequence.Length); |
||||
|
||||
sliceBuffer.Invalidate(); |
||||
|
||||
// Invalidate with make the returned sequence completely unusable and broken, users must not use it beyond the deserializer functions. |
||||
Assert.Throws(typeof(ArgumentOutOfRangeException), () => { var first = sequence.First; }); |
||||
} |
||||
|
||||
private List<MemoryManager<byte>> GetMemoryManagersForSequenceSegments(ReadOnlySequence<byte> sequence) |
||||
{ |
||||
var result = new List<MemoryManager<byte>>(); |
||||
foreach (var memory in sequence) |
||||
{ |
||||
Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(memory, out MemoryManager<byte> memoryManager)); |
||||
result.Add(memoryManager); |
||||
} |
||||
return result; |
||||
} |
||||
#else |
||||
[TestCase] |
||||
public void OnlySupportedOnNetCore() |
||||
{ |
||||
// Test case needs to exist to make C# sanity test happy. |
||||
} |
||||
#endif |
||||
private byte[] GetTestBuffer(int length) |
||||
{ |
||||
var testBuffer = new byte[length]; |
||||
for (int i = 0; i < testBuffer.Length; i++) |
||||
{ |
||||
testBuffer[i] = (byte) i; |
||||
} |
||||
return testBuffer; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,83 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2018 The gRPC Authors |
||||
// |
||||
// Licensed 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. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
using NUnit.Framework; |
||||
|
||||
using System.Runtime.InteropServices; |
||||
|
||||
namespace Grpc.Core.Internal.Tests |
||||
{ |
||||
public class SliceTest |
||||
{ |
||||
[TestCase(0)] |
||||
[TestCase(1)] |
||||
[TestCase(10)] |
||||
[TestCase(100)] |
||||
[TestCase(1000)] |
||||
public void SliceFromNativePtr_CopyToArraySegment(int bufferLength) |
||||
{ |
||||
var origBuffer = GetTestBuffer(bufferLength); |
||||
var gcHandle = GCHandle.Alloc(origBuffer, GCHandleType.Pinned); |
||||
try |
||||
{ |
||||
var slice = new Slice(gcHandle.AddrOfPinnedObject(), origBuffer.Length); |
||||
Assert.AreEqual(bufferLength, slice.Length); |
||||
|
||||
var newBuffer = new byte[bufferLength]; |
||||
slice.CopyTo(new ArraySegment<byte>(newBuffer)); |
||||
CollectionAssert.AreEqual(origBuffer, newBuffer); |
||||
} |
||||
finally |
||||
{ |
||||
gcHandle.Free(); |
||||
} |
||||
} |
||||
|
||||
[TestCase] |
||||
public void SliceFromNativePtr_CopyToArraySegmentTooSmall() |
||||
{ |
||||
var origBuffer = GetTestBuffer(100); |
||||
var gcHandle = GCHandle.Alloc(origBuffer, GCHandleType.Pinned); |
||||
try |
||||
{ |
||||
var slice = new Slice(gcHandle.AddrOfPinnedObject(), origBuffer.Length); |
||||
var tooSmall = new byte[origBuffer.Length - 1]; |
||||
Assert.Catch(typeof(ArgumentException), () => slice.CopyTo(new ArraySegment<byte>(tooSmall))); |
||||
} |
||||
finally |
||||
{ |
||||
gcHandle.Free(); |
||||
} |
||||
} |
||||
|
||||
// create a buffer of given size and fill it with some data |
||||
private byte[] GetTestBuffer(int length) |
||||
{ |
||||
var testBuffer = new byte[length]; |
||||
for (int i = 0; i < testBuffer.Length; i++) |
||||
{ |
||||
testBuffer[i] = (byte) i; |
||||
} |
||||
return testBuffer; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,148 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2019 The gRPC Authors |
||||
// |
||||
// Licensed 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. |
||||
|
||||
#endregion |
||||
|
||||
#if GRPC_CSHARP_SUPPORT_SYSTEM_MEMORY |
||||
|
||||
using Grpc.Core.Utils; |
||||
using System; |
||||
using System.Threading; |
||||
|
||||
using System.Buffers; |
||||
|
||||
namespace Grpc.Core.Internal |
||||
{ |
||||
internal class ReusableSliceBuffer |
||||
{ |
||||
public const int MaxCachedSegments = 1024; // ~4MB payload for 4K slices |
||||
|
||||
readonly SliceSegment[] cachedSegments = new SliceSegment[MaxCachedSegments]; |
||||
int populatedSegmentCount; |
||||
|
||||
public ReadOnlySequence<byte> PopulateFrom(IBufferReader bufferReader) |
||||
{ |
||||
populatedSegmentCount = 0; |
||||
long offset = 0; |
||||
SliceSegment prevSegment = null; |
||||
while (bufferReader.TryGetNextSlice(out Slice slice)) |
||||
{ |
||||
// Initialize cached segment if still null or just allocate a new segment if we already reached MaxCachedSegments |
||||
var current = populatedSegmentCount < cachedSegments.Length ? cachedSegments[populatedSegmentCount] : new SliceSegment(); |
||||
if (current == null) |
||||
{ |
||||
current = cachedSegments[populatedSegmentCount] = new SliceSegment(); |
||||
} |
||||
|
||||
current.Reset(slice, offset); |
||||
prevSegment?.SetNext(current); |
||||
|
||||
populatedSegmentCount ++; |
||||
offset += slice.Length; |
||||
prevSegment = current; |
||||
} |
||||
|
||||
// Not necessary for ending the ReadOnlySequence, but for making sure we |
||||
// don't keep more than MaxCachedSegments alive. |
||||
prevSegment?.SetNext(null); |
||||
|
||||
if (populatedSegmentCount == 0) |
||||
{ |
||||
return ReadOnlySequence<byte>.Empty; |
||||
} |
||||
|
||||
var firstSegment = cachedSegments[0]; |
||||
var lastSegment = prevSegment; |
||||
return new ReadOnlySequence<byte>(firstSegment, 0, lastSegment, lastSegment.Memory.Length); |
||||
} |
||||
|
||||
public void Invalidate() |
||||
{ |
||||
if (populatedSegmentCount == 0) |
||||
{ |
||||
return; |
||||
} |
||||
var segment = cachedSegments[0]; |
||||
while (segment != null) |
||||
{ |
||||
segment.Reset(new Slice(IntPtr.Zero, 0), 0); |
||||
var nextSegment = (SliceSegment) segment.Next; |
||||
segment.SetNext(null); |
||||
segment = nextSegment; |
||||
} |
||||
populatedSegmentCount = 0; |
||||
} |
||||
|
||||
// Represents a segment in ReadOnlySequence |
||||
// Segment is backed by Slice and the instances are reusable. |
||||
private class SliceSegment : ReadOnlySequenceSegment<byte> |
||||
{ |
||||
readonly SliceMemoryManager pointerMemoryManager = new SliceMemoryManager(); |
||||
|
||||
public void Reset(Slice slice, long runningIndex) |
||||
{ |
||||
pointerMemoryManager.Reset(slice); |
||||
Memory = pointerMemoryManager.Memory; // maybe not always necessary |
||||
RunningIndex = runningIndex; |
||||
} |
||||
|
||||
public void SetNext(ReadOnlySequenceSegment<byte> next) |
||||
{ |
||||
Next = next; |
||||
} |
||||
} |
||||
|
||||
// Allow creating instances of Memory<byte> from Slice. |
||||
// Represents a chunk of native memory, but doesn't manage its lifetime. |
||||
// Instances of this class are reuseable - they can be reset to point to a different memory chunk. |
||||
// That is important to make the instances cacheable (rather then creating new instances |
||||
// the old ones will be reused to reduce GC pressure). |
||||
private class SliceMemoryManager : MemoryManager<byte> |
||||
{ |
||||
private Slice slice; |
||||
|
||||
public void Reset(Slice slice) |
||||
{ |
||||
this.slice = slice; |
||||
} |
||||
|
||||
public void Reset() |
||||
{ |
||||
Reset(new Slice(IntPtr.Zero, 0)); |
||||
} |
||||
|
||||
public override Span<byte> GetSpan() |
||||
{ |
||||
return slice.ToSpanUnsafe(); |
||||
} |
||||
|
||||
public override MemoryHandle Pin(int elementIndex = 0) |
||||
{ |
||||
throw new NotSupportedException(); |
||||
} |
||||
|
||||
public override void Unpin() |
||||
{ |
||||
} |
||||
|
||||
protected override void Dispose(bool disposing) |
||||
{ |
||||
// NOP |
||||
} |
||||
} |
||||
} |
||||
} |
||||
#endif |
@ -0,0 +1,68 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2019 The gRPC Authors |
||||
// |
||||
// Licensed 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. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Runtime.InteropServices; |
||||
using System.Threading; |
||||
using Grpc.Core.Utils; |
||||
|
||||
namespace Grpc.Core.Internal |
||||
{ |
||||
/// <summary> |
||||
/// Slice of native memory. |
||||
/// Rough equivalent of grpc_slice (but doesn't support inlined slices, just a pointer to data and length) |
||||
/// </summary> |
||||
internal struct Slice |
||||
{ |
||||
private readonly IntPtr dataPtr; |
||||
private readonly int length; |
||||
|
||||
public Slice(IntPtr dataPtr, int length) |
||||
{ |
||||
this.dataPtr = dataPtr; |
||||
this.length = length; |
||||
} |
||||
|
||||
public int Length => length; |
||||
|
||||
// copies data of the slice to given span. |
||||
// there needs to be enough space in the destination buffer |
||||
public void CopyTo(ArraySegment<byte> destination) |
||||
{ |
||||
Marshal.Copy(dataPtr, destination.Array, destination.Offset, length); |
||||
} |
||||
|
||||
#if GRPC_CSHARP_SUPPORT_SYSTEM_MEMORY |
||||
public Span<byte> ToSpanUnsafe() |
||||
{ |
||||
unsafe |
||||
{ |
||||
return new Span<byte>((byte*) dataPtr, length); |
||||
} |
||||
} |
||||
#endif |
||||
|
||||
/// <summary> |
||||
/// Returns a <see cref="System.String"/> that represents the current <see cref="Grpc.Core.Internal.Slice"/>. |
||||
/// </summary> |
||||
public override string ToString() |
||||
{ |
||||
return string.Format("[Slice: dataPtr={0}, length={1}]", dataPtr, length); |
||||
} |
||||
} |
||||
} |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue