commit
d9bc2bf825
136 changed files with 8676 additions and 526 deletions
@ -0,0 +1,30 @@ |
||||
{ |
||||
"version": "0.2.0", |
||||
"configurations": [ |
||||
{ |
||||
"type": "node", |
||||
"request": "launch", |
||||
"name": "Mocha Tests", |
||||
"cwd": "${workspaceRoot}", |
||||
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/mocha", |
||||
"windows": { |
||||
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/mocha.cmd" |
||||
}, |
||||
"runtimeArgs": [ |
||||
"-u", |
||||
"tdd", |
||||
"--timeout", |
||||
"999999", |
||||
"--colors", |
||||
"${workspaceRoot}/src/node/test" |
||||
], |
||||
"internalConsoleOptions": "openOnSessionStart" |
||||
}, |
||||
{ |
||||
"type": "node", |
||||
"request": "attach", |
||||
"name": "Attach to Process", |
||||
"port": 5858 |
||||
} |
||||
] |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,25 @@ |
||||
#Errors and Cancelletion code samples for grpc-ruby |
||||
|
||||
The examples in this directory show use of grpc errors. |
||||
|
||||
On the server side, errors are returned from service |
||||
implementations by raising a certain `GRPC::BadStatus` exception. |
||||
|
||||
On the client side, GRPC errors get raised when either: |
||||
* the call completes (unary and client-streaming call types) |
||||
* the response `Enumerable` is iterated through (server-streaming and |
||||
bidi call types). |
||||
|
||||
## To run the examples here: |
||||
|
||||
Start the server: |
||||
|
||||
``` |
||||
> ruby error_examples_server.rb |
||||
``` |
||||
|
||||
Then run the client: |
||||
|
||||
``` |
||||
> ruby error_examples_client.rb |
||||
``` |
@ -0,0 +1,117 @@ |
||||
#!/usr/bin/env ruby |
||||
|
||||
# Copyright 2015, Google Inc. |
||||
# All rights reserved. |
||||
# |
||||
# Redistribution and use in source and binary forms, with or without |
||||
# modification, are permitted provided that the following conditions are |
||||
# met: |
||||
# |
||||
# * Redistributions of source code must retain the above copyright |
||||
# notice, this list of conditions and the following disclaimer. |
||||
# * Redistributions in binary form must reproduce the above |
||||
# copyright notice, this list of conditions and the following disclaimer |
||||
# in the documentation and/or other materials provided with the |
||||
# distribution. |
||||
# * Neither the name of Google Inc. nor the names of its |
||||
# contributors may be used to endorse or promote products derived from |
||||
# this software without specific prior written permission. |
||||
# |
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
# Sample app that connects to an error-throwing implementation of |
||||
# Route Guide service. |
||||
# |
||||
# Usage: $ path/to/route_guide_client.rb |
||||
|
||||
this_dir = File.expand_path(File.dirname(__FILE__)) |
||||
lib_dir = File.join(File.dirname(this_dir), 'lib') |
||||
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir) |
||||
|
||||
require 'grpc' |
||||
require 'route_guide_services_pb' |
||||
|
||||
include Routeguide |
||||
|
||||
def run_get_feature_expect_error(stub) |
||||
resp = stub.get_feature(Point.new) |
||||
end |
||||
|
||||
def run_list_features_expect_error(stub) |
||||
resps = stub.list_features(Rectangle.new) |
||||
|
||||
# NOOP iteration to pick up error |
||||
resps.each { } |
||||
end |
||||
|
||||
def run_record_route_expect_error(stub) |
||||
stub.record_route([]) |
||||
end |
||||
|
||||
def run_route_chat_expect_error(stub) |
||||
resps = stub.route_chat([]) |
||||
|
||||
# NOOP iteration to pick up error |
||||
resps.each { } |
||||
end |
||||
|
||||
def main |
||||
stub = RouteGuide::Stub.new('localhost:50051', :this_channel_is_insecure) |
||||
|
||||
begin |
||||
run_get_feature_expect_error(stub) |
||||
rescue GRPC::BadStatus => e |
||||
puts "===== GetFeature exception: =====" |
||||
puts e.inspect |
||||
puts "e.code: #{e.code}" |
||||
puts "e.details: #{e.details}" |
||||
puts "e.metadata: #{e.metadata}" |
||||
puts "=================================" |
||||
end |
||||
|
||||
begin |
||||
run_list_features_expect_error(stub) |
||||
rescue GRPC::BadStatus => e |
||||
error = true |
||||
puts "===== ListFeatures exception: =====" |
||||
puts e.inspect |
||||
puts "e.code: #{e.code}" |
||||
puts "e.details: #{e.details}" |
||||
puts "e.metadata: #{e.metadata}" |
||||
puts "=================================" |
||||
end |
||||
|
||||
begin |
||||
run_route_chat_expect_error(stub) |
||||
rescue GRPC::BadStatus => e |
||||
puts "==== RouteChat exception: ====" |
||||
puts e.inspect |
||||
puts "e.code: #{e.code}" |
||||
puts "e.details: #{e.details}" |
||||
puts "e.metadata: #{e.metadata}" |
||||
puts "=================================" |
||||
end |
||||
|
||||
begin |
||||
run_record_route_expect_error(stub) |
||||
rescue GRPC::BadStatus => e |
||||
puts "==== RecordRoute exception: ====" |
||||
puts e.inspect |
||||
puts "e.code: #{e.code}" |
||||
puts "e.details: #{e.details}" |
||||
puts "e.metadata: #{e.metadata}" |
||||
puts "=================================" |
||||
end |
||||
end |
||||
|
||||
main |
@ -0,0 +1,76 @@ |
||||
#!/usr/bin/env ruby |
||||
# -*- coding: utf-8 -*- |
||||
|
||||
# Copyright 2015, Google Inc. |
||||
# All rights reserved. |
||||
# |
||||
# Redistribution and use in source and binary forms, with or without |
||||
# modification, are permitted provided that the following conditions are |
||||
# met: |
||||
# |
||||
# * Redistributions of source code must retain the above copyright |
||||
# notice, this list of conditions and the following disclaimer. |
||||
# * Redistributions in binary form must reproduce the above |
||||
# copyright notice, this list of conditions and the following disclaimer |
||||
# in the documentation and/or other materials provided with the |
||||
# distribution. |
||||
# * Neither the name of Google Inc. nor the names of its |
||||
# contributors may be used to endorse or promote products derived from |
||||
# this software without specific prior written permission. |
||||
# |
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
# Error-throwing implementation of Route Guide service. |
||||
# |
||||
# Usage: $ path/to/route_guide_server.rb |
||||
|
||||
this_dir = File.expand_path(File.dirname(__FILE__)) |
||||
lib_dir = File.join(File.dirname(this_dir), 'lib') |
||||
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir) |
||||
|
||||
require 'grpc' |
||||
require 'route_guide_services_pb' |
||||
|
||||
include Routeguide |
||||
|
||||
include GRPC::Core::StatusCodes |
||||
|
||||
# CanellingandErrorReturningServiceImpl provides an implementation of the RouteGuide service. |
||||
class CancellingAndErrorReturningServerImpl < RouteGuide::Service |
||||
# def get_feature |
||||
# Note get_feature isn't implemented in this subclass, so the server |
||||
# will get a gRPC UNIMPLEMENTED error when it's called. |
||||
|
||||
def list_features(rectangle, _call) |
||||
raise "string appears on the client in the 'details' field of a 'GRPC::Unknown' exception" |
||||
end |
||||
|
||||
def record_route(call) |
||||
raise GRPC::BadStatus.new_status_exception(CANCELLED) |
||||
end |
||||
|
||||
def route_chat(notes) |
||||
raise GRPC::BadStatus.new_status_exception(ABORTED, details = 'arbitrary', metadata = {somekey: 'val'}) |
||||
end |
||||
end |
||||
|
||||
def main |
||||
port = '0.0.0.0:50051' |
||||
s = GRPC::RpcServer.new |
||||
s.add_http2_port(port, :this_port_is_insecure) |
||||
GRPC.logger.info("... running insecurely on #{port}") |
||||
s.handle(CancellingAndErrorReturningServerImpl.new) |
||||
s.run_till_terminated |
||||
end |
||||
|
||||
main |
@ -0,0 +1,49 @@ |
||||
# c-ares cmake file for gRPC |
||||
# |
||||
# This is currently very experimental, and unsupported. |
||||
# |
||||
# Copyright 2016, Google Inc. |
||||
# All rights reserved. |
||||
# |
||||
# Redistribution and use in source and binary forms, with or without |
||||
# modification, are permitted provided that the following conditions are |
||||
# met: |
||||
# |
||||
# * Redistributions of source code must retain the above copyright |
||||
# notice, this list of conditions and the following disclaimer. |
||||
# * Redistributions in binary form must reproduce the above |
||||
# copyright notice, this list of conditions and the following disclaimer |
||||
# in the documentation and/or other materials provided with the |
||||
# distribution. |
||||
# * Neither the name of Google Inc. nor the names of its |
||||
# contributors may be used to endorse or promote products derived from |
||||
# this software without specific prior written permission. |
||||
# |
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
string(TOLOWER ${CMAKE_SYSTEM_NAME} cares_system_name) |
||||
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../third_party/cares) |
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../third_party/cares/cares) |
||||
|
||||
if(${cares_system_name} MATCHES windows) |
||||
add_definitions(-DCARES_STATICLIB=1) |
||||
add_definitions(-DWIN32_LEAN_AND_MEAN=1) |
||||
else() |
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../third_party/cares/config_${cares_system_name}) |
||||
add_definitions(-DHAVE_CONFIG_H=1) |
||||
add_definitions(-D_GNU_SOURCE=1) |
||||
endif() |
||||
|
||||
file(GLOB lib_sources ../../third_party/cares/cares/*.c) |
||||
add_library(cares ${lib_sources}) |
@ -0,0 +1,149 @@ |
||||
#!/usr/bin/env python2.7 |
||||
|
||||
# Copyright 2015, Google Inc. |
||||
# All rights reserved. |
||||
# |
||||
# Redistribution and use in source and binary forms, with or without |
||||
# modification, are permitted provided that the following conditions are |
||||
# met: |
||||
# |
||||
# * Redistributions of source code must retain the above copyright |
||||
# notice, this list of conditions and the following disclaimer. |
||||
# * Redistributions in binary form must reproduce the above |
||||
# copyright notice, this list of conditions and the following disclaimer |
||||
# in the documentation and/or other materials provided with the |
||||
# distribution. |
||||
# * Neither the name of Google Inc. nor the names of its |
||||
# contributors may be used to endorse or promote products derived from |
||||
# this software without specific prior written permission. |
||||
# |
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
import re |
||||
import os |
||||
import sys |
||||
import yaml |
||||
|
||||
os.chdir(os.path.dirname(sys.argv[0])+'/../..') |
||||
|
||||
out = {} |
||||
|
||||
try: |
||||
def gen_ares_build(x): |
||||
subprocess.call("third_party/cares/cares/buildconf", shell=True) |
||||
subprocess.call("third_party/cares/cares/configure", shell=True) |
||||
|
||||
def config_platform(x): |
||||
if 'linux' in sys.platform: |
||||
return 'src/cares/cares/config_linux/ares_config.h' |
||||
if 'darwin' in sys.platform: |
||||
return 'src/cares/cares/config_darwin/ares_config.h' |
||||
if not os.path.isfile('third_party/cares/cares/ares_config.h'): |
||||
gen_ares_build(x) |
||||
return 'third_party/cares/cares/ares_config.h' |
||||
|
||||
def ares_build(x): |
||||
if os.path.isfile('src/cares/cares/ares_build.h'): |
||||
return 'src/cares/cares/ares_build.h' |
||||
if not os.path.isfile('third_party/cares/cares/ares_build.h'): |
||||
gen_ares_build(x) |
||||
return 'third_party/cares/cares/ares_build.h' |
||||
|
||||
out['libs'] = [{ |
||||
'name': 'ares', |
||||
'defaults': 'ares', |
||||
'build': 'private', |
||||
'language': 'c', |
||||
'secure': 'no', |
||||
'src': [ |
||||
"third_party/cares/cares/ares__close_sockets.c", |
||||
"third_party/cares/cares/ares__get_hostent.c", |
||||
"third_party/cares/cares/ares__read_line.c", |
||||
"third_party/cares/cares/ares__timeval.c", |
||||
"third_party/cares/cares/ares_cancel.c", |
||||
"third_party/cares/cares/ares_create_query.c", |
||||
"third_party/cares/cares/ares_data.c", |
||||
"third_party/cares/cares/ares_destroy.c", |
||||
"third_party/cares/cares/ares_expand_name.c", |
||||
"third_party/cares/cares/ares_expand_string.c", |
||||
"third_party/cares/cares/ares_fds.c", |
||||
"third_party/cares/cares/ares_free_hostent.c", |
||||
"third_party/cares/cares/ares_free_string.c", |
||||
"third_party/cares/cares/ares_getenv.c", |
||||
"third_party/cares/cares/ares_gethostbyaddr.c", |
||||
"third_party/cares/cares/ares_gethostbyname.c", |
||||
"third_party/cares/cares/ares_getnameinfo.c", |
||||
"third_party/cares/cares/ares_getopt.c", |
||||
"third_party/cares/cares/ares_getsock.c", |
||||
"third_party/cares/cares/ares_init.c", |
||||
"third_party/cares/cares/ares_library_init.c", |
||||
"third_party/cares/cares/ares_llist.c", |
||||
"third_party/cares/cares/ares_mkquery.c", |
||||
"third_party/cares/cares/ares_nowarn.c", |
||||
"third_party/cares/cares/ares_options.c", |
||||
"third_party/cares/cares/ares_parse_a_reply.c", |
||||
"third_party/cares/cares/ares_parse_aaaa_reply.c", |
||||
"third_party/cares/cares/ares_parse_mx_reply.c", |
||||
"third_party/cares/cares/ares_parse_naptr_reply.c", |
||||
"third_party/cares/cares/ares_parse_ns_reply.c", |
||||
"third_party/cares/cares/ares_parse_ptr_reply.c", |
||||
"third_party/cares/cares/ares_parse_soa_reply.c", |
||||
"third_party/cares/cares/ares_parse_srv_reply.c", |
||||
"third_party/cares/cares/ares_parse_txt_reply.c", |
||||
"third_party/cares/cares/ares_platform.c", |
||||
"third_party/cares/cares/ares_process.c", |
||||
"third_party/cares/cares/ares_query.c", |
||||
"third_party/cares/cares/ares_search.c", |
||||
"third_party/cares/cares/ares_send.c", |
||||
"third_party/cares/cares/ares_strcasecmp.c", |
||||
"third_party/cares/cares/ares_strdup.c", |
||||
"third_party/cares/cares/ares_strerror.c", |
||||
"third_party/cares/cares/ares_timeout.c", |
||||
"third_party/cares/cares/ares_version.c", |
||||
"third_party/cares/cares/ares_writev.c", |
||||
"third_party/cares/cares/bitncmp.c", |
||||
"third_party/cares/cares/inet_net_pton.c", |
||||
"third_party/cares/cares/inet_ntop.c", |
||||
"third_party/cares/cares/windows_port.c", |
||||
], |
||||
'headers': [ |
||||
"third_party/cares/cares/ares.h", |
||||
"third_party/cares/cares/ares_data.h", |
||||
"third_party/cares/cares/ares_dns.h", |
||||
"third_party/cares/cares/ares_getenv.h", |
||||
"third_party/cares/cares/ares_getopt.h", |
||||
"third_party/cares/cares/ares_inet_net_pton.h", |
||||
"third_party/cares/cares/ares_iphlpapi.h", |
||||
"third_party/cares/cares/ares_ipv6.h", |
||||
"third_party/cares/cares/ares_library_init.h", |
||||
"third_party/cares/cares/ares_llist.h", |
||||
"third_party/cares/cares/ares_nowarn.h", |
||||
"third_party/cares/cares/ares_platform.h", |
||||
"third_party/cares/cares/ares_private.h", |
||||
"third_party/cares/cares/ares_rules.h", |
||||
"third_party/cares/cares/ares_setup.h", |
||||
"third_party/cares/cares/ares_strcasecmp.h", |
||||
"third_party/cares/cares/ares_strdup.h", |
||||
"third_party/cares/cares/ares_version.h", |
||||
"third_party/cares/cares/bitncmp.h", |
||||
"third_party/cares/cares/config-win32.h", |
||||
"third_party/cares/cares/setup_once.h", |
||||
"third_party/cares/ares_build.h", |
||||
"third_party/cares/config_linux/ares_config.h", |
||||
"third_party/cares/config_darwin/ares_config.h" |
||||
], |
||||
}] |
||||
except: |
||||
pass |
||||
|
||||
print yaml.dump(out) |
@ -1,4 +1,4 @@ |
||||
#Overview |
||||
# Overview |
||||
|
||||
This directory contains source code for gRPC protocol buffer compiler (*protoc*) plugins. Along with `protoc`, |
||||
these plugins are used to generate gRPC client and server stubs from `.proto` files. |
||||
|
@ -1,4 +1,4 @@ |
||||
#Overview |
||||
# Overview |
||||
|
||||
This directory contains source code for C library (a.k.a the *gRPC C core*) that provides all gRPC's core functionality through a low level API. Libraries in other languages in this repository (C++, Ruby, |
||||
Python, PHP, NodeJS, Objective-C) are layered on top of this library. |
||||
|
@ -0,0 +1,350 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
#if GRPC_ARES == 1 && !defined(GRPC_UV) |
||||
|
||||
#include <string.h> |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/host_port.h> |
||||
#include <grpc/support/string_util.h> |
||||
|
||||
#include "src/core/ext/client_channel/http_connect_handshaker.h" |
||||
#include "src/core/ext/client_channel/lb_policy_registry.h" |
||||
#include "src/core/ext/client_channel/resolver_registry.h" |
||||
#include "src/core/ext/resolver/dns/c_ares/grpc_ares_wrapper.h" |
||||
#include "src/core/lib/channel/channel_args.h" |
||||
#include "src/core/lib/iomgr/combiner.h" |
||||
#include "src/core/lib/iomgr/resolve_address.h" |
||||
#include "src/core/lib/iomgr/timer.h" |
||||
#include "src/core/lib/support/backoff.h" |
||||
#include "src/core/lib/support/env.h" |
||||
#include "src/core/lib/support/string.h" |
||||
|
||||
#define GRPC_DNS_MIN_CONNECT_TIMEOUT_SECONDS 1 |
||||
#define GRPC_DNS_INITIAL_CONNECT_BACKOFF_SECONDS 1 |
||||
#define GRPC_DNS_RECONNECT_BACKOFF_MULTIPLIER 1.6 |
||||
#define GRPC_DNS_RECONNECT_MAX_BACKOFF_SECONDS 120 |
||||
#define GRPC_DNS_RECONNECT_JITTER 0.2 |
||||
|
||||
typedef struct { |
||||
/** base class: must be first */ |
||||
grpc_resolver base; |
||||
/** name to resolve (usually the same as target_name) */ |
||||
char *name_to_resolve; |
||||
/** default port to use */ |
||||
char *default_port; |
||||
/** channel args. */ |
||||
grpc_channel_args *channel_args; |
||||
/** pollset_set to drive the name resolution process */ |
||||
grpc_pollset_set *interested_parties; |
||||
|
||||
/** Closures used by the combiner */ |
||||
grpc_closure dns_ares_on_retry_timer_locked; |
||||
grpc_closure dns_ares_on_resolved_locked; |
||||
|
||||
/** Combiner guarding the rest of the state */ |
||||
grpc_combiner *combiner; |
||||
/** are we currently resolving? */ |
||||
bool resolving; |
||||
/** which version of the result have we published? */ |
||||
int published_version; |
||||
/** which version of the result is current? */ |
||||
int resolved_version; |
||||
/** pending next completion, or NULL */ |
||||
grpc_closure *next_completion; |
||||
/** target result address for next completion */ |
||||
grpc_channel_args **target_result; |
||||
/** current (fully resolved) result */ |
||||
grpc_channel_args *resolved_result; |
||||
/** retry timer */ |
||||
bool have_retry_timer; |
||||
grpc_timer retry_timer; |
||||
/** retry backoff state */ |
||||
gpr_backoff backoff_state; |
||||
|
||||
/** currently resolving addresses */ |
||||
grpc_resolved_addresses *addresses; |
||||
} ares_dns_resolver; |
||||
|
||||
static void dns_ares_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *r); |
||||
|
||||
static void dns_ares_start_resolving_locked(grpc_exec_ctx *exec_ctx, |
||||
ares_dns_resolver *r); |
||||
static void dns_ares_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx, |
||||
ares_dns_resolver *r); |
||||
|
||||
static void dns_ares_shutdown_locked(grpc_exec_ctx *exec_ctx, grpc_resolver *r); |
||||
static void dns_ares_channel_saw_error_locked(grpc_exec_ctx *exec_ctx, |
||||
grpc_resolver *r); |
||||
static void dns_ares_next_locked(grpc_exec_ctx *exec_ctx, grpc_resolver *r, |
||||
grpc_channel_args **target_result, |
||||
grpc_closure *on_complete); |
||||
|
||||
static const grpc_resolver_vtable dns_ares_resolver_vtable = { |
||||
dns_ares_destroy, dns_ares_shutdown_locked, |
||||
dns_ares_channel_saw_error_locked, dns_ares_next_locked}; |
||||
|
||||
static void dns_ares_shutdown_locked(grpc_exec_ctx *exec_ctx, |
||||
grpc_resolver *resolver) { |
||||
ares_dns_resolver *r = (ares_dns_resolver *)resolver; |
||||
if (r->have_retry_timer) { |
||||
grpc_timer_cancel(exec_ctx, &r->retry_timer); |
||||
} |
||||
if (r->next_completion != NULL) { |
||||
*r->target_result = NULL; |
||||
grpc_closure_sched( |
||||
exec_ctx, r->next_completion, |
||||
GRPC_ERROR_CREATE_FROM_STATIC_STRING("Resolver Shutdown")); |
||||
r->next_completion = NULL; |
||||
} |
||||
} |
||||
|
||||
static void dns_ares_channel_saw_error_locked(grpc_exec_ctx *exec_ctx, |
||||
grpc_resolver *resolver) { |
||||
ares_dns_resolver *r = (ares_dns_resolver *)resolver; |
||||
if (!r->resolving) { |
||||
gpr_backoff_reset(&r->backoff_state); |
||||
dns_ares_start_resolving_locked(exec_ctx, r); |
||||
} |
||||
} |
||||
|
||||
static void dns_ares_on_retry_timer_locked(grpc_exec_ctx *exec_ctx, void *arg, |
||||
grpc_error *error) { |
||||
ares_dns_resolver *r = arg; |
||||
r->have_retry_timer = false; |
||||
if (error == GRPC_ERROR_NONE) { |
||||
if (!r->resolving) { |
||||
dns_ares_start_resolving_locked(exec_ctx, r); |
||||
} |
||||
} |
||||
GRPC_RESOLVER_UNREF(exec_ctx, &r->base, "retry-timer"); |
||||
} |
||||
|
||||
static void dns_ares_on_resolved_locked(grpc_exec_ctx *exec_ctx, void *arg, |
||||
grpc_error *error) { |
||||
ares_dns_resolver *r = arg; |
||||
grpc_channel_args *result = NULL; |
||||
GPR_ASSERT(r->resolving); |
||||
r->resolving = false; |
||||
if (r->addresses != NULL) { |
||||
grpc_lb_addresses *addresses = grpc_lb_addresses_create( |
||||
r->addresses->naddrs, NULL /* user_data_vtable */); |
||||
for (size_t i = 0; i < r->addresses->naddrs; ++i) { |
||||
grpc_lb_addresses_set_address( |
||||
addresses, i, &r->addresses->addrs[i].addr, |
||||
r->addresses->addrs[i].len, false /* is_balancer */, |
||||
NULL /* balancer_name */, NULL /* user_data */); |
||||
} |
||||
grpc_arg new_arg = grpc_lb_addresses_create_channel_arg(addresses); |
||||
result = grpc_channel_args_copy_and_add(r->channel_args, &new_arg, 1); |
||||
grpc_resolved_addresses_destroy(r->addresses); |
||||
grpc_lb_addresses_destroy(exec_ctx, addresses); |
||||
} else { |
||||
gpr_timespec now = gpr_now(GPR_CLOCK_MONOTONIC); |
||||
gpr_timespec next_try = gpr_backoff_step(&r->backoff_state, now); |
||||
gpr_timespec timeout = gpr_time_sub(next_try, now); |
||||
gpr_log(GPR_INFO, "dns resolution failed (will retry): %s", |
||||
grpc_error_string(error)); |
||||
GPR_ASSERT(!r->have_retry_timer); |
||||
r->have_retry_timer = true; |
||||
GRPC_RESOLVER_REF(&r->base, "retry-timer"); |
||||
if (gpr_time_cmp(timeout, gpr_time_0(timeout.clock_type)) > 0) { |
||||
gpr_log(GPR_DEBUG, "retrying in %" PRId64 ".%09d seconds", timeout.tv_sec, |
||||
timeout.tv_nsec); |
||||
} else { |
||||
gpr_log(GPR_DEBUG, "retrying immediately"); |
||||
} |
||||
grpc_timer_init(exec_ctx, &r->retry_timer, next_try, |
||||
&r->dns_ares_on_retry_timer_locked, now); |
||||
} |
||||
if (r->resolved_result != NULL) { |
||||
grpc_channel_args_destroy(exec_ctx, r->resolved_result); |
||||
} |
||||
r->resolved_result = result; |
||||
r->resolved_version++; |
||||
dns_ares_maybe_finish_next_locked(exec_ctx, r); |
||||
GRPC_RESOLVER_UNREF(exec_ctx, &r->base, "dns-resolving"); |
||||
} |
||||
|
||||
static void dns_ares_next_locked(grpc_exec_ctx *exec_ctx, |
||||
grpc_resolver *resolver, |
||||
grpc_channel_args **target_result, |
||||
grpc_closure *on_complete) { |
||||
gpr_log(GPR_DEBUG, "dns_ares_next is called."); |
||||
ares_dns_resolver *r = (ares_dns_resolver *)resolver; |
||||
GPR_ASSERT(!r->next_completion); |
||||
r->next_completion = on_complete; |
||||
r->target_result = target_result; |
||||
if (r->resolved_version == 0 && !r->resolving) { |
||||
gpr_backoff_reset(&r->backoff_state); |
||||
dns_ares_start_resolving_locked(exec_ctx, r); |
||||
} else { |
||||
dns_ares_maybe_finish_next_locked(exec_ctx, r); |
||||
} |
||||
} |
||||
|
||||
static void dns_ares_start_resolving_locked(grpc_exec_ctx *exec_ctx, |
||||
ares_dns_resolver *r) { |
||||
GRPC_RESOLVER_REF(&r->base, "dns-resolving"); |
||||
GPR_ASSERT(!r->resolving); |
||||
r->resolving = true; |
||||
r->addresses = NULL; |
||||
grpc_resolve_address(exec_ctx, r->name_to_resolve, r->default_port, |
||||
r->interested_parties, &r->dns_ares_on_resolved_locked, |
||||
&r->addresses); |
||||
} |
||||
|
||||
static void dns_ares_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx, |
||||
ares_dns_resolver *r) { |
||||
if (r->next_completion != NULL && |
||||
r->resolved_version != r->published_version) { |
||||
*r->target_result = r->resolved_result == NULL |
||||
? NULL |
||||
: grpc_channel_args_copy(r->resolved_result); |
||||
grpc_closure_sched(exec_ctx, r->next_completion, GRPC_ERROR_NONE); |
||||
r->next_completion = NULL; |
||||
r->published_version = r->resolved_version; |
||||
} |
||||
} |
||||
|
||||
static void dns_ares_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *gr) { |
||||
gpr_log(GPR_DEBUG, "dns_ares_destroy"); |
||||
ares_dns_resolver *r = (ares_dns_resolver *)gr; |
||||
if (r->resolved_result != NULL) { |
||||
grpc_channel_args_destroy(exec_ctx, r->resolved_result); |
||||
} |
||||
grpc_pollset_set_destroy(exec_ctx, r->interested_parties); |
||||
gpr_free(r->name_to_resolve); |
||||
gpr_free(r->default_port); |
||||
grpc_channel_args_destroy(exec_ctx, r->channel_args); |
||||
gpr_free(r); |
||||
} |
||||
|
||||
static grpc_resolver *dns_ares_create(grpc_exec_ctx *exec_ctx, |
||||
grpc_resolver_args *args, |
||||
const char *default_port) { |
||||
// Get name from args.
|
||||
const char *path = args->uri->path; |
||||
if (0 != strcmp(args->uri->authority, "")) { |
||||
gpr_log(GPR_ERROR, "authority based dns uri's not supported"); |
||||
return NULL; |
||||
} |
||||
if (path[0] == '/') ++path; |
||||
// Create resolver.
|
||||
ares_dns_resolver *r = gpr_zalloc(sizeof(ares_dns_resolver)); |
||||
grpc_resolver_init(&r->base, &dns_ares_resolver_vtable, args->combiner); |
||||
r->name_to_resolve = gpr_strdup(path); |
||||
r->default_port = gpr_strdup(default_port); |
||||
r->channel_args = grpc_channel_args_copy(args->args); |
||||
r->interested_parties = grpc_pollset_set_create(); |
||||
if (args->pollset_set != NULL) { |
||||
grpc_pollset_set_add_pollset_set(exec_ctx, r->interested_parties, |
||||
args->pollset_set); |
||||
} |
||||
gpr_backoff_init(&r->backoff_state, GRPC_DNS_INITIAL_CONNECT_BACKOFF_SECONDS, |
||||
GRPC_DNS_RECONNECT_BACKOFF_MULTIPLIER, |
||||
GRPC_DNS_RECONNECT_JITTER, |
||||
GRPC_DNS_MIN_CONNECT_TIMEOUT_SECONDS * 1000, |
||||
GRPC_DNS_RECONNECT_MAX_BACKOFF_SECONDS * 1000); |
||||
grpc_closure_init(&r->dns_ares_on_retry_timer_locked, |
||||
dns_ares_on_retry_timer_locked, r, |
||||
grpc_combiner_scheduler(r->base.combiner, false)); |
||||
grpc_closure_init(&r->dns_ares_on_resolved_locked, |
||||
dns_ares_on_resolved_locked, r, |
||||
grpc_combiner_scheduler(r->base.combiner, false)); |
||||
return &r->base; |
||||
} |
||||
|
||||
/*
|
||||
* FACTORY |
||||
*/ |
||||
|
||||
static void dns_ares_factory_ref(grpc_resolver_factory *factory) {} |
||||
|
||||
static void dns_ares_factory_unref(grpc_resolver_factory *factory) {} |
||||
|
||||
static grpc_resolver *dns_factory_create_resolver( |
||||
grpc_exec_ctx *exec_ctx, grpc_resolver_factory *factory, |
||||
grpc_resolver_args *args) { |
||||
return dns_ares_create(exec_ctx, args, "https"); |
||||
} |
||||
|
||||
static char *dns_ares_factory_get_default_host_name( |
||||
grpc_resolver_factory *factory, grpc_uri *uri) { |
||||
const char *path = uri->path; |
||||
if (path[0] == '/') ++path; |
||||
return gpr_strdup(path); |
||||
} |
||||
|
||||
static const grpc_resolver_factory_vtable dns_ares_factory_vtable = { |
||||
dns_ares_factory_ref, dns_ares_factory_unref, dns_factory_create_resolver, |
||||
dns_ares_factory_get_default_host_name, "dns"}; |
||||
static grpc_resolver_factory dns_resolver_factory = {&dns_ares_factory_vtable}; |
||||
|
||||
static grpc_resolver_factory *dns_ares_resolver_factory_create() { |
||||
return &dns_resolver_factory; |
||||
} |
||||
|
||||
void grpc_resolver_dns_ares_init(void) { |
||||
char *resolver = gpr_getenv("GRPC_DNS_RESOLVER"); |
||||
/* TODO(zyc): Turn on c-ares based resolver by default after the address
|
||||
sorter and the CNAME support are added. */ |
||||
if (resolver != NULL && gpr_stricmp(resolver, "ares") == 0) { |
||||
grpc_error *error = grpc_ares_init(); |
||||
if (error != GRPC_ERROR_NONE) { |
||||
GRPC_LOG_IF_ERROR("ares_library_init() failed", error); |
||||
return; |
||||
} |
||||
grpc_resolve_address = grpc_resolve_address_ares; |
||||
grpc_register_resolver_type(dns_ares_resolver_factory_create()); |
||||
} |
||||
gpr_free(resolver); |
||||
} |
||||
|
||||
void grpc_resolver_dns_ares_shutdown(void) { |
||||
char *resolver = gpr_getenv("GRPC_DNS_RESOLVER"); |
||||
if (resolver != NULL && gpr_stricmp(resolver, "ares") == 0) { |
||||
grpc_ares_cleanup(); |
||||
} |
||||
gpr_free(resolver); |
||||
} |
||||
|
||||
#else /* GRPC_ARES == 1 && !defined(GRPC_UV) */ |
||||
|
||||
void grpc_resolver_dns_ares_init(void) {} |
||||
|
||||
void grpc_resolver_dns_ares_shutdown(void) {} |
||||
|
||||
#endif /* GRPC_ARES == 1 && !defined(GRPC_UV) */ |
@ -0,0 +1,65 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2016, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPC_CORE_EXT_RESOLVER_DNS_C_ARES_GRPC_ARES_EV_DRIVER_H |
||||
#define GRPC_CORE_EXT_RESOLVER_DNS_C_ARES_GRPC_ARES_EV_DRIVER_H |
||||
|
||||
#include <ares.h> |
||||
|
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/iomgr/pollset_set.h" |
||||
|
||||
typedef struct grpc_ares_ev_driver grpc_ares_ev_driver; |
||||
|
||||
/* Start \a ev_driver. It will keep working until all IO on its ares_channel is
|
||||
done, or grpc_ares_ev_driver_destroy() is called. It may notify the callbacks |
||||
bound to its ares_channel when necessary. */ |
||||
void grpc_ares_ev_driver_start(grpc_exec_ctx *exec_ctx, |
||||
grpc_ares_ev_driver *ev_driver); |
||||
|
||||
/* Returns the ares_channel owned by \a ev_driver. To bind a c-ares query to
|
||||
\a ev_driver, use the ares_channel owned by \a ev_driver as the arg of the |
||||
query. */ |
||||
ares_channel *grpc_ares_ev_driver_get_channel(grpc_ares_ev_driver *ev_driver); |
||||
|
||||
/* Creates a new grpc_ares_ev_driver. Returns GRPC_ERROR_NONE if \a ev_driver is
|
||||
created successfully. */ |
||||
grpc_error *grpc_ares_ev_driver_create(grpc_ares_ev_driver **ev_driver, |
||||
grpc_pollset_set *pollset_set); |
||||
|
||||
/* Destroys \a ev_driver asynchronously. Pending lookups made on \a ev_driver
|
||||
will be cancelled and their on_done callbacks will be invoked with a status |
||||
of ARES_ECANCELLED. */ |
||||
void grpc_ares_ev_driver_destroy(grpc_ares_ev_driver *ev_driver); |
||||
|
||||
#endif /* GRPC_CORE_EXT_RESOLVER_DNS_C_ARES_GRPC_ARES_EV_DRIVER_H */ |
@ -0,0 +1,319 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2016, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
#include <grpc/support/port_platform.h> |
||||
#include "src/core/lib/iomgr/port.h" |
||||
#if GRPC_ARES == 1 && defined(GRPC_POSIX_SOCKET) |
||||
|
||||
#include "src/core/ext/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 <grpc/support/useful.h> |
||||
#include "src/core/ext/resolver/dns/c_ares/grpc_ares_wrapper.h" |
||||
#include "src/core/lib/iomgr/ev_posix.h" |
||||
#include "src/core/lib/iomgr/iomgr_internal.h" |
||||
#include "src/core/lib/iomgr/sockaddr_utils.h" |
||||
#include "src/core/lib/support/string.h" |
||||
|
||||
typedef struct fd_node { |
||||
/** the owner of this fd node */ |
||||
grpc_ares_ev_driver *ev_driver; |
||||
/** the grpc_fd owned by this fd node */ |
||||
grpc_fd *grpc_fd; |
||||
/** a closure wrapping on_readable_cb, which should be invoked when the
|
||||
grpc_fd in this node becomes readable. */ |
||||
grpc_closure read_closure; |
||||
/** a closure wrapping on_writable_cb, which should be invoked when the
|
||||
grpc_fd in this node becomes writable. */ |
||||
grpc_closure write_closure; |
||||
/** next fd node in the list */ |
||||
struct fd_node *next; |
||||
|
||||
/** mutex guarding the rest of the state */ |
||||
gpr_mu mu; |
||||
/** if the readable closure has been registered */ |
||||
bool readable_registered; |
||||
/** if the writable closure has been registered */ |
||||
bool writable_registered; |
||||
} fd_node; |
||||
|
||||
struct grpc_ares_ev_driver { |
||||
/** the ares_channel owned by this event driver */ |
||||
ares_channel channel; |
||||
/** pollset set for driving the IO events of the channel */ |
||||
grpc_pollset_set *pollset_set; |
||||
/** refcount of the event driver */ |
||||
gpr_refcount refs; |
||||
|
||||
/** mutex guarding the rest of the state */ |
||||
gpr_mu mu; |
||||
/** a list of grpc_fd that this event driver is currently using. */ |
||||
fd_node *fds; |
||||
/** is this event driver currently working? */ |
||||
bool working; |
||||
/** is this event driver being shut down */ |
||||
bool shutting_down; |
||||
}; |
||||
|
||||
static void grpc_ares_notify_on_event_locked(grpc_exec_ctx *exec_ctx, |
||||
grpc_ares_ev_driver *ev_driver); |
||||
|
||||
static grpc_ares_ev_driver *grpc_ares_ev_driver_ref( |
||||
grpc_ares_ev_driver *ev_driver) { |
||||
gpr_log(GPR_DEBUG, "Ref ev_driver %" PRIuPTR, (uintptr_t)ev_driver); |
||||
gpr_ref(&ev_driver->refs); |
||||
return ev_driver; |
||||
} |
||||
|
||||
static void grpc_ares_ev_driver_unref(grpc_ares_ev_driver *ev_driver) { |
||||
gpr_log(GPR_DEBUG, "Unref ev_driver %" PRIuPTR, (uintptr_t)ev_driver); |
||||
if (gpr_unref(&ev_driver->refs)) { |
||||
gpr_log(GPR_DEBUG, "destroy ev_driver %" PRIuPTR, (uintptr_t)ev_driver); |
||||
GPR_ASSERT(ev_driver->fds == NULL); |
||||
gpr_mu_destroy(&ev_driver->mu); |
||||
ares_destroy(ev_driver->channel); |
||||
gpr_free(ev_driver); |
||||
} |
||||
} |
||||
|
||||
static void fd_node_destroy(grpc_exec_ctx *exec_ctx, fd_node *fdn) { |
||||
gpr_log(GPR_DEBUG, "delete fd: %d", grpc_fd_wrapped_fd(fdn->grpc_fd)); |
||||
GPR_ASSERT(!fdn->readable_registered); |
||||
GPR_ASSERT(!fdn->writable_registered); |
||||
gpr_mu_destroy(&fdn->mu); |
||||
grpc_pollset_set_del_fd(exec_ctx, fdn->ev_driver->pollset_set, fdn->grpc_fd); |
||||
grpc_fd_shutdown(exec_ctx, fdn->grpc_fd, |
||||
GRPC_ERROR_CREATE_FROM_STATIC_STRING("fd node destroyed")); |
||||
grpc_fd_orphan(exec_ctx, fdn->grpc_fd, NULL, NULL, "c-ares query finished"); |
||||
gpr_free(fdn); |
||||
} |
||||
|
||||
grpc_error *grpc_ares_ev_driver_create(grpc_ares_ev_driver **ev_driver, |
||||
grpc_pollset_set *pollset_set) { |
||||
*ev_driver = gpr_malloc(sizeof(grpc_ares_ev_driver)); |
||||
int status = ares_init(&(*ev_driver)->channel); |
||||
gpr_log(GPR_DEBUG, "grpc_ares_ev_driver_create"); |
||||
if (status != ARES_SUCCESS) { |
||||
char *err_msg; |
||||
gpr_asprintf(&err_msg, "Failed to init ares channel. C-ares error: %s", |
||||
ares_strerror(status)); |
||||
grpc_error *err = GRPC_ERROR_CREATE_FROM_COPIED_STRING(err_msg); |
||||
gpr_free(err_msg); |
||||
gpr_free(*ev_driver); |
||||
return err; |
||||
} |
||||
gpr_mu_init(&(*ev_driver)->mu); |
||||
gpr_ref_init(&(*ev_driver)->refs, 1); |
||||
(*ev_driver)->pollset_set = pollset_set; |
||||
(*ev_driver)->fds = NULL; |
||||
(*ev_driver)->working = false; |
||||
(*ev_driver)->shutting_down = false; |
||||
return GRPC_ERROR_NONE; |
||||
} |
||||
|
||||
void grpc_ares_ev_driver_destroy(grpc_ares_ev_driver *ev_driver) { |
||||
// It's not safe to shut down remaining fds here directly, becauses
|
||||
// ares_host_callback does not provide an exec_ctx. We mark the event driver
|
||||
// as being shut down. If the event driver is working,
|
||||
// grpc_ares_notify_on_event_locked will shut down the fds; if it's not
|
||||
// working, there are no fds to shut down.
|
||||
gpr_mu_lock(&ev_driver->mu); |
||||
ev_driver->shutting_down = true; |
||||
gpr_mu_unlock(&ev_driver->mu); |
||||
grpc_ares_ev_driver_unref(ev_driver); |
||||
} |
||||
|
||||
// Search fd in the fd_node list head. This is an O(n) search, the max possible
|
||||
// value of n is ARES_GETSOCK_MAXNUM (16). n is typically 1 - 2 in our tests.
|
||||
static fd_node *pop_fd_node(fd_node **head, int fd) { |
||||
fd_node dummy_head; |
||||
dummy_head.next = *head; |
||||
fd_node *node = &dummy_head; |
||||
while (node->next != NULL) { |
||||
if (grpc_fd_wrapped_fd(node->next->grpc_fd) == fd) { |
||||
fd_node *ret = node->next; |
||||
node->next = node->next->next; |
||||
*head = dummy_head.next; |
||||
return ret; |
||||
} |
||||
node = node->next; |
||||
} |
||||
return NULL; |
||||
} |
||||
|
||||
static void on_readable_cb(grpc_exec_ctx *exec_ctx, void *arg, |
||||
grpc_error *error) { |
||||
fd_node *fdn = arg; |
||||
grpc_ares_ev_driver *ev_driver = fdn->ev_driver; |
||||
gpr_mu_lock(&fdn->mu); |
||||
fdn->readable_registered = false; |
||||
gpr_mu_unlock(&fdn->mu); |
||||
|
||||
gpr_log(GPR_DEBUG, "readable on %d", grpc_fd_wrapped_fd(fdn->grpc_fd)); |
||||
if (error == GRPC_ERROR_NONE) { |
||||
ares_process_fd(ev_driver->channel, grpc_fd_wrapped_fd(fdn->grpc_fd), |
||||
ARES_SOCKET_BAD); |
||||
} else { |
||||
// If error is not GRPC_ERROR_NONE, it means the fd has been shutdown or
|
||||
// timed out. The pending lookups made on this ev_driver will be cancelled
|
||||
// by the following ares_cancel() and the on_done callbacks will be invoked
|
||||
// with a status of ARES_ECANCELLED. The remaining file descriptors in this
|
||||
// ev_driver will be cleaned up in the follwing
|
||||
// grpc_ares_notify_on_event_locked().
|
||||
ares_cancel(ev_driver->channel); |
||||
} |
||||
gpr_mu_lock(&ev_driver->mu); |
||||
grpc_ares_notify_on_event_locked(exec_ctx, ev_driver); |
||||
gpr_mu_unlock(&ev_driver->mu); |
||||
grpc_ares_ev_driver_unref(ev_driver); |
||||
} |
||||
|
||||
static void on_writable_cb(grpc_exec_ctx *exec_ctx, void *arg, |
||||
grpc_error *error) { |
||||
fd_node *fdn = arg; |
||||
grpc_ares_ev_driver *ev_driver = fdn->ev_driver; |
||||
gpr_mu_lock(&fdn->mu); |
||||
fdn->writable_registered = false; |
||||
gpr_mu_unlock(&fdn->mu); |
||||
|
||||
gpr_log(GPR_DEBUG, "writable on %d", grpc_fd_wrapped_fd(fdn->grpc_fd)); |
||||
if (error == GRPC_ERROR_NONE) { |
||||
ares_process_fd(ev_driver->channel, ARES_SOCKET_BAD, |
||||
grpc_fd_wrapped_fd(fdn->grpc_fd)); |
||||
} else { |
||||
// If error is not GRPC_ERROR_NONE, it means the fd has been shutdown or
|
||||
// timed out. The pending lookups made on this ev_driver will be cancelled
|
||||
// by the following ares_cancel() and the on_done callbacks will be invoked
|
||||
// with a status of ARES_ECANCELLED. The remaining file descriptors in this
|
||||
// ev_driver will be cleaned up in the follwing
|
||||
// grpc_ares_notify_on_event_locked().
|
||||
ares_cancel(ev_driver->channel); |
||||
} |
||||
gpr_mu_lock(&ev_driver->mu); |
||||
grpc_ares_notify_on_event_locked(exec_ctx, ev_driver); |
||||
gpr_mu_unlock(&ev_driver->mu); |
||||
grpc_ares_ev_driver_unref(ev_driver); |
||||
} |
||||
|
||||
ares_channel *grpc_ares_ev_driver_get_channel(grpc_ares_ev_driver *ev_driver) { |
||||
return &ev_driver->channel; |
||||
} |
||||
|
||||
// Get the file descriptors used by the ev_driver's ares channel, register
|
||||
// driver_closure with these filedescriptors.
|
||||
static void grpc_ares_notify_on_event_locked(grpc_exec_ctx *exec_ctx, |
||||
grpc_ares_ev_driver *ev_driver) { |
||||
fd_node *new_list = NULL; |
||||
if (!ev_driver->shutting_down) { |
||||
ares_socket_t socks[ARES_GETSOCK_MAXNUM]; |
||||
int socks_bitmask = |
||||
ares_getsock(ev_driver->channel, socks, ARES_GETSOCK_MAXNUM); |
||||
for (size_t i = 0; i < ARES_GETSOCK_MAXNUM; i++) { |
||||
if (ARES_GETSOCK_READABLE(socks_bitmask, i) || |
||||
ARES_GETSOCK_WRITABLE(socks_bitmask, i)) { |
||||
fd_node *fdn = pop_fd_node(&ev_driver->fds, socks[i]); |
||||
// Create a new fd_node if sock[i] is not in the fd_node list.
|
||||
if (fdn == NULL) { |
||||
char *fd_name; |
||||
gpr_asprintf(&fd_name, "ares_ev_driver-%" PRIuPTR, i); |
||||
fdn = gpr_malloc(sizeof(fd_node)); |
||||
gpr_log(GPR_DEBUG, "new fd: %d", socks[i]); |
||||
fdn->grpc_fd = grpc_fd_create(socks[i], fd_name); |
||||
fdn->ev_driver = ev_driver; |
||||
fdn->readable_registered = false; |
||||
fdn->writable_registered = false; |
||||
gpr_mu_init(&fdn->mu); |
||||
grpc_closure_init(&fdn->read_closure, on_readable_cb, fdn, |
||||
grpc_schedule_on_exec_ctx); |
||||
grpc_closure_init(&fdn->write_closure, on_writable_cb, fdn, |
||||
grpc_schedule_on_exec_ctx); |
||||
grpc_pollset_set_add_fd(exec_ctx, ev_driver->pollset_set, |
||||
fdn->grpc_fd); |
||||
gpr_free(fd_name); |
||||
} |
||||
fdn->next = new_list; |
||||
new_list = fdn; |
||||
gpr_mu_lock(&fdn->mu); |
||||
// Register read_closure if the socket is readable and read_closure has
|
||||
// not been registered with this socket.
|
||||
if (ARES_GETSOCK_READABLE(socks_bitmask, i) && |
||||
!fdn->readable_registered) { |
||||
grpc_ares_ev_driver_ref(ev_driver); |
||||
gpr_log(GPR_DEBUG, "notify read on: %d", |
||||
grpc_fd_wrapped_fd(fdn->grpc_fd)); |
||||
grpc_fd_notify_on_read(exec_ctx, fdn->grpc_fd, &fdn->read_closure); |
||||
fdn->readable_registered = true; |
||||
} |
||||
// Register write_closure if the socket is writable and write_closure
|
||||
// has not been registered with this socket.
|
||||
if (ARES_GETSOCK_WRITABLE(socks_bitmask, i) && |
||||
!fdn->writable_registered) { |
||||
gpr_log(GPR_DEBUG, "notify write on: %d", |
||||
grpc_fd_wrapped_fd(fdn->grpc_fd)); |
||||
grpc_ares_ev_driver_ref(ev_driver); |
||||
grpc_fd_notify_on_write(exec_ctx, fdn->grpc_fd, &fdn->write_closure); |
||||
fdn->writable_registered = true; |
||||
} |
||||
gpr_mu_unlock(&fdn->mu); |
||||
} |
||||
} |
||||
} |
||||
// Any remaining fds in ev_driver->fds were not returned by ares_getsock() and
|
||||
// are therefore no longer in use, so they can be shut down and removed from
|
||||
// the list.
|
||||
while (ev_driver->fds != NULL) { |
||||
fd_node *cur = ev_driver->fds; |
||||
ev_driver->fds = ev_driver->fds->next; |
||||
fd_node_destroy(exec_ctx, cur); |
||||
} |
||||
ev_driver->fds = new_list; |
||||
// If the ev driver has no working fd, all the tasks are done.
|
||||
if (new_list == NULL) { |
||||
ev_driver->working = false; |
||||
gpr_log(GPR_DEBUG, "ev driver stop working"); |
||||
} |
||||
} |
||||
|
||||
void grpc_ares_ev_driver_start(grpc_exec_ctx *exec_ctx, |
||||
grpc_ares_ev_driver *ev_driver) { |
||||
gpr_mu_lock(&ev_driver->mu); |
||||
if (!ev_driver->working) { |
||||
ev_driver->working = true; |
||||
grpc_ares_notify_on_event_locked(exec_ctx, ev_driver); |
||||
} |
||||
gpr_mu_unlock(&ev_driver->mu); |
||||
} |
||||
|
||||
#endif /* GRPC_ARES == 1 && defined(GRPC_POSIX_SOCKET) */ |
@ -0,0 +1,289 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2016, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
#if GRPC_ARES == 1 && !defined(GRPC_UV) |
||||
|
||||
#include "src/core/ext/resolver/dns/c_ares/grpc_ares_wrapper.h" |
||||
#include "src/core/lib/iomgr/sockaddr.h" |
||||
#include "src/core/lib/iomgr/socket_utils_posix.h" |
||||
|
||||
#include <string.h> |
||||
#include <sys/types.h> |
||||
|
||||
#include <ares.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/host_port.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/time.h> |
||||
#include <grpc/support/useful.h> |
||||
#include "src/core/ext/resolver/dns/c_ares/grpc_ares_ev_driver.h" |
||||
#include "src/core/lib/iomgr/executor.h" |
||||
#include "src/core/lib/iomgr/iomgr_internal.h" |
||||
#include "src/core/lib/iomgr/sockaddr_utils.h" |
||||
#include "src/core/lib/support/string.h" |
||||
|
||||
static gpr_once g_basic_init = GPR_ONCE_INIT; |
||||
static gpr_mu g_init_mu; |
||||
|
||||
typedef struct grpc_ares_request { |
||||
/** following members are set in grpc_resolve_address_ares_impl */ |
||||
/** host to resolve, parsed from the name to resolve */ |
||||
char *host; |
||||
/** port to fill in sockaddr_in, parsed from the name to resolve */ |
||||
char *port; |
||||
/** default port to use */ |
||||
char *default_port; |
||||
/** closure to call when the request completes */ |
||||
grpc_closure *on_done; |
||||
/** the pointer to receive the resolved addresses */ |
||||
grpc_resolved_addresses **addrs_out; |
||||
/** the evernt driver used by this request */ |
||||
grpc_ares_ev_driver *ev_driver; |
||||
/** number of ongoing queries */ |
||||
gpr_refcount pending_queries; |
||||
|
||||
/** mutex guarding the rest of the state */ |
||||
gpr_mu mu; |
||||
/** is there at least one successful query, set in on_done_cb */ |
||||
bool success; |
||||
/** the errors explaining the request failure, set in on_done_cb */ |
||||
grpc_error *error; |
||||
} grpc_ares_request; |
||||
|
||||
static void do_basic_init(void) { gpr_mu_init(&g_init_mu); } |
||||
|
||||
static uint16_t strhtons(const char *port) { |
||||
if (strcmp(port, "http") == 0) { |
||||
return htons(80); |
||||
} else if (strcmp(port, "https") == 0) { |
||||
return htons(443); |
||||
} |
||||
return htons((unsigned short)atoi(port)); |
||||
} |
||||
|
||||
static void grpc_ares_request_unref(grpc_exec_ctx *exec_ctx, |
||||
grpc_ares_request *r) { |
||||
/* If there are no pending queries, invoke on_done callback and destroy the
|
||||
request */ |
||||
if (gpr_unref(&r->pending_queries)) { |
||||
/* TODO(zyc): Sort results with RFC6724 before invoking on_done. */ |
||||
if (exec_ctx == NULL) { |
||||
/* A new exec_ctx is created here, as the c-ares interface does not
|
||||
provide one in ares_host_callback. It's safe to schedule on_done with |
||||
the newly created exec_ctx, since the caller has been warned not to |
||||
acquire locks in on_done. ares_dns_resolver is using combiner to |
||||
protect resources needed by on_done. */ |
||||
grpc_exec_ctx new_exec_ctx = GRPC_EXEC_CTX_INIT; |
||||
grpc_closure_sched(&new_exec_ctx, r->on_done, r->error); |
||||
grpc_exec_ctx_finish(&new_exec_ctx); |
||||
} else { |
||||
grpc_closure_sched(exec_ctx, r->on_done, r->error); |
||||
} |
||||
gpr_mu_destroy(&r->mu); |
||||
grpc_ares_ev_driver_destroy(r->ev_driver); |
||||
gpr_free(r->host); |
||||
gpr_free(r->port); |
||||
gpr_free(r->default_port); |
||||
gpr_free(r); |
||||
} |
||||
} |
||||
|
||||
static void on_done_cb(void *arg, int status, int timeouts, |
||||
struct hostent *hostent) { |
||||
grpc_ares_request *r = (grpc_ares_request *)arg; |
||||
gpr_mu_lock(&r->mu); |
||||
if (status == ARES_SUCCESS) { |
||||
GRPC_ERROR_UNREF(r->error); |
||||
r->error = GRPC_ERROR_NONE; |
||||
r->success = true; |
||||
grpc_resolved_addresses **addresses = r->addrs_out; |
||||
if (*addresses == NULL) { |
||||
*addresses = gpr_malloc(sizeof(grpc_resolved_addresses)); |
||||
(*addresses)->naddrs = 0; |
||||
(*addresses)->addrs = NULL; |
||||
} |
||||
size_t prev_naddr = (*addresses)->naddrs; |
||||
size_t i; |
||||
for (i = 0; hostent->h_addr_list[i] != NULL; i++) { |
||||
} |
||||
(*addresses)->naddrs += i; |
||||
(*addresses)->addrs = |
||||
gpr_realloc((*addresses)->addrs, |
||||
sizeof(grpc_resolved_address) * (*addresses)->naddrs); |
||||
for (i = prev_naddr; i < (*addresses)->naddrs; i++) { |
||||
memset(&(*addresses)->addrs[i], 0, sizeof(grpc_resolved_address)); |
||||
if (hostent->h_addrtype == AF_INET6) { |
||||
(*addresses)->addrs[i].len = sizeof(struct sockaddr_in6); |
||||
struct sockaddr_in6 *addr = |
||||
(struct sockaddr_in6 *)&(*addresses)->addrs[i].addr; |
||||
addr->sin6_family = (sa_family_t)hostent->h_addrtype; |
||||
addr->sin6_port = strhtons(r->port); |
||||
|
||||
char output[INET6_ADDRSTRLEN]; |
||||
memcpy(&addr->sin6_addr, hostent->h_addr_list[i - prev_naddr], |
||||
sizeof(struct in6_addr)); |
||||
ares_inet_ntop(AF_INET6, &addr->sin6_addr, output, INET6_ADDRSTRLEN); |
||||
gpr_log(GPR_DEBUG, |
||||
"c-ares resolver gets a AF_INET6 result: \n" |
||||
" addr: %s\n port: %s\n sin6_scope_id: %d\n", |
||||
output, r->port, addr->sin6_scope_id); |
||||
} else { |
||||
(*addresses)->addrs[i].len = sizeof(struct sockaddr_in); |
||||
struct sockaddr_in *addr = |
||||
(struct sockaddr_in *)&(*addresses)->addrs[i].addr; |
||||
memcpy(&addr->sin_addr, hostent->h_addr_list[i - prev_naddr], |
||||
sizeof(struct in_addr)); |
||||
addr->sin_family = (sa_family_t)hostent->h_addrtype; |
||||
addr->sin_port = strhtons(r->port); |
||||
|
||||
char output[INET_ADDRSTRLEN]; |
||||
ares_inet_ntop(AF_INET, &addr->sin_addr, output, INET_ADDRSTRLEN); |
||||
gpr_log(GPR_DEBUG, |
||||
"c-ares resolver gets a AF_INET result: \n" |
||||
" addr: %s\n port: %s\n", |
||||
output, r->port); |
||||
} |
||||
} |
||||
} else if (!r->success) { |
||||
char *error_msg; |
||||
gpr_asprintf(&error_msg, "C-ares status is not ARES_SUCCESS: %s", |
||||
ares_strerror(status)); |
||||
grpc_error *error = GRPC_ERROR_CREATE_FROM_COPIED_STRING(error_msg); |
||||
gpr_free(error_msg); |
||||
if (r->error == GRPC_ERROR_NONE) { |
||||
r->error = error; |
||||
} else { |
||||
r->error = grpc_error_add_child(error, r->error); |
||||
} |
||||
} |
||||
gpr_mu_unlock(&r->mu); |
||||
grpc_ares_request_unref(NULL, r); |
||||
} |
||||
|
||||
void grpc_resolve_address_ares_impl(grpc_exec_ctx *exec_ctx, const char *name, |
||||
const char *default_port, |
||||
grpc_pollset_set *interested_parties, |
||||
grpc_closure *on_done, |
||||
grpc_resolved_addresses **addrs) { |
||||
/* TODO(zyc): Enable tracing after #9603 is checked in */ |
||||
/* if (grpc_dns_trace) {
|
||||
gpr_log(GPR_DEBUG, "resolve_address (blocking): name=%s, default_port=%s", |
||||
name, default_port); |
||||
} */ |
||||
|
||||
/* parse name, splitting it into host and port parts */ |
||||
char *host; |
||||
char *port; |
||||
gpr_split_host_port(name, &host, &port); |
||||
if (host == NULL) { |
||||
grpc_error *err = grpc_error_set_str( |
||||
GRPC_ERROR_CREATE_FROM_STATIC_STRING("unparseable host:port"), |
||||
GRPC_ERROR_STR_TARGET_ADDRESS, grpc_slice_from_copied_string(name)); |
||||
grpc_closure_sched(exec_ctx, on_done, err); |
||||
goto error_cleanup; |
||||
} else if (port == NULL) { |
||||
if (default_port == NULL) { |
||||
grpc_error *err = grpc_error_set_str( |
||||
GRPC_ERROR_CREATE_FROM_STATIC_STRING("no port in name"), |
||||
GRPC_ERROR_STR_TARGET_ADDRESS, grpc_slice_from_copied_string(name)); |
||||
grpc_closure_sched(exec_ctx, on_done, err); |
||||
goto error_cleanup; |
||||
} |
||||
port = gpr_strdup(default_port); |
||||
} |
||||
|
||||
grpc_ares_ev_driver *ev_driver; |
||||
grpc_error *err = grpc_ares_ev_driver_create(&ev_driver, interested_parties); |
||||
if (err != GRPC_ERROR_NONE) { |
||||
GRPC_LOG_IF_ERROR("grpc_ares_ev_driver_create() failed", err); |
||||
goto error_cleanup; |
||||
} |
||||
|
||||
grpc_ares_request *r = gpr_malloc(sizeof(grpc_ares_request)); |
||||
gpr_mu_init(&r->mu); |
||||
r->ev_driver = ev_driver; |
||||
r->on_done = on_done; |
||||
r->addrs_out = addrs; |
||||
r->default_port = gpr_strdup(default_port); |
||||
r->port = port; |
||||
r->host = host; |
||||
r->success = false; |
||||
r->error = GRPC_ERROR_NONE; |
||||
ares_channel *channel = grpc_ares_ev_driver_get_channel(r->ev_driver); |
||||
gpr_ref_init(&r->pending_queries, 2); |
||||
if (grpc_ipv6_loopback_available()) { |
||||
gpr_ref(&r->pending_queries); |
||||
ares_gethostbyname(*channel, r->host, AF_INET6, on_done_cb, r); |
||||
} |
||||
ares_gethostbyname(*channel, r->host, AF_INET, on_done_cb, r); |
||||
/* TODO(zyc): Handle CNAME records here. */ |
||||
grpc_ares_ev_driver_start(exec_ctx, r->ev_driver); |
||||
grpc_ares_request_unref(exec_ctx, r); |
||||
return; |
||||
|
||||
error_cleanup: |
||||
gpr_free(host); |
||||
gpr_free(port); |
||||
} |
||||
|
||||
void (*grpc_resolve_address_ares)( |
||||
grpc_exec_ctx *exec_ctx, const char *name, const char *default_port, |
||||
grpc_pollset_set *interested_parties, grpc_closure *on_done, |
||||
grpc_resolved_addresses **addrs) = grpc_resolve_address_ares_impl; |
||||
|
||||
grpc_error *grpc_ares_init(void) { |
||||
gpr_once_init(&g_basic_init, do_basic_init); |
||||
gpr_mu_lock(&g_init_mu); |
||||
int status = ares_library_init(ARES_LIB_INIT_ALL); |
||||
gpr_mu_unlock(&g_init_mu); |
||||
|
||||
if (status != ARES_SUCCESS) { |
||||
char *error_msg; |
||||
gpr_asprintf(&error_msg, "ares_library_init failed: %s", |
||||
ares_strerror(status)); |
||||
grpc_error *error = GRPC_ERROR_CREATE_FROM_COPIED_STRING(error_msg); |
||||
gpr_free(error_msg); |
||||
return error; |
||||
} |
||||
return GRPC_ERROR_NONE; |
||||
} |
||||
|
||||
void grpc_ares_cleanup(void) { |
||||
gpr_mu_lock(&g_init_mu); |
||||
ares_library_cleanup(); |
||||
gpr_mu_unlock(&g_init_mu); |
||||
} |
||||
|
||||
#endif /* GRPC_ARES == 1 && !defined(GRPC_UV) */ |
@ -0,0 +1,63 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2016, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPC_CORE_EXT_RESOLVER_DNS_C_ARES_GRPC_ARES_WRAPPER_H |
||||
#define GRPC_CORE_EXT_RESOLVER_DNS_C_ARES_GRPC_ARES_WRAPPER_H |
||||
|
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/iomgr/iomgr.h" |
||||
#include "src/core/lib/iomgr/polling_entity.h" |
||||
#include "src/core/lib/iomgr/resolve_address.h" |
||||
|
||||
/* Asynchronously resolve addr. Use \a default_port if a port isn't designated
|
||||
in addr, otherwise use the port in addr. grpc_ares_init() must be called at |
||||
least once before this function. \a on_done may be called directly in this |
||||
function without being scheduled with \a exec_ctx, it must not try to acquire |
||||
locks that are being held by the caller. */ |
||||
extern void (*grpc_resolve_address_ares)(grpc_exec_ctx *exec_ctx, |
||||
const char *addr, |
||||
const char *default_port, |
||||
grpc_pollset_set *interested_parties, |
||||
grpc_closure *on_done, |
||||
grpc_resolved_addresses **addresses); |
||||
|
||||
/* Initialize gRPC ares wrapper. Must be called at least once before
|
||||
grpc_resolve_address_ares(). */ |
||||
grpc_error *grpc_ares_init(void); |
||||
|
||||
/* Uninitialized gRPC ares wrapper. If there was more than one previous call to
|
||||
grpc_ares_init(), this function uninitializes the gRPC ares wrapper only if |
||||
it has been called the same number of times as grpc_ares_init(). */ |
||||
void grpc_ares_cleanup(void); |
||||
|
||||
#endif /* GRPC_CORE_EXT_RESOLVER_DNS_C_ARES_GRPC_ARES_WRAPPER_H */ |
@ -0,0 +1,61 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#ifdef GRPC_TEST_OBJC |
||||
|
||||
#import "../GRPCCall.h" |
||||
|
||||
/**
|
||||
* Methods used for gRPC internal tests. DO NOT USE. |
||||
*/ |
||||
@interface GRPCCall (InternalTests) |
||||
|
||||
/**
|
||||
* Enables logging of op batches. Memory consumption increases as more ops are logged. |
||||
* |
||||
* This function is for internal testing of gRPC only. It is not part of gRPC's public interface. |
||||
* Do not use in production. To enable, set the preprocessor flag GRPC_TEST_OBJC. |
||||
*/ |
||||
+ (void)enableOpBatchLog:(BOOL)enabled; |
||||
|
||||
/**
|
||||
* Obtain the logged op batches. Invoking this method will clean the log. |
||||
* |
||||
* This function is for internal testing of gRPC only. It is not part of gRPC's public interface. |
||||
* Do not use in production. To enable, set the preprocessor flag GRPC_TEST_OBJC. |
||||
*/ |
||||
+ (NSArray *)obtainAndCleanOpBatchLog; |
||||
|
||||
@end |
||||
|
||||
#endif |
@ -0,0 +1,52 @@ |
||||
/* |
||||
* |
||||
* Copyright 2017, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#ifdef GRPC_TEST_OBJC |
||||
|
||||
#import "GRPCCall+InternalTests.h" |
||||
|
||||
#import "../private/GRPCOpBatchLog.h" |
||||
|
||||
@implementation GRPCCall (InternalTests) |
||||
|
||||
+ (void)enableOpBatchLog:(BOOL)enabled { |
||||
[GRPCOpBatchLog enableOpBatchLog:enabled]; |
||||
} |
||||
|
||||
+ (NSArray *)obtainAndCleanOpBatchLog { |
||||
return [GRPCOpBatchLog obtainAndCleanOpBatchLog]; |
||||
} |
||||
|
||||
@end |
||||
|
||||
#endif |
@ -0,0 +1,59 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2016, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
|
||||
#ifdef GRPC_TEST_OBJC |
||||
|
||||
/**
|
||||
* Logs the op batches of a client. Used for testing. |
||||
*/ |
||||
@interface GRPCOpBatchLog : NSObject |
||||
|
||||
/**
|
||||
* Enables logging of op batches. Memory consumption increases as more ops are logged. |
||||
*/ |
||||
+ (void)enableOpBatchLog:(BOOL)enabled; |
||||
|
||||
/**
|
||||
* Add an op batch to log. |
||||
*/ |
||||
+ (void)addOpBatchToLog:(NSArray *)batch; |
||||
|
||||
/**
|
||||
* Obtain the logged op batches. Invoking this method will clean the log. |
||||
*/ |
||||
+ (NSArray *)obtainAndCleanOpBatchLog; |
||||
|
||||
@end |
||||
|
||||
#endif |
@ -0,0 +1,72 @@ |
||||
/* |
||||
* |
||||
* Copyright 2016, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#ifdef GRPC_TEST_OBJC |
||||
|
||||
#import "GRPCOpBatchLog.h" |
||||
|
||||
static NSMutableArray *opBatchLog = nil; |
||||
|
||||
@implementation GRPCOpBatchLog |
||||
|
||||
+ (void)enableOpBatchLog:(BOOL)enabled { |
||||
@synchronized (opBatchLog) { |
||||
if (enabled) { |
||||
if (!opBatchLog) { |
||||
opBatchLog = [NSMutableArray array]; |
||||
} |
||||
} else { |
||||
if (opBatchLog) { |
||||
opBatchLog = nil; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
+ (void)addOpBatchToLog:(NSArray *)batch { |
||||
@synchronized (opBatchLog) { |
||||
[opBatchLog addObject:batch]; |
||||
} |
||||
} |
||||
|
||||
+ (NSArray *)obtainAndCleanOpBatchLog { |
||||
@synchronized (opBatchLog) { |
||||
NSArray *out = opBatchLog; |
||||
opBatchLog = [NSMutableArray array]; |
||||
return out; |
||||
} |
||||
} |
||||
|
||||
@end |
||||
|
||||
#endif |
@ -0,0 +1,50 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2016, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#import <Foundation/Foundation.h> |
||||
|
||||
#import "GRXImmediateWriter.h" |
||||
|
||||
/**
|
||||
* Utility to construct GRXWriter instances from values that are immediately available when |
||||
* required. |
||||
*/ |
||||
@interface GRXImmediateSingleWriter : GRXImmediateWriter |
||||
|
||||
/**
|
||||
* Returns a writer that sends the passed value to its writeable and then finishes (releasing the |
||||
* value). |
||||
*/ |
||||
+ (GRXWriter *)writerWithValue:(id)value; |
||||
|
||||
@end |
@ -0,0 +1,89 @@ |
||||
/* |
||||
* |
||||
* Copyright 2016, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#import "GRXImmediateSingleWriter.h" |
||||
|
||||
@implementation GRXImmediateSingleWriter { |
||||
id _value; |
||||
id<GRXWriteable> _writeable; |
||||
} |
||||
|
||||
@synthesize state = _state; |
||||
|
||||
- (instancetype)initWithValue:(id)value { |
||||
if (self = [super init]) { |
||||
_value = value; |
||||
_state = GRXWriterStateNotStarted; |
||||
} |
||||
return self; |
||||
} |
||||
|
||||
+ (GRXWriter *)writerWithValue:(id)value { |
||||
return [[self alloc] initWithValue:value]; |
||||
} |
||||
|
||||
- (void)startWithWriteable:(id<GRXWriteable>)writeable { |
||||
_state = GRXWriterStateStarted; |
||||
_writeable = writeable; |
||||
[writeable writeValue:_value]; |
||||
[self finish]; |
||||
} |
||||
|
||||
- (void)finish { |
||||
_state = GRXWriterStateFinished; |
||||
_value = nil; |
||||
id<GRXWriteable> writeable = _writeable; |
||||
_writeable = nil; |
||||
[writeable writesFinishedWithError:nil]; |
||||
} |
||||
|
||||
// Overwrite the setter to disallow manual state transition. The getter |
||||
// of _state is synthesized. |
||||
- (void)setState:(GRXWriterState)newState { |
||||
// Manual state transition is not allowed |
||||
return; |
||||
} |
||||
|
||||
// Overrides [requestWriter(Transformations):map:] for Protocol Buffers |
||||
// encoding. |
||||
// We need the return value of this map to be a GRXImmediateSingleWriter but |
||||
// the original \a map function returns a new Writer of another type. So we |
||||
// need to override this function here. |
||||
- (GRXWriter *)map:(id (^)(id))map { |
||||
// Since _value is available when creating the object, we can simply |
||||
// apply the map and store the output. |
||||
_value = map(_value); |
||||
return self; |
||||
} |
||||
|
||||
@end |
@ -0,0 +1,43 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2016, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include <ares.h> |
||||
|
||||
int main(void) { |
||||
ares_channel channelptr; |
||||
|
||||
ares_init(&channelptr); |
||||
ares_destroy(channelptr); |
||||
|
||||
return 0; |
||||
} |
Binary file not shown.
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue