Skip to content

Commit

Permalink
Merge pull request grpc#16190 from muxi/config-isolation
Browse files Browse the repository at this point in the history
Implement L38: gRPC Objective-C API Upgrade
  • Loading branch information
muxi authored Jan 7, 2019
2 parents 46bd2f7 + b4a9269 commit 36b47ce
Show file tree
Hide file tree
Showing 72 changed files with 6,396 additions and 792 deletions.
2 changes: 1 addition & 1 deletion gRPC.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Pod::Spec.new do |s|

ss.source_files = "#{src_dir}/*.{h,m}", "#{src_dir}/**/*.{h,m}"
ss.exclude_files = "#{src_dir}/GRPCCall+GID.{h,m}"
ss.private_header_files = "#{src_dir}/private/*.h"
ss.private_header_files = "#{src_dir}/private/*.h", "#{src_dir}/internal/*.h"

ss.dependency 'gRPC-Core', version
end
Expand Down
4 changes: 4 additions & 0 deletions include/grpc/impl/codegen/grpc_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ typedef struct {
* is 10000. Setting this to "0" will disable c-ares query timeouts
* entirely. */
#define GRPC_ARG_DNS_ARES_QUERY_TIMEOUT_MS "grpc.dns_ares_query_timeout"
/** gRPC Objective-C channel pooling domain string. */
#define GRPC_ARG_CHANNEL_POOL_DOMAIN "grpc.channel_pooling_domain"
/** gRPC Objective-C channel pooling id. */
#define GRPC_ARG_CHANNEL_ID "grpc.channel_id"
/** \} */

/** Result of a grpc call. If the caller satisfies the prerequisites of a
Expand Down
100 changes: 96 additions & 4 deletions src/compiler/objective_c_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,29 @@ void PrintAdvancedSignature(Printer* printer, const MethodDescriptor* method,
PrintMethodSignature(printer, method, vars);
}

void PrintV2Signature(Printer* printer, const MethodDescriptor* method,
map< ::grpc::string, ::grpc::string> vars) {
if (method->client_streaming()) {
vars["return_type"] = "GRPCStreamingProtoCall *";
} else {
vars["return_type"] = "GRPCUnaryProtoCall *";
}
vars["method_name"] =
grpc_generator::LowercaseFirstLetter(vars["method_name"]);

PrintAllComments(method, printer);

printer->Print(vars, "- ($return_type$)$method_name$With");
if (method->client_streaming()) {
printer->Print("ResponseHandler:(id<GRPCProtoResponseHandler>)handler");
} else {
printer->Print(vars,
"Message:($request_class$ *)message "
"responseHandler:(id<GRPCProtoResponseHandler>)handler");
}
printer->Print(" callOptions:(GRPCCallOptions *_Nullable)callOptions");
}

inline map< ::grpc::string, ::grpc::string> GetMethodVars(
const MethodDescriptor* method) {
map< ::grpc::string, ::grpc::string> res;
Expand All @@ -135,6 +158,16 @@ void PrintMethodDeclarations(Printer* printer, const MethodDescriptor* method) {
printer->Print(";\n\n\n");
}

void PrintV2MethodDeclarations(Printer* printer,
const MethodDescriptor* method) {
map< ::grpc::string, ::grpc::string> vars = GetMethodVars(method);

PrintProtoRpcDeclarationAsPragma(printer, method, vars);

PrintV2Signature(printer, method, vars);
printer->Print(";\n\n");
}

void PrintSimpleImplementation(Printer* printer, const MethodDescriptor* method,
map< ::grpc::string, ::grpc::string> vars) {
printer->Print("{\n");
Expand Down Expand Up @@ -177,19 +210,42 @@ void PrintAdvancedImplementation(Printer* printer,
printer->Print("}\n");
}

void PrintV2Implementation(Printer* printer, const MethodDescriptor* method,
map< ::grpc::string, ::grpc::string> vars) {
printer->Print(" {\n");
if (method->client_streaming()) {
printer->Print(vars, " return [self RPCToMethod:@\"$method_name$\"\n");
printer->Print(" responseHandler:handler\n");
printer->Print(" callOptions:callOptions\n");
printer->Print(
vars, " responseClass:[$response_class$ class]];\n}\n\n");
} else {
printer->Print(vars, " return [self RPCToMethod:@\"$method_name$\"\n");
printer->Print(" message:message\n");
printer->Print(" responseHandler:handler\n");
printer->Print(" callOptions:callOptions\n");
printer->Print(
vars, " responseClass:[$response_class$ class]];\n}\n\n");
}
}

void PrintMethodImplementations(Printer* printer,
const MethodDescriptor* method) {
map< ::grpc::string, ::grpc::string> vars = GetMethodVars(method);

PrintProtoRpcDeclarationAsPragma(printer, method, vars);

// TODO(jcanizales): Print documentation from the method.
printer->Print("// Deprecated methods.\n");
PrintSimpleSignature(printer, method, vars);
PrintSimpleImplementation(printer, method, vars);

printer->Print("// Returns a not-yet-started RPC object.\n");
PrintAdvancedSignature(printer, method, vars);
PrintAdvancedImplementation(printer, method, vars);

PrintV2Signature(printer, method, vars);
PrintV2Implementation(printer, method, vars);
}

} // namespace
Expand Down Expand Up @@ -231,6 +287,25 @@ ::grpc::string GetProtocol(const ServiceDescriptor* service) {
return output;
}

::grpc::string GetV2Protocol(const ServiceDescriptor* service) {
::grpc::string output;

// Scope the output stream so it closes and finalizes output to the string.
grpc::protobuf::io::StringOutputStream output_stream(&output);
Printer printer(&output_stream, '$');

map< ::grpc::string, ::grpc::string> vars = {
{"service_class", ServiceClassName(service) + "2"}};

printer.Print(vars, "@protocol $service_class$ <NSObject>\n\n");
for (int i = 0; i < service->method_count(); i++) {
PrintV2MethodDeclarations(&printer, service->method(i));
}
printer.Print("@end\n\n");

return output;
}

::grpc::string GetInterface(const ServiceDescriptor* service) {
::grpc::string output;

Expand All @@ -248,10 +323,16 @@ ::grpc::string GetInterface(const ServiceDescriptor* service) {
" */\n");
printer.Print(vars,
"@interface $service_class$ :"
" GRPCProtoService<$service_class$>\n");
" GRPCProtoService<$service_class$, $service_class$2>\n");
printer.Print(
"- (instancetype)initWithHost:(NSString *)host"
"- (instancetype)initWithHost:(NSString *)host "
"callOptions:(GRPCCallOptions "
"*_Nullable)callOptions"
" NS_DESIGNATED_INITIALIZER;\n");
printer.Print("- (instancetype)initWithHost:(NSString *)host;\n");
printer.Print(
"+ (instancetype)serviceWithHost:(NSString *)host "
"callOptions:(GRPCCallOptions *_Nullable)callOptions;\n");
printer.Print("+ (instancetype)serviceWithHost:(NSString *)host;\n");
printer.Print("@end\n");

Expand All @@ -273,11 +354,18 @@ ::grpc::string GetSource(const ServiceDescriptor* service) {
printer.Print(vars,
"@implementation $service_class$\n\n"
"// Designated initializer\n"
"- (instancetype)initWithHost:(NSString *)host {\n"
"- (instancetype)initWithHost:(NSString *)host "
"callOptions:(GRPCCallOptions *_Nullable)callOptions {\n"
" self = [super initWithHost:host\n"
" packageName:@\"$package$\"\n"
" serviceName:@\"$service_name$\"];\n"
" serviceName:@\"$service_name$\"\n"
" callOptions:callOptions];\n"
" return self;\n"
"}\n\n"
"- (instancetype)initWithHost:(NSString *)host {\n"
" return [super initWithHost:host\n"
" packageName:@\"$package$\"\n"
" serviceName:@\"$service_name$\"];\n"
"}\n\n");

printer.Print(
Expand All @@ -293,6 +381,10 @@ ::grpc::string GetSource(const ServiceDescriptor* service) {
"#pragma mark - Class Methods\n\n"
"+ (instancetype)serviceWithHost:(NSString *)host {\n"
" return [[self alloc] initWithHost:host];\n"
"}\n\n"
"+ (instancetype)serviceWithHost:(NSString *)host "
"callOptions:(GRPCCallOptions *_Nullable)callOptions {\n"
" return [[self alloc] initWithHost:host callOptions:callOptions];\n"
"}\n\n");

printer.Print("#pragma mark - Method Implementations\n\n");
Expand Down
7 changes: 6 additions & 1 deletion src/compiler/objective_c_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ using ::grpc::string;
string GetAllMessageClasses(const FileDescriptor* file);

// Returns the content to be included defining the @protocol segment at the
// insertion point of the generated implementation file.
// insertion point of the generated implementation file. This interface is
// legacy and for backwards compatibility.
string GetProtocol(const ServiceDescriptor* service);

// Returns the content to be included defining the @protocol segment at the
// insertion point of the generated implementation file.
string GetV2Protocol(const ServiceDescriptor* service);

// Returns the content to be included defining the @interface segment at the
// insertion point of the generated implementation file.
string GetInterface(const ServiceDescriptor* service);
Expand Down
21 changes: 17 additions & 4 deletions src/compiler/objective_c_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
SystemImport("RxLibrary/GRXWriteable.h") +
SystemImport("RxLibrary/GRXWriter.h");

::grpc::string forward_declarations = "@class GRPCProtoCall;\n\n";
::grpc::string forward_declarations =
"@class GRPCProtoCall;\n"
"@class GRPCUnaryProtoCall;\n"
"@class GRPCStreamingProtoCall;\n"
"@class GRPCCallOptions;\n"
"@protocol GRPCProtoResponseHandler;\n"
"\n";

::grpc::string class_declarations =
grpc_objective_c_generator::GetAllMessageClasses(file);
Expand All @@ -103,6 +109,12 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
class_imports += ImportProtoHeaders(file->dependency(i), " ");
}

::grpc::string ng_protocols;
for (int i = 0; i < file->service_count(); i++) {
const grpc::protobuf::ServiceDescriptor* service = file->service(i);
ng_protocols += grpc_objective_c_generator::GetV2Protocol(service);
}

::grpc::string protocols;
for (int i = 0; i < file->service_count(); i++) {
const grpc::protobuf::ServiceDescriptor* service = file->service(i);
Expand All @@ -120,9 +132,10 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
PreprocIfNot(kProtocolOnly, system_imports) + "\n" +
class_declarations + "\n" +
PreprocIfNot(kForwardDeclare, class_imports) + "\n" +
forward_declarations + "\n" + kNonNullBegin + "\n" + protocols +
"\n" + PreprocIfNot(kProtocolOnly, interfaces) + "\n" +
kNonNullEnd + "\n");
forward_declarations + "\n" + kNonNullBegin + "\n" +
ng_protocols + protocols + "\n" +
PreprocIfNot(kProtocolOnly, interfaces) + "\n" + kNonNullEnd +
"\n");
}

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,21 @@ GRPCAPI grpc_channel* grpc_cronet_secure_channel_create(
"grpc_create_cronet_transport: stream_engine = %p, target=%s", engine,
target);

// Disable client authority filter when using Cronet
grpc_arg disable_client_authority_filter_arg;
disable_client_authority_filter_arg.key =
const_cast<char*>(GRPC_ARG_DISABLE_CLIENT_AUTHORITY_FILTER);
disable_client_authority_filter_arg.type = GRPC_ARG_INTEGER;
disable_client_authority_filter_arg.value.integer = 1;
grpc_channel_args* new_args = grpc_channel_args_copy_and_add(
args, &disable_client_authority_filter_arg, 1);

grpc_transport* ct =
grpc_create_cronet_transport(engine, target, args, reserved);
grpc_create_cronet_transport(engine, target, new_args, reserved);

grpc_core::ExecCtx exec_ctx;
return grpc_channel_create(target, args, GRPC_CLIENT_DIRECT_CHANNEL, ct);
grpc_channel* channel =
grpc_channel_create(target, new_args, GRPC_CLIENT_DIRECT_CHANNEL, ct);
grpc_channel_args_destroy(new_args);
return channel;
}
97 changes: 43 additions & 54 deletions src/core/lib/iomgr/cfstream_handle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,62 +52,52 @@ CFStreamHandle* CFStreamHandle::CreateStreamHandle(
void CFStreamHandle::ReadCallback(CFReadStreamRef stream,
CFStreamEventType type,
void* client_callback_info) {
grpc_core::ExecCtx exec_ctx;
CFStreamHandle* handle = static_cast<CFStreamHandle*>(client_callback_info);
CFSTREAM_HANDLE_REF(handle, "read callback");
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
grpc_core::ExecCtx exec_ctx;
if (grpc_tcp_trace.enabled()) {
gpr_log(GPR_DEBUG, "CFStream ReadCallback (%p, %p, %lu, %p)", handle,
stream, type, client_callback_info);
}
switch (type) {
case kCFStreamEventOpenCompleted:
handle->open_event_.SetReady();
break;
case kCFStreamEventHasBytesAvailable:
case kCFStreamEventEndEncountered:
handle->read_event_.SetReady();
break;
case kCFStreamEventErrorOccurred:
handle->open_event_.SetReady();
handle->read_event_.SetReady();
break;
default:
GPR_UNREACHABLE_CODE(return );
}
CFSTREAM_HANDLE_UNREF(handle, "read callback");
});
if (grpc_tcp_trace.enabled()) {
gpr_log(GPR_DEBUG, "CFStream ReadCallback (%p, %p, %lu, %p)", handle,
stream, type, client_callback_info);
}
switch (type) {
case kCFStreamEventOpenCompleted:
handle->open_event_.SetReady();
break;
case kCFStreamEventHasBytesAvailable:
case kCFStreamEventEndEncountered:
handle->read_event_.SetReady();
break;
case kCFStreamEventErrorOccurred:
handle->open_event_.SetReady();
handle->read_event_.SetReady();
break;
default:
GPR_UNREACHABLE_CODE(return );
}
}
void CFStreamHandle::WriteCallback(CFWriteStreamRef stream,
CFStreamEventType type,
void* clientCallBackInfo) {
grpc_core::ExecCtx exec_ctx;
CFStreamHandle* handle = static_cast<CFStreamHandle*>(clientCallBackInfo);
CFSTREAM_HANDLE_REF(handle, "write callback");
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
grpc_core::ExecCtx exec_ctx;
if (grpc_tcp_trace.enabled()) {
gpr_log(GPR_DEBUG, "CFStream WriteCallback (%p, %p, %lu, %p)", handle,
stream, type, clientCallBackInfo);
}
switch (type) {
case kCFStreamEventOpenCompleted:
handle->open_event_.SetReady();
break;
case kCFStreamEventCanAcceptBytes:
case kCFStreamEventEndEncountered:
handle->write_event_.SetReady();
break;
case kCFStreamEventErrorOccurred:
handle->open_event_.SetReady();
handle->write_event_.SetReady();
break;
default:
GPR_UNREACHABLE_CODE(return );
}
CFSTREAM_HANDLE_UNREF(handle, "write callback");
});
if (grpc_tcp_trace.enabled()) {
gpr_log(GPR_DEBUG, "CFStream WriteCallback (%p, %p, %lu, %p)", handle,
stream, type, clientCallBackInfo);
}
switch (type) {
case kCFStreamEventOpenCompleted:
handle->open_event_.SetReady();
break;
case kCFStreamEventCanAcceptBytes:
case kCFStreamEventEndEncountered:
handle->write_event_.SetReady();
break;
case kCFStreamEventErrorOccurred:
handle->open_event_.SetReady();
handle->write_event_.SetReady();
break;
default:
GPR_UNREACHABLE_CODE(return );
}
}

CFStreamHandle::CFStreamHandle(CFReadStreamRef read_stream,
Expand All @@ -116,6 +106,7 @@ CFStreamHandle::CFStreamHandle(CFReadStreamRef read_stream,
open_event_.InitEvent();
read_event_.InitEvent();
write_event_.InitEvent();
dispatch_queue_ = dispatch_queue_create(nullptr, DISPATCH_QUEUE_SERIAL);
CFStreamClientContext ctx = {0, static_cast<void*>(this),
CFStreamHandle::Retain, CFStreamHandle::Release,
nil};
Expand All @@ -129,10 +120,8 @@ CFStreamHandle::CFStreamHandle(CFReadStreamRef read_stream,
kCFStreamEventOpenCompleted | kCFStreamEventCanAcceptBytes |
kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered,
CFStreamHandle::WriteCallback, &ctx);
CFReadStreamScheduleWithRunLoop(read_stream, CFRunLoopGetMain(),
kCFRunLoopCommonModes);
CFWriteStreamScheduleWithRunLoop(write_stream, CFRunLoopGetMain(),
kCFRunLoopCommonModes);
CFReadStreamSetDispatchQueue(read_stream, dispatch_queue_);
CFWriteStreamSetDispatchQueue(write_stream, dispatch_queue_);
}

CFStreamHandle::~CFStreamHandle() {
Expand Down
Loading

0 comments on commit 36b47ce

Please sign in to comment.