diff options
author | Craig Tiller <craig.tiller@gmail.com> | 2015-07-05 21:08:33 -0700 |
---|---|---|
committer | Craig Tiller <craig.tiller@gmail.com> | 2015-07-05 21:08:33 -0700 |
commit | 27166a6f65581a9ee820882e5d7506d6bd07ade6 (patch) | |
tree | 0e89c5e09a955b2a074a8b34e0f3fe0a4135f346 /src | |
parent | 4efb6966bdfb62c725c6614b0d85ea374250bb51 (diff) | |
parent | d1dd3a68a2d4af56f1409327c197590dac6968cb (diff) |
Merge github.com:grpc/grpc into flow-like-lava-to-a-barnyard
Conflicts:
src/core/surface/call.c
src/core/transport/chttp2_transport.c
src/core/transport/transport.h
Diffstat (limited to 'src')
313 files changed, 7563 insertions, 6965 deletions
diff --git a/src/compiler/config.h b/src/compiler/config.h index e81de8d6c8..cd52aca57d 100644 --- a/src/compiler/config.h +++ b/src/compiler/config.h @@ -35,6 +35,7 @@ #define SRC_COMPILER_CONFIG_H #include <grpc++/config.h> +#include <grpc++/config_protobuf.h> #ifndef GRPC_CUSTOM_DESCRIPTOR #include <google/protobuf/descriptor.h> @@ -48,7 +49,8 @@ #ifndef GRPC_CUSTOM_CODEGENERATOR #include <google/protobuf/compiler/code_generator.h> #define GRPC_CUSTOM_CODEGENERATOR ::google::protobuf::compiler::CodeGenerator -#define GRPC_CUSTOM_GENERATORCONTEXT ::google::protobuf::compiler::GeneratorContext +#define GRPC_CUSTOM_GENERATORCONTEXT \ + ::google::protobuf::compiler::GeneratorContext #endif #ifndef GRPC_CUSTOM_PRINTER @@ -57,7 +59,8 @@ #include <google/protobuf/io/zero_copy_stream_impl_lite.h> #define GRPC_CUSTOM_PRINTER ::google::protobuf::io::Printer #define GRPC_CUSTOM_CODEDOUTPUTSTREAM ::google::protobuf::io::CodedOutputStream -#define GRPC_CUSTOM_STRINGOUTPUTSTREAM ::google::protobuf::io::StringOutputStream +#define GRPC_CUSTOM_STRINGOUTPUTSTREAM \ + ::google::protobuf::io::StringOutputStream #endif #ifndef GRPC_CUSTOM_PLUGINMAIN diff --git a/src/compiler/cpp_generator.cc b/src/compiler/cpp_generator.cc index b0d2b5d229..75659947df 100644 --- a/src/compiler/cpp_generator.cc +++ b/src/compiler/cpp_generator.cc @@ -97,7 +97,8 @@ grpc::string GetHeaderPrologue(const grpc::protobuf::FileDescriptor *file, vars["filename_base"] = grpc_generator::StripProto(file->name()); printer.Print(vars, "// Generated by the gRPC protobuf plugin.\n"); - printer.Print(vars, "// If you make any local change, they will be lost.\n"); + printer.Print(vars, + "// If you make any local change, they will be lost.\n"); printer.Print(vars, "// source: $filename$\n"); printer.Print(vars, "#ifndef GRPC_$filename_identifier$__INCLUDED\n"); printer.Print(vars, "#define GRPC_$filename_identifier$__INCLUDED\n"); @@ -113,6 +114,7 @@ grpc::string GetHeaderIncludes(const grpc::protobuf::FileDescriptor *file, grpc::string temp = "#include <grpc++/impl/internal_stub.h>\n" "#include <grpc++/impl/rpc_method.h>\n" + "#include <grpc++/impl/proto_utils.h>\n" "#include <grpc++/impl/service_type.h>\n" "#include <grpc++/async_unary_call.h>\n" "#include <grpc++/status.h>\n" @@ -141,10 +143,10 @@ grpc::string GetHeaderIncludes(const grpc::protobuf::FileDescriptor *file, return temp; } -void PrintHeaderClientMethodInterfaces(grpc::protobuf::io::Printer *printer, - const grpc::protobuf::MethodDescriptor *method, - std::map<grpc::string, grpc::string> *vars, - bool is_public) { +void PrintHeaderClientMethodInterfaces( + grpc::protobuf::io::Printer *printer, + const grpc::protobuf::MethodDescriptor *method, + std::map<grpc::string, grpc::string> *vars, bool is_public) { (*vars)["Method"] = method->name(); (*vars)["Request"] = grpc_cpp_generator::ClassName(method->input_type(), true); @@ -157,19 +159,17 @@ void PrintHeaderClientMethodInterfaces(grpc::protobuf::io::Printer *printer, *vars, "virtual ::grpc::Status $Method$(::grpc::ClientContext* context, " "const $Request$& request, $Response$* response) = 0;\n"); - printer->Print( - *vars, - "std::unique_ptr< " - "::grpc::ClientAsyncResponseReaderInterface< $Response$>> " - "Async$Method$(::grpc::ClientContext* context, " - "const $Request$& request, " - "::grpc::CompletionQueue* cq) {\n"); + printer->Print(*vars, + "std::unique_ptr< " + "::grpc::ClientAsyncResponseReaderInterface< $Response$>> " + "Async$Method$(::grpc::ClientContext* context, " + "const $Request$& request, " + "::grpc::CompletionQueue* cq) {\n"); printer->Indent(); - printer->Print( - *vars, - "return std::unique_ptr< " - "::grpc::ClientAsyncResponseReaderInterface< $Response$>>(" - "Async$Method$Raw(context, request, cq));\n"); + printer->Print(*vars, + "return std::unique_ptr< " + "::grpc::ClientAsyncResponseReaderInterface< $Response$>>(" + "Async$Method$Raw(context, request, cq));\n"); printer->Outdent(); printer->Print("}\n"); } else if (ClientOnlyStreaming(method)) { @@ -188,14 +188,14 @@ void PrintHeaderClientMethodInterfaces(grpc::protobuf::io::Printer *printer, printer->Print( *vars, "std::unique_ptr< ::grpc::ClientAsyncWriterInterface< $Request$>>" - " Async$Method$(::grpc::ClientContext* context, $Response$* response, " + " Async$Method$(::grpc::ClientContext* context, $Response$* " + "response, " "::grpc::CompletionQueue* cq, void* tag) {\n"); printer->Indent(); - printer->Print( - *vars, - "return std::unique_ptr< " - "::grpc::ClientAsyncWriterInterface< $Request$>>(" - "Async$Method$Raw(context, response, cq, tag));\n"); + printer->Print(*vars, + "return std::unique_ptr< " + "::grpc::ClientAsyncWriterInterface< $Request$>>(" + "Async$Method$Raw(context, response, cq, tag));\n"); printer->Outdent(); printer->Print("}\n"); } else if (ServerOnlyStreaming(method)) { @@ -218,18 +218,17 @@ void PrintHeaderClientMethodInterfaces(grpc::protobuf::io::Printer *printer, "::grpc::ClientContext* context, const $Request$& request, " "::grpc::CompletionQueue* cq, void* tag) {\n"); printer->Indent(); - printer->Print( - *vars, - "return std::unique_ptr< " - "::grpc::ClientAsyncReaderInterface< $Response$>>(" - "Async$Method$Raw(context, request, cq, tag));\n"); + printer->Print(*vars, + "return std::unique_ptr< " + "::grpc::ClientAsyncReaderInterface< $Response$>>(" + "Async$Method$Raw(context, request, cq, tag));\n"); printer->Outdent(); printer->Print("}\n"); } else if (BidiStreaming(method)) { - printer->Print( - *vars, - "std::unique_ptr< ::grpc::ClientReaderWriterInterface< $Request$, $Response$>> " - "$Method$(::grpc::ClientContext* context) {\n"); + printer->Print(*vars, + "std::unique_ptr< ::grpc::ClientReaderWriterInterface< " + "$Request$, $Response$>> " + "$Method$(::grpc::ClientContext* context) {\n"); printer->Indent(); printer->Print( *vars, @@ -267,12 +266,11 @@ void PrintHeaderClientMethodInterfaces(grpc::protobuf::io::Printer *printer, "virtual ::grpc::ClientWriterInterface< $Request$>*" " $Method$Raw(" "::grpc::ClientContext* context, $Response$* response) = 0;\n"); - printer->Print( - *vars, - "virtual ::grpc::ClientAsyncWriterInterface< $Request$>*" - " Async$Method$Raw(::grpc::ClientContext* context, " - "$Response$* response, " - "::grpc::CompletionQueue* cq, void* tag) = 0;\n"); + printer->Print(*vars, + "virtual ::grpc::ClientAsyncWriterInterface< $Request$>*" + " Async$Method$Raw(::grpc::ClientContext* context, " + "$Response$* response, " + "::grpc::CompletionQueue* cq, void* tag) = 0;\n"); } else if (ServerOnlyStreaming(method)) { printer->Print( *vars, @@ -285,16 +283,15 @@ void PrintHeaderClientMethodInterfaces(grpc::protobuf::io::Printer *printer, "::grpc::ClientContext* context, const $Request$& request, " "::grpc::CompletionQueue* cq, void* tag) = 0;\n"); } else if (BidiStreaming(method)) { - printer->Print( - *vars, - "virtual ::grpc::ClientReaderWriterInterface< $Request$, $Response$>* " - "$Method$Raw(::grpc::ClientContext* context) = 0;\n"); - printer->Print( - *vars, - "virtual ::grpc::ClientAsyncReaderWriterInterface< " - "$Request$, $Response$>* " - "Async$Method$Raw(::grpc::ClientContext* context, " - "::grpc::CompletionQueue* cq, void* tag) = 0;\n"); + printer->Print(*vars, + "virtual ::grpc::ClientReaderWriterInterface< $Request$, " + "$Response$>* " + "$Method$Raw(::grpc::ClientContext* context) = 0;\n"); + printer->Print(*vars, + "virtual ::grpc::ClientAsyncReaderWriterInterface< " + "$Request$, $Response$>* " + "Async$Method$Raw(::grpc::ClientContext* context, " + "::grpc::CompletionQueue* cq, void* tag) = 0;\n"); } } } @@ -321,11 +318,10 @@ void PrintHeaderClientMethod(grpc::protobuf::io::Printer *printer, "const $Request$& request, " "::grpc::CompletionQueue* cq) {\n"); printer->Indent(); - printer->Print( - *vars, - "return std::unique_ptr< " - "::grpc::ClientAsyncResponseReader< $Response$>>(" - "Async$Method$Raw(context, request, cq));\n"); + printer->Print(*vars, + "return std::unique_ptr< " + "::grpc::ClientAsyncResponseReader< $Response$>>(" + "Async$Method$Raw(context, request, cq));\n"); printer->Outdent(); printer->Print("}\n"); } else if (ClientOnlyStreaming(method)) { @@ -335,17 +331,16 @@ void PrintHeaderClientMethod(grpc::protobuf::io::Printer *printer, " $Method$(" "::grpc::ClientContext* context, $Response$* response) {\n"); printer->Indent(); - printer->Print( - *vars, - "return std::unique_ptr< ::grpc::ClientWriter< $Request$>>" - "($Method$Raw(context, response));\n"); + printer->Print(*vars, + "return std::unique_ptr< ::grpc::ClientWriter< $Request$>>" + "($Method$Raw(context, response));\n"); printer->Outdent(); printer->Print("}\n"); - printer->Print( - *vars, - "std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>>" - " Async$Method$(::grpc::ClientContext* context, $Response$* response, " - "::grpc::CompletionQueue* cq, void* tag) {\n"); + printer->Print(*vars, + "std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>>" + " Async$Method$(::grpc::ClientContext* context, " + "$Response$* response, " + "::grpc::CompletionQueue* cq, void* tag) {\n"); printer->Indent(); printer->Print( *vars, @@ -385,53 +380,47 @@ void PrintHeaderClientMethod(grpc::protobuf::io::Printer *printer, "std::unique_ptr< ::grpc::ClientReaderWriter< $Request$, $Response$>>" " $Method$(::grpc::ClientContext* context) {\n"); printer->Indent(); - printer->Print( - *vars, - "return std::unique_ptr< " - "::grpc::ClientReaderWriter< $Request$, $Response$>>(" - "$Method$Raw(context));\n"); + printer->Print(*vars, + "return std::unique_ptr< " + "::grpc::ClientReaderWriter< $Request$, $Response$>>(" + "$Method$Raw(context));\n"); printer->Outdent(); printer->Print("}\n"); - printer->Print( - *vars, - "std::unique_ptr< ::grpc::ClientAsyncReaderWriter< " - "$Request$, $Response$>> " - "Async$Method$(::grpc::ClientContext* context, " - "::grpc::CompletionQueue* cq, void* tag) {\n"); + printer->Print(*vars, + "std::unique_ptr< ::grpc::ClientAsyncReaderWriter< " + "$Request$, $Response$>> " + "Async$Method$(::grpc::ClientContext* context, " + "::grpc::CompletionQueue* cq, void* tag) {\n"); printer->Indent(); - printer->Print( - *vars, - "return std::unique_ptr< " - "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>>(" - "Async$Method$Raw(context, cq, tag));\n"); + printer->Print(*vars, + "return std::unique_ptr< " + "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>>(" + "Async$Method$Raw(context, cq, tag));\n"); printer->Outdent(); printer->Print("}\n"); } } else { if (NoStreaming(method)) { - printer->Print( - *vars, - "::grpc::ClientAsyncResponseReader< $Response$>* " - "Async$Method$Raw(::grpc::ClientContext* context, " - "const $Request$& request, " - "::grpc::CompletionQueue* cq) GRPC_OVERRIDE;\n"); + printer->Print(*vars, + "::grpc::ClientAsyncResponseReader< $Response$>* " + "Async$Method$Raw(::grpc::ClientContext* context, " + "const $Request$& request, " + "::grpc::CompletionQueue* cq) GRPC_OVERRIDE;\n"); } else if (ClientOnlyStreaming(method)) { - printer->Print( - *vars, - "::grpc::ClientWriter< $Request$>* $Method$Raw(" - "::grpc::ClientContext* context, $Response$* response) " - "GRPC_OVERRIDE;\n"); + printer->Print(*vars, + "::grpc::ClientWriter< $Request$>* $Method$Raw(" + "::grpc::ClientContext* context, $Response$* response) " + "GRPC_OVERRIDE;\n"); printer->Print( *vars, "::grpc::ClientAsyncWriter< $Request$>* Async$Method$Raw(" "::grpc::ClientContext* context, $Response$* response, " "::grpc::CompletionQueue* cq, void* tag) GRPC_OVERRIDE;\n"); } else if (ServerOnlyStreaming(method)) { - printer->Print( - *vars, - "::grpc::ClientReader< $Response$>* $Method$Raw(" - "::grpc::ClientContext* context, const $Request$& request)" - " GRPC_OVERRIDE;\n"); + printer->Print(*vars, + "::grpc::ClientReader< $Response$>* $Method$Raw(" + "::grpc::ClientContext* context, const $Request$& request)" + " GRPC_OVERRIDE;\n"); printer->Print( *vars, "::grpc::ClientAsyncReader< $Response$>* Async$Method$Raw(" @@ -629,7 +618,7 @@ grpc::string GetHeaderServices(const grpc::protobuf::FileDescriptor *file, const Parameters ¶ms) { grpc::string output; { - // Scope the output stream so it closes and finalizes output to the string. + // Scope the output stream so it closes and finalizes output to the string. grpc::protobuf::io::StringOutputStream output_stream(&output); grpc::protobuf::io::Printer printer(&output_stream, '$'); std::map<grpc::string, grpc::string> vars; @@ -693,7 +682,8 @@ grpc::string GetSourcePrologue(const grpc::protobuf::FileDescriptor *file, vars["filename_base"] = grpc_generator::StripProto(file->name()); printer.Print(vars, "// Generated by the gRPC protobuf plugin.\n"); - printer.Print(vars, "// If you make any local change, they will be lost.\n"); + printer.Print(vars, + "// If you make any local change, they will be lost.\n"); printer.Print(vars, "// source: $filename$\n\n"); printer.Print(vars, "#include \"$filename_base$.pb.h\"\n"); printer.Print(vars, "#include \"$filename_base$.grpc.pb.h\"\n"); @@ -849,9 +839,12 @@ void PrintSourceServerMethod(grpc::protobuf::io::Printer *printer, "::grpc::Status $ns$$Service$::Service::$Method$(" "::grpc::ServerContext* context, " "const $Request$* request, $Response$* response) {\n"); + printer->Print(" (void) context;\n"); + printer->Print(" (void) request;\n"); + printer->Print(" (void) response;\n"); printer->Print( " return ::grpc::Status(" - "::grpc::StatusCode::UNIMPLEMENTED);\n"); + "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"); printer->Print("}\n\n"); } else if (ClientOnlyStreaming(method)) { printer->Print(*vars, @@ -859,9 +852,12 @@ void PrintSourceServerMethod(grpc::protobuf::io::Printer *printer, "::grpc::ServerContext* context, " "::grpc::ServerReader< $Request$>* reader, " "$Response$* response) {\n"); + printer->Print(" (void) context;\n"); + printer->Print(" (void) reader;\n"); + printer->Print(" (void) response;\n"); printer->Print( " return ::grpc::Status(" - "::grpc::StatusCode::UNIMPLEMENTED);\n"); + "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"); printer->Print("}\n\n"); } else if (ServerOnlyStreaming(method)) { printer->Print(*vars, @@ -869,9 +865,12 @@ void PrintSourceServerMethod(grpc::protobuf::io::Printer *printer, "::grpc::ServerContext* context, " "const $Request$* request, " "::grpc::ServerWriter< $Response$>* writer) {\n"); + printer->Print(" (void) context;\n"); + printer->Print(" (void) request;\n"); + printer->Print(" (void) writer;\n"); printer->Print( " return ::grpc::Status(" - "::grpc::StatusCode::UNIMPLEMENTED);\n"); + "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"); printer->Print("}\n\n"); } else if (BidiStreaming(method)) { printer->Print(*vars, @@ -879,9 +878,11 @@ void PrintSourceServerMethod(grpc::protobuf::io::Printer *printer, "::grpc::ServerContext* context, " "::grpc::ServerReaderWriter< $Response$, $Request$>* " "stream) {\n"); + printer->Print(" (void) context;\n"); + printer->Print(" (void) stream;\n"); printer->Print( " return ::grpc::Status(" - "::grpc::StatusCode::UNIMPLEMENTED);\n"); + "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"); printer->Print("}\n\n"); } } @@ -1045,8 +1046,7 @@ void PrintSourceService(grpc::protobuf::io::Printer *printer, " new ::grpc::RpcMethodHandler< $ns$$Service$::Service, " "$Request$, " "$Response$>(\n" - " std::mem_fn(&$ns$$Service$::Service::$Method$), this),\n" - " new $Request$, new $Response$));\n"); + " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n"); } else if (ClientOnlyStreaming(method)) { printer->Print( *vars, @@ -1055,8 +1055,7 @@ void PrintSourceService(grpc::protobuf::io::Printer *printer, " ::grpc::RpcMethod::CLIENT_STREAMING,\n" " new ::grpc::ClientStreamingHandler< " "$ns$$Service$::Service, $Request$, $Response$>(\n" - " std::mem_fn(&$ns$$Service$::Service::$Method$), this),\n" - " new $Request$, new $Response$));\n"); + " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n"); } else if (ServerOnlyStreaming(method)) { printer->Print( *vars, @@ -1065,8 +1064,7 @@ void PrintSourceService(grpc::protobuf::io::Printer *printer, " ::grpc::RpcMethod::SERVER_STREAMING,\n" " new ::grpc::ServerStreamingHandler< " "$ns$$Service$::Service, $Request$, $Response$>(\n" - " std::mem_fn(&$ns$$Service$::Service::$Method$), this),\n" - " new $Request$, new $Response$));\n"); + " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n"); } else if (BidiStreaming(method)) { printer->Print( *vars, @@ -1075,8 +1073,7 @@ void PrintSourceService(grpc::protobuf::io::Printer *printer, " ::grpc::RpcMethod::BIDI_STREAMING,\n" " new ::grpc::BidiStreamingHandler< " "$ns$$Service$::Service, $Request$, $Response$>(\n" - " std::mem_fn(&$ns$$Service$::Service::$Method$), this),\n" - " new $Request$, new $Response$));\n"); + " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n"); } } printer->Print("return service_;\n"); diff --git a/src/compiler/objective_c_generator.cc b/src/compiler/objective_c_generator.cc index b235911479..79a84b4a7a 100644 --- a/src/compiler/objective_c_generator.cc +++ b/src/compiler/objective_c_generator.cc @@ -68,18 +68,19 @@ void PrintMethodSignature(Printer *printer, printer->Print(vars, "- ($return_type$)$method_name$With"); if (method->client_streaming()) { - printer->Print("RequestsWriter:(id<GRXWriter>)request"); + printer->Print("RequestsWriter:(id<GRXWriter>)requestWriter"); } else { printer->Print(vars, "Request:($request_class$ *)request"); } // TODO(jcanizales): Put this on a new line and align colons. - // TODO(jcanizales): eventHandler for server streaming? - printer->Print(" handler:(void(^)("); if (method->server_streaming()) { - printer->Print("BOOL done, "); + printer->Print(vars, " eventHandler:(void(^)(BOOL done, " + "$response_class$ *response, NSError *error))eventHandler"); + } else { + printer->Print(vars, " handler:(void(^)($response_class$ *response, " + "NSError *error))handler"); } - printer->Print(vars, "$response_class$ *response, NSError *error))handler"); } void PrintSimpleSignature(Printer *printer, @@ -125,11 +126,15 @@ void PrintSimpleImplementation(Printer *printer, printer->Print("{\n"); printer->Print(vars, " [[self RPCTo$method_name$With"); if (method->client_streaming()) { - printer->Print("RequestsWriter:request"); + printer->Print("RequestsWriter:requestWriter"); } else { printer->Print("Request:request"); } - printer->Print(" handler:handler] start];\n"); + if (method->server_streaming()) { + printer->Print(" eventHandler:eventHandler] start];\n"); + } else { + printer->Print(" handler:handler] start];\n"); + } printer->Print("}\n"); } @@ -141,7 +146,7 @@ void PrintAdvancedImplementation(Printer *printer, printer->Print(" requestsWriter:"); if (method->client_streaming()) { - printer->Print("request\n"); + printer->Print("requestWriter\n"); } else { printer->Print("[GRXWriter writerWithValue:request]\n"); } @@ -150,7 +155,7 @@ void PrintAdvancedImplementation(Printer *printer, printer->Print(" responsesWriteable:[GRXWriteable "); if (method->server_streaming()) { - printer->Print("writeableWithStreamHandler:handler]];\n"); + printer->Print("writeableWithStreamHandler:eventHandler]];\n"); } else { printer->Print("writeableWithSingleValueHandler:handler]];\n"); } diff --git a/src/compiler/objective_c_plugin.cc b/src/compiler/objective_c_plugin.cc index b5ac2bafa9..2b5ab758fc 100644 --- a/src/compiler/objective_c_plugin.cc +++ b/src/compiler/objective_c_plugin.cc @@ -62,8 +62,8 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { { // Generate .pbrpc.h - string imports = string("#import \"") + file_name + ".pbobjc.h\"\n" - "#import <gRPC/ProtoService.h>\n"; + string imports = string("#import \"") + file_name + ".pbobjc.h\"\n\n" + "#import <ProtoRPC/ProtoService.h>\n"; // TODO(jcanizales): Instead forward-declare the input and output types // and import the files in the .pbrpc.m @@ -87,10 +87,10 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { { // Generate .pbrpc.m - string imports = string("#import \"") + file_name + ".pbrpc.h\"\n" - "#import <gRPC/GRXWriteable.h>\n" - "#import <gRPC/GRXWriter+Immediate.h>\n" - "#import <gRPC/ProtoRPC.h>\n"; + string imports = string("#import \"") + file_name + ".pbrpc.h\"\n\n" + "#import <ProtoRPC/ProtoRPC.h>\n" + "#import <RxLibrary/GRXWriteable.h>\n" + "#import <RxLibrary/GRXWriter+Immediate.h>\n"; string definitions; for (int i = 0; i < file->service_count(); i++) { diff --git a/src/compiler/python_generator.cc b/src/compiler/python_generator.cc index 72149bc4e3..724b69c703 100644 --- a/src/compiler/python_generator.cc +++ b/src/compiler/python_generator.cc @@ -42,7 +42,7 @@ #include <tuple> #include <vector> -#include "grpc++/config.h" +#include <grpc++/config.h> #include "src/compiler/config.h" #include "src/compiler/generator_helpers.h" #include "src/compiler/python_generator.h" diff --git a/src/core/census/context.c b/src/core/census/context.c index 1358c5127b..df238ec98c 100644 --- a/src/core/census/context.c +++ b/src/core/census/context.c @@ -31,7 +31,7 @@ * */ -#include "context.h" +#include "src/core/census/context.h" #include <string.h> #include <grpc/census.h> diff --git a/src/core/channel/channel_args.c b/src/core/channel/channel_args.c index 509ae0df89..166d559a45 100644 --- a/src/core/channel/channel_args.c +++ b/src/core/channel/channel_args.c @@ -36,6 +36,7 @@ #include "src/core/support/string.h" #include <grpc/support/alloc.h> +#include <grpc/support/string_util.h> #include <string.h> @@ -114,3 +115,27 @@ int grpc_channel_args_is_census_enabled(const grpc_channel_args *a) { } return 0; } + +grpc_compression_level grpc_channel_args_get_compression_level( + const grpc_channel_args *a) { + size_t i; + if (a) { + for (i = 0; a && i < a->num_args; ++i) { + if (a->args[i].type == GRPC_ARG_INTEGER && + !strcmp(GRPC_COMPRESSION_LEVEL_ARG, a->args[i].key)) { + return a->args[i].value.integer; + break; + } + } + } + return GRPC_COMPRESS_LEVEL_NONE; +} + +void grpc_channel_args_set_compression_level( + grpc_channel_args **a, grpc_compression_level level) { + grpc_arg tmp; + tmp.type = GRPC_ARG_INTEGER; + tmp.key = GRPC_COMPRESSION_LEVEL_ARG; + tmp.value.integer = level; + *a = grpc_channel_args_copy_and_add(*a, &tmp); +} diff --git a/src/core/channel/channel_args.h b/src/core/channel/channel_args.h index eb5bf63986..bf747b26e6 100644 --- a/src/core/channel/channel_args.h +++ b/src/core/channel/channel_args.h @@ -34,21 +34,31 @@ #ifndef GRPC_INTERNAL_CORE_CHANNEL_CHANNEL_ARGS_H #define GRPC_INTERNAL_CORE_CHANNEL_CHANNEL_ARGS_H +#include <grpc/compression.h> #include <grpc/grpc.h> /* Copy some arguments */ grpc_channel_args *grpc_channel_args_copy(const grpc_channel_args *src); -/* Copy some arguments and add the to_add parameter in the end. +/** Copy some arguments and add the to_add parameter in the end. If to_add is NULL, it is equivalent to call grpc_channel_args_copy. */ grpc_channel_args *grpc_channel_args_copy_and_add(const grpc_channel_args *src, const grpc_arg *to_add); -/* Destroy arguments created by grpc_channel_args_copy */ +/** Destroy arguments created by grpc_channel_args_copy */ void grpc_channel_args_destroy(grpc_channel_args *a); -/* Reads census_enabled settings from channel args. Returns 1 if census_enabled - is specified in channel args, otherwise returns 0. */ +/** Reads census_enabled settings from channel args. Returns 1 if census_enabled + * is specified in channel args, otherwise returns 0. */ int grpc_channel_args_is_census_enabled(const grpc_channel_args *a); +/** Returns the compression level set in \a a. */ +grpc_compression_level grpc_channel_args_get_compression_level( + const grpc_channel_args *a); + +/** Sets the compression level in \a a to \a level. Setting it to + * GRPC_COMPRESS_LEVEL_NONE disables compression for the channel. */ +void grpc_channel_args_set_compression_level( + grpc_channel_args **a, grpc_compression_level level); + #endif /* GRPC_INTERNAL_CORE_CHANNEL_CHANNEL_ARGS_H */ diff --git a/src/core/channel/child_channel.c b/src/core/channel/child_channel.c index cdc1c11681..6690265d75 100644 --- a/src/core/channel/child_channel.c +++ b/src/core/channel/child_channel.c @@ -157,17 +157,9 @@ static void lb_destroy_channel_elem(grpc_channel_element *elem) { } const grpc_channel_filter grpc_child_channel_top_filter = { - lb_start_transport_op, - lb_channel_op, - - sizeof(lb_call_data), - lb_init_call_elem, - lb_destroy_call_elem, - - sizeof(lb_channel_data), - lb_init_channel_elem, - lb_destroy_channel_elem, - + lb_start_transport_op, lb_channel_op, + sizeof(lb_call_data), lb_init_call_elem, lb_destroy_call_elem, + sizeof(lb_channel_data), lb_init_channel_elem, lb_destroy_channel_elem, "child-channel", }; diff --git a/src/core/channel/client_channel.c b/src/core/channel/client_channel.c index 31b1fc3bde..711e105464 100644 --- a/src/core/channel/client_channel.c +++ b/src/core/channel/client_channel.c @@ -102,10 +102,17 @@ struct call_data { static int prepare_activate(grpc_call_element *elem, grpc_child_channel *on_child) { call_data *calld = elem->call_data; + channel_data *chand = elem->channel_data; if (calld->state == CALL_CANCELLED) return 0; /* no more access to calld->s.waiting allowed */ GPR_ASSERT(calld->state == CALL_WAITING); + + if (calld->s.waiting_op.bind_pollset) { + grpc_transport_setup_del_interested_party(chand->transport_setup, + calld->s.waiting_op.bind_pollset); + } + calld->state = CALL_ACTIVE; /* create a child call */ @@ -150,7 +157,7 @@ static void handle_op_after_cancellation(grpc_call_element *elem, channel_data *chand = elem->channel_data; if (op->send_ops) { grpc_stream_ops_unref_owned_objects(op->send_ops->ops, op->send_ops->nops); - op->on_done_send(op->send_user_data, 0); + op->on_done_send->cb(op->on_done_send->cb_arg, 0); } if (op->recv_ops) { char status[GPR_LTOA_MIN_BUFSIZE]; @@ -169,10 +176,10 @@ static void handle_op_after_cancellation(grpc_call_element *elem, mdb.deadline = gpr_inf_future; grpc_sopb_add_metadata(op->recv_ops, mdb); *op->recv_state = GRPC_STREAM_CLOSED; - op->on_done_recv(op->recv_user_data, 1); + op->on_done_recv->cb(op->on_done_recv->cb_arg, 1); } if (op->on_consumed) { - op->on_consumed(op->on_consumed_user_data, 0); + op->on_consumed->cb(op->on_consumed->cb_arg, 0); } } @@ -199,6 +206,7 @@ static void cc_start_transport_op(grpc_call_element *elem, handle_op_after_cancellation(elem, op); } else { calld->state = CALL_WAITING; + calld->s.waiting_op.bind_pollset = NULL; if (chand->active_child) { /* channel is connected - use the connected stack */ if (prepare_activate(elem, chand->active_child)) { @@ -230,14 +238,14 @@ static void cc_start_transport_op(grpc_call_element *elem, } calld->s.waiting_op = *op; chand->waiting_children[chand->waiting_child_count++] = calld; + grpc_transport_setup_add_interested_party(chand->transport_setup, + op->bind_pollset); gpr_mu_unlock(&chand->mu); /* finally initiate transport setup if needed */ if (initiate_transport_setup) { grpc_transport_setup_initiate(chand->transport_setup); } - grpc_transport_setup_add_interested_party(chand->transport_setup, - op->bind_pollset); } } break; @@ -258,17 +266,15 @@ static void cc_start_transport_op(grpc_call_element *elem, calld->s.waiting_op.send_ops = op->send_ops; calld->s.waiting_op.is_last_send = op->is_last_send; calld->s.waiting_op.on_done_send = op->on_done_send; - calld->s.waiting_op.send_user_data = op->send_user_data; } if (op->recv_ops) { calld->s.waiting_op.recv_ops = op->recv_ops; calld->s.waiting_op.recv_state = op->recv_state; calld->s.waiting_op.on_done_recv = op->on_done_recv; - calld->s.waiting_op.recv_user_data = op->recv_user_data; } gpr_mu_unlock(&chand->mu); if (op->on_consumed) { - op->on_consumed(op->on_consumed_user_data, 0); + op->on_consumed->cb(op->on_consumed->cb_arg, 0); } } break; diff --git a/src/core/channel/client_setup.c b/src/core/channel/client_setup.c index f305d8ba9e..5be8fa66e9 100644 --- a/src/core/channel/client_setup.c +++ b/src/core/channel/client_setup.c @@ -56,12 +56,14 @@ struct grpc_client_setup { gpr_cv cv; grpc_client_setup_request *active_request; int refs; + /** The set of pollsets that are currently interested in this + connection being established */ + grpc_pollset_set interested_parties; }; struct grpc_client_setup_request { /* pointer back to the setup object */ grpc_client_setup *setup; - grpc_pollset_set interested_parties; gpr_timespec deadline; }; @@ -71,7 +73,7 @@ gpr_timespec grpc_client_setup_request_deadline(grpc_client_setup_request *r) { grpc_pollset_set *grpc_client_setup_get_interested_parties( grpc_client_setup_request *r) { - return &r->interested_parties; + return &r->setup->interested_parties; } static void destroy_setup(grpc_client_setup *s) { @@ -79,13 +81,11 @@ static void destroy_setup(grpc_client_setup *s) { gpr_cv_destroy(&s->cv); s->done(s->user_data); grpc_channel_args_destroy(s->args); + grpc_pollset_set_destroy(&s->interested_parties); gpr_free(s); } -static void destroy_request(grpc_client_setup_request *r) { - grpc_pollset_set_destroy(&r->interested_parties); - gpr_free(r); -} +static void destroy_request(grpc_client_setup_request *r) { gpr_free(r); } /* initiate handshaking */ static void setup_initiate(grpc_transport_setup *sp) { @@ -94,8 +94,6 @@ static void setup_initiate(grpc_transport_setup *sp) { int in_alarm = 0; r->setup = s; - grpc_pollset_set_init(&r->interested_parties); - /* TODO(klempner): Actually set a deadline */ r->deadline = gpr_time_add(gpr_now(), gpr_time_from_seconds(60)); gpr_mu_lock(&s->mu); @@ -120,33 +118,23 @@ static void setup_initiate(grpc_transport_setup *sp) { } } +/** implementation of add_interested_party for setup vtable */ static void setup_add_interested_party(grpc_transport_setup *sp, grpc_pollset *pollset) { grpc_client_setup *s = (grpc_client_setup *)sp; gpr_mu_lock(&s->mu); - if (!s->active_request) { - gpr_mu_unlock(&s->mu); - return; - } - - grpc_pollset_set_add_pollset(&s->active_request->interested_parties, pollset); - + grpc_pollset_set_add_pollset(&s->interested_parties, pollset); gpr_mu_unlock(&s->mu); } +/** implementation of del_interested_party for setup vtable */ static void setup_del_interested_party(grpc_transport_setup *sp, grpc_pollset *pollset) { grpc_client_setup *s = (grpc_client_setup *)sp; gpr_mu_lock(&s->mu); - if (!s->active_request) { - gpr_mu_unlock(&s->mu); - return; - } - - grpc_pollset_set_del_pollset(&s->active_request->interested_parties, pollset); - + grpc_pollset_set_del_pollset(&s->interested_parties, pollset); gpr_mu_unlock(&s->mu); } @@ -179,7 +167,8 @@ static void setup_cancel(grpc_transport_setup *sp) { } } -int grpc_client_setup_cb_begin(grpc_client_setup_request *r, const char *reason) { +int grpc_client_setup_cb_begin(grpc_client_setup_request *r, + const char *reason) { gpr_mu_lock(&r->setup->mu); if (r->setup->cancelled) { gpr_mu_unlock(&r->setup->mu); @@ -190,7 +179,8 @@ int grpc_client_setup_cb_begin(grpc_client_setup_request *r, const char *reason) return 1; } -void grpc_client_setup_cb_end(grpc_client_setup_request *r, const char *reason) { +void grpc_client_setup_cb_end(grpc_client_setup_request *r, + const char *reason) { gpr_mu_lock(&r->setup->mu); r->setup->in_cb--; if (r->setup->cancelled) gpr_cv_signal(&r->setup->cv); @@ -223,11 +213,13 @@ void grpc_client_setup_create_and_attach( s->in_alarm = 0; s->in_cb = 0; s->cancelled = 0; + grpc_pollset_set_init(&s->interested_parties); grpc_client_channel_set_transport_setup(newly_minted_channel, &s->base); } -int grpc_client_setup_request_should_continue(grpc_client_setup_request *r, const char *reason) { +int grpc_client_setup_request_should_continue(grpc_client_setup_request *r, + const char *reason) { int result; if (gpr_time_cmp(gpr_now(), r->deadline) > 0) { result = 0; @@ -239,7 +231,8 @@ int grpc_client_setup_request_should_continue(grpc_client_setup_request *r, cons return result; } -static void backoff_alarm_done(void *arg /* grpc_client_setup */, int success) { +static void backoff_alarm_done(void *arg /* grpc_client_setup_request */, + int success) { grpc_client_setup_request *r = arg; grpc_client_setup *s = r->setup; /* Handle status cancelled? */ diff --git a/src/core/channel/client_setup.h b/src/core/channel/client_setup.h index cbabb510b1..7d40338840 100644 --- a/src/core/channel/client_setup.h +++ b/src/core/channel/client_setup.h @@ -52,7 +52,8 @@ void grpc_client_setup_create_and_attach( /* Check that r is the active request: needs to be performed at each callback. If this races, we'll have two connection attempts running at once and the old one will get cleaned up in due course, which is fine. */ -int grpc_client_setup_request_should_continue(grpc_client_setup_request *r, const char *reason); +int grpc_client_setup_request_should_continue(grpc_client_setup_request *r, + const char *reason); void grpc_client_setup_request_finish(grpc_client_setup_request *r, int was_successful); const grpc_channel_args *grpc_client_setup_get_channel_args( @@ -61,7 +62,8 @@ const grpc_channel_args *grpc_client_setup_get_channel_args( /* Call before calling back into the setup listener, and call only if this function returns 1. If it returns 1, also promise to call grpc_client_setup_cb_end */ -int grpc_client_setup_cb_begin(grpc_client_setup_request *r, const char *reason); +int grpc_client_setup_cb_begin(grpc_client_setup_request *r, + const char *reason); void grpc_client_setup_cb_end(grpc_client_setup_request *r, const char *reason); /* Get the deadline for a request passed in to initiate. Implementations should diff --git a/src/core/channel/http_client_filter.c b/src/core/channel/http_client_filter.c index 9805f325a6..08a2c0df3c 100644 --- a/src/core/channel/http_client_filter.c +++ b/src/core/channel/http_client_filter.c @@ -43,8 +43,13 @@ typedef struct call_data { int got_initial_metadata; grpc_stream_op_buffer *recv_ops; - void (*on_done_recv)(void *user_data, int success); - void *recv_user_data; + + /** Closure to call when finished with the hc_on_recv hook */ + grpc_iomgr_closure *on_done_recv; + /** Receive closures are chained: we inject this closure as the on_done_recv + up-call on transport_op, and remember to call our on_done_recv member + after handling it. */ + grpc_iomgr_closure hc_on_recv; } call_data; typedef struct channel_data { @@ -84,7 +89,7 @@ static void hc_on_recv(void *user_data, int success) { grpc_metadata_batch_filter(&op->data.metadata, client_filter, elem); } } - calld->on_done_recv(calld->recv_user_data, success); + calld->on_done_recv->cb(calld->on_done_recv->cb_arg, success); } static void hc_mutate_op(grpc_call_element *elem, grpc_transport_op *op) { @@ -117,9 +122,7 @@ static void hc_mutate_op(grpc_call_element *elem, grpc_transport_op *op) { /* substitute our callback for the higher callback */ calld->recv_ops = op->recv_ops; calld->on_done_recv = op->on_done_recv; - calld->recv_user_data = op->recv_user_data; - op->on_done_recv = hc_on_recv; - op->recv_user_data = elem; + op->on_done_recv = &calld->hc_on_recv; } } @@ -154,6 +157,8 @@ static void init_call_elem(grpc_call_element *elem, call_data *calld = elem->call_data; calld->sent_initial_metadata = 0; calld->got_initial_metadata = 0; + calld->on_done_recv = NULL; + grpc_iomgr_closure_init(&calld->hc_on_recv, hc_on_recv, elem); if (initial_op) hc_mutate_op(elem, initial_op); } diff --git a/src/core/channel/http_server_filter.c b/src/core/channel/http_server_filter.c index 11a53b1e70..d3a01fd1a8 100644 --- a/src/core/channel/http_server_filter.c +++ b/src/core/channel/http_server_filter.c @@ -47,8 +47,12 @@ typedef struct call_data { grpc_linked_mdelem status; grpc_stream_op_buffer *recv_ops; - void (*on_done_recv)(void *user_data, int success); - void *recv_user_data; + /** Closure to call when finished with the hs_on_recv hook */ + grpc_iomgr_closure *on_done_recv; + /** Receive closures are chained: we inject this closure as the on_done_recv + up-call on transport_op, and remember to call our on_done_recv member + after handling it. */ + grpc_iomgr_closure hs_on_recv; } call_data; typedef struct channel_data { @@ -174,7 +178,7 @@ static void hs_on_recv(void *user_data, int success) { } } } - calld->on_done_recv(calld->recv_user_data, success); + calld->on_done_recv->cb(calld->on_done_recv->cb_arg, success); } static void hs_mutate_op(grpc_call_element *elem, grpc_transport_op *op) { @@ -200,9 +204,7 @@ static void hs_mutate_op(grpc_call_element *elem, grpc_transport_op *op) { /* substitute our callback for the higher callback */ calld->recv_ops = op->recv_ops; calld->on_done_recv = op->on_done_recv; - calld->recv_user_data = op->recv_user_data; - op->on_done_recv = hs_on_recv; - op->recv_user_data = elem; + op->on_done_recv = &calld->hs_on_recv; } } @@ -238,6 +240,7 @@ static void init_call_elem(grpc_call_element *elem, call_data *calld = elem->call_data; /* initialize members */ memset(calld, 0, sizeof(*calld)); + grpc_iomgr_closure_init(&calld->hs_on_recv, hs_on_recv, elem); if (initial_op) hs_mutate_op(elem, initial_op); } diff --git a/src/core/compression/algorithm.c b/src/core/compression/algorithm.c index ca07002ff9..4db48df6cb 100644 --- a/src/core/compression/algorithm.c +++ b/src/core/compression/algorithm.c @@ -31,7 +31,8 @@ * */ -#include "src/core/compression/algorithm.h" +#include <stdlib.h> +#include <grpc/compression.h> const char *grpc_compression_algorithm_name( grpc_compression_algorithm algorithm) { @@ -47,3 +48,20 @@ const char *grpc_compression_algorithm_name( } return "error"; } + +/* TODO(dgq): Add the ability to specify parameters to the individual + * compression algorithms */ +grpc_compression_algorithm grpc_compression_algorithm_for_level( + grpc_compression_level level) { + switch (level) { + case GRPC_COMPRESS_LEVEL_NONE: + return GRPC_COMPRESS_NONE; + case GRPC_COMPRESS_LEVEL_LOW: + case GRPC_COMPRESS_LEVEL_MED: + case GRPC_COMPRESS_LEVEL_HIGH: + return GRPC_COMPRESS_DEFLATE; + default: + /* we shouldn't be making it here */ + abort(); + } +} diff --git a/src/core/compression/message_compress.h b/src/core/compression/message_compress.h index e8aef1a713..b3eb8f579f 100644 --- a/src/core/compression/message_compress.h +++ b/src/core/compression/message_compress.h @@ -34,7 +34,7 @@ #ifndef GRPC_INTERNAL_CORE_COMPRESSION_MESSAGE_COMPRESS_H #define GRPC_INTERNAL_CORE_COMPRESSION_MESSAGE_COMPRESS_H -#include "src/core/compression/algorithm.h" +#include <grpc/compression.h> #include <grpc/support/slice_buffer.h> /* compress 'input' to 'output' using 'algorithm'. @@ -49,4 +49,4 @@ int grpc_msg_compress(grpc_compression_algorithm algorithm, int grpc_msg_decompress(grpc_compression_algorithm algorithm, gpr_slice_buffer *input, gpr_slice_buffer *output); -#endif /* GRPC_INTERNAL_CORE_COMPRESSION_MESSAGE_COMPRESS_H */ +#endif /* GRPC_INTERNAL_CORE_COMPRESSION_MESSAGE_COMPRESS_H */ diff --git a/src/core/debug/trace.c b/src/core/debug/trace.c index 32c35e7fb3..b53dfe804b 100644 --- a/src/core/debug/trace.c +++ b/src/core/debug/trace.c @@ -35,6 +35,7 @@ #include <string.h> +#include <grpc/grpc.h> #include <grpc/support/alloc.h> #include <grpc/support/log.h> #include "src/core/support/env.h" @@ -80,27 +81,10 @@ static void parse(const char *s) { char **strings = NULL; size_t nstrings = 0; size_t i; - tracer *t; split(s, &strings, &nstrings); for (i = 0; i < nstrings; i++) { - const char *s = strings[i]; - if (0 == strcmp(s, "all")) { - for (t = tracers; t; t = t->next) { - *t->flag = 1; - } - } else { - int found = 0; - for (t = tracers; t; t = t->next) { - if (0 == strcmp(s, t->name)) { - *t->flag = 1; - found = 1; - } - } - if (!found) { - gpr_log(GPR_ERROR, "Unknown trace var: '%s'", s); - } - } + grpc_tracer_set_enabled(strings[i], 1); } for (i = 0; i < nstrings; i++) { @@ -115,9 +99,34 @@ void grpc_tracer_init(const char *env_var) { parse(e); gpr_free(e); } +} + +void grpc_tracer_shutdown(void) { while (tracers) { tracer *t = tracers; tracers = t->next; gpr_free(t); } } + +int grpc_tracer_set_enabled(const char *name, int enabled) { + tracer *t; + if (0 == strcmp(name, "all")) { + for (t = tracers; t; t = t->next) { + *t->flag = 1; + } + } else { + int found = 0; + for (t = tracers; t; t = t->next) { + if (0 == strcmp(name, t->name)) { + *t->flag = enabled; + found = 1; + } + } + if (!found) { + gpr_log(GPR_ERROR, "Unknown trace var: '%s'", name); + return 0; /* early return */ + } + } + return 1; +} diff --git a/src/core/debug/trace.h b/src/core/debug/trace.h index c02f14b7f2..fc8615bc69 100644 --- a/src/core/debug/trace.h +++ b/src/core/debug/trace.h @@ -38,5 +38,6 @@ void grpc_register_tracer(const char *name, int *flag); void grpc_tracer_init(const char *env_var_name); +void grpc_tracer_shutdown(void); #endif /* GRPC_INTERNAL_CORE_DEBUG_TRACE_H */ diff --git a/src/core/httpcli/format_request.c b/src/core/httpcli/format_request.c index af25219084..e875423e87 100644 --- a/src/core/httpcli/format_request.c +++ b/src/core/httpcli/format_request.c @@ -40,6 +40,7 @@ #include "src/core/support/string.h" #include <grpc/support/alloc.h> #include <grpc/support/slice.h> +#include <grpc/support/string_util.h> #include <grpc/support/useful.h> static void fill_common_header(const grpc_httpcli_request *request, gpr_strvec *buf) { diff --git a/src/core/httpcli/httpcli.c b/src/core/httpcli/httpcli.c index d39bcfe55d..914355a408 100644 --- a/src/core/httpcli/httpcli.c +++ b/src/core/httpcli/httpcli.c @@ -46,6 +46,7 @@ #include "src/core/support/string.h" #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> typedef struct { gpr_slice request_text; @@ -61,6 +62,7 @@ typedef struct { void *user_data; grpc_httpcli_context *context; grpc_pollset *pollset; + grpc_iomgr_object iomgr_obj; } internal_request; static grpc_httpcli_get_override g_get_override = NULL; @@ -88,6 +90,7 @@ static void finish(internal_request *req, int success) { } gpr_slice_unref(req->request_text); gpr_free(req->host); + grpc_iomgr_unregister_object(&req->iomgr_obj); gpr_free(req); } @@ -229,6 +232,7 @@ void grpc_httpcli_get(grpc_httpcli_context *context, grpc_pollset *pollset, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data) { internal_request *req; + char *name; if (g_get_override && g_get_override(request, deadline, on_response, user_data)) { return; @@ -243,6 +247,9 @@ void grpc_httpcli_get(grpc_httpcli_context *context, grpc_pollset *pollset, req->use_ssl = request->use_ssl; req->context = context; req->pollset = pollset; + gpr_asprintf(&name, "HTTP:GET:%s:%s", request->host, request->path); + grpc_iomgr_register_object(&req->iomgr_obj, name); + gpr_free(name); if (req->use_ssl) { req->host = gpr_strdup(request->host); } @@ -258,6 +265,7 @@ void grpc_httpcli_post(grpc_httpcli_context *context, grpc_pollset *pollset, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data) { internal_request *req; + char *name; if (g_post_override && g_post_override(request, body_bytes, body_size, deadline, on_response, user_data)) { return; @@ -273,10 +281,14 @@ void grpc_httpcli_post(grpc_httpcli_context *context, grpc_pollset *pollset, req->use_ssl = request->use_ssl; req->context = context; req->pollset = pollset; + gpr_asprintf(&name, "HTTP:GET:%s:%s", request->host, request->path); + grpc_iomgr_register_object(&req->iomgr_obj, name); + gpr_free(name); if (req->use_ssl) { req->host = gpr_strdup(request->host); } + grpc_pollset_set_add_pollset(&req->context->pollset_set, req->pollset); grpc_resolve_address(request->host, req->use_ssl ? "https" : "http", on_resolved, req); } diff --git a/src/core/httpcli/httpcli.h b/src/core/httpcli/httpcli.h index cee374fe8e..06699e88c2 100644 --- a/src/core/httpcli/httpcli.h +++ b/src/core/httpcli/httpcli.h @@ -93,6 +93,10 @@ void grpc_httpcli_context_init(grpc_httpcli_context *context); void grpc_httpcli_context_destroy(grpc_httpcli_context *context); /* Asynchronously perform a HTTP GET. + 'context' specifies the http context under which to do the get + 'pollset' indicates a grpc_pollset that is interested in the result + of the get - work on this pollset may be used to progress the get + operation 'request' contains request parameters - these are caller owned and can be destroyed once the call returns 'deadline' contains a deadline for the request (or gpr_inf_future) @@ -106,7 +110,19 @@ void grpc_httpcli_get(grpc_httpcli_context *context, grpc_pollset *pollset, grpc_httpcli_response_cb on_response, void *user_data); /* Asynchronously perform a HTTP POST. - When there is no body, pass in NULL as body_bytes. + 'context' specifies the http context under which to do the post + 'pollset' indicates a grpc_pollset that is interested in the result + of the post - work on this pollset may be used to progress the post + operation + 'request' contains request parameters - these are caller owned and can be + destroyed once the call returns + 'body_bytes' and 'body_size' specify the payload for the post. + When there is no body, pass in NULL as body_bytes. + 'deadline' contains a deadline for the request (or gpr_inf_future) + 'em' points to a caller owned event manager that must be alive for the + lifetime of the request + 'on_response' is a callback to report results to (and 'user_data' is a user + supplied pointer to pass to said call) Does not support ?var1=val1&var2=val2 in the path. */ void grpc_httpcli_post(grpc_httpcli_context *context, grpc_pollset *pollset, const grpc_httpcli_request *request, diff --git a/src/core/httpcli/httpcli_security_connector.c b/src/core/httpcli/httpcli_security_connector.c index 6eed5eaf12..ce0d3d5a70 100644 --- a/src/core/httpcli/httpcli_security_connector.c +++ b/src/core/httpcli/httpcli_security_connector.c @@ -39,6 +39,7 @@ #include "src/core/support/string.h" #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> #include "src/core/tsi/ssl_transport_security.h" typedef struct { diff --git a/src/core/iomgr/endpoint_pair_posix.c b/src/core/iomgr/endpoint_pair_posix.c index 9b3b63f1e7..fa2d2555d6 100644 --- a/src/core/iomgr/endpoint_pair_posix.c +++ b/src/core/iomgr/endpoint_pair_posix.c @@ -47,6 +47,7 @@ #include "src/core/support/string.h" #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> static void create_sockets(int sv[2]) { int flags; diff --git a/src/core/iomgr/fd_posix.c b/src/core/iomgr/fd_posix.c index 2ac1866a66..632d2a4609 100644 --- a/src/core/iomgr/fd_posix.c +++ b/src/core/iomgr/fd_posix.c @@ -74,6 +74,7 @@ static void freelist_fd(grpc_fd *fd) { gpr_mu_lock(&fd_freelist_mu); fd->freelist_next = fd_freelist; fd_freelist = fd; + grpc_iomgr_unregister_object(&fd->iomgr_object); gpr_mu_unlock(&fd_freelist_mu); } @@ -100,6 +101,7 @@ static grpc_fd *alloc_fd(int fd) { &r->inactive_watcher_root; r->freelist_next = NULL; r->read_watcher = r->write_watcher = NULL; + r->on_done_closure = NULL; return r; } @@ -112,7 +114,8 @@ static void destroy(grpc_fd *fd) { #ifdef GRPC_FD_REF_COUNT_DEBUG #define REF_BY(fd, n, reason) ref_by(fd, n, reason, __FILE__, __LINE__) #define UNREF_BY(fd, n, reason) unref_by(fd, n, reason, __FILE__, __LINE__) -static void ref_by(grpc_fd *fd, int n, const char *reason, const char *file, int line) { +static void ref_by(grpc_fd *fd, int n, const char *reason, const char *file, + int line) { gpr_log(GPR_DEBUG, "FD %d %p ref %d %d -> %d [%s; %s:%d]", fd->fd, fd, n, gpr_atm_no_barrier_load(&fd->refst), gpr_atm_no_barrier_load(&fd->refst) + n, reason, file, line); @@ -125,7 +128,8 @@ static void ref_by(grpc_fd *fd, int n) { } #ifdef GRPC_FD_REF_COUNT_DEBUG -static void unref_by(grpc_fd *fd, int n, const char *reason, const char *file, int line) { +static void unref_by(grpc_fd *fd, int n, const char *reason, const char *file, + int line) { gpr_atm old; gpr_log(GPR_DEBUG, "FD %d %p unref %d %d -> %d [%s; %s:%d]", fd->fd, fd, n, gpr_atm_no_barrier_load(&fd->refst), @@ -136,10 +140,6 @@ static void unref_by(grpc_fd *fd, int n) { #endif old = gpr_atm_full_fetch_add(&fd->refst, -n); if (old == n) { - if (fd->on_done_closure) { - grpc_iomgr_add_callback(fd->on_done_closure); - } - grpc_iomgr_unregister_object(&fd->iomgr_object); freelist_fd(fd); } else { GPR_ASSERT(old > n); @@ -197,13 +197,24 @@ static void wake_all_watchers_locked(grpc_fd *fd) { } } +static int has_watchers(grpc_fd *fd) { + return fd->read_watcher != NULL || fd->write_watcher != NULL || fd->inactive_watcher_root.next != &fd->inactive_watcher_root; +} + void grpc_fd_orphan(grpc_fd *fd, grpc_iomgr_closure *on_done, const char *reason) { fd->on_done_closure = on_done; shutdown(fd->fd, SHUT_RDWR); REF_BY(fd, 1, reason); /* remove active status, but keep referenced */ gpr_mu_lock(&fd->watcher_mu); - wake_all_watchers_locked(fd); + if (!has_watchers(fd)) { + close(fd->fd); + if (fd->on_done_closure) { + grpc_iomgr_add_callback(fd->on_done_closure); + } + } else { + wake_all_watchers_locked(fd); + } gpr_mu_unlock(&fd->watcher_mu); UNREF_BY(fd, 2, reason); /* drop the reference */ } @@ -225,7 +236,7 @@ void grpc_fd_unref(grpc_fd *fd) { unref_by(fd, 2); } #endif static void process_callback(grpc_iomgr_closure *closure, int success, - int allow_synchronous_callback) { + int allow_synchronous_callback) { if (allow_synchronous_callback) { closure->cb(closure->cb_arg, success); } else { @@ -265,7 +276,7 @@ static void notify_on(grpc_fd *fd, gpr_atm *st, grpc_iomgr_closure *closure, GPR_ASSERT(gpr_atm_no_barrier_load(st) == READY); gpr_atm_rel_store(st, NOT_READY); process_callback(closure, !gpr_atm_acq_load(&fd->shutdown), - allow_synchronous_callback); + allow_synchronous_callback); return; default: /* WAITING */ /* upcallptr was set to a different closure. This is an error! */ @@ -309,7 +320,7 @@ static void set_ready(grpc_fd *fd, gpr_atm *st, /* only one set_ready can be active at once (but there may be a racing notify_on) */ int success; - grpc_iomgr_closure* closure; + grpc_iomgr_closure *closure; size_t ncb = 0; gpr_mu_lock(&fd->set_state_mu); @@ -352,14 +363,22 @@ gpr_uint32 grpc_fd_begin_poll(grpc_fd *fd, grpc_pollset *pollset, GRPC_FD_REF(fd, "poll"); gpr_mu_lock(&fd->watcher_mu); + /* if we are shutdown, then don't add to the watcher set */ + if (gpr_atm_no_barrier_load(&fd->shutdown)) { + watcher->fd = NULL; + watcher->pollset = NULL; + gpr_mu_unlock(&fd->watcher_mu); + GRPC_FD_UNREF(fd, "poll"); + return 0; + } /* if there is nobody polling for read, but we need to, then start doing so */ - if (!fd->read_watcher && gpr_atm_acq_load(&fd->readst) > READY) { + if (read_mask && !fd->read_watcher && gpr_atm_acq_load(&fd->readst) > READY) { fd->read_watcher = watcher; mask |= read_mask; } /* if there is nobody polling for write, but we need to, then start doing so */ - if (!fd->write_watcher && gpr_atm_acq_load(&fd->writest) > READY) { + if (write_mask && !fd->write_watcher && gpr_atm_acq_load(&fd->writest) > READY) { fd->write_watcher = watcher; mask |= write_mask; } @@ -381,6 +400,10 @@ void grpc_fd_end_poll(grpc_fd_watcher *watcher, int got_read, int got_write) { int kick = 0; grpc_fd *fd = watcher->fd; + if (fd == NULL) { + return; + } + gpr_mu_lock(&fd->watcher_mu); if (watcher == fd->read_watcher) { /* remove read watcher, kick if we still need a read */ @@ -402,6 +425,12 @@ void grpc_fd_end_poll(grpc_fd_watcher *watcher, int got_read, int got_write) { if (kick) { maybe_wake_one_watcher_locked(fd); } + if (grpc_fd_is_orphaned(fd) && !has_watchers(fd)) { + close(fd->fd); + if (fd->on_done_closure != NULL) { + grpc_iomgr_add_callback(fd->on_done_closure); + } + } gpr_mu_unlock(&fd->watcher_mu); GRPC_FD_UNREF(fd, "poll"); diff --git a/src/core/iomgr/fd_posix.h b/src/core/iomgr/fd_posix.h index 523d040d17..94d0019fa4 100644 --- a/src/core/iomgr/fd_posix.h +++ b/src/core/iomgr/fd_posix.h @@ -62,12 +62,12 @@ struct grpc_fd { gpr_atm shutdown; /* The watcher list. - + The following watcher related fields are protected by watcher_mu. - + An fd_watcher is an ephemeral object created when an fd wants to begin polling, and destroyed after the poll. - + It denotes the fd's interest in whether to read poll or write poll or both or neither on this fd. @@ -175,4 +175,4 @@ void grpc_fd_unref(grpc_fd *fd); void grpc_fd_global_init(void); void grpc_fd_global_shutdown(void); -#endif /* GRPC_INTERNAL_CORE_IOMGR_FD_POSIX_H */ +#endif /* GRPC_INTERNAL_CORE_IOMGR_FD_POSIX_H */ diff --git a/src/core/iomgr/iomgr.c b/src/core/iomgr/iomgr.c index c6c44658df..c47528aa94 100644 --- a/src/core/iomgr/iomgr.c +++ b/src/core/iomgr/iomgr.c @@ -40,8 +40,9 @@ #include "src/core/support/string.h" #include <grpc/support/alloc.h> #include <grpc/support/log.h> -#include <grpc/support/thd.h> +#include <grpc/support/string_util.h> #include <grpc/support/sync.h> +#include <grpc/support/thd.h> static gpr_mu g_mu; static gpr_cv g_rcv; @@ -143,8 +144,8 @@ void grpc_iomgr_shutdown(void) { } if (g_root_object.next != &g_root_object) { int timeout = 0; - gpr_timespec short_deadline = gpr_time_add(gpr_now(), - gpr_time_from_millis(100)); + gpr_timespec short_deadline = + gpr_time_add(gpr_now(), gpr_time_from_millis(100)); while (gpr_cv_wait(&g_rcv, &g_mu, short_deadline) && g_cbs_head == NULL) { if (gpr_time_cmp(gpr_now(), shutdown_deadline) > 0) { timeout = 1; @@ -193,7 +194,6 @@ void grpc_iomgr_unregister_object(grpc_iomgr_object *obj) { gpr_mu_unlock(&g_mu); } - void grpc_iomgr_closure_init(grpc_iomgr_closure *closure, grpc_iomgr_cb_func cb, void *cb_arg) { closure->cb = cb; @@ -217,12 +217,10 @@ void grpc_iomgr_add_delayed_callback(grpc_iomgr_closure *closure, int success) { gpr_mu_unlock(&g_mu); } - void grpc_iomgr_add_callback(grpc_iomgr_closure *closure) { grpc_iomgr_add_delayed_callback(closure, 1 /* GPR_TRUE */); } - int grpc_maybe_call_delayed_callbacks(gpr_mu *drop_mu, int success) { int n = 0; gpr_mu *retake_mu = NULL; diff --git a/src/core/iomgr/iomgr.h b/src/core/iomgr/iomgr.h index a10e481e48..6d4a82917b 100644 --- a/src/core/iomgr/iomgr.h +++ b/src/core/iomgr/iomgr.h @@ -73,4 +73,8 @@ void grpc_iomgr_shutdown(void); * Can be called from within a callback or from anywhere else */ void grpc_iomgr_add_callback(grpc_iomgr_closure *closure); +/** As per grpc_iomgr_add_callback, with the ability to set the success + argument. */ +void grpc_iomgr_add_delayed_callback(grpc_iomgr_closure *iocb, int success); + #endif /* GRPC_INTERNAL_CORE_IOMGR_IOMGR_H */ diff --git a/src/core/iomgr/pollset.h b/src/core/iomgr/pollset.h index 334e0ebde1..c40188b3c9 100644 --- a/src/core/iomgr/pollset.h +++ b/src/core/iomgr/pollset.h @@ -58,16 +58,18 @@ void grpc_pollset_shutdown(grpc_pollset *pollset, void *shutdown_done_arg); void grpc_pollset_destroy(grpc_pollset *pollset); - /* Do some work on a pollset. May involve invoking asynchronous callbacks, or actually polling file descriptors. Requires GRPC_POLLSET_MU(pollset) locked. - May unlock GRPC_POLLSET_MU(pollset) during its execution. */ + May unlock GRPC_POLLSET_MU(pollset) during its execution. + + Returns true if some work has been done, and false if the deadline + got attained. */ int grpc_pollset_work(grpc_pollset *pollset, gpr_timespec deadline); -/* Break a pollset out of polling work +/* Break one polling thread out of polling work for this pollset. Requires GRPC_POLLSET_MU(pollset) locked. */ void grpc_pollset_kick(grpc_pollset *pollset); -#endif /* GRPC_INTERNAL_CORE_IOMGR_POLLSET_H */ +#endif /* GRPC_INTERNAL_CORE_IOMGR_POLLSET_H */ diff --git a/src/core/iomgr/pollset_kick_posix.c b/src/core/iomgr/pollset_kick_posix.c index 21c9bb6542..51021784f2 100644 --- a/src/core/iomgr/pollset_kick_posix.c +++ b/src/core/iomgr/pollset_kick_posix.c @@ -73,7 +73,7 @@ static grpc_kick_fd_info *allocate_wfd(void) { return info; } -static void destroy_wfd(grpc_kick_fd_info* wfd) { +static void destroy_wfd(grpc_kick_fd_info *wfd) { grpc_wakeup_fd_destroy(&wfd->wakeup_fd); gpr_free(wfd); } @@ -104,7 +104,8 @@ void grpc_pollset_kick_destroy(grpc_pollset_kick_state *kick_state) { GPR_ASSERT(kick_state->fd_list.next == &kick_state->fd_list); } -grpc_kick_fd_info *grpc_pollset_kick_pre_poll(grpc_pollset_kick_state *kick_state) { +grpc_kick_fd_info *grpc_pollset_kick_pre_poll( + grpc_pollset_kick_state *kick_state) { grpc_kick_fd_info *fd_info; gpr_mu_lock(&kick_state->mu); if (kick_state->kicked) { @@ -120,11 +121,13 @@ grpc_kick_fd_info *grpc_pollset_kick_pre_poll(grpc_pollset_kick_state *kick_stat return fd_info; } -void grpc_pollset_kick_consume(grpc_pollset_kick_state *kick_state, grpc_kick_fd_info *fd_info) { +void grpc_pollset_kick_consume(grpc_pollset_kick_state *kick_state, + grpc_kick_fd_info *fd_info) { grpc_wakeup_fd_consume_wakeup(&fd_info->wakeup_fd); } -void grpc_pollset_kick_post_poll(grpc_pollset_kick_state *kick_state, grpc_kick_fd_info *fd_info) { +void grpc_pollset_kick_post_poll(grpc_pollset_kick_state *kick_state, + grpc_kick_fd_info *fd_info) { gpr_mu_lock(&kick_state->mu); fd_info->next->prev = fd_info->prev; fd_info->prev->next = fd_info->next; @@ -162,5 +165,4 @@ void grpc_pollset_kick_global_destroy(void) { gpr_mu_destroy(&fd_freelist_mu); } - -#endif /* GPR_POSIX_SOCKET */ +#endif /* GPR_POSIX_SOCKET */ diff --git a/src/core/iomgr/pollset_kick_posix.h b/src/core/iomgr/pollset_kick_posix.h index b35c2cfbe0..77e32a8d51 100644 --- a/src/core/iomgr/pollset_kick_posix.h +++ b/src/core/iomgr/pollset_kick_posix.h @@ -37,6 +37,11 @@ #include "src/core/iomgr/wakeup_fd_posix.h" #include <grpc/support/sync.h> +/* pollset kicking allows breaking a thread out of polling work for + a given pollset. + writing a byte to a pipe is used as a posix-ly portable base + mechanism, and eventfds are utilized on Linux for better performance. */ + typedef struct grpc_kick_fd_info { grpc_wakeup_fd_info wakeup_fd; /* used for polling list and free list */ @@ -51,7 +56,8 @@ typedef struct grpc_pollset_kick_state { struct grpc_kick_fd_info fd_list; } grpc_pollset_kick_state; -#define GRPC_POLLSET_KICK_GET_FD(kick_fd_info) GRPC_WAKEUP_FD_GET_READ_FD(&(kick_fd_info)->wakeup_fd) +#define GRPC_POLLSET_KICK_GET_FD(kick_fd_info) \ + GRPC_WAKEUP_FD_GET_READ_FD(&(kick_fd_info)->wakeup_fd) /* This is an abstraction around the typical pipe mechanism for waking up a thread sitting in a poll() style call. */ @@ -66,18 +72,22 @@ void grpc_pollset_kick_destroy(grpc_pollset_kick_state *kick_state); * applicable. Intended for testing. */ void grpc_pollset_kick_global_init_fallback_fd(void); -/* Must be called before entering poll(). If return value is -1, this consumed +/* Must be called before entering poll(). If return value is NULL, this consumed an existing kick. Otherwise the return value is an FD to add to the poll set. */ -grpc_kick_fd_info *grpc_pollset_kick_pre_poll(grpc_pollset_kick_state *kick_state); +grpc_kick_fd_info *grpc_pollset_kick_pre_poll( + grpc_pollset_kick_state *kick_state); /* Consume an existing kick. Must be called after poll returns that the fd was readable, and before calling kick_post_poll. */ -void grpc_pollset_kick_consume(grpc_pollset_kick_state *kick_state, grpc_kick_fd_info *fd_info); +void grpc_pollset_kick_consume(grpc_pollset_kick_state *kick_state, + grpc_kick_fd_info *fd_info); /* Must be called after pre_poll, and after consume if applicable */ -void grpc_pollset_kick_post_poll(grpc_pollset_kick_state *kick_state, grpc_kick_fd_info *fd_info); +void grpc_pollset_kick_post_poll(grpc_pollset_kick_state *kick_state, + grpc_kick_fd_info *fd_info); +/* Actually kick */ void grpc_pollset_kick_kick(grpc_pollset_kick_state *kick_state); -#endif /* GRPC_INTERNAL_CORE_IOMGR_POLLSET_KICK_POSIX_H */ +#endif /* GRPC_INTERNAL_CORE_IOMGR_POLLSET_KICK_POSIX_H */ diff --git a/src/core/iomgr/pollset_multipoller_with_epoll.c b/src/core/iomgr/pollset_multipoller_with_epoll.c index 05b522bbf2..1900bbf9e1 100644 --- a/src/core/iomgr/pollset_multipoller_with_epoll.c +++ b/src/core/iomgr/pollset_multipoller_with_epoll.c @@ -54,17 +54,25 @@ static void multipoll_with_epoll_pollset_add_fd(grpc_pollset *pollset, pollset_hdr *h = pollset->data.ptr; struct epoll_event ev; int err; - - ev.events = EPOLLIN | EPOLLOUT | EPOLLET; - ev.data.ptr = fd; - err = epoll_ctl(h->epoll_fd, EPOLL_CTL_ADD, fd->fd, &ev); - if (err < 0) { - /* FDs may be added to a pollset multiple times, so EEXIST is normal. */ - if (errno != EEXIST) { - gpr_log(GPR_ERROR, "epoll_ctl add for %d failed: %s", fd->fd, - strerror(errno)); + grpc_fd_watcher watcher; + + /* We pretend to be polling whilst adding an fd to keep the fd from being + closed during the add. This may result in a spurious wakeup being assigned + to this pollset whilst adding, but that should be benign. */ + GPR_ASSERT(grpc_fd_begin_poll(fd, pollset, 0, 0, &watcher) == 0); + if (watcher.fd != NULL) { + ev.events = EPOLLIN | EPOLLOUT | EPOLLET; + ev.data.ptr = fd; + err = epoll_ctl(h->epoll_fd, EPOLL_CTL_ADD, fd->fd, &ev); + if (err < 0) { + /* FDs may be added to a pollset multiple times, so EEXIST is normal. */ + if (errno != EEXIST) { + gpr_log(GPR_ERROR, "epoll_ctl add for %d failed: %s", fd->fd, + strerror(errno)); + } } } + grpc_fd_end_poll(&watcher, 0, 0); } static void multipoll_with_epoll_pollset_del_fd(grpc_pollset *pollset, @@ -83,7 +91,7 @@ static void multipoll_with_epoll_pollset_del_fd(grpc_pollset *pollset, /* TODO(klempner): We probably want to turn this down a bit */ #define GRPC_EPOLL_MAX_EVENTS 1000 -static int multipoll_with_epoll_pollset_maybe_work( +static void multipoll_with_epoll_pollset_maybe_work( grpc_pollset *pollset, gpr_timespec deadline, gpr_timespec now, int allow_synchronous_callback) { struct epoll_event ep_ev[GRPC_EPOLL_MAX_EVENTS]; @@ -97,14 +105,7 @@ static int multipoll_with_epoll_pollset_maybe_work( * here. */ - if (gpr_time_cmp(deadline, gpr_inf_future) == 0) { - timeout_ms = -1; - } else { - timeout_ms = gpr_time_to_millis(gpr_time_sub(deadline, now)); - if (timeout_ms <= 0) { - return 1; - } - } + timeout_ms = grpc_poll_deadline_to_millis_timeout(deadline, now); pollset->counter += 1; gpr_mu_unlock(&pollset->mu); @@ -140,10 +141,10 @@ static int multipoll_with_epoll_pollset_maybe_work( gpr_mu_lock(&pollset->mu); pollset->counter -= 1; - return 1; } -static void multipoll_with_epoll_pollset_finish_shutdown(grpc_pollset *pollset) {} +static void multipoll_with_epoll_pollset_finish_shutdown( + grpc_pollset *pollset) {} static void multipoll_with_epoll_pollset_destroy(grpc_pollset *pollset) { pollset_hdr *h = pollset->data.ptr; @@ -158,9 +159,12 @@ static void epoll_kick(grpc_pollset *pollset) { } static const grpc_pollset_vtable multipoll_with_epoll_pollset = { - multipoll_with_epoll_pollset_add_fd, multipoll_with_epoll_pollset_del_fd, - multipoll_with_epoll_pollset_maybe_work, epoll_kick, - multipoll_with_epoll_pollset_finish_shutdown, multipoll_with_epoll_pollset_destroy}; + multipoll_with_epoll_pollset_add_fd, + multipoll_with_epoll_pollset_del_fd, + multipoll_with_epoll_pollset_maybe_work, + epoll_kick, + multipoll_with_epoll_pollset_finish_shutdown, + multipoll_with_epoll_pollset_destroy}; static void epoll_become_multipoller(grpc_pollset *pollset, grpc_fd **fds, size_t nfds) { diff --git a/src/core/iomgr/pollset_multipoller_with_poll_posix.c b/src/core/iomgr/pollset_multipoller_with_poll_posix.c index afce5f65db..7b717bd159 100644 --- a/src/core/iomgr/pollset_multipoller_with_poll_posix.c +++ b/src/core/iomgr/pollset_multipoller_with_poll_posix.c @@ -103,7 +103,7 @@ static void end_polling(grpc_pollset *pollset) { } } -static int multipoll_with_poll_pollset_maybe_work( +static void multipoll_with_poll_pollset_maybe_work( grpc_pollset *pollset, gpr_timespec deadline, gpr_timespec now, int allow_synchronous_callback) { int timeout; @@ -113,14 +113,7 @@ static int multipoll_with_poll_pollset_maybe_work( grpc_kick_fd_info *kfd; h = pollset->data.ptr; - if (gpr_time_cmp(deadline, gpr_inf_future) == 0) { - timeout = -1; - } else { - timeout = gpr_time_to_millis(gpr_time_sub(deadline, now)); - if (timeout <= 0) { - return 1; - } - } + timeout = grpc_poll_deadline_to_millis_timeout(deadline, now); if (h->pfd_capacity < h->fd_count + 1) { h->pfd_capacity = GPR_MAX(h->pfd_capacity * 3 / 2, h->fd_count + 1); gpr_free(h->pfds); @@ -133,7 +126,7 @@ static int multipoll_with_poll_pollset_maybe_work( kfd = grpc_pollset_kick_pre_poll(&pollset->kick_state); if (kfd == NULL) { /* Already kicked */ - return 1; + return; } h->pfds[0].fd = GRPC_POLLSET_KICK_GET_FD(kfd); h->pfds[0].events = POLLIN; @@ -161,7 +154,7 @@ static int multipoll_with_poll_pollset_maybe_work( h->del_count = 0; if (h->pfd_count == 0) { end_polling(pollset); - return 0; + return; } pollset->counter++; gpr_mu_unlock(&pollset->mu); @@ -186,6 +179,9 @@ static int multipoll_with_poll_pollset_maybe_work( grpc_pollset_kick_consume(&pollset->kick_state, kfd); } for (i = 1; i < np; i++) { + if (h->watchers[i].fd == NULL) { + continue; + } if (h->pfds[i].revents & (POLLIN | POLLHUP | POLLERR)) { grpc_fd_become_readable(h->watchers[i].fd, allow_synchronous_callback); } @@ -198,8 +194,6 @@ static int multipoll_with_poll_pollset_maybe_work( gpr_mu_lock(&pollset->mu); pollset->counter--; - - return 1; } static void multipoll_with_poll_pollset_kick(grpc_pollset *p) { @@ -231,9 +225,12 @@ static void multipoll_with_poll_pollset_destroy(grpc_pollset *pollset) { } static const grpc_pollset_vtable multipoll_with_poll_pollset = { - multipoll_with_poll_pollset_add_fd, multipoll_with_poll_pollset_del_fd, - multipoll_with_poll_pollset_maybe_work, multipoll_with_poll_pollset_kick, - multipoll_with_poll_pollset_finish_shutdown, multipoll_with_poll_pollset_destroy}; + multipoll_with_poll_pollset_add_fd, + multipoll_with_poll_pollset_del_fd, + multipoll_with_poll_pollset_maybe_work, + multipoll_with_poll_pollset_kick, + multipoll_with_poll_pollset_finish_shutdown, + multipoll_with_poll_pollset_destroy}; void grpc_poll_become_multipoller(grpc_pollset *pollset, grpc_fd **fds, size_t nfds) { diff --git a/src/core/iomgr/pollset_posix.c b/src/core/iomgr/pollset_posix.c index 796b1b9ebf..15ed8e75e6 100644 --- a/src/core/iomgr/pollset_posix.c +++ b/src/core/iomgr/pollset_posix.c @@ -99,6 +99,7 @@ void grpc_pollset_init(grpc_pollset *pollset) { grpc_pollset_kick_init(&pollset->kick_state); pollset->in_flight_cbs = 0; pollset->shutting_down = 0; + pollset->called_shutdown = 0; become_basic_pollset(pollset, NULL); } @@ -122,7 +123,6 @@ static void finish_shutdown(grpc_pollset *pollset) { int grpc_pollset_work(grpc_pollset *pollset, gpr_timespec deadline) { /* pollset->mu already held */ gpr_timespec now = gpr_now(); - int r; if (gpr_time_cmp(now, deadline) > 0) { return 0; } @@ -136,12 +136,13 @@ int grpc_pollset_work(grpc_pollset *pollset, gpr_timespec deadline) { return 1; } gpr_tls_set(&g_current_thread_poller, (gpr_intptr)pollset); - r = pollset->vtable->maybe_work(pollset, deadline, now, 1); + pollset->vtable->maybe_work(pollset, deadline, now, 1); gpr_tls_set(&g_current_thread_poller, 0); if (pollset->shutting_down) { if (pollset->counter > 0) { grpc_pollset_kick(pollset); - } else if (pollset->in_flight_cbs == 0) { + } else if (!pollset->called_shutdown && pollset->in_flight_cbs == 0) { + pollset->called_shutdown = 1; gpr_mu_unlock(&pollset->mu); finish_shutdown(pollset); /* Continuing to access pollset here is safe -- it is the caller's @@ -151,27 +152,29 @@ int grpc_pollset_work(grpc_pollset *pollset, gpr_timespec deadline) { gpr_mu_lock(&pollset->mu); } } - return r; + return 1; } void grpc_pollset_shutdown(grpc_pollset *pollset, void (*shutdown_done)(void *arg), void *shutdown_done_arg) { - int in_flight_cbs; - int counter; + int call_shutdown = 0; gpr_mu_lock(&pollset->mu); GPR_ASSERT(!pollset->shutting_down); pollset->shutting_down = 1; - in_flight_cbs = pollset->in_flight_cbs; - counter = pollset->counter; + if (!pollset->called_shutdown && pollset->in_flight_cbs == 0 && + pollset->counter == 0) { + pollset->called_shutdown = 1; + call_shutdown = 1; + } pollset->shutdown_done_cb = shutdown_done; pollset->shutdown_done_arg = shutdown_done_arg; - if (counter > 0) { + if (pollset->counter > 0) { grpc_pollset_kick(pollset); } gpr_mu_unlock(&pollset->mu); - if (in_flight_cbs == 0 && counter == 0) { + if (call_shutdown) { finish_shutdown(pollset); } } @@ -184,6 +187,22 @@ void grpc_pollset_destroy(grpc_pollset *pollset) { gpr_mu_destroy(&pollset->mu); } +int grpc_poll_deadline_to_millis_timeout(gpr_timespec deadline, gpr_timespec now) { + gpr_timespec timeout; + static const int max_spin_polling_us = 10; + if (gpr_time_cmp(deadline, gpr_inf_future) == 0) { + return -1; + } + if (gpr_time_cmp( + deadline, + gpr_time_add(now, gpr_time_from_micros(max_spin_polling_us))) <= 0) { + return 0; + } + timeout = gpr_time_sub(deadline, now); + return gpr_time_to_millis( + gpr_time_add(timeout, gpr_time_from_nanos(GPR_NS_PER_SEC - 1))); +} + /* * basic_pollset - a vtable that provides polling for zero or one file * descriptor via poll() @@ -318,9 +337,9 @@ static void basic_pollset_del_fd(grpc_pollset *pollset, grpc_fd *fd) { } } -static int basic_pollset_maybe_work(grpc_pollset *pollset, - gpr_timespec deadline, gpr_timespec now, - int allow_synchronous_callback) { +static void basic_pollset_maybe_work(grpc_pollset *pollset, + gpr_timespec deadline, gpr_timespec now, + int allow_synchronous_callback) { struct pollfd pfd[2]; grpc_fd *fd; grpc_fd_watcher fd_watcher; @@ -333,25 +352,18 @@ static int basic_pollset_maybe_work(grpc_pollset *pollset, /* Give do_promote priority so we don't starve it out */ gpr_mu_unlock(&pollset->mu); gpr_mu_lock(&pollset->mu); - return 1; + return; } fd = pollset->data.ptr; if (fd && grpc_fd_is_orphaned(fd)) { GRPC_FD_UNREF(fd, "basicpoll"); fd = pollset->data.ptr = NULL; } - if (gpr_time_cmp(deadline, gpr_inf_future) == 0) { - timeout = -1; - } else { - timeout = gpr_time_to_millis(gpr_time_sub(deadline, now)); - if (timeout <= 0) { - return 1; - } - } + timeout = grpc_poll_deadline_to_millis_timeout(deadline, now); kfd = grpc_pollset_kick_pre_poll(&pollset->kick_state); if (kfd == NULL) { /* Already kicked */ - return 1; + return; } pfd[0].fd = GRPC_POLLSET_KICK_GET_FD(kfd); pfd[0].events = POLLIN; @@ -405,7 +417,6 @@ static int basic_pollset_maybe_work(grpc_pollset *pollset, gpr_mu_lock(&pollset->mu); pollset->counter--; - return 1; } static void basic_pollset_destroy(grpc_pollset *pollset) { @@ -417,7 +428,7 @@ static void basic_pollset_destroy(grpc_pollset *pollset) { } static const grpc_pollset_vtable basic_pollset = { - basic_pollset_add_fd, basic_pollset_del_fd, basic_pollset_maybe_work, + basic_pollset_add_fd, basic_pollset_del_fd, basic_pollset_maybe_work, kick_using_pollset_kick, basic_pollset_destroy, basic_pollset_destroy}; static void become_basic_pollset(grpc_pollset *pollset, grpc_fd *fd_or_null) { diff --git a/src/core/iomgr/pollset_posix.h b/src/core/iomgr/pollset_posix.h index 2b897caa4b..53585a2886 100644 --- a/src/core/iomgr/pollset_posix.h +++ b/src/core/iomgr/pollset_posix.h @@ -56,6 +56,7 @@ typedef struct grpc_pollset { int counter; int in_flight_cbs; int shutting_down; + int called_shutdown; void (*shutdown_done_cb)(void *arg); void *shutdown_done_arg; union { @@ -67,8 +68,8 @@ typedef struct grpc_pollset { struct grpc_pollset_vtable { void (*add_fd)(grpc_pollset *pollset, struct grpc_fd *fd); void (*del_fd)(grpc_pollset *pollset, struct grpc_fd *fd); - int (*maybe_work)(grpc_pollset *pollset, gpr_timespec deadline, - gpr_timespec now, int allow_synchronous_callback); + void (*maybe_work)(grpc_pollset *pollset, gpr_timespec deadline, + gpr_timespec now, int allow_synchronous_callback); void (*kick)(grpc_pollset *pollset); void (*finish_shutdown)(grpc_pollset *pollset); void (*destroy)(grpc_pollset *pollset); @@ -93,6 +94,15 @@ int grpc_kick_read_fd(grpc_pollset *p); /* Call after polling has been kicked to leave the kicked state */ void grpc_kick_drain(grpc_pollset *p); +/* Convert a timespec to milliseconds: + - very small or negative poll times are clamped to zero to do a + non-blocking poll (which becomes spin polling) + - other small values are rounded up to one millisecond + - longer than a millisecond polls are rounded up to the next nearest + millisecond to avoid spinning + - infinite timeouts are converted to -1 */ +int grpc_poll_deadline_to_millis_timeout(gpr_timespec deadline, gpr_timespec now); + /* turn a pollset into a multipoller: platform specific */ typedef void (*grpc_platform_become_multipoller_type)(grpc_pollset *pollset, struct grpc_fd **fds, diff --git a/src/core/iomgr/pollset_set.h b/src/core/iomgr/pollset_set.h index 335ffb21b9..98e3b552a7 100644 --- a/src/core/iomgr/pollset_set.h +++ b/src/core/iomgr/pollset_set.h @@ -39,7 +39,7 @@ /* A grpc_pollset_set is a set of pollsets that are interested in an action. Adding a pollset to a pollset_set automatically adds any fd's (etc) that have been registered with the set_set with that pollset. - Registering fd's automatically iterates all current pollsets. */ + Registering fd's automatically adds them to all current pollsets. */ #ifdef GPR_POSIX_SOCKET #include "src/core/iomgr/pollset_set_posix.h" diff --git a/src/core/iomgr/pollset_set_posix.c b/src/core/iomgr/pollset_set_posix.c index c00d6e6e57..005e938398 100644 --- a/src/core/iomgr/pollset_set_posix.c +++ b/src/core/iomgr/pollset_set_posix.c @@ -49,7 +49,11 @@ void grpc_pollset_set_init(grpc_pollset_set *pollset_set) { } void grpc_pollset_set_destroy(grpc_pollset_set *pollset_set) { + size_t i; gpr_mu_destroy(&pollset_set->mu); + for (i = 0; i < pollset_set->fd_count; i++) { + GRPC_FD_UNREF(pollset_set->fds[i], "pollset"); + } gpr_free(pollset_set->pollsets); gpr_free(pollset_set->fds); } @@ -95,6 +99,7 @@ void grpc_pollset_set_add_fd(grpc_pollset_set *pollset_set, grpc_fd *fd) { pollset_set->fds = gpr_realloc( pollset_set->fds, pollset_set->fd_capacity * sizeof(*pollset_set->fds)); } + GRPC_FD_REF(fd, "pollset_set"); pollset_set->fds[pollset_set->fd_count++] = fd; for (i = 0; i < pollset_set->pollset_count; i++) { grpc_pollset_add_fd(pollset_set->pollsets[i], fd); @@ -110,6 +115,7 @@ void grpc_pollset_set_del_fd(grpc_pollset_set *pollset_set, grpc_fd *fd) { pollset_set->fd_count--; GPR_SWAP(grpc_fd *, pollset_set->fds[i], pollset_set->fds[pollset_set->pollset_count]); + GRPC_FD_UNREF(fd, "pollset_set"); break; } } diff --git a/src/core/iomgr/pollset_set_windows.c b/src/core/iomgr/pollset_set_windows.c index d295c64b5e..b9c209cd2c 100644 --- a/src/core/iomgr/pollset_set_windows.c +++ b/src/core/iomgr/pollset_set_windows.c @@ -44,4 +44,7 @@ void grpc_pollset_set_destroy(grpc_pollset_set *pollset_set) {} void grpc_pollset_set_add_pollset(grpc_pollset_set *pollset_set, grpc_pollset *pollset) {} +void grpc_pollset_set_del_pollset(grpc_pollset_set *pollset_set, + grpc_pollset *pollset) {} + #endif /* GPR_WINSOCK_SOCKET */ diff --git a/src/core/iomgr/pollset_set_windows.h b/src/core/iomgr/pollset_set_windows.h index 0f91f1ede7..cada0d2b61 100644 --- a/src/core/iomgr/pollset_set_windows.h +++ b/src/core/iomgr/pollset_set_windows.h @@ -34,8 +34,6 @@ #ifndef GRPC_INTERNAL_CORE_IOMGR_POLLSET_SET_WINDOWS_H #define GRPC_INTERNAL_CORE_IOMGR_POLLSET_SET_WINDOWS_H -typedef struct grpc_pollset_set { - void *unused; -} grpc_pollset_set; +typedef struct grpc_pollset_set { void *unused; } grpc_pollset_set; #endif /* GRPC_INTERNAL_CORE_IOMGR_POLLSET_WINDOWS_H */ diff --git a/src/core/iomgr/pollset_windows.c b/src/core/iomgr/pollset_windows.c index 88db774cc4..8d6bc79c96 100644 --- a/src/core/iomgr/pollset_windows.c +++ b/src/core/iomgr/pollset_windows.c @@ -39,6 +39,7 @@ #include "src/core/iomgr/alarm_internal.h" #include "src/core/iomgr/iomgr_internal.h" +#include "src/core/iomgr/pollset.h" #include "src/core/iomgr/pollset_windows.h" /* There isn't really any such thing as a pollset under Windows, due to the @@ -47,17 +48,24 @@ won't actually do any polling, and return as quickly as possible. */ void grpc_pollset_init(grpc_pollset *pollset) { + memset(pollset, 0, sizeof(*pollset)); gpr_mu_init(&pollset->mu); + gpr_cv_init(&pollset->cv); } void grpc_pollset_shutdown(grpc_pollset *pollset, void (*shutdown_done)(void *arg), void *shutdown_done_arg) { + gpr_mu_lock(&pollset->mu); + pollset->shutting_down = 1; + gpr_cv_broadcast(&pollset->cv); + gpr_mu_unlock(&pollset->mu); shutdown_done(shutdown_done_arg); } void grpc_pollset_destroy(grpc_pollset *pollset) { gpr_mu_destroy(&pollset->mu); + gpr_cv_destroy(&pollset->cv); } int grpc_pollset_work(grpc_pollset *pollset, gpr_timespec deadline) { @@ -66,15 +74,20 @@ int grpc_pollset_work(grpc_pollset *pollset, gpr_timespec deadline) { if (gpr_time_cmp(now, deadline) > 0) { return 0 /* GPR_FALSE */; } - if (grpc_maybe_call_delayed_callbacks(NULL, 1 /* GPR_TRUE */)) { + if (grpc_maybe_call_delayed_callbacks(&pollset->mu, 1 /* GPR_TRUE */)) { return 1 /* GPR_TRUE */; } - if (grpc_alarm_check(NULL, now, &deadline)) { + if (grpc_alarm_check(&pollset->mu, now, &deadline)) { return 1 /* GPR_TRUE */; } - return 0 /* GPR_FALSE */; + if (!pollset->shutting_down) { + gpr_cv_wait(&pollset->cv, &pollset->mu, deadline); + } + return 1 /* GPR_TRUE */; } -void grpc_pollset_kick(grpc_pollset *p) { } +void grpc_pollset_kick(grpc_pollset *p) { + gpr_cv_signal(&p->cv); +} -#endif /* GPR_WINSOCK_SOCKET */ +#endif /* GPR_WINSOCK_SOCKET */ diff --git a/src/core/iomgr/pollset_windows.h b/src/core/iomgr/pollset_windows.h index acd82d0a0a..57a2907926 100644 --- a/src/core/iomgr/pollset_windows.h +++ b/src/core/iomgr/pollset_windows.h @@ -37,18 +37,18 @@ #include <windows.h> #include <grpc/support/sync.h> -#include "src/core/iomgr/pollset_kick.h" #include "src/core/iomgr/socket_windows.h" /* There isn't really any such thing as a pollset under Windows, due to the nature of the IO completion ports. A Windows "pollset" is merely a mutex - and a condition variable, as this is the minimal set of features we need - implemented for the rest of grpc. But we won't use them directly. */ + and a condition variable, used to synchronize with the IOCP. */ typedef struct grpc_pollset { gpr_mu mu; + gpr_cv cv; + int shutting_down; } grpc_pollset; #define GRPC_POLLSET_MU(pollset) (&(pollset)->mu) -#endif /* GRPC_INTERNAL_CORE_IOMGR_POLLSET_WINDOWS_H */ +#endif /* GRPC_INTERNAL_CORE_IOMGR_POLLSET_WINDOWS_H */ diff --git a/src/core/iomgr/resolve_address_posix.c b/src/core/iomgr/resolve_address_posix.c index fcf48fe0d7..dbf884c769 100644 --- a/src/core/iomgr/resolve_address_posix.c +++ b/src/core/iomgr/resolve_address_posix.c @@ -47,6 +47,7 @@ #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/thd.h> #include <grpc/support/time.h> @@ -154,9 +155,9 @@ static void do_request(void *rp) { grpc_resolve_cb cb = r->cb; gpr_free(r->name); gpr_free(r->default_port); + cb(arg, resolved); grpc_iomgr_unregister_object(&r->iomgr_object); gpr_free(r); - cb(arg, resolved); } void grpc_resolved_addresses_destroy(grpc_resolved_addresses *addrs) { diff --git a/src/core/iomgr/resolve_address_windows.c b/src/core/iomgr/resolve_address_windows.c index 7d0d2f9e7a..fb5fd0d4f6 100644 --- a/src/core/iomgr/resolve_address_windows.c +++ b/src/core/iomgr/resolve_address_windows.c @@ -46,6 +46,7 @@ #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/thd.h> #include <grpc/support/time.h> @@ -134,9 +135,9 @@ static void do_request(void *rp) { grpc_resolve_cb cb = r->cb; gpr_free(r->name); gpr_free(r->default_port); + grpc_iomgr_unregister_object(&r->iomgr_object); gpr_free(r); cb(arg, resolved); - grpc_iomgr_unregister_object(&r->iomgr_object); } void grpc_resolved_addresses_destroy(grpc_resolved_addresses *addrs) { diff --git a/src/core/iomgr/sockaddr_utils.c b/src/core/iomgr/sockaddr_utils.c index 3d202a5cc8..e91b94f8c8 100644 --- a/src/core/iomgr/sockaddr_utils.c +++ b/src/core/iomgr/sockaddr_utils.c @@ -40,6 +40,7 @@ #include <grpc/support/host_port.h> #include <grpc/support/log.h> #include <grpc/support/port_platform.h> +#include <grpc/support/string_util.h> static const gpr_uint8 kV4MappedPrefix[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}; diff --git a/src/core/iomgr/socket_windows.c b/src/core/iomgr/socket_windows.c index e4ba0a2b66..fbf3fdc949 100644 --- a/src/core/iomgr/socket_windows.c +++ b/src/core/iomgr/socket_windows.c @@ -85,13 +85,13 @@ int grpc_winsocket_shutdown(grpc_winsocket *socket) { both memory and sockets. */ void grpc_winsocket_orphan(grpc_winsocket *winsocket) { SOCKET socket = winsocket->socket; + grpc_iomgr_unregister_object(&winsocket->iomgr_object); if (winsocket->read_info.outstanding || winsocket->write_info.outstanding) { grpc_iocp_socket_orphan(winsocket); } else { grpc_winsocket_destroy(winsocket); } closesocket(socket); - grpc_iomgr_unregister_object(&winsocket->iomgr_object); } void grpc_winsocket_destroy(grpc_winsocket *winsocket) { diff --git a/src/core/iomgr/tcp_client.h b/src/core/iomgr/tcp_client.h index e1fdf235ec..0fa08b52b0 100644 --- a/src/core/iomgr/tcp_client.h +++ b/src/core/iomgr/tcp_client.h @@ -41,10 +41,12 @@ /* Asynchronously connect to an address (specified as (addr, len)), and call cb with arg and the completed connection when done (or call cb with arg and - NULL on failure) */ + NULL on failure). + interested_parties points to a set of pollsets that would be interested + in this connection being established (in order to continue their work) */ void grpc_tcp_client_connect(void (*cb)(void *arg, grpc_endpoint *tcp), void *arg, grpc_pollset_set *interested_parties, const struct sockaddr *addr, int addr_len, gpr_timespec deadline); -#endif /* GRPC_INTERNAL_CORE_IOMGR_TCP_CLIENT_H */ +#endif /* GRPC_INTERNAL_CORE_IOMGR_TCP_CLIENT_H */ diff --git a/src/core/iomgr/tcp_client_posix.c b/src/core/iomgr/tcp_client_posix.c index 2cd4aa2f45..bbf7711588 100644 --- a/src/core/iomgr/tcp_client_posix.c +++ b/src/core/iomgr/tcp_client_posix.c @@ -51,6 +51,7 @@ #include "src/core/support/string.h" #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> #include <grpc/support/time.h> typedef struct { @@ -222,8 +223,7 @@ void grpc_tcp_client_connect(void (*cb)(void *arg, grpc_endpoint *ep), fdobj = grpc_fd_create(fd, name); if (err >= 0) { - cb(arg, - grpc_tcp_create(fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE)); + cb(arg, grpc_tcp_create(fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE)); goto done; } diff --git a/src/core/iomgr/tcp_client_windows.c b/src/core/iomgr/tcp_client_windows.c index 88141a3dc2..b1a169b519 100644 --- a/src/core/iomgr/tcp_client_windows.c +++ b/src/core/iomgr/tcp_client_windows.c @@ -52,7 +52,7 @@ #include "src/core/iomgr/socket_windows.h" typedef struct { - void(*cb)(void *arg, grpc_endpoint *tcp); + void (*cb)(void *arg, grpc_endpoint *tcp); void *cb_arg; gpr_mu mu; grpc_winsocket *socket; @@ -86,7 +86,7 @@ static void on_connect(void *acp, int from_iocp) { SOCKET sock = ac->socket->socket; grpc_endpoint *ep = NULL; grpc_winsocket_callback_info *info = &ac->socket->write_info; - void(*cb)(void *arg, grpc_endpoint *tcp) = ac->cb; + void (*cb)(void *arg, grpc_endpoint *tcp) = ac->cb; void *cb_arg = ac->cb_arg; int aborted; @@ -99,8 +99,7 @@ static void on_connect(void *acp, int from_iocp) { DWORD transfered_bytes = 0; DWORD flags; BOOL wsa_success = WSAGetOverlappedResult(sock, &info->overlapped, - &transfered_bytes, FALSE, - &flags); + &transfered_bytes, FALSE, &flags); info->outstanding = 0; GPR_ASSERT(transfered_bytes == 0); if (!wsa_success) { @@ -176,9 +175,9 @@ void grpc_tcp_client_connect(void (*cb)(void *arg, grpc_endpoint *tcp), /* Grab the function pointer for ConnectEx for that specific socket. It may change depending on the interface. */ - status = WSAIoctl(sock, SIO_GET_EXTENSION_FUNCTION_POINTER, - &guid, sizeof(guid), &ConnectEx, sizeof(ConnectEx), - &ioctl_num_bytes, NULL, NULL); + status = + WSAIoctl(sock, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, sizeof(guid), + &ConnectEx, sizeof(ConnectEx), &ioctl_num_bytes, NULL, NULL); if (status != 0) { message = "Unable to retrieve ConnectEx pointer: %s"; @@ -187,8 +186,7 @@ void grpc_tcp_client_connect(void (*cb)(void *arg, grpc_endpoint *tcp), grpc_sockaddr_make_wildcard6(0, &local_address); - status = bind(sock, (struct sockaddr *) &local_address, - sizeof(local_address)); + status = bind(sock, (struct sockaddr *)&local_address, sizeof(local_address)); if (status != 0) { message = "Unable to bind socket: %s"; goto failure; @@ -234,4 +232,4 @@ failure: cb(arg, NULL); } -#endif /* GPR_WINSOCK_SOCKET */ +#endif /* GPR_WINSOCK_SOCKET */ diff --git a/src/core/iomgr/tcp_posix.c b/src/core/iomgr/tcp_posix.c index e3289f6806..9ad089af66 100644 --- a/src/core/iomgr/tcp_posix.c +++ b/src/core/iomgr/tcp_posix.c @@ -266,7 +266,7 @@ typedef struct { grpc_endpoint base; grpc_fd *em_fd; int fd; - int iov_size; /* Number of slices to allocate per read attempt */ + int iov_size; /* Number of slices to allocate per read attempt */ int finished_edge; size_t slice_size; gpr_refcount refcount; @@ -412,8 +412,7 @@ static void grpc_tcp_continue_read(grpc_tcp *tcp) { ++tcp->iov_size; } GPR_ASSERT(slice_state_has_available(&read_state)); - slice_state_transfer_ownership(&read_state, &final_slices, - &final_nslices); + slice_state_transfer_ownership(&read_state, &final_slices, &final_nslices); call_read_cb(tcp, final_slices, final_nslices, GRPC_ENDPOINT_CB_OK); slice_state_destroy(&read_state); grpc_tcp_unref(tcp); diff --git a/src/core/iomgr/tcp_server_posix.c b/src/core/iomgr/tcp_server_posix.c index 759a493795..5854031c9b 100644 --- a/src/core/iomgr/tcp_server_posix.c +++ b/src/core/iomgr/tcp_server_posix.c @@ -63,6 +63,7 @@ #include "src/core/support/string.h" #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> #include <grpc/support/sync.h> #include <grpc/support/time.h> @@ -107,6 +108,7 @@ struct grpc_tcp_server { /* destroyed port count: how many ports are completely destroyed */ size_t destroyed_ports; + /* is this server shutting down? (boolean) */ int shutdown; /* all listening ports */ @@ -118,7 +120,9 @@ struct grpc_tcp_server { void (*shutdown_complete)(void *); void *shutdown_complete_arg; + /* all pollsets interested in new connections */ grpc_pollset **pollsets; + /* number of pollsets in the pollsets array */ size_t pollset_count; }; @@ -159,6 +163,9 @@ static void destroyed_port(void *server, int success) { static void dont_care_about_shutdown_completion(void *ignored) {} +/* called when all listening endpoints have been shutdown, so no further + events will be received on them - at this point it's safe to destroy + things */ static void deactivated_all_ports(grpc_tcp_server *s) { size_t i; @@ -335,9 +342,8 @@ static void on_read(void *arg, int success) { for (i = 0; i < sp->server->pollset_count; i++) { grpc_pollset_add_fd(sp->server->pollsets[i], fdobj); } - sp->server->cb( - sp->server->cb_arg, - grpc_tcp_create(fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE)); + sp->server->cb(sp->server->cb_arg, + grpc_tcp_create(fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE)); gpr_free(name); gpr_free(addr_str); diff --git a/src/core/iomgr/tcp_server_windows.c b/src/core/iomgr/tcp_server_windows.c index 9ef369dfd8..d70968de88 100644 --- a/src/core/iomgr/tcp_server_windows.c +++ b/src/core/iomgr/tcp_server_windows.c @@ -41,6 +41,7 @@ #include <grpc/support/alloc.h> #include <grpc/support/log.h> #include <grpc/support/log_win32.h> +#include <grpc/support/string_util.h> #include <grpc/support/sync.h> #include <grpc/support/time.h> diff --git a/src/core/iomgr/tcp_windows.c b/src/core/iomgr/tcp_windows.c index 3341f558a3..15759c398a 100644 --- a/src/core/iomgr/tcp_windows.c +++ b/src/core/iomgr/tcp_windows.c @@ -41,6 +41,7 @@ #include <grpc/support/log.h> #include <grpc/support/log_win32.h> #include <grpc/support/slice_buffer.h> +#include <grpc/support/string_util.h> #include <grpc/support/useful.h> #include "src/core/iomgr/alarm.h" @@ -153,7 +154,7 @@ static void on_read(void *tcpp, int from_iocp) { status = GRPC_ENDPOINT_CB_ERROR; } else { if (info->bytes_transfered != 0) { - sub = gpr_slice_sub(tcp->read_slice, 0, info->bytes_transfered); + sub = gpr_slice_sub_no_ref(tcp->read_slice, 0, info->bytes_transfered); status = GRPC_ENDPOINT_CB_OK; slice = ⊂ nslices = 1; diff --git a/src/core/json/json.h b/src/core/json/json.h index 69cbac17dc..b78b42a5b2 100644 --- a/src/core/json/json.h +++ b/src/core/json/json.h @@ -60,7 +60,7 @@ typedef struct grpc_json { * strings in the tree. The input stream's UTF-8 isn't validated, * as in, what you input is what you get as an output. * - * All the keys and values in the grpc_json_t objects will be strings + * All the keys and values in the grpc_json objects will be strings * pointing at your input buffer. * * Delete the allocated tree afterward using grpc_json_destroy(). diff --git a/src/core/json/json_reader.c b/src/core/json/json_reader.c index 5ea4e9569c..c14094c290 100644 --- a/src/core/json/json_reader.c +++ b/src/core/json/json_reader.c @@ -151,7 +151,7 @@ grpc_json_reader_status grpc_json_reader_run(grpc_json_reader* reader) { case GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL: case GRPC_JSON_STATE_VALUE_NUMBER_ZERO: case GRPC_JSON_STATE_VALUE_NUMBER_EPM: - success = json_reader_set_number(reader); + success = (gpr_uint32)json_reader_set_number(reader); if (!success) return GRPC_JSON_PARSE_ERROR; json_reader_string_clear(reader); reader->state = GRPC_JSON_STATE_VALUE_END; @@ -177,7 +177,7 @@ grpc_json_reader_status grpc_json_reader_run(grpc_json_reader* reader) { case GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL: case GRPC_JSON_STATE_VALUE_NUMBER_ZERO: case GRPC_JSON_STATE_VALUE_NUMBER_EPM: - success = json_reader_set_number(reader); + success = (gpr_uint32)json_reader_set_number(reader); if (!success) return GRPC_JSON_PARSE_ERROR; json_reader_string_clear(reader); reader->state = GRPC_JSON_STATE_VALUE_END; @@ -410,8 +410,8 @@ grpc_json_reader_status grpc_json_reader_run(grpc_json_reader* reader) { } else { return GRPC_JSON_PARSE_ERROR; } - reader->unicode_char <<= 4; - reader->unicode_char |= c; + reader->unicode_char = (gpr_uint16)(reader->unicode_char << 4); + reader->unicode_char = (gpr_uint16)(reader->unicode_char | c); switch (reader->state) { case GRPC_JSON_STATE_STRING_ESCAPE_U1: @@ -438,8 +438,8 @@ grpc_json_reader_status grpc_json_reader_run(grpc_json_reader* reader) { if (reader->unicode_high_surrogate == 0) return GRPC_JSON_PARSE_ERROR; utf32 = 0x10000; - utf32 += (reader->unicode_high_surrogate - 0xd800) * 0x400; - utf32 += reader->unicode_char - 0xdc00; + utf32 += (gpr_uint32)((reader->unicode_high_surrogate - 0xd800) * 0x400); + utf32 += (gpr_uint32)(reader->unicode_char - 0xdc00); json_reader_string_add_utf32(reader, utf32); reader->unicode_high_surrogate = 0; } else { diff --git a/src/core/json/json_string.c b/src/core/json/json_string.c index 13f816995b..03c1099167 100644 --- a/src/core/json/json_string.c +++ b/src/core/json/json_string.c @@ -83,7 +83,7 @@ static void json_writer_output_check(void* userdata, size_t needed) { if (state->free_space >= needed) return; needed -= state->free_space; /* Round up by 256 bytes. */ - needed = (needed + 0xff) & ~0xff; + needed = (needed + 0xff) & ~0xffU; state->output = gpr_realloc(state->output, state->allocated + needed); state->free_space += needed; state->allocated += needed; @@ -128,7 +128,7 @@ static void json_reader_string_add_char(void* userdata, gpr_uint32 c) { json_reader_userdata* state = userdata; GPR_ASSERT(state->string_ptr < state->input); GPR_ASSERT(c <= 0xff); - *state->string_ptr++ = (char)c; + *state->string_ptr++ = (gpr_uint8)c; } /* We are converting a UTF-32 character into UTF-8 here, @@ -138,22 +138,22 @@ static void json_reader_string_add_utf32(void* userdata, gpr_uint32 c) { if (c <= 0x7f) { json_reader_string_add_char(userdata, c); } else if (c <= 0x7ff) { - int b1 = 0xc0 | ((c >> 6) & 0x1f); - int b2 = 0x80 | (c & 0x3f); + gpr_uint32 b1 = 0xc0 | ((c >> 6) & 0x1f); + gpr_uint32 b2 = 0x80 | (c & 0x3f); json_reader_string_add_char(userdata, b1); json_reader_string_add_char(userdata, b2); } else if (c <= 0xffff) { - int b1 = 0xe0 | ((c >> 12) & 0x0f); - int b2 = 0x80 | ((c >> 6) & 0x3f); - int b3 = 0x80 | (c & 0x3f); + gpr_uint32 b1 = 0xe0 | ((c >> 12) & 0x0f); + gpr_uint32 b2 = 0x80 | ((c >> 6) & 0x3f); + gpr_uint32 b3 = 0x80 | (c & 0x3f); json_reader_string_add_char(userdata, b1); json_reader_string_add_char(userdata, b2); json_reader_string_add_char(userdata, b3); } else if (c <= 0x1fffff) { - int b1 = 0xf0 | ((c >> 18) & 0x07); - int b2 = 0x80 | ((c >> 12) & 0x3f); - int b3 = 0x80 | ((c >> 6) & 0x3f); - int b4 = 0x80 | (c & 0x3f); + gpr_uint32 b1 = 0xf0 | ((c >> 18) & 0x07); + gpr_uint32 b2 = 0x80 | ((c >> 12) & 0x3f); + gpr_uint32 b3 = 0x80 | ((c >> 6) & 0x3f); + gpr_uint32 b4 = 0x80 | (c & 0x3f); json_reader_string_add_char(userdata, b1); json_reader_string_add_char(userdata, b2); json_reader_string_add_char(userdata, b3); diff --git a/src/core/json/json_writer.c b/src/core/json/json_writer.c index a40bf1733e..bed9a9bfa5 100644 --- a/src/core/json/json_writer.c +++ b/src/core/json/json_writer.c @@ -66,7 +66,7 @@ static void json_writer_output_indent( " " " "; - unsigned spaces = writer->depth * writer->indent; + unsigned spaces = (unsigned)(writer->depth * writer->indent); if (writer->indent == 0) return; @@ -78,7 +78,7 @@ static void json_writer_output_indent( while (spaces >= (sizeof(spacesstr) - 1)) { json_writer_output_string_with_len(writer, spacesstr, sizeof(spacesstr) - 1); - spaces -= (sizeof(spacesstr) - 1); + spaces -= (unsigned)(sizeof(spacesstr) - 1); } if (spaces == 0) return; @@ -119,7 +119,7 @@ static void json_writer_escape_string(grpc_json_writer* writer, break; } else if ((c >= 32) && (c <= 126)) { if ((c == '\\') || (c == '"')) json_writer_output_char(writer, '\\'); - json_writer_output_char(writer, c); + json_writer_output_char(writer, (char)c); } else if ((c < 32) || (c == 127)) { switch (c) { case '\b': @@ -160,7 +160,7 @@ static void json_writer_escape_string(grpc_json_writer* writer, } for (i = 0; i < extra; i++) { utf32 <<= 6; - c = *string++; + c = (gpr_uint8)(*string++); /* Breaks out and bail on any invalid UTF-8 sequence, including \0. */ if ((c & 0xc0) != 0x80) { valid = 0; @@ -193,10 +193,10 @@ static void json_writer_escape_string(grpc_json_writer* writer, * That range is exactly 20 bits. */ utf32 -= 0x10000; - json_writer_escape_utf16(writer, 0xd800 | (utf32 >> 10)); - json_writer_escape_utf16(writer, 0xdc00 | (utf32 & 0x3ff)); + json_writer_escape_utf16(writer, (gpr_uint16)(0xd800 | (utf32 >> 10))); + json_writer_escape_utf16(writer, (gpr_uint16)(0xdc00 | (utf32 & 0x3ff))); } else { - json_writer_escape_utf16(writer, utf32); + json_writer_escape_utf16(writer, (gpr_uint16)utf32); } } } diff --git a/src/core/security/base64.c b/src/core/security/base64.c index 3b8fea8f73..8dfaef846f 100644 --- a/src/core/security/base64.c +++ b/src/core/security/base64.c @@ -120,7 +120,68 @@ char *grpc_base64_encode(const void *vdata, size_t data_size, int url_safe, } gpr_slice grpc_base64_decode(const char *b64, int url_safe) { - size_t b64_len = strlen(b64); + return grpc_base64_decode_with_len(b64, strlen(b64), url_safe); +} + +static void decode_one_char(const unsigned char *codes, unsigned char *result, + size_t *result_offset) { + gpr_uint32 packed = (codes[0] << 2) | (codes[1] >> 4); + result[(*result_offset)++] = (unsigned char)packed; +} + +static void decode_two_chars(const unsigned char *codes, unsigned char *result, + size_t *result_offset) { + gpr_uint32 packed = (codes[0] << 10) | (codes[1] << 4) | (codes[2] >> 2); + result[(*result_offset)++] = (unsigned char)(packed >> 8); + result[(*result_offset)++] = (unsigned char)(packed); +} + +static int decode_group(const unsigned char *codes, size_t num_codes, + unsigned char *result, size_t *result_offset) { + GPR_ASSERT(num_codes <= 4); + + /* Short end groups that may not have padding. */ + if (num_codes == 1) { + gpr_log(GPR_ERROR, "Invalid group. Must be at least 2 bytes."); + return 0; + } + if (num_codes == 2) { + decode_one_char(codes, result, result_offset); + return 1; + } + if (num_codes == 3) { + decode_two_chars(codes, result, result_offset); + return 1; + } + + /* Regular 4 byte groups with padding or not. */ + GPR_ASSERT(num_codes == 4); + if (codes[0] == GRPC_BASE64_PAD_BYTE || codes[1] == GRPC_BASE64_PAD_BYTE) { + gpr_log(GPR_ERROR, "Invalid padding detected."); + return 0; + } + if (codes[2] == GRPC_BASE64_PAD_BYTE) { + if (codes[3] == GRPC_BASE64_PAD_BYTE) { + decode_one_char(codes, result, result_offset); + } else { + gpr_log(GPR_ERROR, "Invalid padding detected."); + return 0; + } + } else if (codes[3] == GRPC_BASE64_PAD_BYTE) { + decode_two_chars(codes, result, result_offset); + } else { + /* No padding. */ + gpr_uint32 packed = + (codes[0] << 18) | (codes[1] << 12) | (codes[2] << 6) | codes[3]; + result[(*result_offset)++] = (unsigned char)(packed >> 16); + result[(*result_offset)++] = (unsigned char)(packed >> 8); + result[(*result_offset)++] = (unsigned char)(packed); + } + return 1; +} + +gpr_slice grpc_base64_decode_with_len(const char *b64, size_t b64_len, + int url_safe) { gpr_slice result = gpr_slice_malloc(b64_len); unsigned char *current = GPR_SLICE_START_PTR(result); size_t result_size = 0; @@ -128,7 +189,7 @@ gpr_slice grpc_base64_decode(const char *b64, int url_safe) { size_t num_codes = 0; while (b64_len--) { - unsigned char c = *b64++; + unsigned char c = (unsigned char)(*b64++); signed char code; if (c >= GPR_ARRAY_SIZE(base64_bytes)) continue; if (url_safe) { @@ -149,45 +210,17 @@ gpr_slice grpc_base64_decode(const char *b64, int url_safe) { goto fail; } } else { - codes[num_codes++] = code; + codes[num_codes++] = (unsigned char)code; if (num_codes == 4) { - if (codes[0] == GRPC_BASE64_PAD_BYTE || - codes[1] == GRPC_BASE64_PAD_BYTE) { - gpr_log(GPR_ERROR, "Invalid padding detected."); - goto fail; - } - if (codes[2] == GRPC_BASE64_PAD_BYTE) { - if (codes[3] == GRPC_BASE64_PAD_BYTE) { - /* Double padding. */ - gpr_uint32 packed = (codes[0] << 2) | (codes[1] >> 4); - current[result_size++] = (unsigned char)packed; - } else { - gpr_log(GPR_ERROR, "Invalid padding detected."); - goto fail; - } - } else if (codes[3] == GRPC_BASE64_PAD_BYTE) { - /* Single padding. */ - gpr_uint32 packed = - (codes[0] << 10) | (codes[1] << 4) | (codes[2] >> 2); - current[result_size++] = (unsigned char)(packed >> 8); - current[result_size++] = (unsigned char)(packed); - } else { - /* No padding. */ - gpr_uint32 packed = - (codes[0] << 18) | (codes[1] << 12) | (codes[2] << 6) | codes[3]; - current[result_size++] = (unsigned char)(packed >> 16); - current[result_size++] = (unsigned char)(packed >> 8); - current[result_size++] = (unsigned char)(packed); - } + if (!decode_group(codes, num_codes, current, &result_size)) goto fail; num_codes = 0; } } } - if (num_codes != 0) { - gpr_log(GPR_ERROR, "Invalid base64."); - gpr_slice_unref(result); - return gpr_empty_slice(); + if (num_codes != 0 && + !decode_group(codes, num_codes, current, &result_size)) { + goto fail; } GPR_SLICE_SET_LENGTH(result, result_size); return result; diff --git a/src/core/security/base64.h b/src/core/security/base64.h index 6a7cd8e96c..b9abc07b52 100644 --- a/src/core/security/base64.h +++ b/src/core/security/base64.h @@ -45,4 +45,8 @@ char *grpc_base64_encode(const void *data, size_t data_size, int url_safe, slice in case of failure. */ gpr_slice grpc_base64_decode(const char *b64, int url_safe); +/* Same as above except that the length is provided by the caller. */ +gpr_slice grpc_base64_decode_with_len(const char *b64, size_t b64_len, + int url_safe); + #endif /* GRPC_INTERNAL_CORE_SECURITY_BASE64_H */ diff --git a/src/core/security/client_auth_filter.c b/src/core/security/client_auth_filter.c index 9d55362da6..e9bd45db68 100644 --- a/src/core/security/client_auth_filter.c +++ b/src/core/security/client_auth_filter.c @@ -37,6 +37,7 @@ #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> #include "src/core/support/string.h" #include "src/core/channel/channel_stack.h" @@ -52,6 +53,10 @@ typedef struct { grpc_credentials *creds; grpc_mdstr *host; grpc_mdstr *method; + /* pollset bound to this call; if we need to make external + network requests, they should be done under this pollset + so that work can progress when this call wants work to + progress */ grpc_pollset *pollset; grpc_transport_op op; size_t op_md_idx; @@ -302,13 +307,10 @@ static void init_channel_elem(grpc_channel_element *elem, chand->security_connector = (grpc_channel_security_connector *)grpc_security_connector_ref(sc); chand->md_ctx = metadata_context; - chand->authority_string = - grpc_mdstr_from_string(chand->md_ctx, ":authority"); + chand->authority_string = grpc_mdstr_from_string(chand->md_ctx, ":authority"); chand->path_string = grpc_mdstr_from_string(chand->md_ctx, ":path"); - chand->error_msg_key = - grpc_mdstr_from_string(chand->md_ctx, "grpc-message"); - chand->status_key = - grpc_mdstr_from_string(chand->md_ctx, "grpc-status"); + chand->error_msg_key = grpc_mdstr_from_string(chand->md_ctx, "grpc-message"); + chand->status_key = grpc_mdstr_from_string(chand->md_ctx, "grpc-status"); } /* Destructor for channel data */ @@ -332,6 +334,6 @@ static void destroy_channel_elem(grpc_channel_element *elem) { } const grpc_channel_filter grpc_client_auth_filter = { - auth_start_transport_op, channel_op, sizeof(call_data), init_call_elem, - destroy_call_elem, sizeof(channel_data), init_channel_elem, - destroy_channel_elem, "client-auth"}; + auth_start_transport_op, channel_op, sizeof(call_data), + init_call_elem, destroy_call_elem, sizeof(channel_data), + init_channel_elem, destroy_channel_elem, "client-auth"}; diff --git a/src/core/security/credentials.c b/src/core/security/credentials.c index f3d0cf5452..cf663faf2d 100644 --- a/src/core/security/credentials.c +++ b/src/core/security/credentials.c @@ -46,6 +46,7 @@ #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> #include <grpc/support/sync.h> #include <grpc/support/time.h> @@ -485,8 +486,8 @@ static int oauth2_token_fetcher_has_request_metadata_only( grpc_credentials_status grpc_oauth2_token_fetcher_credentials_parse_server_response( - const grpc_httpcli_response *response, - grpc_credentials_md_store **token_md, gpr_timespec *token_lifetime) { + const grpc_httpcli_response *response, grpc_credentials_md_store **token_md, + gpr_timespec *token_lifetime) { char *null_terminated_body = NULL; char *new_access_token = NULL; grpc_credentials_status status = GRPC_CREDENTIALS_OK; @@ -609,7 +610,8 @@ static void oauth2_token_fetcher_get_request_metadata( if (c->access_token_md != NULL && (gpr_time_cmp(gpr_time_sub(c->token_expiration, gpr_now()), refresh_threshold) > 0)) { - cached_access_token_md = grpc_credentials_md_store_ref(c->access_token_md); + cached_access_token_md = + grpc_credentials_md_store_ref(c->access_token_md); } gpr_mu_unlock(&c->mu); } @@ -639,8 +641,7 @@ static void init_oauth2_token_fetcher(grpc_oauth2_token_fetcher_credentials *c, /* -- ComputeEngine credentials. -- */ static grpc_credentials_vtable compute_engine_vtable = { - oauth2_token_fetcher_destroy, - oauth2_token_fetcher_has_request_metadata, + oauth2_token_fetcher_destroy, oauth2_token_fetcher_has_request_metadata, oauth2_token_fetcher_has_request_metadata_only, oauth2_token_fetcher_get_request_metadata, NULL}; @@ -685,8 +686,7 @@ static void service_account_destroy(grpc_credentials *creds) { } static grpc_credentials_vtable service_account_vtable = { - service_account_destroy, - oauth2_token_fetcher_has_request_metadata, + service_account_destroy, oauth2_token_fetcher_has_request_metadata, oauth2_token_fetcher_has_request_metadata_only, oauth2_token_fetcher_get_request_metadata, NULL}; @@ -759,8 +759,7 @@ static void refresh_token_destroy(grpc_credentials *creds) { } static grpc_credentials_vtable refresh_token_vtable = { - refresh_token_destroy, - oauth2_token_fetcher_has_request_metadata, + refresh_token_destroy, oauth2_token_fetcher_has_request_metadata, oauth2_token_fetcher_has_request_metadata_only, oauth2_token_fetcher_get_request_metadata, NULL}; @@ -899,8 +898,7 @@ static int fake_transport_security_has_request_metadata_only( return 0; } -static grpc_security_status -fake_transport_security_create_security_connector( +static grpc_security_status fake_transport_security_create_security_connector( grpc_credentials *c, const char *target, const grpc_channel_args *args, grpc_credentials *request_metadata_creds, grpc_channel_security_connector **sc, grpc_channel_args **new_args) { diff --git a/src/core/security/credentials.h b/src/core/security/credentials.h index 9c876d4226..75af73a0c6 100644 --- a/src/core/security/credentials.h +++ b/src/core/security/credentials.h @@ -108,7 +108,6 @@ grpc_credentials_md_store *grpc_credentials_md_store_ref( grpc_credentials_md_store *store); void grpc_credentials_md_store_unref(grpc_credentials_md_store *store); - /* --- grpc_credentials. --- */ /* It is the caller's responsibility to gpr_free the result if not NULL. */ @@ -177,8 +176,8 @@ grpc_credentials *grpc_credentials_contains_type( /* Exposed for testing only. */ grpc_credentials_status grpc_oauth2_token_fetcher_credentials_parse_server_response( - const struct grpc_httpcli_response *response, grpc_credentials_md_store **token_md, - gpr_timespec *token_lifetime); + const struct grpc_httpcli_response *response, + grpc_credentials_md_store **token_md, gpr_timespec *token_lifetime); /* Simulates an oauth2 token fetch with the specified value for testing. */ grpc_credentials *grpc_fake_oauth2_credentials_create( diff --git a/src/core/security/credentials_posix.c b/src/core/security/credentials_posix.c index 79622cb024..20f67a7f14 100644 --- a/src/core/security/credentials_posix.c +++ b/src/core/security/credentials_posix.c @@ -39,6 +39,7 @@ #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> #include "src/core/support/env.h" #include "src/core/support/string.h" diff --git a/src/core/security/credentials_win32.c b/src/core/security/credentials_win32.c index ddb310468b..92dfd9bdfe 100644 --- a/src/core/security/credentials_win32.c +++ b/src/core/security/credentials_win32.c @@ -39,6 +39,7 @@ #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> #include "src/core/support/env.h" #include "src/core/support/string.h" diff --git a/src/core/security/google_default_credentials.c b/src/core/security/google_default_credentials.c index 5d40627ba4..5822ce6337 100644 --- a/src/core/security/google_default_credentials.c +++ b/src/core/security/google_default_credentials.c @@ -55,9 +55,7 @@ static int compute_engine_detection_done = 0; static gpr_mu g_mu; static gpr_once g_once = GPR_ONCE_INIT; -static void init_default_credentials(void) { - gpr_mu_init(&g_mu); -} +static void init_default_credentials(void) { gpr_mu_init(&g_mu); } typedef struct { grpc_pollset pollset; diff --git a/src/core/security/json_token.c b/src/core/security/json_token.c index eadae33609..6116f1d767 100644 --- a/src/core/security/json_token.c +++ b/src/core/security/json_token.c @@ -37,6 +37,7 @@ #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> #include "src/core/security/base64.h" #include "src/core/support/string.h" diff --git a/src/core/security/secure_endpoint.c b/src/core/security/secure_endpoint.c index 7bb1de4413..73496d1153 100644 --- a/src/core/security/secure_endpoint.c +++ b/src/core/security/secure_endpoint.c @@ -116,7 +116,7 @@ static void on_read(void *user_data, gpr_slice *slices, size_t nslices, grpc_endpoint_cb_status error) { unsigned i; gpr_uint8 keep_looping = 0; - int input_buffer_count = 0; + size_t input_buffer_count = 0; tsi_result result = TSI_OK; secure_endpoint *ep = (secure_endpoint *)user_data; gpr_uint8 *cur = GPR_SLICE_START_PTR(ep->read_staging_buffer); @@ -129,7 +129,7 @@ static void on_read(void *user_data, gpr_slice *slices, size_t nslices, size_t message_size = GPR_SLICE_LENGTH(encrypted); while (message_size > 0 || keep_looping) { - size_t unprotected_buffer_size_written = end - cur; + size_t unprotected_buffer_size_written = (size_t)(end - cur); size_t processed_message_size = message_size; gpr_mu_lock(&ep->protector_mu); result = tsi_frame_protector_unprotect(ep->protector, message_bytes, @@ -166,7 +166,7 @@ static void on_read(void *user_data, gpr_slice *slices, size_t nslices, &ep->input_buffer, gpr_slice_split_head( &ep->read_staging_buffer, - cur - GPR_SLICE_START_PTR(ep->read_staging_buffer))); + (size_t)(cur - GPR_SLICE_START_PTR(ep->read_staging_buffer)))); } /* TODO(yangg) experiment with moving this block after read_cb to see if it @@ -225,7 +225,7 @@ static grpc_endpoint_write_status endpoint_write(grpc_endpoint *secure_ep, grpc_endpoint_write_cb cb, void *user_data) { unsigned i; - int output_buffer_count = 0; + size_t output_buffer_count = 0; tsi_result result = TSI_OK; secure_endpoint *ep = (secure_endpoint *)secure_ep; gpr_uint8 *cur = GPR_SLICE_START_PTR(ep->write_staging_buffer); @@ -248,7 +248,7 @@ static grpc_endpoint_write_status endpoint_write(grpc_endpoint *secure_ep, gpr_uint8 *message_bytes = GPR_SLICE_START_PTR(plain); size_t message_size = GPR_SLICE_LENGTH(plain); while (message_size > 0) { - size_t protected_buffer_size_to_send = end - cur; + size_t protected_buffer_size_to_send = (size_t)(end - cur); size_t processed_message_size = message_size; gpr_mu_lock(&ep->protector_mu); result = tsi_frame_protector_protect(ep->protector, message_bytes, @@ -273,7 +273,7 @@ static grpc_endpoint_write_status endpoint_write(grpc_endpoint *secure_ep, if (result == TSI_OK) { size_t still_pending_size; do { - size_t protected_buffer_size_to_send = end - cur; + size_t protected_buffer_size_to_send = (size_t)(end - cur); gpr_mu_lock(&ep->protector_mu); result = tsi_frame_protector_protect_flush(ep->protector, cur, &protected_buffer_size_to_send, @@ -290,7 +290,7 @@ static grpc_endpoint_write_status endpoint_write(grpc_endpoint *secure_ep, &ep->output_buffer, gpr_slice_split_head( &ep->write_staging_buffer, - cur - GPR_SLICE_START_PTR(ep->write_staging_buffer))); + (size_t)(cur - GPR_SLICE_START_PTR(ep->write_staging_buffer)))); } } diff --git a/src/core/security/secure_transport_setup.c b/src/core/security/secure_transport_setup.c index 3e1db9a12d..1b39ab141e 100644 --- a/src/core/security/secure_transport_setup.c +++ b/src/core/security/secure_transport_setup.c @@ -235,7 +235,7 @@ static void on_handshake_data_received_from_peer( gpr_slice_unref(slices[i]); /* split_tail above increments refcount. */ } gpr_slice_buffer_addn(&s->left_overs, &slices[i + 1], - num_left_overs - has_left_overs_in_current_slice); + num_left_overs - (size_t)has_left_overs_in_current_slice); check_peer(s); } diff --git a/src/core/security/security_connector.c b/src/core/security/security_connector.c index 11505f8cb0..34cb0395a2 100644 --- a/src/core/security/security_connector.c +++ b/src/core/security/security_connector.c @@ -47,6 +47,7 @@ #include <grpc/support/host_port.h> #include <grpc/support/log.h> #include <grpc/support/slice_buffer.h> +#include <grpc/support/string_util.h> #include "src/core/tsi/fake_transport_security.h" #include "src/core/tsi/ssl_transport_security.h" @@ -195,12 +196,12 @@ typedef struct { static void fake_channel_destroy(grpc_security_connector *sc) { grpc_channel_security_connector *c = (grpc_channel_security_connector *)sc; grpc_credentials_unref(c->request_metadata_creds); - grpc_auth_context_unref(sc->auth_context); + GRPC_AUTH_CONTEXT_UNREF(sc->auth_context, "connector"); gpr_free(sc); } static void fake_server_destroy(grpc_security_connector *sc) { - grpc_auth_context_unref(sc->auth_context); + GRPC_AUTH_CONTEXT_UNREF(sc->auth_context, "connector"); gpr_free(sc); } @@ -241,7 +242,7 @@ static grpc_security_status fake_check_peer(grpc_security_connector *sc, status = GRPC_SECURITY_ERROR; goto end; } - grpc_auth_context_unref(sc->auth_context); + GRPC_AUTH_CONTEXT_UNREF(sc->auth_context, "connector"); sc->auth_context = grpc_auth_context_create(NULL, 1); sc->auth_context->properties[0] = grpc_auth_property_init_from_cstring( GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME, @@ -322,7 +323,7 @@ static void ssl_channel_destroy(grpc_security_connector *sc) { if (c->target_name != NULL) gpr_free(c->target_name); if (c->overridden_target_name != NULL) gpr_free(c->overridden_target_name); tsi_peer_destruct(&c->peer); - grpc_auth_context_unref(sc->auth_context); + GRPC_AUTH_CONTEXT_UNREF(sc->auth_context, "connector"); gpr_free(sc); } @@ -332,7 +333,7 @@ static void ssl_server_destroy(grpc_security_connector *sc) { if (c->handshaker_factory != NULL) { tsi_ssl_handshaker_factory_destroy(c->handshaker_factory); } - grpc_auth_context_unref(sc->auth_context); + GRPC_AUTH_CONTEXT_UNREF(sc->auth_context, "connector"); gpr_free(sc); } @@ -385,29 +386,13 @@ static int ssl_host_matches_name(const tsi_peer *peer, const char *peer_name) { return r; } -static grpc_auth_context *tsi_ssl_peer_to_auth_context(const tsi_peer *peer) { - /* We bet that iterating over a handful of properties twice will be faster - than having to realloc on average . */ - size_t auth_prop_count = 1; /* for transport_security_type. */ +grpc_auth_context *tsi_ssl_peer_to_auth_context(const tsi_peer *peer) { size_t i; - const char *peer_identity_property_name = NULL; grpc_auth_context *ctx = NULL; - for (i = 0; i < peer->property_count; i++) { - const tsi_peer_property *prop = &peer->properties[i]; - if (prop->name == NULL) continue; - if (strcmp(prop->name, TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY) == 0) { - auth_prop_count++; - /* If there is no subject alt name, have the CN as the identity. */ - if (peer_identity_property_name == NULL) { - peer_identity_property_name = prop->name; - } - } else if (strcmp(prop->name, - TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY) == 0) { - auth_prop_count++; - peer_identity_property_name = prop->name; - } - } - ctx = grpc_auth_context_create(NULL, auth_prop_count); + + /* The caller has checked the certificate type property. */ + GPR_ASSERT(peer->property_count >= 1); + ctx = grpc_auth_context_create(NULL, peer->property_count); ctx->properties[0] = grpc_auth_property_init_from_cstring( GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME, GRPC_SSL_TRANSPORT_SECURITY_TYPE); @@ -416,15 +401,19 @@ static grpc_auth_context *tsi_ssl_peer_to_auth_context(const tsi_peer *peer) { const tsi_peer_property *prop = &peer->properties[i]; if (prop->name == NULL) continue; if (strcmp(prop->name, TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY) == 0) { + /* If there is no subject alt name, have the CN as the identity. */ + if (ctx->peer_identity_property_name == NULL) { + ctx->peer_identity_property_name = GRPC_X509_CN_PROPERTY_NAME; + } ctx->properties[ctx->property_count++] = grpc_auth_property_init( GRPC_X509_CN_PROPERTY_NAME, prop->value.data, prop->value.length); } else if (strcmp(prop->name, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY) == 0) { + ctx->peer_identity_property_name = GRPC_X509_SAN_PROPERTY_NAME; ctx->properties[ctx->property_count++] = grpc_auth_property_init( GRPC_X509_SAN_PROPERTY_NAME, prop->value.data, prop->value.length); } } - GPR_ASSERT(auth_prop_count == ctx->property_count); return ctx; } @@ -448,6 +437,9 @@ static grpc_security_status ssl_check_peer(grpc_security_connector *sc, gpr_log(GPR_ERROR, "Peer name %s is not in peer certificate", peer_name); return GRPC_SECURITY_ERROR; } + if (sc->auth_context != NULL) { + GRPC_AUTH_CONTEXT_UNREF(sc->auth_context, "connector"); + } sc->auth_context = tsi_ssl_peer_to_auth_context(peer); return GRPC_SECURITY_OK; } @@ -549,7 +541,7 @@ grpc_security_status grpc_ssl_channel_security_connector_create( alpn_protocol_strings[i] = (const unsigned char *)grpc_chttp2_get_alpn_version_index(i); alpn_protocol_string_lengths[i] = - strlen(grpc_chttp2_get_alpn_version_index(i)); + (unsigned char)strlen(grpc_chttp2_get_alpn_version_index(i)); } if (config == NULL || target_name == NULL) { @@ -588,7 +580,7 @@ grpc_security_status grpc_ssl_channel_security_connector_create( config->pem_private_key, config->pem_private_key_size, config->pem_cert_chain, config->pem_cert_chain_size, pem_root_certs, pem_root_certs_size, ssl_cipher_suites(), alpn_protocol_strings, - alpn_protocol_string_lengths, num_alpn_protocols, &c->handshaker_factory); + alpn_protocol_string_lengths, (uint16_t)num_alpn_protocols, &c->handshaker_factory); if (result != TSI_OK) { gpr_log(GPR_ERROR, "Handshaker factory creation failed with %s.", tsi_result_to_string(result)); @@ -622,7 +614,7 @@ grpc_security_status grpc_ssl_server_security_connector_create( alpn_protocol_strings[i] = (const unsigned char *)grpc_chttp2_get_alpn_version_index(i); alpn_protocol_string_lengths[i] = - strlen(grpc_chttp2_get_alpn_version_index(i)); + (unsigned char)strlen(grpc_chttp2_get_alpn_version_index(i)); } if (config == NULL || config->num_key_cert_pairs == 0) { @@ -641,7 +633,7 @@ grpc_security_status grpc_ssl_server_security_connector_create( (const unsigned char **)config->pem_cert_chains, config->pem_cert_chains_sizes, config->num_key_cert_pairs, config->pem_root_certs, config->pem_root_certs_size, ssl_cipher_suites(), - alpn_protocol_strings, alpn_protocol_string_lengths, num_alpn_protocols, + alpn_protocol_strings, alpn_protocol_string_lengths, (uint16_t)num_alpn_protocols, &c->handshaker_factory); if (result != TSI_OK) { gpr_log(GPR_ERROR, "Handshaker factory creation failed with %s.", @@ -660,4 +652,3 @@ error: gpr_free(alpn_protocol_string_lengths); return GRPC_SECURITY_ERROR; } - diff --git a/src/core/security/security_connector.h b/src/core/security/security_connector.h index 0617041448..ee3057b43b 100644 --- a/src/core/security/security_connector.h +++ b/src/core/security/security_connector.h @@ -203,4 +203,7 @@ grpc_security_status grpc_ssl_server_security_connector_create( const tsi_peer_property *tsi_peer_get_property_by_name( const tsi_peer *peer, const char *name); +/* Exposed for testing only. */ +grpc_auth_context *tsi_ssl_peer_to_auth_context(const tsi_peer *peer); + #endif /* GRPC_INTERNAL_CORE_SECURITY_SECURITY_CONNECTOR_H */ diff --git a/src/core/security/security_context.c b/src/core/security/security_context.c index 14c194c8f6..4d56549f9b 100644 --- a/src/core/security/security_context.c +++ b/src/core/security/security_context.c @@ -40,6 +40,7 @@ #include <grpc/grpc_security.h> #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> /* --- grpc_call --- */ @@ -88,7 +89,7 @@ grpc_client_security_context *grpc_client_security_context_create(void) { void grpc_client_security_context_destroy(void *ctx) { grpc_client_security_context *c = (grpc_client_security_context *)ctx; grpc_credentials_unref(c->creds); - grpc_auth_context_unref(c->auth_context); + GRPC_AUTH_CONTEXT_UNREF(c->auth_context, "client_security_context"); gpr_free(ctx); } @@ -103,7 +104,7 @@ grpc_server_security_context *grpc_server_security_context_create(void) { void grpc_server_security_context_destroy(void *ctx) { grpc_server_security_context *c = (grpc_server_security_context *)ctx; - grpc_auth_context_unref(c->auth_context); + GRPC_AUTH_CONTEXT_UNREF(c->auth_context, "server_security_context"); gpr_free(ctx); } @@ -119,21 +120,40 @@ grpc_auth_context *grpc_auth_context_create(grpc_auth_context *chained, memset(ctx->properties, 0, property_count * sizeof(grpc_auth_property)); ctx->property_count = property_count; gpr_ref_init(&ctx->refcount, 1); - if (chained != NULL) ctx->chained = grpc_auth_context_ref(chained); + if (chained != NULL) ctx->chained = GRPC_AUTH_CONTEXT_REF(chained, "chained"); return ctx; } +#ifdef GRPC_AUTH_CONTEXT_REFCOUNT_DEBUG +grpc_auth_context *grpc_auth_context_ref(grpc_auth_context *ctx, + const char *file, int line, + const char *reason) { + if (ctx == NULL) return NULL; + gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, + "AUTH_CONTEXT:%p ref %d -> %d %s", ctx, (int)ctx->refcount.count, + (int)ctx->refcount.count + 1, reason); +#else grpc_auth_context *grpc_auth_context_ref(grpc_auth_context *ctx) { if (ctx == NULL) return NULL; +#endif gpr_ref(&ctx->refcount); return ctx; } +#ifdef GRPC_AUTH_CONTEXT_REFCOUNT_DEBUG +void grpc_auth_context_unref(grpc_auth_context *ctx, const char *file, int line, + const char *reason) { + if (ctx == NULL) return; + gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, + "AUTH_CONTEXT:%p unref %d -> %d %s", ctx, (int)ctx->refcount.count, + (int)ctx->refcount.count - 1, reason); +#else void grpc_auth_context_unref(grpc_auth_context *ctx) { if (ctx == NULL) return; +#endif if (gpr_unref(&ctx->refcount)) { size_t i; - grpc_auth_context_unref(ctx->chained); + GRPC_AUTH_CONTEXT_UNREF(ctx->chained, "chained"); if (ctx->properties != NULL) { for (i = 0; i < ctx->property_count; i++) { grpc_auth_property_reset(&ctx->properties[i]); @@ -222,8 +242,8 @@ grpc_auth_property grpc_auth_property_init(const char *name, const char *value, } void grpc_auth_property_reset(grpc_auth_property *property) { - if (property->name != NULL) gpr_free(property->name); - if (property->value != NULL) gpr_free(property->value); + gpr_free(property->name); + gpr_free(property->value); memset(property, 0, sizeof(grpc_auth_property)); } diff --git a/src/core/security/security_context.h b/src/core/security/security_context.h index d8909cd6f1..20c4390898 100644 --- a/src/core/security/security_context.h +++ b/src/core/security/security_context.h @@ -55,9 +55,22 @@ grpc_auth_context *grpc_auth_context_create(grpc_auth_context *chained, size_t property_count); /* Refcounting. */ -grpc_auth_context *grpc_auth_context_ref( - grpc_auth_context *ctx); -void grpc_auth_context_unref(grpc_auth_context *ctx); +#ifdef GRPC_AUTH_CONTEXT_REFCOUNT_DEBUG +#define GRPC_AUTH_CONTEXT_REF(p, r) \ + grpc_auth_context_ref((p), __FILE__, __LINE__, (r)) +#define GRPC_AUTH_CONTEXT_UNREF(p, r) \ + grpc_auth_context_unref((p), __FILE__, __LINE__, (r)) +grpc_auth_context *grpc_auth_context_ref(grpc_auth_context *policy, + const char *file, int line, + const char *reason); +void grpc_auth_context_unref(grpc_auth_context *policy, const char *file, + int line, const char *reason); +#else +#define GRPC_AUTH_CONTEXT_REF(p, r) grpc_auth_context_ref((p)) +#define GRPC_AUTH_CONTEXT_UNREF(p, r) grpc_auth_context_unref((p)) +grpc_auth_context *grpc_auth_context_ref(grpc_auth_context *policy); +void grpc_auth_context_unref(grpc_auth_context *policy); +#endif grpc_auth_property grpc_auth_property_init_from_cstring(const char *name, const char *value); diff --git a/src/core/security/server_auth_filter.c b/src/core/security/server_auth_filter.c index 1823f75808..b8639287a5 100644 --- a/src/core/security/server_auth_filter.c +++ b/src/core/security/server_auth_filter.c @@ -78,14 +78,17 @@ static void init_call_elem(grpc_call_element *elem, calld->unused = 0; GPR_ASSERT(initial_op && initial_op->context != NULL && - chand->security_connector->auth_context != NULL && initial_op->context[GRPC_CONTEXT_SECURITY].value == NULL); /* Create a security context for the call and reference the auth context from the channel. */ + if (initial_op->context[GRPC_CONTEXT_SECURITY].value != NULL) { + initial_op->context[GRPC_CONTEXT_SECURITY].destroy( + initial_op->context[GRPC_CONTEXT_SECURITY].value); + } server_ctx = grpc_server_security_context_create(); - server_ctx->auth_context = - grpc_auth_context_ref(chand->security_connector->auth_context); + server_ctx->auth_context = GRPC_AUTH_CONTEXT_REF( + chand->security_connector->auth_context, "server_security_context"); initial_op->context[GRPC_CONTEXT_SECURITY].value = server_ctx; initial_op->context[GRPC_CONTEXT_SECURITY].destroy = grpc_server_security_context_destroy; diff --git a/src/core/security/server_secure_chttp2.c b/src/core/security/server_secure_chttp2.c index b312bdd0b6..53afa1caad 100644 --- a/src/core/security/server_secure_chttp2.c +++ b/src/core/security/server_secure_chttp2.c @@ -138,10 +138,12 @@ static void destroy_done(void *statep) { callbacks) */ static void destroy(grpc_server *server, void *statep) { grpc_server_secure_state *state = statep; + grpc_tcp_server *tcp; gpr_mu_lock(&state->mu); state->is_shutdown = 1; - grpc_tcp_server_destroy(state->tcp, destroy_done, state); + tcp = state->tcp; gpr_mu_unlock(&state->mu); + grpc_tcp_server_destroy(tcp, destroy_done, state); } int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr, diff --git a/src/core/support/cmdline.c b/src/core/support/cmdline.c index 530952c437..45a3182f73 100644 --- a/src/core/support/cmdline.c +++ b/src/core/support/cmdline.c @@ -40,6 +40,7 @@ #include "src/core/support/string.h" #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> typedef enum { ARGTYPE_INT, ARGTYPE_BOOL, ARGTYPE_STRING } argtype; @@ -227,7 +228,7 @@ static void value_state(gpr_cmdline *cl, char *arg) { cl->cur_arg->name); print_usage_and_die(cl); } - *(int *)cl->cur_arg->value = intval; + *(int *)cl->cur_arg->value = (int)intval; break; case ARGTYPE_BOOL: if (0 == strcmp(arg, "1") || 0 == strcmp(arg, "true")) { @@ -286,8 +287,8 @@ static void normal_state(gpr_cmdline *cl, char *arg) { eq = strchr(arg, '='); if (eq != NULL) { /* copy the string into a temp buffer and extract the name */ - tmp = arg_name = gpr_malloc(eq - arg + 1); - memcpy(arg_name, arg, eq - arg); + tmp = arg_name = gpr_malloc((size_t)(eq - arg + 1)); + memcpy(arg_name, arg, (size_t)(eq - arg)); arg_name[eq - arg] = 0; } else { arg_name = arg; diff --git a/src/core/support/cpu_linux.c b/src/core/support/cpu_linux.c index 37e840d4cf..282d4daab1 100644 --- a/src/core/support/cpu_linux.c +++ b/src/core/support/cpu_linux.c @@ -51,7 +51,9 @@ static int ncpus = 0; static void init_num_cpus() { - ncpus = sysconf(_SC_NPROCESSORS_ONLN); + /* This must be signed. sysconf returns -1 when the number cannot be + determined */ + ncpus = (int)sysconf(_SC_NPROCESSORS_ONLN); if (ncpus < 1) { gpr_log(GPR_ERROR, "Cannot determine number of CPUs: assuming 1"); ncpus = 1; @@ -61,7 +63,7 @@ static void init_num_cpus() { unsigned gpr_cpu_num_cores(void) { static gpr_once once = GPR_ONCE_INIT; gpr_once_init(&once, init_num_cpus); - return ncpus; + return (unsigned)ncpus; } unsigned gpr_cpu_current_cpu(void) { @@ -70,7 +72,7 @@ unsigned gpr_cpu_current_cpu(void) { gpr_log(GPR_ERROR, "Error determining current CPU: %s\n", strerror(errno)); return 0; } - return cpu; + return (unsigned)cpu; } #endif /* GPR_CPU_LINUX */ diff --git a/src/core/support/env_linux.c b/src/core/support/env_linux.c index bdadfb6ca4..2e03365e33 100644 --- a/src/core/support/env_linux.c +++ b/src/core/support/env_linux.c @@ -45,6 +45,7 @@ #include <stdlib.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> #include "src/core/support/string.h" diff --git a/src/core/support/env_posix.c b/src/core/support/env_posix.c index 45f89b6737..1dd2af56bc 100644 --- a/src/core/support/env_posix.c +++ b/src/core/support/env_posix.c @@ -42,6 +42,7 @@ #include <grpc/support/log.h> #include "src/core/support/string.h" +#include <grpc/support/string_util.h> char *gpr_getenv(const char *name) { char *result = getenv(name); diff --git a/src/core/support/env_win32.c b/src/core/support/env_win32.c index 9b4cd698ad..6b1ff102b0 100644 --- a/src/core/support/env_win32.c +++ b/src/core/support/env_win32.c @@ -42,6 +42,7 @@ #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> char *gpr_getenv(const char *name) { size_t size; diff --git a/src/core/support/file.c b/src/core/support/file.c index 3a4ac6f2f0..c1361d8a9e 100644 --- a/src/core/support/file.c +++ b/src/core/support/file.c @@ -38,6 +38,7 @@ #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> #include "src/core/support/string.h" @@ -57,7 +58,8 @@ gpr_slice gpr_load_file(const char *filename, int add_null_terminator, goto end; } fseek(file, 0, SEEK_END); - contents_size = ftell(file); + /* Converting to size_t on the assumption that it will not fail */ + contents_size = (size_t)ftell(file); fseek(file, 0, SEEK_SET); contents = gpr_malloc(contents_size + (add_null_terminator ? 1 : 0)); bytes_read = fread(contents, 1, contents_size, file); diff --git a/src/core/support/file_posix.c b/src/core/support/file_posix.c index 11a459ad36..c11c07148a 100644 --- a/src/core/support/file_posix.c +++ b/src/core/support/file_posix.c @@ -44,6 +44,7 @@ #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> #include "src/core/support/string.h" diff --git a/src/core/support/file_win32.c b/src/core/support/file_win32.c index f59d3af397..355744f79a 100644 --- a/src/core/support/file_win32.c +++ b/src/core/support/file_win32.c @@ -42,6 +42,7 @@ #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> #include "src/core/support/file.h" #include "src/core/support/string_win32.h" diff --git a/src/core/support/histogram.c b/src/core/support/histogram.c index 673affde71..9029703891 100644 --- a/src/core/support/histogram.c +++ b/src/core/support/histogram.c @@ -164,7 +164,9 @@ static double threshold_for_count_below(gpr_histogram *h, double count_below) { size_t lower_idx; size_t upper_idx; - GPR_ASSERT(h->count >= 1); + if (h->count == 0) { + return 0.0; + } if (count_below <= 0) { return h->min_seen; @@ -189,12 +191,12 @@ static double threshold_for_count_below(gpr_histogram *h, double count_below) { break; } } - return (bucket_start(h, lower_idx) + bucket_start(h, upper_idx)) / 2.0; + return (bucket_start(h, (double)lower_idx) + bucket_start(h, (double)upper_idx)) / 2.0; } else { /* treat values as uniform throughout the bucket, and find where this value should lie */ - lower_bound = bucket_start(h, lower_idx); - upper_bound = bucket_start(h, lower_idx + 1); + lower_bound = bucket_start(h, (double)lower_idx); + upper_bound = bucket_start(h, (double)(lower_idx + 1)); return GPR_CLAMP(upper_bound - (upper_bound - lower_bound) * (count_so_far - count_below) / h->buckets[lower_idx], diff --git a/src/core/support/host_port.c b/src/core/support/host_port.c index fa49f1a33a..0906ebc2a3 100644 --- a/src/core/support/host_port.c +++ b/src/core/support/host_port.c @@ -38,6 +38,7 @@ #include "src/core/support/string.h" #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> int gpr_join_host_port(char **out, const char *host, int port) { if (host[0] != '[' && strchr(host, ':') != NULL) { @@ -75,7 +76,7 @@ void gpr_split_host_port(const char *name, char **host, char **port) { return; } host_start = name + 1; - host_len = rbracket - host_start; + host_len = (size_t)(rbracket - host_start); if (memchr(host_start, ':', host_len) == NULL) { /* Require all bracketed hosts to contain a colon, because a hostname or IPv4 address should never use brackets. */ @@ -86,7 +87,7 @@ void gpr_split_host_port(const char *name, char **host, char **port) { if (colon != NULL && strchr(colon + 1, ':') == NULL) { /* Exactly 1 colon. Split into host:port. */ host_start = name; - host_len = colon - name; + host_len = (size_t)(colon - name); port_start = colon + 1; } else { /* 0 or 2+ colons. Bare hostname or IPv6 litearal. */ diff --git a/src/core/support/log_win32.c b/src/core/support/log_win32.c index 159c7e052c..d249be7d2e 100644 --- a/src/core/support/log_win32.c +++ b/src/core/support/log_win32.c @@ -42,6 +42,7 @@ #include <grpc/support/log_win32.h> #include <grpc/support/log.h> #include <grpc/support/time.h> +#include <grpc/support/string_util.h> #include "src/core/support/string.h" #include "src/core/support/string_win32.h" @@ -93,23 +94,22 @@ void gpr_default_log(gpr_log_func_args *args) { fprintf(stderr, "%s%s.%09u %5lu %s:%d] %s\n", gpr_log_severity_string(args->severity), time_buffer, - (int)(now.tv_nsec), GetCurrentThreadId(), - args->file, args->line, args->message); + (int)(now.tv_nsec), GetCurrentThreadId(), args->file, args->line, + args->message); } char *gpr_format_message(DWORD messageid) { LPTSTR tmessage; char *message; - DWORD status = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, messageid, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPTSTR)(&tmessage), 0, NULL); - if (status == 0) return gpr_strdup("Unable to retreive error string"); + DWORD status = FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, messageid, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR)(&tmessage), 0, NULL); + if (status == 0) return gpr_strdup("Unable to retrieve error string"); message = gpr_tchar_to_char(tmessage); LocalFree(tmessage); return message; } -#endif /* GPR_WIN32 */ +#endif /* GPR_WIN32 */ diff --git a/src/core/support/murmur_hash.c b/src/core/support/murmur_hash.c index cc84691508..37fdca82ba 100644 --- a/src/core/support/murmur_hash.c +++ b/src/core/support/murmur_hash.c @@ -48,7 +48,7 @@ gpr_uint32 gpr_murmur_hash3(const void *key, size_t len, gpr_uint32 seed) { const gpr_uint8 *data = (const gpr_uint8 *)key; - const int nblocks = len / 4; + const size_t nblocks = len / 4; int i; gpr_uint32 h1 = seed; @@ -57,11 +57,11 @@ gpr_uint32 gpr_murmur_hash3(const void *key, size_t len, gpr_uint32 seed) { const gpr_uint32 c1 = 0xcc9e2d51; const gpr_uint32 c2 = 0x1b873593; - const gpr_uint32 *blocks = (const uint32_t *)(data + nblocks * 4); - const uint8_t *tail = (const uint8_t *)(data + nblocks * 4); + const gpr_uint32 *blocks = ((const gpr_uint32 *)key) + nblocks; + const gpr_uint8 *tail = (const gpr_uint8 *)(data + nblocks * 4); /* body */ - for (i = -nblocks; i; i++) { + for (i = -(int)nblocks; i; i++) { k1 = GETBLOCK32(blocks, i); k1 *= c1; @@ -78,9 +78,9 @@ gpr_uint32 gpr_murmur_hash3(const void *key, size_t len, gpr_uint32 seed) { /* tail */ switch (len & 3) { case 3: - k1 ^= tail[2] << 16; + k1 ^= ((gpr_uint32)tail[2]) << 16; case 2: - k1 ^= tail[1] << 8; + k1 ^= ((gpr_uint32)tail[1]) << 8; case 1: k1 ^= tail[0]; k1 *= c1; @@ -90,7 +90,7 @@ gpr_uint32 gpr_murmur_hash3(const void *key, size_t len, gpr_uint32 seed) { }; /* finalization */ - h1 ^= len; + h1 ^= (gpr_uint32)len; FMIX32(h1); return h1; } diff --git a/src/core/support/slice.c b/src/core/support/slice.c index 4cff029286..a2d62fc1e5 100644 --- a/src/core/support/slice.c +++ b/src/core/support/slice.c @@ -194,7 +194,7 @@ gpr_slice gpr_slice_malloc(size_t length) { } else { /* small slice: just inline the data */ slice.refcount = NULL; - slice.data.inlined.length = length; + slice.data.inlined.length = (gpr_uint8)length; } return slice; } @@ -202,11 +202,11 @@ gpr_slice gpr_slice_malloc(size_t length) { gpr_slice gpr_slice_sub_no_ref(gpr_slice source, size_t begin, size_t end) { gpr_slice subset; + GPR_ASSERT(end >= begin); + if (source.refcount) { /* Enforce preconditions */ - GPR_ASSERT(source.data.refcounted.length >= begin); GPR_ASSERT(source.data.refcounted.length >= end); - GPR_ASSERT(end >= begin); /* Build the result */ subset.refcount = source.refcount; @@ -214,8 +214,10 @@ gpr_slice gpr_slice_sub_no_ref(gpr_slice source, size_t begin, size_t end) { subset.data.refcounted.bytes = source.data.refcounted.bytes + begin; subset.data.refcounted.length = end - begin; } else { + /* Enforce preconditions */ + GPR_ASSERT(source.data.inlined.length >= end); subset.refcount = NULL; - subset.data.inlined.length = end - begin; + subset.data.inlined.length = (gpr_uint8)(end - begin); memcpy(subset.data.inlined.bytes, source.data.inlined.bytes + begin, end - begin); } @@ -227,7 +229,7 @@ gpr_slice gpr_slice_sub(gpr_slice source, size_t begin, size_t end) { if (end - begin <= sizeof(subset.data.inlined.bytes)) { subset.refcount = NULL; - subset.data.inlined.length = end - begin; + subset.data.inlined.length = (gpr_uint8)(end - begin); memcpy(subset.data.inlined.bytes, GPR_SLICE_START_PTR(source) + begin, end - begin); } else { @@ -245,17 +247,17 @@ gpr_slice gpr_slice_split_tail(gpr_slice *source, size_t split) { /* inlined data, copy it out */ GPR_ASSERT(source->data.inlined.length >= split); tail.refcount = NULL; - tail.data.inlined.length = source->data.inlined.length - split; + tail.data.inlined.length = (gpr_uint8)(source->data.inlined.length - split); memcpy(tail.data.inlined.bytes, source->data.inlined.bytes + split, tail.data.inlined.length); - source->data.inlined.length = split; + source->data.inlined.length = (gpr_uint8)split; } else { size_t tail_length = source->data.refcounted.length - split; GPR_ASSERT(source->data.refcounted.length >= split); if (tail_length < sizeof(tail.data.inlined.bytes)) { /* Copy out the bytes - it'll be cheaper than refcounting */ tail.refcount = NULL; - tail.data.inlined.length = tail_length; + tail.data.inlined.length = (gpr_uint8)tail_length; memcpy(tail.data.inlined.bytes, source->data.refcounted.bytes + split, tail_length); } else { @@ -280,16 +282,16 @@ gpr_slice gpr_slice_split_head(gpr_slice *source, size_t split) { GPR_ASSERT(source->data.inlined.length >= split); head.refcount = NULL; - head.data.inlined.length = split; + head.data.inlined.length = (gpr_uint8)split; memcpy(head.data.inlined.bytes, source->data.inlined.bytes, split); - source->data.inlined.length -= split; + source->data.inlined.length = (gpr_uint8)(source->data.inlined.length - split); memmove(source->data.inlined.bytes, source->data.inlined.bytes + split, source->data.inlined.length); } else if (split < sizeof(head.data.inlined.bytes)) { GPR_ASSERT(source->data.refcounted.length >= split); head.refcount = NULL; - head.data.inlined.length = split; + head.data.inlined.length = (gpr_uint8)split; memcpy(head.data.inlined.bytes, source->data.refcounted.bytes, split); source->data.refcounted.bytes += split; source->data.refcounted.length -= split; @@ -311,7 +313,7 @@ gpr_slice gpr_slice_split_head(gpr_slice *source, size_t split) { } int gpr_slice_cmp(gpr_slice a, gpr_slice b) { - int d = GPR_SLICE_LENGTH(a) - GPR_SLICE_LENGTH(b); + int d = (int)(GPR_SLICE_LENGTH(a) - GPR_SLICE_LENGTH(b)); if (d != 0) return d; return memcmp(GPR_SLICE_START_PTR(a), GPR_SLICE_START_PTR(b), GPR_SLICE_LENGTH(a)); @@ -319,7 +321,7 @@ int gpr_slice_cmp(gpr_slice a, gpr_slice b) { int gpr_slice_str_cmp(gpr_slice a, const char *b) { size_t b_length = strlen(b); - int d = GPR_SLICE_LENGTH(a) - b_length; + int d = (int)(GPR_SLICE_LENGTH(a) - b_length); if (d != 0) return d; return memcmp(GPR_SLICE_START_PTR(a), b, b_length); } diff --git a/src/core/support/slice_buffer.c b/src/core/support/slice_buffer.c index 91b5d8c98b..6e6c72a2bf 100644 --- a/src/core/support/slice_buffer.c +++ b/src/core/support/slice_buffer.c @@ -81,7 +81,7 @@ gpr_uint8 *gpr_slice_buffer_tiny_add(gpr_slice_buffer *sb, unsigned n) { if ((back->data.inlined.length + n) > sizeof(back->data.inlined.bytes)) goto add_new; out = back->data.inlined.bytes + back->data.inlined.length; - back->data.inlined.length += n; + back->data.inlined.length = (gpr_uint8)(back->data.inlined.length + n); return out; add_new: @@ -89,7 +89,7 @@ add_new: back = &sb->slices[sb->count]; sb->count++; back->refcount = NULL; - back->data.inlined.length = n; + back->data.inlined.length = (gpr_uint8)n; return back->data.inlined.bytes; } @@ -116,7 +116,7 @@ void gpr_slice_buffer_add(gpr_slice_buffer *sb, gpr_slice s) { GPR_SLICE_INLINED_SIZE) { memcpy(back->data.inlined.bytes + back->data.inlined.length, s.data.inlined.bytes, s.data.inlined.length); - back->data.inlined.length += s.data.inlined.length; + back->data.inlined.length = (gpr_uint8)(back->data.inlined.length + s.data.inlined.length); } else { size_t cp1 = GPR_SLICE_INLINED_SIZE - back->data.inlined.length; memcpy(back->data.inlined.bytes + back->data.inlined.length, @@ -126,7 +126,7 @@ void gpr_slice_buffer_add(gpr_slice_buffer *sb, gpr_slice s) { back = &sb->slices[n]; sb->count = n + 1; back->refcount = NULL; - back->data.inlined.length = s.data.inlined.length - cp1; + back->data.inlined.length = (gpr_uint8)(s.data.inlined.length - cp1); memcpy(back->data.inlined.bytes, s.data.inlined.bytes + cp1, s.data.inlined.length - cp1); } @@ -190,3 +190,19 @@ void gpr_slice_buffer_swap(gpr_slice_buffer *a, gpr_slice_buffer *b) { GPR_SWAP(gpr_slice *, a->slices, b->slices); } } + +void gpr_slice_buffer_move_into(gpr_slice_buffer *src, gpr_slice_buffer *dst) { + /* anything to move? */ + if (src->count == 0) { + return; + } + /* anything in dst? */ + if (dst->count == 0) { + gpr_slice_buffer_swap(src, dst); + return; + } + /* both buffers have data - copy, and reset src */ + gpr_slice_buffer_addn(dst, src->slices, src->count); + src->count = 0; + src->length = 0; +} diff --git a/src/core/support/string.c b/src/core/support/string.c index bfd7ce1590..6a80ccc841 100644 --- a/src/core/support/string.c +++ b/src/core/support/string.c @@ -94,7 +94,7 @@ char *gpr_hexdump(const char *buf, size_t len, gpr_uint32 flags) { if (len) hexout_append(&out, ' '); hexout_append(&out, '\''); for (cur = beg; cur != end; ++cur) { - hexout_append(&out, isprint(*cur) ? *cur : '.'); + hexout_append(&out, isprint(*cur) ? *(char*)cur : '.'); } hexout_append(&out, '\''); } @@ -113,7 +113,7 @@ int gpr_parse_bytes_to_uint32(const char *buf, size_t len, gpr_uint32 *result) { for (i = 0; i < len; i++) { if (buf[i] < '0' || buf[i] > '9') return 0; /* bad char */ - new = 10 * out + (buf[i] - '0'); + new = 10 * out + (gpr_uint32)(buf[i] - '0'); if (new < out) return 0; /* overflow */ out = new; } @@ -143,7 +143,7 @@ int gpr_ltoa(long value, char *string) { if (neg) value = -value; while (value) { - string[i++] = '0' + value % 10; + string[i++] = (char)('0' + value % 10); value /= 10; } if (neg) string[i++] = '-'; diff --git a/src/core/support/string.h b/src/core/support/string.h index faf3342708..31e9fcb5e9 100644 --- a/src/core/support/string.h +++ b/src/core/support/string.h @@ -44,10 +44,6 @@ extern "C" { /* String utility functions */ -/* Returns a copy of src that can be passed to gpr_free(). - If allocation fails or if src is NULL, returns NULL. */ -char *gpr_strdup(const char *src); - /* flag to include plaintext after a hexdump */ #define GPR_HEXDUMP_PLAINTEXT 0x00000001 @@ -71,16 +67,6 @@ int gpr_ltoa(long value, char *output); /* Reverse a run of bytes */ void gpr_reverse_bytes(char *str, int len); -/* printf to a newly-allocated string. The set of supported formats may vary - between platforms. - - On success, returns the number of bytes printed (excluding the final '\0'), - and *strp points to a string which must later be destroyed with gpr_free(). - - On error, returns -1 and sets *strp to NULL. If the format string is bad, - the result is undefined. */ -int gpr_asprintf(char **strp, const char *format, ...); - /* Join a set of strings, returning the resulting string. Total combined length (excluding null terminator) is returned in total_length if it is non-null. */ diff --git a/src/core/support/subprocess_posix.c b/src/core/support/subprocess_posix.c index b4631fa0ed..171054e4da 100644 --- a/src/core/support/subprocess_posix.c +++ b/src/core/support/subprocess_posix.c @@ -66,8 +66,8 @@ gpr_subprocess *gpr_subprocess_create(int argc, const char **argv) { if (pid == -1) { return NULL; } else if (pid == 0) { - exec_args = gpr_malloc((argc + 1) * sizeof(char *)); - memcpy(exec_args, argv, argc * sizeof(char *)); + exec_args = gpr_malloc(((size_t)argc + 1) * sizeof(char *)); + memcpy(exec_args, argv, (size_t)argc * sizeof(char *)); exec_args[argc] = NULL; execv(exec_args[0], exec_args); /* if we reach here, an error has occurred */ diff --git a/src/core/support/time.c b/src/core/support/time.c index 7dbf95059f..d47b08b266 100644 --- a/src/core/support/time.c +++ b/src/core/support/time.c @@ -86,11 +86,11 @@ gpr_timespec gpr_time_from_nanos(long ns) { result = gpr_inf_past; } else if (ns >= 0) { result.tv_sec = ns / GPR_NS_PER_SEC; - result.tv_nsec = ns - result.tv_sec * GPR_NS_PER_SEC; + result.tv_nsec = (int)(ns - result.tv_sec * GPR_NS_PER_SEC); } else { /* Calculation carefully formulated to avoid any possible under/overflow. */ result.tv_sec = (-(999999999 - (ns + GPR_NS_PER_SEC)) / GPR_NS_PER_SEC) - 1; - result.tv_nsec = ns - result.tv_sec * GPR_NS_PER_SEC; + result.tv_nsec = (int)(ns - result.tv_sec * GPR_NS_PER_SEC); } return result; } @@ -103,11 +103,11 @@ gpr_timespec gpr_time_from_micros(long us) { result = gpr_inf_past; } else if (us >= 0) { result.tv_sec = us / 1000000; - result.tv_nsec = (us - result.tv_sec * 1000000) * 1000; + result.tv_nsec = (int)((us - result.tv_sec * 1000000) * 1000); } else { /* Calculation carefully formulated to avoid any possible under/overflow. */ result.tv_sec = (-(999999 - (us + 1000000)) / 1000000) - 1; - result.tv_nsec = (us - result.tv_sec * 1000000) * 1000; + result.tv_nsec = (int)((us - result.tv_sec * 1000000) * 1000); } return result; } @@ -120,11 +120,11 @@ gpr_timespec gpr_time_from_millis(long ms) { result = gpr_inf_past; } else if (ms >= 0) { result.tv_sec = ms / 1000; - result.tv_nsec = (ms - result.tv_sec * 1000) * 1000000; + result.tv_nsec = (int)((ms - result.tv_sec * 1000) * 1000000); } else { /* Calculation carefully formulated to avoid any possible under/overflow. */ result.tv_sec = (-(999 - (ms + 1000)) / 1000) - 1; - result.tv_nsec = (ms - result.tv_sec * 1000) * 1000000; + result.tv_nsec = (int)((ms - result.tv_sec * 1000) * 1000000); } return result; } @@ -245,10 +245,10 @@ gpr_int32 gpr_time_to_millis(gpr_timespec t) { care?) */ return -2147483647; } else { - return t.tv_sec * GPR_MS_PER_SEC + t.tv_nsec / GPR_NS_PER_MS; + return (gpr_int32)(t.tv_sec * GPR_MS_PER_SEC + t.tv_nsec / GPR_NS_PER_MS); } } double gpr_timespec_to_micros(gpr_timespec t) { - return t.tv_sec * GPR_US_PER_SEC + t.tv_nsec * 1e-3; + return (double)t.tv_sec * GPR_US_PER_SEC + t.tv_nsec * 1e-3; } diff --git a/src/core/support/time_posix.c b/src/core/support/time_posix.c index 3675f1eb22..afb58ef231 100644 --- a/src/core/support/time_posix.c +++ b/src/core/support/time_posix.c @@ -51,7 +51,7 @@ static struct timespec timespec_from_gpr(gpr_timespec gts) { static gpr_timespec gpr_from_timespec(struct timespec ts) { gpr_timespec rv; rv.tv_sec = ts.tv_sec; - rv.tv_nsec = ts.tv_nsec; + rv.tv_nsec = (int)ts.tv_nsec; return rv; } diff --git a/src/core/surface/byte_buffer.c b/src/core/surface/byte_buffer.c index 12244f6644..a930949f2d 100644 --- a/src/core/surface/byte_buffer.c +++ b/src/core/surface/byte_buffer.c @@ -35,25 +35,45 @@ #include <grpc/support/alloc.h> #include <grpc/support/log.h> -grpc_byte_buffer *grpc_byte_buffer_create(gpr_slice *slices, size_t nslices) { +grpc_byte_buffer *grpc_raw_byte_buffer_create(gpr_slice *slices, + size_t nslices) { + return grpc_raw_compressed_byte_buffer_create(slices, nslices, + GRPC_COMPRESS_NONE); +} + +grpc_byte_buffer *grpc_raw_compressed_byte_buffer_create( + gpr_slice *slices, size_t nslices, grpc_compression_algorithm compression) { size_t i; grpc_byte_buffer *bb = malloc(sizeof(grpc_byte_buffer)); - - bb->type = GRPC_BB_SLICE_BUFFER; - gpr_slice_buffer_init(&bb->data.slice_buffer); + bb->type = GRPC_BB_RAW; + bb->data.raw.compression = compression; + gpr_slice_buffer_init(&bb->data.raw.slice_buffer); for (i = 0; i < nslices; i++) { gpr_slice_ref(slices[i]); - gpr_slice_buffer_add(&bb->data.slice_buffer, slices[i]); + gpr_slice_buffer_add(&bb->data.raw.slice_buffer, slices[i]); } + return bb; +} + +grpc_byte_buffer *grpc_raw_byte_buffer_from_reader( + grpc_byte_buffer_reader *reader) { + grpc_byte_buffer *bb = malloc(sizeof(grpc_byte_buffer)); + gpr_slice slice; + bb->type = GRPC_BB_RAW; + bb->data.raw.compression = GRPC_COMPRESS_NONE; + gpr_slice_buffer_init(&bb->data.raw.slice_buffer); + while (grpc_byte_buffer_reader_next(reader, &slice)) { + gpr_slice_buffer_add(&bb->data.raw.slice_buffer, slice); + } return bb; } grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb) { switch (bb->type) { - case GRPC_BB_SLICE_BUFFER: - return grpc_byte_buffer_create(bb->data.slice_buffer.slices, - bb->data.slice_buffer.count); + case GRPC_BB_RAW: + return grpc_raw_byte_buffer_create(bb->data.raw.slice_buffer.slices, + bb->data.raw.slice_buffer.count); } gpr_log(GPR_INFO, "should never get here"); abort(); @@ -63,8 +83,8 @@ grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb) { void grpc_byte_buffer_destroy(grpc_byte_buffer *bb) { if (!bb) return; switch (bb->type) { - case GRPC_BB_SLICE_BUFFER: - gpr_slice_buffer_destroy(&bb->data.slice_buffer); + case GRPC_BB_RAW: + gpr_slice_buffer_destroy(&bb->data.raw.slice_buffer); break; } free(bb); @@ -72,8 +92,8 @@ void grpc_byte_buffer_destroy(grpc_byte_buffer *bb) { size_t grpc_byte_buffer_length(grpc_byte_buffer *bb) { switch (bb->type) { - case GRPC_BB_SLICE_BUFFER: - return bb->data.slice_buffer.length; + case GRPC_BB_RAW: + return bb->data.raw.slice_buffer.length; } gpr_log(GPR_ERROR, "should never reach here"); abort(); diff --git a/src/core/surface/byte_buffer_reader.c b/src/core/surface/byte_buffer_reader.c index fd5289bac3..283db83833 100644 --- a/src/core/surface/byte_buffer_reader.c +++ b/src/core/surface/byte_buffer_reader.c @@ -33,42 +33,73 @@ #include <grpc/byte_buffer_reader.h> +#include <grpc/compression.h> #include <grpc/grpc.h> #include <grpc/support/alloc.h> #include <grpc/support/log.h> #include <grpc/support/slice_buffer.h> #include <grpc/byte_buffer.h> -grpc_byte_buffer_reader *grpc_byte_buffer_reader_create( - grpc_byte_buffer *buffer) { - grpc_byte_buffer_reader *reader = malloc(sizeof(grpc_byte_buffer_reader)); - reader->buffer = buffer; +#include "src/core/compression/message_compress.h" + +static int is_compressed(grpc_byte_buffer *buffer) { switch (buffer->type) { - case GRPC_BB_SLICE_BUFFER: + case GRPC_BB_RAW: + if (buffer->data.raw.compression == GRPC_COMPRESS_NONE) { + return 0 /* GPR_FALSE */; + } + break; + } + return 1 /* GPR_TRUE */; +} + +void grpc_byte_buffer_reader_init(grpc_byte_buffer_reader *reader, + grpc_byte_buffer *buffer) { + gpr_slice_buffer decompressed_slices_buffer; + reader->buffer_in = buffer; + switch (reader->buffer_in->type) { + case GRPC_BB_RAW: + gpr_slice_buffer_init(&decompressed_slices_buffer); + if (is_compressed(reader->buffer_in)) { + grpc_msg_decompress(reader->buffer_in->data.raw.compression, + &reader->buffer_in->data.raw.slice_buffer, + &decompressed_slices_buffer); + reader->buffer_out = + grpc_raw_byte_buffer_create(decompressed_slices_buffer.slices, + decompressed_slices_buffer.count); + gpr_slice_buffer_destroy(&decompressed_slices_buffer); + } else { /* not compressed, use the input buffer as output */ + reader->buffer_out = reader->buffer_in; + } reader->current.index = 0; + break; + } +} + +void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader *reader) { + switch (reader->buffer_in->type) { + case GRPC_BB_RAW: + /* keeping the same if-else structure as in the init function */ + if (is_compressed(reader->buffer_in)) { + grpc_byte_buffer_destroy(reader->buffer_out); + } + break; } - return reader; } int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader *reader, gpr_slice *slice) { - grpc_byte_buffer *buffer = reader->buffer; - gpr_slice_buffer *slice_buffer; - switch (buffer->type) { - case GRPC_BB_SLICE_BUFFER: - slice_buffer = &buffer->data.slice_buffer; + switch (reader->buffer_in->type) { + case GRPC_BB_RAW: { + gpr_slice_buffer *slice_buffer; + slice_buffer = &reader->buffer_out->data.raw.slice_buffer; if (reader->current.index < slice_buffer->count) { *slice = gpr_slice_ref(slice_buffer->slices[reader->current.index]); reader->current.index += 1; return 1; - } else { - return 0; } break; + } } return 0; } - -void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader *reader) { - free(reader); -} diff --git a/src/core/surface/call.c b/src/core/surface/call.c index eea02211ae..304af259d6 100644 --- a/src/core/surface/call.c +++ b/src/core/surface/call.c @@ -42,6 +42,7 @@ #include "src/core/surface/completion_queue.h" #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> #include <assert.h> #include <stdio.h> @@ -98,6 +99,8 @@ typedef enum { /* Status came from 'the wire' - or somewhere below the surface layer */ STATUS_FROM_WIRE, + /* Status came from the server sending status */ + STATUS_FROM_SERVER_STATUS, STATUS_SOURCE_COUNT } status_source; @@ -147,12 +150,19 @@ struct grpc_call { gpr_uint8 receiving; /* are we currently completing requests */ gpr_uint8 completing; + /** has grpc_call_destroy been called */ + gpr_uint8 destroy_called; /* pairs with completed_requests */ gpr_uint8 num_completed_requests; /* are we currently reading a message? */ gpr_uint8 reading_message; /* have we bound a pollset yet? */ gpr_uint8 bound_pollset; + /* is an error status set */ + gpr_uint8 error_status_set; + /** should the alarm be cancelled */ + gpr_uint8 cancel_alarm; + /* flags with bits corresponding to write states allowing us to determine what was sent */ gpr_uint16 last_send_contains; @@ -188,6 +198,7 @@ struct grpc_call { and a strong upper bound of a count of masters to be calculated. */ gpr_uint8 request_set[GRPC_IOREQ_OP_COUNT]; grpc_ioreq_data request_data[GRPC_IOREQ_OP_COUNT]; + gpr_uint32 request_flags[GRPC_IOREQ_OP_COUNT]; reqinfo_master masters[GRPC_IOREQ_OP_COUNT]; /* Dynamic array of ioreq's that have completed: the count of @@ -210,6 +221,9 @@ struct grpc_call { /* Received call statuses from various sources */ received_status status[STATUS_SOURCE_COUNT]; + /* Compression level for the call */ + grpc_compression_level compression_level; + /* Contexts for various subsystems (security, tracing, ...). */ grpc_call_context_element context[GRPC_CONTEXT_COUNT]; @@ -231,7 +245,11 @@ struct grpc_call { gpr_slice_buffer incoming_message; gpr_uint32 incoming_message_length; + gpr_uint32 incoming_message_flags; grpc_iomgr_closure destroy_closure; + grpc_iomgr_closure on_done_recv; + grpc_iomgr_closure on_done_send; + grpc_iomgr_closure on_done_bind; }; #define CALL_STACK_FROM_CALL(call) ((grpc_call_stack *)((call) + 1)) @@ -250,6 +268,7 @@ static void recv_metadata(grpc_call *call, grpc_metadata_batch *metadata); static void finish_read_ops(grpc_call *call); static grpc_call_error cancel_with_status(grpc_call *c, grpc_status_code status, const char *description); +static void finished_loose_op(void *call, int success); static void lock(grpc_call *call); static void unlock(grpc_call *call); @@ -293,16 +312,18 @@ grpc_call *grpc_call_create(grpc_channel *channel, grpc_completion_queue *cq, grpc_sopb_init(&call->send_ops); grpc_sopb_init(&call->recv_ops); gpr_slice_buffer_init(&call->incoming_message); - /* dropped in destroy */ - gpr_ref_init(&call->internal_refcount, 1); + grpc_iomgr_closure_init(&call->on_done_recv, call_on_done_recv, call); + grpc_iomgr_closure_init(&call->on_done_send, call_on_done_send, call); + grpc_iomgr_closure_init(&call->on_done_bind, finished_loose_op, call); + /* dropped in destroy and when READ_STATE_STREAM_CLOSED received */ + gpr_ref_init(&call->internal_refcount, 2); /* server hack: start reads immediately so we can get initial metadata. TODO(ctiller): figure out a cleaner solution */ if (!call->is_client) { memset(&initial_op, 0, sizeof(initial_op)); initial_op.recv_ops = &call->recv_ops; initial_op.recv_state = &call->recv_state; - initial_op.on_done_recv = call_on_done_recv; - initial_op.recv_user_data = call; + initial_op.on_done_recv = &call->on_done_recv; initial_op.context = call->context; call->receiving = 1; GRPC_CALL_INTERNAL_REF(call, "receiving"); @@ -397,14 +418,22 @@ void grpc_call_internal_unref(grpc_call *c, int allow_immediate_deletion) { static void set_status_code(grpc_call *call, status_source source, gpr_uint32 status) { + if (call->status[source].is_set) return; + call->status[source].is_set = 1; call->status[source].code = status; + call->error_status_set = status != GRPC_STATUS_OK; if (status != GRPC_STATUS_OK && !grpc_bbq_empty(&call->incoming_queue)) { grpc_bbq_flush(&call->incoming_queue); } } +static void set_decode_compression_level(grpc_call *call, + grpc_compression_level clevel) { + call->compression_level = clevel; +} + static void set_status_details(grpc_call *call, status_source source, grpc_mdstr *status) { if (call->status[source].details != NULL) { @@ -435,7 +464,8 @@ static int need_more_data(grpc_call *call) { (is_op_live(call, GRPC_IOREQ_RECV_CLOSE) && grpc_bbq_empty(&call->incoming_queue)) || (call->write_state == WRITE_STATE_INITIAL && !call->is_client) || - (call->cancel_with_status != GRPC_STATUS_OK); + (call->cancel_with_status != GRPC_STATUS_OK) || + call->destroy_called; } static void unlock(grpc_call *call) { @@ -446,6 +476,7 @@ static void unlock(grpc_call *call) { int i; const gpr_uint32 MAX_RECV_PEEK_AHEAD = 65536; size_t buffered_bytes; + int cancel_alarm = 0; memset(&op, 0, sizeof(op)); @@ -453,11 +484,13 @@ static void unlock(grpc_call *call) { start_op = op.cancel_with_status != GRPC_STATUS_OK; call->cancel_with_status = GRPC_STATUS_OK; /* reset */ + cancel_alarm = call->cancel_alarm; + call->cancel_alarm = 0; + if (!call->receiving && need_more_data(call)) { op.recv_ops = &call->recv_ops; op.recv_state = &call->recv_state; - op.on_done_recv = call_on_done_recv; - op.recv_user_data = call; + op.on_done_recv = &call->on_done_recv; if (grpc_bbq_empty(&call->incoming_queue) && call->reading_message) { op.max_recv_bytes = call->incoming_message_length - call->incoming_message.length + MAX_RECV_PEEK_AHEAD; @@ -499,6 +532,10 @@ static void unlock(grpc_call *call) { gpr_mu_unlock(&call->mu); + if (cancel_alarm) { + grpc_alarm_cancel(&call->alarm); + } + if (start_op) { execute_op(call, &op); } @@ -590,10 +627,18 @@ static void finish_live_ioreq_op(grpc_call *call, grpc_ioreq_op op, call->write_state = WRITE_STATE_WRITE_CLOSED; } break; + case GRPC_IOREQ_SEND_STATUS: + if (call->request_data[GRPC_IOREQ_SEND_STATUS].send_status.details != + NULL) { + grpc_mdstr_unref( + call->request_data[GRPC_IOREQ_SEND_STATUS].send_status.details); + call->request_data[GRPC_IOREQ_SEND_STATUS].send_status.details = + NULL; + } + break; case GRPC_IOREQ_RECV_CLOSE: case GRPC_IOREQ_SEND_INITIAL_METADATA: case GRPC_IOREQ_SEND_TRAILING_METADATA: - case GRPC_IOREQ_SEND_STATUS: case GRPC_IOREQ_SEND_CLOSE: break; case GRPC_IOREQ_RECV_STATUS: @@ -676,13 +721,13 @@ static void call_on_done_send(void *pc, int success) { } static void finish_message(grpc_call *call) { - /* TODO(ctiller): this could be a lot faster if coded directly */ - grpc_byte_buffer *byte_buffer = grpc_byte_buffer_create( - call->incoming_message.slices, call->incoming_message.count); + if (call->error_status_set == 0) { + /* TODO(ctiller): this could be a lot faster if coded directly */ + grpc_byte_buffer *byte_buffer = grpc_raw_byte_buffer_create( + call->incoming_message.slices, call->incoming_message.count); + grpc_bbq_push(&call->incoming_queue, byte_buffer); + } gpr_slice_buffer_reset_and_unref(&call->incoming_message); - - grpc_bbq_push(&call->incoming_queue, byte_buffer); - GPR_ASSERT(call->incoming_message.count == 0); call->reading_message = 0; } @@ -711,6 +756,7 @@ static int begin_message(grpc_call *call, grpc_begin_message msg) { } else if (msg.length > 0) { call->reading_message = 1; call->incoming_message_length = msg.length; + call->incoming_message_flags = msg.flags; return 1; } else { finish_message(call); @@ -782,10 +828,8 @@ static void call_on_done_recv(void *pc, int success) { if (call->recv_state == GRPC_STREAM_CLOSED) { GPR_ASSERT(call->read_state <= READ_STATE_STREAM_CLOSED); call->read_state = READ_STATE_STREAM_CLOSED; - if (call->have_alarm) { - grpc_alarm_cancel(&call->alarm); - call->have_alarm = 0; - } + call->cancel_alarm |= call->have_alarm; + GRPC_CALL_INTERNAL_UNREF(call, "closed", 0); } finish_read_ops(call); } else { @@ -800,7 +844,7 @@ static void call_on_done_recv(void *pc, int success) { unlock(call); GRPC_CALL_INTERNAL_UNREF(call, "receiving", 0); - GRPC_TIMER_BEGIN(GRPC_PTAG_CALL_ON_DONE_RECV, 0); + GRPC_TIMER_END(GRPC_PTAG_CALL_ON_DONE_RECV, 0); } static int prepare_application_metadata(grpc_call *call, size_t count, @@ -847,9 +891,9 @@ static void copy_byte_buffer_to_stream_ops(grpc_byte_buffer *byte_buffer, size_t i; switch (byte_buffer->type) { - case GRPC_BB_SLICE_BUFFER: - for (i = 0; i < byte_buffer->data.slice_buffer.count; i++) { - gpr_slice slice = byte_buffer->data.slice_buffer.slices[i]; + case GRPC_BB_RAW: + for (i = 0; i < byte_buffer->data.raw.slice_buffer.count; i++) { + gpr_slice slice = byte_buffer->data.raw.slice_buffer.slices[i]; gpr_slice_ref(slice); grpc_sopb_add_slice(sopb, slice); } @@ -859,9 +903,9 @@ static void copy_byte_buffer_to_stream_ops(grpc_byte_buffer *byte_buffer, static int fill_send_ops(grpc_call *call, grpc_transport_op *op) { grpc_ioreq_data data; + gpr_uint32 flags; grpc_metadata_batch mdb; size_t i; - char status_str[GPR_LTOA_MIN_BUFSIZE]; GPR_ASSERT(op->send_ops == NULL); switch (call->write_state) { @@ -885,8 +929,9 @@ static int fill_send_ops(grpc_call *call, grpc_transport_op *op) { case WRITE_STATE_STARTED: if (is_op_live(call, GRPC_IOREQ_SEND_MESSAGE)) { data = call->request_data[GRPC_IOREQ_SEND_MESSAGE]; + flags = call->request_flags[GRPC_IOREQ_SEND_MESSAGE]; grpc_sopb_add_begin_message( - &call->send_ops, grpc_byte_buffer_length(data.send_message), 0); + &call->send_ops, grpc_byte_buffer_length(data.send_message), flags); copy_byte_buffer_to_stream_ops(data.send_message, &call->send_ops); op->send_ops = &call->send_ops; call->last_send_contains |= 1 << GRPC_IOREQ_SEND_MESSAGE; @@ -905,13 +950,10 @@ static int fill_send_ops(grpc_call *call, grpc_transport_op *op) { /* send status */ /* TODO(ctiller): cache common status values */ data = call->request_data[GRPC_IOREQ_SEND_STATUS]; - gpr_ltoa(data.send_status.code, status_str); grpc_metadata_batch_add_tail( &mdb, &call->status_link, - grpc_mdelem_from_metadata_strings( - call->metadata_context, - grpc_mdstr_ref(grpc_channel_get_status_string(call->channel)), - grpc_mdstr_from_string(call->metadata_context, status_str))); + grpc_channel_get_reffed_status_elem(call->channel, + data.send_status.code)); if (data.send_status.details) { grpc_metadata_batch_add_tail( &mdb, &call->details_link, @@ -919,8 +961,9 @@ static int fill_send_ops(grpc_call *call, grpc_transport_op *op) { call->metadata_context, grpc_mdstr_ref( grpc_channel_get_message_string(call->channel)), - grpc_mdstr_from_string(call->metadata_context, - data.send_status.details))); + data.send_status.details)); + call->request_data[GRPC_IOREQ_SEND_STATUS].send_status.details = + NULL; } grpc_sopb_add_metadata(&call->send_ops, mdb); } @@ -930,8 +973,7 @@ static int fill_send_ops(grpc_call *call, grpc_transport_op *op) { break; } if (op->send_ops) { - op->on_done_send = call_on_done_send; - op->send_user_data = call; + op->on_done_send = &call->on_done_send; } return op->send_ops != NULL; } @@ -965,7 +1007,7 @@ static void finish_read_ops(grpc_call *call) { switch (call->read_state) { case READ_STATE_STREAM_CLOSED: - if (empty) { + if (empty && !call->have_alarm) { finish_ioreq_op(call, GRPC_IOREQ_RECV_CLOSE, 1); } /* fallthrough */ @@ -1020,9 +1062,18 @@ static grpc_call_error start_ioreq(grpc_call *call, const grpc_ioreq *reqs, GRPC_CALL_ERROR_INVALID_METADATA); } } + if (op == GRPC_IOREQ_SEND_STATUS) { + set_status_code(call, STATUS_FROM_SERVER_STATUS, + reqs[i].data.send_status.code); + if (reqs[i].data.send_status.details) { + set_status_details(call, STATUS_FROM_SERVER_STATUS, + grpc_mdstr_ref(reqs[i].data.send_status.details)); + } + } have_ops |= 1u << op; call->request_data[op] = data; + call->request_flags[op] = reqs[i].flags; call->request_set[op] = set; } @@ -1052,10 +1103,9 @@ grpc_call_error grpc_call_start_ioreq_and_call_back( void grpc_call_destroy(grpc_call *c) { int cancel; lock(c); - if (c->have_alarm) { - grpc_alarm_cancel(&c->alarm); - c->have_alarm = 0; - } + GPR_ASSERT(!c->destroy_called); + c->destroy_called = 1; + c->cancel_alarm |= c->have_alarm; cancel = c->read_state != READ_STATE_STREAM_CLOSED; unlock(c); if (cancel) grpc_call_cancel(c); @@ -1096,14 +1146,31 @@ static void finished_loose_op(void *call, int success_ignored) { GRPC_CALL_INTERNAL_UNREF(call, "loose-op", 0); } +typedef struct { + grpc_call *call; + grpc_iomgr_closure closure; +} finished_loose_op_allocated_args; + +static void finished_loose_op_allocated(void *alloc, int success) { + finished_loose_op_allocated_args *args = alloc; + finished_loose_op(args->call, success); + gpr_free(args); +} + static void execute_op(grpc_call *call, grpc_transport_op *op) { grpc_call_element *elem; GPR_ASSERT(op->on_consumed == NULL); if (op->cancel_with_status != GRPC_STATUS_OK || op->bind_pollset) { GRPC_CALL_INTERNAL_REF(call, "loose-op"); - op->on_consumed = finished_loose_op; - op->on_consumed_user_data = call; + if (op->bind_pollset) { + op->on_consumed = &call->on_done_bind; + } else { + finished_loose_op_allocated_args *args = gpr_malloc(sizeof(*args)); + args->call = call; + grpc_iomgr_closure_init(&args->closure, finished_loose_op_allocated, args); + op->on_consumed = &args->closure; + } } elem = CALL_ELEM_FROM_CALL(call, 0); @@ -1117,12 +1184,14 @@ grpc_call *grpc_call_from_top_element(grpc_call_element *elem) { static void call_alarm(void *arg, int success) { grpc_call *call = arg; + lock(call); + call->have_alarm = 0; if (success) { - lock(call); cancel_with_status(call, GRPC_STATUS_DEADLINE_EXCEEDED, "Deadline Exceeded"); - unlock(call); } + finish_read_ops(call); + unlock(call); GRPC_CALL_INTERNAL_UNREF(call, "alarm", 1); } @@ -1160,6 +1229,33 @@ static gpr_uint32 decode_status(grpc_mdelem *md) { return status; } +/* just as for status above, we need to offset: metadata userdata can't hold a + * zero (null), which in this case is used to signal no compression */ +#define COMPRESS_OFFSET 1 +static void destroy_compression(void *ignored) {} + +static gpr_uint32 decode_compression(grpc_mdelem *md) { + grpc_compression_level clevel; + void *user_data = grpc_mdelem_get_user_data(md, destroy_status); + if (user_data) { + clevel = ((grpc_compression_level)(gpr_intptr)user_data) - COMPRESS_OFFSET; + } else { + gpr_uint32 parsed_clevel_bytes; + if (gpr_parse_bytes_to_uint32(grpc_mdstr_as_c_string(md->value), + GPR_SLICE_LENGTH(md->value->slice), + &parsed_clevel_bytes)) { + /* the following cast is safe, as a gpr_uint32 should be able to hold all + * possible values of the grpc_compression_level enum */ + clevel = (grpc_compression_level) parsed_clevel_bytes; + } else { + clevel = GRPC_COMPRESS_LEVEL_NONE; /* could not parse, no compression */ + } + grpc_mdelem_set_user_data(md, destroy_compression, + (void *)(gpr_intptr)(clevel + COMPRESS_OFFSET)); + } + return clevel; +} + static void recv_metadata(grpc_call *call, grpc_metadata_batch *md) { grpc_linked_mdelem *l; grpc_metadata_array *dest; @@ -1175,6 +1271,8 @@ static void recv_metadata(grpc_call *call, grpc_metadata_batch *md) { set_status_code(call, STATUS_FROM_WIRE, decode_status(md)); } else if (key == grpc_channel_get_message_string(call->channel)) { set_status_details(call, STATUS_FROM_WIRE, grpc_mdstr_ref(md->value)); + } else if (key == grpc_channel_get_compresssion_level_string(call->channel)) { + set_decode_compression_level(call, decode_compression(md)); } else { dest = &call->buffered_metadata[is_trailing]; if (dest->count == dest->capacity) { @@ -1239,6 +1337,14 @@ static void finish_batch_with_close(grpc_call *call, int success, void *tag) { grpc_cq_end_op(call->cq, tag, call, 1); } +static int are_write_flags_valid(gpr_uint32 flags) { + /* check that only bits in GRPC_WRITE_(INTERNAL?)_USED_MASK are set */ + const gpr_uint32 allowed_write_positions = + (GRPC_WRITE_USED_MASK | GRPC_WRITE_INTERNAL_USED_MASK); + const gpr_uint32 invalid_positions = ~allowed_write_positions; + return !(flags & invalid_positions); +} + grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops, size_t nops, void *tag) { grpc_ioreq reqs[GRPC_IOREQ_OP_COUNT]; @@ -1261,30 +1367,43 @@ grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops, op = &ops[in]; switch (op->op) { case GRPC_OP_SEND_INITIAL_METADATA: + /* Flag validation: currently allow no flags */ + if (op->flags != 0) return GRPC_CALL_ERROR_INVALID_FLAGS; req = &reqs[out++]; req->op = GRPC_IOREQ_SEND_INITIAL_METADATA; req->data.send_metadata.count = op->data.send_initial_metadata.count; req->data.send_metadata.metadata = op->data.send_initial_metadata.metadata; + req->flags = op->flags; break; case GRPC_OP_SEND_MESSAGE: + if (!are_write_flags_valid(op->flags)) { + return GRPC_CALL_ERROR_INVALID_FLAGS; + } req = &reqs[out++]; req->op = GRPC_IOREQ_SEND_MESSAGE; req->data.send_message = op->data.send_message; + req->flags = ops->flags; break; case GRPC_OP_SEND_CLOSE_FROM_CLIENT: + /* Flag validation: currently allow no flags */ + if (op->flags != 0) return GRPC_CALL_ERROR_INVALID_FLAGS; if (!call->is_client) { return GRPC_CALL_ERROR_NOT_ON_SERVER; } req = &reqs[out++]; req->op = GRPC_IOREQ_SEND_CLOSE; + req->flags = op->flags; break; case GRPC_OP_SEND_STATUS_FROM_SERVER: + /* Flag validation: currently allow no flags */ + if (op->flags != 0) return GRPC_CALL_ERROR_INVALID_FLAGS; if (call->is_client) { return GRPC_CALL_ERROR_NOT_ON_CLIENT; } req = &reqs[out++]; req->op = GRPC_IOREQ_SEND_TRAILING_METADATA; + req->flags = op->flags; req->data.send_metadata.count = op->data.send_status_from_server.trailing_metadata_count; req->data.send_metadata.metadata = @@ -1293,29 +1412,42 @@ grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops, req->op = GRPC_IOREQ_SEND_STATUS; req->data.send_status.code = op->data.send_status_from_server.status; req->data.send_status.details = - op->data.send_status_from_server.status_details; + op->data.send_status_from_server.status_details != NULL + ? grpc_mdstr_from_string( + call->metadata_context, + op->data.send_status_from_server.status_details) + : NULL; req = &reqs[out++]; req->op = GRPC_IOREQ_SEND_CLOSE; break; case GRPC_OP_RECV_INITIAL_METADATA: + /* Flag validation: currently allow no flags */ + if (op->flags != 0) return GRPC_CALL_ERROR_INVALID_FLAGS; if (!call->is_client) { return GRPC_CALL_ERROR_NOT_ON_SERVER; } req = &reqs[out++]; req->op = GRPC_IOREQ_RECV_INITIAL_METADATA; req->data.recv_metadata = op->data.recv_initial_metadata; + req->flags = op->flags; break; case GRPC_OP_RECV_MESSAGE: + /* Flag validation: currently allow no flags */ + if (op->flags != 0) return GRPC_CALL_ERROR_INVALID_FLAGS; req = &reqs[out++]; req->op = GRPC_IOREQ_RECV_MESSAGE; req->data.recv_message = op->data.recv_message; + req->flags = op->flags; break; case GRPC_OP_RECV_STATUS_ON_CLIENT: + /* Flag validation: currently allow no flags */ + if (op->flags != 0) return GRPC_CALL_ERROR_INVALID_FLAGS; if (!call->is_client) { return GRPC_CALL_ERROR_NOT_ON_SERVER; } req = &reqs[out++]; req->op = GRPC_IOREQ_RECV_STATUS; + req->flags = op->flags; req->data.recv_status.set_value = set_status_value_directly; req->data.recv_status.user_data = op->data.recv_status_on_client.status; req = &reqs[out++]; @@ -1333,8 +1465,11 @@ grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops, finish_func = finish_batch_with_close; break; case GRPC_OP_RECV_CLOSE_ON_SERVER: + /* Flag validation: currently allow no flags */ + if (op->flags != 0) return GRPC_CALL_ERROR_INVALID_FLAGS; req = &reqs[out++]; req->op = GRPC_IOREQ_RECV_STATUS; + req->flags = op->flags; req->data.recv_status.set_value = set_cancelled_value; req->data.recv_status.user_data = op->data.recv_close_on_server.cancelled; diff --git a/src/core/surface/call.h b/src/core/surface/call.h index 17db8c2cdc..fb3662b50d 100644 --- a/src/core/surface/call.h +++ b/src/core/surface/call.h @@ -72,13 +72,14 @@ typedef union { grpc_byte_buffer *send_message; struct { grpc_status_code code; - const char *details; + grpc_mdstr *details; } send_status; } grpc_ioreq_data; typedef struct { grpc_ioreq_op op; grpc_ioreq_data data; + gpr_uint32 flags; /**< A copy of the write flags from grpc_op */ } grpc_ioreq; typedef void (*grpc_ioreq_completion_func)(grpc_call *call, int success, diff --git a/src/core/surface/call_log_batch.c b/src/core/surface/call_log_batch.c index 9905401bee..55663298c9 100644 --- a/src/core/surface/call_log_batch.c +++ b/src/core/surface/call_log_batch.c @@ -35,6 +35,7 @@ #include "src/core/support/string.h" #include <grpc/support/alloc.h> +#include <grpc/support/string_util.h> int grpc_trace_batch = 0; diff --git a/src/core/surface/channel.c b/src/core/surface/channel.c index 70485d71da..a3c4dcebc1 100644 --- a/src/core/surface/channel.c +++ b/src/core/surface/channel.c @@ -37,12 +37,20 @@ #include <string.h> #include "src/core/iomgr/iomgr.h" +#include "src/core/support/string.h" #include "src/core/surface/call.h" #include "src/core/surface/client.h" #include "src/core/surface/init.h" #include <grpc/support/alloc.h> #include <grpc/support/log.h> +/** Cache grpc-status: X mdelems for X = 0..NUM_CACHED_STATUS_ELEMS. + * Avoids needing to take a metadata context lock for sending status + * if the status code is <= NUM_CACHED_STATUS_ELEMS. + * Sized to allow the most commonly used codes to fit in + * (OK, Cancelled, Unknown). */ +#define NUM_CACHED_STATUS_ELEMS 3 + typedef struct registered_call { grpc_mdelem *path; grpc_mdelem *authority; @@ -54,10 +62,14 @@ struct grpc_channel { gpr_refcount refs; gpr_uint32 max_message_length; grpc_mdctx *metadata_context; + /** mdstr for the grpc-status key */ grpc_mdstr *grpc_status_string; + grpc_mdstr *grpc_compression_level_string; grpc_mdstr *grpc_message_string; grpc_mdstr *path_string; grpc_mdstr *authority_string; + /** mdelem for grpc-status: 0 thru grpc-status: 2 */ + grpc_mdelem *grpc_status_elem[NUM_CACHED_STATUS_ELEMS]; gpr_mu registered_call_mu; registered_call *registered_calls; @@ -87,7 +99,16 @@ grpc_channel *grpc_channel_create_from_filters( gpr_ref_init(&channel->refs, 1 + is_client); channel->metadata_context = mdctx; channel->grpc_status_string = grpc_mdstr_from_string(mdctx, "grpc-status"); + channel->grpc_compression_level_string = + grpc_mdstr_from_string(mdctx, "grpc-compression-level"); channel->grpc_message_string = grpc_mdstr_from_string(mdctx, "grpc-message"); + for (i = 0; i < NUM_CACHED_STATUS_ELEMS; i++) { + char buf[GPR_LTOA_MIN_BUFSIZE]; + gpr_ltoa(i, buf); + channel->grpc_status_elem[i] = grpc_mdelem_from_metadata_strings( + mdctx, grpc_mdstr_ref(channel->grpc_status_string), + grpc_mdstr_from_string(mdctx, buf)); + } channel->path_string = grpc_mdstr_from_string(mdctx, ":path"); channel->authority_string = grpc_mdstr_from_string(mdctx, ":authority"); grpc_channel_stack_init(filters, num_filters, args, channel->metadata_context, @@ -181,8 +202,13 @@ void grpc_channel_internal_ref(grpc_channel *c) { static void destroy_channel(void *p, int ok) { grpc_channel *channel = p; + size_t i; grpc_channel_stack_destroy(CHANNEL_STACK_FROM_CHANNEL(channel)); + for (i = 0; i < NUM_CACHED_STATUS_ELEMS; i++) { + grpc_mdelem_unref(channel->grpc_status_elem[i]); + } grpc_mdstr_unref(channel->grpc_status_string); + grpc_mdstr_unref(channel->grpc_compression_level_string); grpc_mdstr_unref(channel->grpc_message_string); grpc_mdstr_unref(channel->path_string); grpc_mdstr_unref(channel->authority_string); @@ -200,7 +226,7 @@ static void destroy_channel(void *p, int ok) { #ifdef GRPC_CHANNEL_REF_COUNT_DEBUG void grpc_channel_internal_unref(grpc_channel *channel, const char *reason) { - gpr_log(GPR_DEBUG, "CHANNEL: unref %p %d -> %d [%s]", channel, + gpr_log(GPR_DEBUG, "CHANNEL: unref %p %d -> %d [%s]", channel, channel->refs.count, channel->refs.count - 1, reason); #else void grpc_channel_internal_unref(grpc_channel *channel) { @@ -247,6 +273,23 @@ grpc_mdstr *grpc_channel_get_status_string(grpc_channel *channel) { return channel->grpc_status_string; } +grpc_mdstr *grpc_channel_get_compresssion_level_string(grpc_channel *channel) { + return channel->grpc_compression_level_string; +} + + +grpc_mdelem *grpc_channel_get_reffed_status_elem(grpc_channel *channel, int i) { + if (i >= 0 && i < NUM_CACHED_STATUS_ELEMS) { + return grpc_mdelem_ref(channel->grpc_status_elem[i]); + } else { + char tmp[GPR_LTOA_MIN_BUFSIZE]; + gpr_ltoa(i, tmp); + return grpc_mdelem_from_metadata_strings( + channel->metadata_context, grpc_mdstr_ref(channel->grpc_status_string), + grpc_mdstr_from_string(channel->metadata_context, tmp)); + } +} + grpc_mdstr *grpc_channel_get_message_string(grpc_channel *channel) { return channel->grpc_message_string; } diff --git a/src/core/surface/channel.h b/src/core/surface/channel.h index d1f62f2598..3c04676b43 100644 --- a/src/core/surface/channel.h +++ b/src/core/surface/channel.h @@ -40,9 +40,20 @@ grpc_channel *grpc_channel_create_from_filters( const grpc_channel_filter **filters, size_t count, const grpc_channel_args *args, grpc_mdctx *mdctx, int is_client); +/** Get a (borrowed) pointer to this channels underlying channel stack */ grpc_channel_stack *grpc_channel_get_channel_stack(grpc_channel *channel); + +/** Get a (borrowed) pointer to the channel wide metadata context */ grpc_mdctx *grpc_channel_get_metadata_context(grpc_channel *channel); + +/** Get a grpc_mdelem of grpc-status: X where X is the numeric value of + status_code. + + The returned elem is owned by the caller. */ +grpc_mdelem *grpc_channel_get_reffed_status_elem(grpc_channel *channel, + int status_code); grpc_mdstr *grpc_channel_get_status_string(grpc_channel *channel); +grpc_mdstr *grpc_channel_get_compresssion_level_string(grpc_channel *channel); grpc_mdstr *grpc_channel_get_message_string(grpc_channel *channel); gpr_uint32 grpc_channel_get_max_message_length(grpc_channel *channel); diff --git a/src/core/surface/channel_create.c b/src/core/surface/channel_create.c index a49b754d9a..d069a04a9a 100644 --- a/src/core/surface/channel_create.c +++ b/src/core/surface/channel_create.c @@ -53,6 +53,7 @@ #include "src/core/transport/chttp2_transport.h" #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> #include <grpc/support/sync.h> #include <grpc/support/useful.h> @@ -137,7 +138,8 @@ static void on_resolved(void *rp, grpc_resolved_addresses *resolved) { request *r = rp; /* if we're not still the active request, abort */ - if (!grpc_client_setup_request_should_continue(r->cs_request, "on_resolved")) { + if (!grpc_client_setup_request_should_continue(r->cs_request, + "on_resolved")) { if (resolved) { grpc_resolved_addresses_destroy(resolved); } diff --git a/src/core/surface/completion_queue.c b/src/core/surface/completion_queue.c index 6808c976e1..030a8b4e6f 100644 --- a/src/core/surface/completion_queue.c +++ b/src/core/surface/completion_queue.c @@ -73,6 +73,7 @@ struct grpc_completion_queue { event *queue; /* Fixed size chained hash table of events for pluck() */ event *buckets[NUM_TAG_BUCKETS]; + int is_server_cq; }; grpc_completion_queue *grpc_completion_queue_create(void) { @@ -86,21 +87,12 @@ grpc_completion_queue *grpc_completion_queue_create(void) { return cc; } - - - - - - - - - - - - #ifdef GRPC_CQ_REF_COUNT_DEBUG -void grpc_cq_internal_ref(grpc_completion_queue *cc, const char *reason) { - gpr_log(GPR_DEBUG, "CQ:%p ref %d -> %d %s", cc, (int)cc->owning_refs.count, (int)cc->owning_refs.count + 1, reason); +void grpc_cq_internal_ref(grpc_completion_queue *cc, const char *reason, + const char *file, int line) { + gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "CQ:%p ref %d -> %d %s", + cc, (int)cc->owning_refs.count, (int)cc->owning_refs.count + 1, + reason); #else void grpc_cq_internal_ref(grpc_completion_queue *cc) { #endif @@ -113,8 +105,11 @@ static void on_pollset_destroy_done(void *arg) { } #ifdef GRPC_CQ_REF_COUNT_DEBUG -void grpc_cq_internal_unref(grpc_completion_queue *cc, const char *reason) { - gpr_log(GPR_DEBUG, "CQ:%p unref %d -> %d %s", cc, (int)cc->owning_refs.count, (int)cc->owning_refs.count - 1, reason); +void grpc_cq_internal_unref(grpc_completion_queue *cc, const char *reason, + const char *file, int line) { + gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "CQ:%p unref %d -> %d %s", + cc, (int)cc->owning_refs.count, (int)cc->owning_refs.count - 1, + reason); #else void grpc_cq_internal_unref(grpc_completion_queue *cc) { #endif @@ -333,3 +328,7 @@ void grpc_cq_hack_spin_pollset(grpc_completion_queue *cc) { gpr_time_add(gpr_now(), gpr_time_from_millis(100))); gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset)); } + +void grpc_cq_mark_server_cq(grpc_completion_queue *cc) { cc->is_server_cq = 1; } + +int grpc_cq_is_server_cq(grpc_completion_queue *cc) { return cc->is_server_cq; } diff --git a/src/core/surface/completion_queue.h b/src/core/surface/completion_queue.h index 2249d0e789..1b9010f462 100644 --- a/src/core/surface/completion_queue.h +++ b/src/core/surface/completion_queue.h @@ -40,10 +40,14 @@ #include <grpc/grpc.h> #ifdef GRPC_CQ_REF_COUNT_DEBUG -void grpc_cq_internal_ref(grpc_completion_queue *cc, const char *reason); -void grpc_cq_internal_unref(grpc_completion_queue *cc, const char *reason); -#define GRPC_CQ_INTERNAL_REF(cc, reason) grpc_cq_internal_ref(cc, reason) -#define GRPC_CQ_INTERNAL_UNREF(cc, reason) grpc_cq_internal_unref(cc, reason) +void grpc_cq_internal_ref(grpc_completion_queue *cc, const char *reason, + const char *file, int line); +void grpc_cq_internal_unref(grpc_completion_queue *cc, const char *reason, + const char *file, int line); +#define GRPC_CQ_INTERNAL_REF(cc, reason) \ + grpc_cq_internal_ref(cc, reason, __FILE__, __LINE__) +#define GRPC_CQ_INTERNAL_UNREF(cc, reason) \ + grpc_cq_internal_unref(cc, reason, __FILE__, __LINE__) #else void grpc_cq_internal_ref(grpc_completion_queue *cc); void grpc_cq_internal_unref(grpc_completion_queue *cc); @@ -63,4 +67,7 @@ grpc_pollset *grpc_cq_pollset(grpc_completion_queue *cc); void grpc_cq_hack_spin_pollset(grpc_completion_queue *cc); +void grpc_cq_mark_server_cq(grpc_completion_queue *cc); +int grpc_cq_is_server_cq(grpc_completion_queue *cc); + #endif /* GRPC_INTERNAL_CORE_SURFACE_COMPLETION_QUEUE_H */ diff --git a/src/core/surface/event_string.c b/src/core/surface/event_string.c index 448bb1162b..33cd4a43aa 100644 --- a/src/core/surface/event_string.c +++ b/src/core/surface/event_string.c @@ -37,6 +37,7 @@ #include "src/core/support/string.h" #include <grpc/byte_buffer.h> +#include <grpc/support/string_util.h> static void addhdr(gpr_strvec *buf, grpc_event *ev) { char *tmp; diff --git a/src/core/surface/init.c b/src/core/surface/init.c index ac6871c6f2..ca61a38a35 100644 --- a/src/core/surface/init.c +++ b/src/core/surface/init.c @@ -78,6 +78,7 @@ void grpc_shutdown(void) { grpc_iomgr_shutdown(); census_shutdown(); grpc_timers_global_destroy(); + grpc_tracer_shutdown(); } gpr_mu_unlock(&g_init_mu); } diff --git a/src/core/surface/lame_client.c b/src/core/surface/lame_client.c index 6c07b01544..85e1ab5554 100644 --- a/src/core/surface/lame_client.c +++ b/src/core/surface/lame_client.c @@ -56,7 +56,7 @@ static void lame_start_transport_op(grpc_call_element *elem, GRPC_CALL_LOG_OP(GPR_INFO, elem, op); if (op->send_ops) { grpc_stream_ops_unref_owned_objects(op->send_ops->ops, op->send_ops->nops); - op->on_done_send(op->send_user_data, 0); + op->on_done_send->cb(op->on_done_send->cb_arg, 0); } if (op->recv_ops) { char tmp[GPR_LTOA_MIN_BUFSIZE]; @@ -75,10 +75,10 @@ static void lame_start_transport_op(grpc_call_element *elem, mdb.deadline = gpr_inf_future; grpc_sopb_add_metadata(op->recv_ops, mdb); *op->recv_state = GRPC_STREAM_CLOSED; - op->on_done_recv(op->recv_user_data, 1); + op->on_done_recv->cb(op->on_done_recv->cb_arg, 1); } if (op->on_consumed) { - op->on_consumed(op->on_consumed_user_data, 0); + op->on_consumed->cb(op->on_consumed->cb_arg, 0); } } @@ -118,9 +118,9 @@ static void init_channel_elem(grpc_channel_element *elem, static void destroy_channel_elem(grpc_channel_element *elem) {} static const grpc_channel_filter lame_filter = { - lame_start_transport_op, channel_op, sizeof(call_data), init_call_elem, - destroy_call_elem, sizeof(channel_data), init_channel_elem, - destroy_channel_elem, "lame-client", + lame_start_transport_op, channel_op, sizeof(call_data), + init_call_elem, destroy_call_elem, sizeof(channel_data), + init_channel_elem, destroy_channel_elem, "lame-client", }; grpc_channel *grpc_lame_client_channel_create(void) { diff --git a/src/core/surface/secure_channel_create.c b/src/core/surface/secure_channel_create.c index 3875eb0614..fae3e4e90a 100644 --- a/src/core/surface/secure_channel_create.c +++ b/src/core/surface/secure_channel_create.c @@ -56,6 +56,7 @@ #include <grpc/grpc_security.h> #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> #include <grpc/support/sync.h> #include <grpc/support/useful.h> #include "src/core/tsi/transport_security_interface.h" @@ -96,7 +97,8 @@ static void on_secure_transport_setup_done(void *rp, if (status != GRPC_SECURITY_OK) { gpr_log(GPR_ERROR, "Secure transport setup failed with error %d.", status); done(r, 0); - } else if (grpc_client_setup_cb_begin(r->cs_request, "on_secure_transport_setup_done")) { + } else if (grpc_client_setup_cb_begin(r->cs_request, + "on_secure_transport_setup_done")) { grpc_create_chttp2_transport( r->setup->setup_callback, r->setup->setup_user_data, grpc_client_setup_get_channel_args(r->cs_request), secure_endpoint, @@ -112,7 +114,8 @@ static void on_secure_transport_setup_done(void *rp, static void on_connect(void *rp, grpc_endpoint *tcp) { request *r = rp; - if (!grpc_client_setup_request_should_continue(r->cs_request, "on_connect.secure")) { + if (!grpc_client_setup_request_should_continue(r->cs_request, + "on_connect.secure")) { if (tcp) { grpc_endpoint_shutdown(tcp); grpc_endpoint_destroy(tcp); @@ -152,7 +155,8 @@ static void on_resolved(void *rp, grpc_resolved_addresses *resolved) { request *r = rp; /* if we're not still the active request, abort */ - if (!grpc_client_setup_request_should_continue(r->cs_request, "on_resolved.secure")) { + if (!grpc_client_setup_request_should_continue(r->cs_request, + "on_resolved.secure")) { if (resolved) { grpc_resolved_addresses_destroy(resolved); } diff --git a/src/core/surface/server.c b/src/core/surface/server.c index 4cf9213e66..13ec5bee94 100644 --- a/src/core/surface/server.c +++ b/src/core/surface/server.c @@ -48,6 +48,7 @@ #include "src/core/transport/metadata.h" #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/string_util.h> #include <grpc/support/useful.h> typedef enum { PENDING_START, ALL_CALLS, CALL_LIST_COUNT } call_list; @@ -140,7 +141,15 @@ struct grpc_server { grpc_pollset **pollsets; size_t cq_count; - gpr_mu mu; + /* The two following mutexes control access to server-state + mu_global controls access to non-call-related state (e.g., channel state) + mu_call controls access to call-related state (e.g., the call lists) + + If they are ever required to be nested, you must lock mu_global + before mu_call. This is currently used in shutdown processing + (grpc_server_shutdown_and_notify and maybe_finish_shutdown) */ + gpr_mu mu_global; /* mutex for server and channel state */ + gpr_mu mu_call; /* mutex for call-specific state */ registered_method *registered_methods; requested_call_array requested_calls; @@ -182,9 +191,9 @@ struct call_data { grpc_stream_op_buffer *recv_ops; grpc_stream_state *recv_state; - void (*on_done_recv)(void *user_data, int success); - void *recv_user_data; + grpc_iomgr_closure *on_done_recv; + grpc_iomgr_closure server_on_recv; grpc_iomgr_closure kill_zombie_closure; call_data **root[CALL_LIST_COUNT]; @@ -199,6 +208,8 @@ static void begin_call(grpc_server *server, call_data *calld, static void fail_call(grpc_server *server, requested_call *rc); static void shutdown_channel(channel_data *chand, int send_goaway, int send_disconnect); +/* Before calling maybe_finish_shutdown, we must hold mu_global and not + hold mu_call */ static void maybe_finish_shutdown(grpc_server *server); static int call_list_join(call_data **root, call_data *call, call_list list) { @@ -272,7 +283,8 @@ static void server_delete(grpc_server *server) { registered_method *rm; size_t i; grpc_channel_args_destroy(server->channel_args); - gpr_mu_destroy(&server->mu); + gpr_mu_destroy(&server->mu_global); + gpr_mu_destroy(&server->mu_call); gpr_free(server->channel_filters); requested_call_array_destroy(&server->requested_calls); while ((rm = server->registered_methods) != NULL) { @@ -334,11 +346,11 @@ static void finish_start_new_rpc_and_unlock(grpc_server *server, if (array->count == 0) { calld->state = PENDING; call_list_join(pending_root, calld, PENDING_START); - gpr_mu_unlock(&server->mu); + gpr_mu_unlock(&server->mu_call); } else { rc = array->calls[--array->count]; calld->state = ACTIVATED; - gpr_mu_unlock(&server->mu); + gpr_mu_unlock(&server->mu_call); begin_call(server, calld, &rc); } } @@ -351,7 +363,7 @@ static void start_new_rpc(grpc_call_element *elem) { gpr_uint32 hash; channel_registered_method *rm; - gpr_mu_lock(&server->mu); + gpr_mu_lock(&server->mu_call); if (chand->registered_methods && calld->path && calld->host) { /* TODO(ctiller): unify these two searches */ /* check for an exact match with host */ @@ -403,11 +415,16 @@ static void maybe_finish_shutdown(grpc_server *server) { if (!server->shutdown || server->shutdown_published) { return; } + + gpr_mu_lock(&server->mu_call); if (server->lists[ALL_CALLS] != NULL) { gpr_log(GPR_DEBUG, "Waiting for all calls to finish before destroying server"); + gpr_mu_unlock(&server->mu_call); return; } + gpr_mu_unlock(&server->mu_call); + if (server->root_channel_data.next != &server->root_channel_data) { gpr_log(GPR_DEBUG, "Waiting for all channels to close before destroying server"); @@ -451,6 +468,7 @@ static void server_on_recv(void *ptr, int success) { grpc_call_element *elem = ptr; call_data *calld = elem->call_data; channel_data *chand = elem->channel_data; + int remove_res; if (success && !calld->got_initial_metadata) { size_t i; @@ -475,16 +493,16 @@ static void server_on_recv(void *ptr, int success) { case GRPC_STREAM_SEND_CLOSED: break; case GRPC_STREAM_RECV_CLOSED: - gpr_mu_lock(&chand->server->mu); + gpr_mu_lock(&chand->server->mu_call); if (calld->state == NOT_STARTED) { calld->state = ZOMBIED; grpc_iomgr_closure_init(&calld->kill_zombie_closure, kill_zombie, elem); grpc_iomgr_add_callback(&calld->kill_zombie_closure); } - gpr_mu_unlock(&chand->server->mu); + gpr_mu_unlock(&chand->server->mu_call); break; case GRPC_STREAM_CLOSED: - gpr_mu_lock(&chand->server->mu); + gpr_mu_lock(&chand->server->mu_call); if (calld->state == NOT_STARTED) { calld->state = ZOMBIED; grpc_iomgr_closure_init(&calld->kill_zombie_closure, kill_zombie, elem); @@ -494,16 +512,18 @@ static void server_on_recv(void *ptr, int success) { calld->state = ZOMBIED; grpc_iomgr_closure_init(&calld->kill_zombie_closure, kill_zombie, elem); grpc_iomgr_add_callback(&calld->kill_zombie_closure); - } - if (call_list_remove(calld, ALL_CALLS)) { + remove_res = call_list_remove(calld, ALL_CALLS); + gpr_mu_unlock(&chand->server->mu_call); + gpr_mu_lock(&chand->server->mu_global); + if (remove_res) { decrement_call_count(chand); } - gpr_mu_unlock(&chand->server->mu); + gpr_mu_unlock(&chand->server->mu_global); break; } - calld->on_done_recv(calld->recv_user_data, success); + calld->on_done_recv->cb(calld->on_done_recv->cb_arg, success); } static void server_mutate_op(grpc_call_element *elem, grpc_transport_op *op) { @@ -514,9 +534,7 @@ static void server_mutate_op(grpc_call_element *elem, grpc_transport_op *op) { calld->recv_ops = op->recv_ops; calld->recv_state = op->recv_state; calld->on_done_recv = op->on_done_recv; - calld->recv_user_data = op->recv_user_data; - op->on_done_recv = server_on_recv; - op->recv_user_data = elem; + op->on_done_recv = &calld->server_on_recv; } } @@ -542,10 +560,10 @@ static void channel_op(grpc_channel_element *elem, case GRPC_TRANSPORT_CLOSED: /* if the transport is closed for a server channel, we destroy the channel */ - gpr_mu_lock(&server->mu); + gpr_mu_lock(&server->mu_global); server_ref(server); destroy_channel(chand); - gpr_mu_unlock(&server->mu); + gpr_mu_unlock(&server->mu_global); server_unref(server); break; case GRPC_TRANSPORT_GOAWAY: @@ -590,7 +608,8 @@ static void finish_shutdown_channel(void *p, int success) { gpr_free(sca); } -static void shutdown_channel(channel_data *chand, int send_goaway, int send_disconnect) { +static void shutdown_channel(channel_data *chand, int send_goaway, + int send_disconnect) { shutdown_channel_args *sca; GRPC_CHANNEL_INTERNAL_REF(chand->channel, "shutdown"); sca = gpr_malloc(sizeof(shutdown_channel_args)); @@ -611,10 +630,15 @@ static void init_call_elem(grpc_call_element *elem, calld->deadline = gpr_inf_future; calld->call = grpc_call_from_top_element(elem); - gpr_mu_lock(&chand->server->mu); + grpc_iomgr_closure_init(&calld->server_on_recv, server_on_recv, elem); + + gpr_mu_lock(&chand->server->mu_call); call_list_join(&chand->server->lists[ALL_CALLS], calld, ALL_CALLS); + gpr_mu_unlock(&chand->server->mu_call); + + gpr_mu_lock(&chand->server->mu_global); chand->num_calls++; - gpr_mu_unlock(&chand->server->mu); + gpr_mu_unlock(&chand->server->mu_global); server_ref(chand->server); @@ -627,14 +651,16 @@ static void destroy_call_elem(grpc_call_element *elem) { int removed[CALL_LIST_COUNT]; size_t i; - gpr_mu_lock(&chand->server->mu); + gpr_mu_lock(&chand->server->mu_call); for (i = 0; i < CALL_LIST_COUNT; i++) { removed[i] = call_list_remove(elem->call_data, i); } + gpr_mu_unlock(&chand->server->mu_call); if (removed[ALL_CALLS]) { + gpr_mu_lock(&chand->server->mu_global); decrement_call_count(chand); + gpr_mu_unlock(&chand->server->mu_global); } - gpr_mu_unlock(&chand->server->mu); if (calld->host) { grpc_mdstr_unref(calld->host); @@ -677,12 +703,12 @@ static void destroy_channel_elem(grpc_channel_element *elem) { gpr_free(chand->registered_methods); } if (chand->server) { - gpr_mu_lock(&chand->server->mu); + gpr_mu_lock(&chand->server->mu_global); chand->next->prev = chand->prev; chand->prev->next = chand->next; chand->next = chand->prev = chand; maybe_finish_shutdown(chand->server); - gpr_mu_unlock(&chand->server->mu); + gpr_mu_unlock(&chand->server->mu_global); grpc_mdstr_unref(chand->path_key); grpc_mdstr_unref(chand->authority_key); server_unref(chand->server); @@ -708,6 +734,7 @@ void grpc_server_register_completion_queue(grpc_server *server, if (server->cqs[i] == cq) return; } GRPC_CQ_INTERNAL_REF(cq, "server"); + grpc_cq_mark_server_cq(cq); n = server->cq_count++; server->cqs = gpr_realloc(server->cqs, server->cq_count * sizeof(grpc_completion_queue *)); @@ -728,7 +755,8 @@ grpc_server *grpc_server_create_from_filters(grpc_channel_filter **filters, memset(server, 0, sizeof(grpc_server)); - gpr_mu_init(&server->mu); + gpr_mu_init(&server->mu_global); + gpr_mu_init(&server->mu_call); /* decremented by grpc_server_destroy */ gpr_ref_init(&server->internal_refcount, 1); @@ -878,11 +906,11 @@ grpc_transport_setup_result grpc_server_setup_transport( result = grpc_connected_channel_bind_transport( grpc_channel_get_channel_stack(channel), transport); - gpr_mu_lock(&s->mu); + gpr_mu_lock(&s->mu_global); chand->next = &s->root_channel_data; chand->prev = chand->next->prev; chand->next->prev = chand->prev->next = chand; - gpr_mu_unlock(&s->mu); + gpr_mu_unlock(&s->mu_global); gpr_free(filters); @@ -899,7 +927,7 @@ void grpc_server_shutdown_and_notify(grpc_server *server, shutdown_tag *sdt; /* lock, and gather up some stuff to do */ - gpr_mu_lock(&server->mu); + gpr_mu_lock(&server->mu_global); grpc_cq_begin_op(cq, NULL); server->shutdown_tags = gpr_realloc(server->shutdown_tags, @@ -908,7 +936,7 @@ void grpc_server_shutdown_and_notify(grpc_server *server, sdt->tag = tag; sdt->cq = cq; if (server->shutdown) { - gpr_mu_unlock(&server->mu); + gpr_mu_unlock(&server->mu_global); return; } @@ -918,6 +946,7 @@ void grpc_server_shutdown_and_notify(grpc_server *server, } /* collect all unregistered then registered calls */ + gpr_mu_lock(&server->mu_call); requested_calls = server->requested_calls; memset(&server->requested_calls, 0, sizeof(server->requested_calls)); for (rm = server->registered_methods; rm; rm = rm->next) { @@ -936,10 +965,11 @@ void grpc_server_shutdown_and_notify(grpc_server *server, gpr_free(rm->requested.calls); memset(&rm->requested, 0, sizeof(rm->requested)); } + gpr_mu_unlock(&server->mu_call); server->shutdown = 1; maybe_finish_shutdown(server); - gpr_mu_unlock(&server->mu); + gpr_mu_unlock(&server->mu_global); /* terminate all the requested calls */ for (i = 0; i < requested_calls.count; i++) { @@ -955,10 +985,10 @@ void grpc_server_shutdown_and_notify(grpc_server *server, void grpc_server_listener_destroy_done(void *s) { grpc_server *server = s; - gpr_mu_lock(&server->mu); + gpr_mu_lock(&server->mu_global); server->listeners_destroyed++; maybe_finish_shutdown(server); - gpr_mu_unlock(&server->mu); + gpr_mu_unlock(&server->mu_global); } void grpc_server_cancel_all_calls(grpc_server *server) { @@ -969,12 +999,12 @@ void grpc_server_cancel_all_calls(grpc_server *server) { int is_first = 1; size_t i; - gpr_mu_lock(&server->mu); + gpr_mu_lock(&server->mu_call); GPR_ASSERT(server->shutdown); if (!server->lists[ALL_CALLS]) { - gpr_mu_unlock(&server->mu); + gpr_mu_unlock(&server->mu_call); return; } @@ -994,7 +1024,7 @@ void grpc_server_cancel_all_calls(grpc_server *server) { is_first = 0; } - gpr_mu_unlock(&server->mu); + gpr_mu_unlock(&server->mu_call); for (i = 0; i < call_count; i++) { grpc_call_cancel_with_status(calls[i], GRPC_STATUS_UNAVAILABLE, @@ -1008,8 +1038,8 @@ void grpc_server_cancel_all_calls(grpc_server *server) { void grpc_server_destroy(grpc_server *server) { listener *l; - gpr_mu_lock(&server->mu); - GPR_ASSERT(server->shutdown); + gpr_mu_lock(&server->mu_global); + GPR_ASSERT(server->shutdown || !server->listeners); GPR_ASSERT(server->listeners_destroyed == num_listeners(server)); while (server->listeners) { @@ -1018,7 +1048,7 @@ void grpc_server_destroy(grpc_server *server) { gpr_free(l); } - gpr_mu_unlock(&server->mu); + gpr_mu_unlock(&server->mu_global); server_unref(server); } @@ -1040,9 +1070,9 @@ static grpc_call_error queue_call_request(grpc_server *server, requested_call *rc) { call_data *calld = NULL; requested_call_array *requested_calls = NULL; - gpr_mu_lock(&server->mu); + gpr_mu_lock(&server->mu_call); if (server->shutdown) { - gpr_mu_unlock(&server->mu); + gpr_mu_unlock(&server->mu_call); fail_call(server, rc); return GRPC_CALL_OK; } @@ -1061,12 +1091,12 @@ static grpc_call_error queue_call_request(grpc_server *server, if (calld) { GPR_ASSERT(calld->state == PENDING); calld->state = ACTIVATED; - gpr_mu_unlock(&server->mu); + gpr_mu_unlock(&server->mu_call); begin_call(server, calld, rc); return GRPC_CALL_OK; } else { *requested_call_array_add(requested_calls) = *rc; - gpr_mu_unlock(&server->mu); + gpr_mu_unlock(&server->mu_call); return GRPC_CALL_OK; } } @@ -1080,6 +1110,9 @@ grpc_call_error grpc_server_request_call( GRPC_SERVER_LOG_REQUEST_CALL(GPR_INFO, server, call, details, initial_metadata, cq_bound_to_call, cq_for_notification, tag); + if (!grpc_cq_is_server_cq(cq_for_notification)) { + return GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE; + } grpc_cq_begin_op(cq_for_notification, NULL); rc.type = BATCH_CALL; rc.tag = tag; @@ -1098,6 +1131,9 @@ grpc_call_error grpc_server_request_registered_call( grpc_completion_queue *cq_for_notification, void *tag) { requested_call rc; registered_method *registered_method = rm; + if (!grpc_cq_is_server_cq(cq_for_notification)) { + return GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE; + } grpc_cq_begin_op(cq_for_notification, NULL); rc.type = REGISTERED_CALL; rc.tag = tag; @@ -1152,6 +1188,7 @@ static void begin_call(grpc_server *server, call_data *calld, rc->data.batch.details->deadline = calld->deadline; r->op = GRPC_IOREQ_RECV_INITIAL_METADATA; r->data.recv_metadata = rc->data.batch.initial_metadata; + r->flags = 0; r++; publish = publish_registered_or_batch; break; @@ -1159,10 +1196,12 @@ static void begin_call(grpc_server *server, call_data *calld, *rc->data.registered.deadline = calld->deadline; r->op = GRPC_IOREQ_RECV_INITIAL_METADATA; r->data.recv_metadata = rc->data.registered.initial_metadata; + r->flags = 0; r++; if (rc->data.registered.optional_payload) { r->op = GRPC_IOREQ_RECV_MESSAGE; r->data.recv_message = rc->data.registered.optional_payload; + r->flags = 0; r++; } publish = publish_registered_or_batch; @@ -1201,8 +1240,8 @@ const grpc_channel_args *grpc_server_get_channel_args(grpc_server *server) { int grpc_server_has_open_connections(grpc_server *server) { int r; - gpr_mu_lock(&server->mu); + gpr_mu_lock(&server->mu_global); r = server->root_channel_data.next != &server->root_channel_data; - gpr_mu_unlock(&server->mu); + gpr_mu_unlock(&server->mu_global); return r; } diff --git a/src/core/transport/chttp2/alpn.h b/src/core/transport/chttp2/alpn.h index fcbefc060f..f38b4c3167 100644 --- a/src/core/transport/chttp2/alpn.h +++ b/src/core/transport/chttp2/alpn.h @@ -46,4 +46,4 @@ size_t grpc_chttp2_num_alpn_versions(void); * grpc_chttp2_num_alpn_versions()) */ const char *grpc_chttp2_get_alpn_version_index(size_t i); -#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_ALPN_H */ +#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_ALPN_H */ diff --git a/src/core/transport/chttp2/bin_encoder.c b/src/core/transport/chttp2/bin_encoder.c index f5ca6c4e50..dee6dbec8b 100644 --- a/src/core/transport/chttp2/bin_encoder.c +++ b/src/core/transport/chttp2/bin_encoder.c @@ -46,70 +46,18 @@ typedef struct { gpr_uint8 length; } b64_huff_sym; -static const b64_huff_sym huff_alphabet[64] = {{0x21, 6}, - {0x5d, 7}, - {0x5e, 7}, - {0x5f, 7}, - {0x60, 7}, - {0x61, 7}, - {0x62, 7}, - {0x63, 7}, - {0x64, 7}, - {0x65, 7}, - {0x66, 7}, - {0x67, 7}, - {0x68, 7}, - {0x69, 7}, - {0x6a, 7}, - {0x6b, 7}, - {0x6c, 7}, - {0x6d, 7}, - {0x6e, 7}, - {0x6f, 7}, - {0x70, 7}, - {0x71, 7}, - {0x72, 7}, - {0xfc, 8}, - {0x73, 7}, - {0xfd, 8}, - {0x3, 5}, - {0x23, 6}, - {0x4, 5}, - {0x24, 6}, - {0x5, 5}, - {0x25, 6}, - {0x26, 6}, - {0x27, 6}, - {0x6, 5}, - {0x74, 7}, - {0x75, 7}, - {0x28, 6}, - {0x29, 6}, - {0x2a, 6}, - {0x7, 5}, - {0x2b, 6}, - {0x76, 7}, - {0x2c, 6}, - {0x8, 5}, - {0x9, 5}, - {0x2d, 6}, - {0x77, 7}, - {0x78, 7}, - {0x79, 7}, - {0x7a, 7}, - {0x7b, 7}, - {0x0, 5}, - {0x1, 5}, - {0x2, 5}, - {0x19, 6}, - {0x1a, 6}, - {0x1b, 6}, - {0x1c, 6}, - {0x1d, 6}, - {0x1e, 6}, - {0x1f, 6}, - {0x7fb, 11}, - {0x18, 6}}; +static const b64_huff_sym huff_alphabet[64] = { + {0x21, 6}, {0x5d, 7}, {0x5e, 7}, {0x5f, 7}, {0x60, 7}, {0x61, 7}, + {0x62, 7}, {0x63, 7}, {0x64, 7}, {0x65, 7}, {0x66, 7}, {0x67, 7}, + {0x68, 7}, {0x69, 7}, {0x6a, 7}, {0x6b, 7}, {0x6c, 7}, {0x6d, 7}, + {0x6e, 7}, {0x6f, 7}, {0x70, 7}, {0x71, 7}, {0x72, 7}, {0xfc, 8}, + {0x73, 7}, {0xfd, 8}, {0x3, 5}, {0x23, 6}, {0x4, 5}, {0x24, 6}, + {0x5, 5}, {0x25, 6}, {0x26, 6}, {0x27, 6}, {0x6, 5}, {0x74, 7}, + {0x75, 7}, {0x28, 6}, {0x29, 6}, {0x2a, 6}, {0x7, 5}, {0x2b, 6}, + {0x76, 7}, {0x2c, 6}, {0x8, 5}, {0x9, 5}, {0x2d, 6}, {0x77, 7}, + {0x78, 7}, {0x79, 7}, {0x7a, 7}, {0x7b, 7}, {0x0, 5}, {0x1, 5}, + {0x2, 5}, {0x19, 6}, {0x1a, 6}, {0x1b, 6}, {0x1c, 6}, {0x1d, 6}, + {0x1e, 6}, {0x1f, 6}, {0x7fb, 11}, {0x18, 6}}; static const gpr_uint8 tail_xtra[3] = {0, 2, 3}; diff --git a/src/core/transport/chttp2/bin_encoder.h b/src/core/transport/chttp2/bin_encoder.h index 9c88ac9725..d3e5a855dd 100644 --- a/src/core/transport/chttp2/bin_encoder.h +++ b/src/core/transport/chttp2/bin_encoder.h @@ -53,4 +53,4 @@ gpr_slice grpc_chttp2_base64_encode_and_huffman_compress(gpr_slice input); int grpc_is_binary_header(const char *key, size_t length); -#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_BIN_ENCODER_H */ +#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_BIN_ENCODER_H */ diff --git a/src/core/transport/chttp2/frame.h b/src/core/transport/chttp2/frame.h index c9e3e13042..879ee036fa 100644 --- a/src/core/transport/chttp2/frame.h +++ b/src/core/transport/chttp2/frame.h @@ -45,23 +45,9 @@ typedef enum { GRPC_CHTTP2_CONNECTION_ERROR } grpc_chttp2_parse_error; -typedef struct { - gpr_uint8 end_of_stream; - gpr_uint8 need_flush_reads; - gpr_uint8 metadata_boundary; - gpr_uint8 ack_settings; - gpr_uint8 send_ping_ack; - gpr_uint8 process_ping_reply; - gpr_uint8 goaway; - gpr_uint8 rst_stream; - - gpr_int64 initial_window_update; - gpr_uint32 window_update; - gpr_uint32 goaway_last_stream_index; - gpr_uint32 goaway_error; - gpr_slice goaway_text; - gpr_uint32 rst_stream_reason; -} grpc_chttp2_parse_state; +/* defined in internal.h */ +typedef struct grpc_chttp2_stream_parsing grpc_chttp2_stream_parsing; +typedef struct grpc_chttp2_transport_parsing grpc_chttp2_transport_parsing; #define GRPC_CHTTP2_FRAME_DATA 0 #define GRPC_CHTTP2_FRAME_HEADER 1 @@ -80,4 +66,4 @@ typedef struct { #define GRPC_CHTTP2_DATA_FLAG_PADDED 8 #define GRPC_CHTTP2_FLAG_HAS_PRIORITY 0x20 -#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_FRAME_H */ +#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_FRAME_H */ diff --git a/src/core/transport/chttp2/frame_data.c b/src/core/transport/chttp2/frame_data.c index a1ae9ed2e6..0ad62a9999 100644 --- a/src/core/transport/chttp2/frame_data.c +++ b/src/core/transport/chttp2/frame_data.c @@ -35,6 +35,7 @@ #include <string.h> +#include "src/core/transport/chttp2/internal.h" #include "src/core/support/string.h" #include <grpc/support/alloc.h> #include <grpc/support/log.h> @@ -69,16 +70,15 @@ grpc_chttp2_parse_error grpc_chttp2_data_parser_begin_frame( } grpc_chttp2_parse_error grpc_chttp2_data_parser_parse( - void *parser, grpc_chttp2_parse_state *state, gpr_slice slice, - int is_last) { + void *parser, grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) { gpr_uint8 *const beg = GPR_SLICE_START_PTR(slice); gpr_uint8 *const end = GPR_SLICE_END_PTR(slice); gpr_uint8 *cur = beg; grpc_chttp2_data_parser *p = parser; if (is_last && p->is_last_frame) { - state->end_of_stream = 1; - state->need_flush_reads = 1; + stream_parsing->received_close = 1; } if (cur == end) { @@ -105,51 +105,52 @@ grpc_chttp2_parse_error grpc_chttp2_data_parser_parse( } /* fallthrough */ case GRPC_CHTTP2_DATA_FH_1: - p->frame_size = ((gpr_uint32) * cur) << 24; + p->frame_size = ((gpr_uint32)*cur) << 24; if (++cur == end) { p->state = GRPC_CHTTP2_DATA_FH_2; return GRPC_CHTTP2_PARSE_OK; } /* fallthrough */ case GRPC_CHTTP2_DATA_FH_2: - p->frame_size |= ((gpr_uint32) * cur) << 16; + p->frame_size |= ((gpr_uint32)*cur) << 16; if (++cur == end) { p->state = GRPC_CHTTP2_DATA_FH_3; return GRPC_CHTTP2_PARSE_OK; } /* fallthrough */ case GRPC_CHTTP2_DATA_FH_3: - p->frame_size |= ((gpr_uint32) * cur) << 8; + p->frame_size |= ((gpr_uint32)*cur) << 8; if (++cur == end) { p->state = GRPC_CHTTP2_DATA_FH_4; return GRPC_CHTTP2_PARSE_OK; } /* fallthrough */ case GRPC_CHTTP2_DATA_FH_4: - p->frame_size |= ((gpr_uint32) * cur); + p->frame_size |= ((gpr_uint32)*cur); p->state = GRPC_CHTTP2_DATA_FRAME; ++cur; - state->need_flush_reads = 1; grpc_sopb_add_begin_message(&p->incoming_sopb, p->frame_size, 0); /* fallthrough */ case GRPC_CHTTP2_DATA_FRAME: if (cur == end) { + grpc_chttp2_list_add_parsing_seen_stream(transport_parsing, + stream_parsing); return GRPC_CHTTP2_PARSE_OK; - } else if ((gpr_uint32)(end - cur) == p->frame_size) { - state->need_flush_reads = 1; + } + grpc_chttp2_list_add_parsing_seen_stream(transport_parsing, + stream_parsing); + if ((gpr_uint32)(end - cur) == p->frame_size) { grpc_sopb_add_slice(&p->incoming_sopb, gpr_slice_sub(slice, cur - beg, end - beg)); p->state = GRPC_CHTTP2_DATA_FH_0; return GRPC_CHTTP2_PARSE_OK; } else if ((gpr_uint32)(end - cur) > p->frame_size) { - state->need_flush_reads = 1; grpc_sopb_add_slice( &p->incoming_sopb, gpr_slice_sub(slice, cur - beg, cur + p->frame_size - beg)); cur += p->frame_size; goto fh_0; /* loop */ } else { - state->need_flush_reads = 1; grpc_sopb_add_slice(&p->incoming_sopb, gpr_slice_sub(slice, cur - beg, end - beg)); p->frame_size -= (end - cur); diff --git a/src/core/transport/chttp2/frame_data.h b/src/core/transport/chttp2/frame_data.h index 24e557accd..8d6cfcb841 100644 --- a/src/core/transport/chttp2/frame_data.h +++ b/src/core/transport/chttp2/frame_data.h @@ -72,9 +72,10 @@ grpc_chttp2_parse_error grpc_chttp2_data_parser_begin_frame( /* handle a slice of a data frame - is_last indicates the last slice of a frame */ grpc_chttp2_parse_error grpc_chttp2_data_parser_parse( - void *parser, grpc_chttp2_parse_state *state, gpr_slice slice, int is_last); + void *parser, grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last); /* create a slice with an empty data frame and is_last set */ gpr_slice grpc_chttp2_data_frame_create_empty_close(gpr_uint32 id); -#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_FRAME_DATA_H */ +#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_FRAME_DATA_H */ diff --git a/src/core/transport/chttp2/frame_goaway.c b/src/core/transport/chttp2/frame_goaway.c index 95b75d4fde..1ccbba840c 100644 --- a/src/core/transport/chttp2/frame_goaway.c +++ b/src/core/transport/chttp2/frame_goaway.c @@ -32,6 +32,7 @@ */ #include "src/core/transport/chttp2/frame_goaway.h" +#include "src/core/transport/chttp2/internal.h" #include <string.h> @@ -62,8 +63,8 @@ grpc_chttp2_parse_error grpc_chttp2_goaway_parser_begin_frame( } grpc_chttp2_parse_error grpc_chttp2_goaway_parser_parse( - void *parser, grpc_chttp2_parse_state *state, gpr_slice slice, - int is_last) { + void *parser, grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) { gpr_uint8 *const beg = GPR_SLICE_START_PTR(slice); gpr_uint8 *const end = GPR_SLICE_END_PTR(slice); gpr_uint8 *cur = beg; @@ -75,7 +76,7 @@ grpc_chttp2_parse_error grpc_chttp2_goaway_parser_parse( p->state = GRPC_CHTTP2_GOAWAY_LSI0; return GRPC_CHTTP2_PARSE_OK; } - p->last_stream_id = ((gpr_uint32) * cur) << 24; + p->last_stream_id = ((gpr_uint32)*cur) << 24; ++cur; /* fallthrough */ case GRPC_CHTTP2_GOAWAY_LSI1: @@ -83,7 +84,7 @@ grpc_chttp2_parse_error grpc_chttp2_goaway_parser_parse( p->state = GRPC_CHTTP2_GOAWAY_LSI1; return GRPC_CHTTP2_PARSE_OK; } - p->last_stream_id |= ((gpr_uint32) * cur) << 16; + p->last_stream_id |= ((gpr_uint32)*cur) << 16; ++cur; /* fallthrough */ case GRPC_CHTTP2_GOAWAY_LSI2: @@ -91,7 +92,7 @@ grpc_chttp2_parse_error grpc_chttp2_goaway_parser_parse( p->state = GRPC_CHTTP2_GOAWAY_LSI2; return GRPC_CHTTP2_PARSE_OK; } - p->last_stream_id |= ((gpr_uint32) * cur) << 8; + p->last_stream_id |= ((gpr_uint32)*cur) << 8; ++cur; /* fallthrough */ case GRPC_CHTTP2_GOAWAY_LSI3: @@ -99,7 +100,7 @@ grpc_chttp2_parse_error grpc_chttp2_goaway_parser_parse( p->state = GRPC_CHTTP2_GOAWAY_LSI3; return GRPC_CHTTP2_PARSE_OK; } - p->last_stream_id |= ((gpr_uint32) * cur); + p->last_stream_id |= ((gpr_uint32)*cur); ++cur; /* fallthrough */ case GRPC_CHTTP2_GOAWAY_ERR0: @@ -107,7 +108,7 @@ grpc_chttp2_parse_error grpc_chttp2_goaway_parser_parse( p->state = GRPC_CHTTP2_GOAWAY_ERR0; return GRPC_CHTTP2_PARSE_OK; } - p->error_code = ((gpr_uint32) * cur) << 24; + p->error_code = ((gpr_uint32)*cur) << 24; ++cur; /* fallthrough */ case GRPC_CHTTP2_GOAWAY_ERR1: @@ -115,7 +116,7 @@ grpc_chttp2_parse_error grpc_chttp2_goaway_parser_parse( p->state = GRPC_CHTTP2_GOAWAY_ERR1; return GRPC_CHTTP2_PARSE_OK; } - p->error_code |= ((gpr_uint32) * cur) << 16; + p->error_code |= ((gpr_uint32)*cur) << 16; ++cur; /* fallthrough */ case GRPC_CHTTP2_GOAWAY_ERR2: @@ -123,7 +124,7 @@ grpc_chttp2_parse_error grpc_chttp2_goaway_parser_parse( p->state = GRPC_CHTTP2_GOAWAY_ERR2; return GRPC_CHTTP2_PARSE_OK; } - p->error_code |= ((gpr_uint32) * cur) << 8; + p->error_code |= ((gpr_uint32)*cur) << 8; ++cur; /* fallthrough */ case GRPC_CHTTP2_GOAWAY_ERR3: @@ -131,7 +132,7 @@ grpc_chttp2_parse_error grpc_chttp2_goaway_parser_parse( p->state = GRPC_CHTTP2_GOAWAY_ERR3; return GRPC_CHTTP2_PARSE_OK; } - p->error_code |= ((gpr_uint32) * cur); + p->error_code |= ((gpr_uint32)*cur); ++cur; /* fallthrough */ case GRPC_CHTTP2_GOAWAY_DEBUG: @@ -139,10 +140,11 @@ grpc_chttp2_parse_error grpc_chttp2_goaway_parser_parse( p->debug_pos += end - cur; p->state = GRPC_CHTTP2_GOAWAY_DEBUG; if (is_last) { - state->goaway = 1; - state->goaway_last_stream_index = p->last_stream_id; - state->goaway_error = p->error_code; - state->goaway_text = + transport_parsing->goaway_received = 1; + transport_parsing->goaway_last_stream_index = p->last_stream_id; + gpr_slice_unref(transport_parsing->goaway_text); + transport_parsing->goaway_error = p->error_code; + transport_parsing->goaway_text = gpr_slice_new(p->debug_data, p->debug_length, gpr_free); p->debug_data = NULL; } diff --git a/src/core/transport/chttp2/frame_goaway.h b/src/core/transport/chttp2/frame_goaway.h index 7638891514..9c5edfc821 100644 --- a/src/core/transport/chttp2/frame_goaway.h +++ b/src/core/transport/chttp2/frame_goaway.h @@ -65,10 +65,11 @@ void grpc_chttp2_goaway_parser_destroy(grpc_chttp2_goaway_parser *p); grpc_chttp2_parse_error grpc_chttp2_goaway_parser_begin_frame( grpc_chttp2_goaway_parser *parser, gpr_uint32 length, gpr_uint8 flags); grpc_chttp2_parse_error grpc_chttp2_goaway_parser_parse( - void *parser, grpc_chttp2_parse_state *state, gpr_slice slice, int is_last); + void *parser, grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last); void grpc_chttp2_goaway_append(gpr_uint32 last_stream_id, gpr_uint32 error_code, gpr_slice debug_data, gpr_slice_buffer *slice_buffer); -#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_FRAME_GOAWAY_H */ +#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_FRAME_GOAWAY_H */ diff --git a/src/core/transport/chttp2/frame_ping.c b/src/core/transport/chttp2/frame_ping.c index 26004b3b7c..05451c7a8a 100644 --- a/src/core/transport/chttp2/frame_ping.c +++ b/src/core/transport/chttp2/frame_ping.c @@ -32,9 +32,11 @@ */ #include "src/core/transport/chttp2/frame_ping.h" +#include "src/core/transport/chttp2/internal.h" #include <string.h> +#include <grpc/support/alloc.h> #include <grpc/support/log.h> gpr_slice grpc_chttp2_ping_create(gpr_uint8 ack, gpr_uint8 *opaque_8bytes) { @@ -67,12 +69,13 @@ grpc_chttp2_parse_error grpc_chttp2_ping_parser_begin_frame( } grpc_chttp2_parse_error grpc_chttp2_ping_parser_parse( - void *parser, grpc_chttp2_parse_state *state, gpr_slice slice, - int is_last) { + void *parser, grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) { gpr_uint8 *const beg = GPR_SLICE_START_PTR(slice); gpr_uint8 *const end = GPR_SLICE_END_PTR(slice); gpr_uint8 *cur = beg; grpc_chttp2_ping_parser *p = parser; + grpc_chttp2_outstanding_ping *ping; while (p->byte != 8 && cur != end) { p->opaque_8bytes[p->byte] = *cur; @@ -83,9 +86,18 @@ grpc_chttp2_parse_error grpc_chttp2_ping_parser_parse( if (p->byte == 8) { GPR_ASSERT(is_last); if (p->is_ack) { - state->process_ping_reply = 1; + for (ping = transport_parsing->pings.next; + ping != &transport_parsing->pings; ping = ping->next) { + if (0 == memcmp(p->opaque_8bytes, ping->id, 8)) { + grpc_iomgr_add_delayed_callback(ping->on_recv, 1); + } + ping->next->prev = ping->prev; + ping->prev->next = ping->next; + gpr_free(ping); + } } else { - state->send_ping_ack = 1; + gpr_slice_buffer_add(&transport_parsing->qbuf, + grpc_chttp2_ping_create(1, p->opaque_8bytes)); } } diff --git a/src/core/transport/chttp2/frame_ping.h b/src/core/transport/chttp2/frame_ping.h index 11d38b80ea..99197e8352 100644 --- a/src/core/transport/chttp2/frame_ping.h +++ b/src/core/transport/chttp2/frame_ping.h @@ -48,6 +48,7 @@ gpr_slice grpc_chttp2_ping_create(gpr_uint8 ack, gpr_uint8 *opaque_8bytes); grpc_chttp2_parse_error grpc_chttp2_ping_parser_begin_frame( grpc_chttp2_ping_parser *parser, gpr_uint32 length, gpr_uint8 flags); grpc_chttp2_parse_error grpc_chttp2_ping_parser_parse( - void *parser, grpc_chttp2_parse_state *state, gpr_slice slice, int is_last); + void *parser, grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last); -#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_FRAME_PING_H */ +#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_FRAME_PING_H */ diff --git a/src/core/transport/chttp2/frame_rst_stream.c b/src/core/transport/chttp2/frame_rst_stream.c index 3016aac7a2..a878d936c1 100644 --- a/src/core/transport/chttp2/frame_rst_stream.c +++ b/src/core/transport/chttp2/frame_rst_stream.c @@ -32,6 +32,7 @@ */ #include "src/core/transport/chttp2/frame_rst_stream.h" +#include "src/core/transport/chttp2/internal.h" #include <grpc/support/log.h> @@ -61,7 +62,8 @@ gpr_slice grpc_chttp2_rst_stream_create(gpr_uint32 id, gpr_uint32 code) { grpc_chttp2_parse_error grpc_chttp2_rst_stream_parser_begin_frame( grpc_chttp2_rst_stream_parser *parser, gpr_uint32 length, gpr_uint8 flags) { if (length != 4) { - gpr_log(GPR_ERROR, "invalid rst_stream: length=%d, flags=%02x", length, flags); + gpr_log(GPR_ERROR, "invalid rst_stream: length=%d, flags=%02x", length, + flags); return GRPC_CHTTP2_CONNECTION_ERROR; } parser->byte = 0; @@ -69,8 +71,8 @@ grpc_chttp2_parse_error grpc_chttp2_rst_stream_parser_begin_frame( } grpc_chttp2_parse_error grpc_chttp2_rst_stream_parser_parse( - void *parser, grpc_chttp2_parse_state *state, gpr_slice slice, - int is_last) { + void *parser, grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) { gpr_uint8 *const beg = GPR_SLICE_START_PTR(slice); gpr_uint8 *const end = GPR_SLICE_END_PTR(slice); gpr_uint8 *cur = beg; @@ -84,12 +86,13 @@ grpc_chttp2_parse_error grpc_chttp2_rst_stream_parser_parse( if (p->byte == 4) { GPR_ASSERT(is_last); - state->rst_stream = 1; - state->rst_stream_reason = - (((gpr_uint32)p->reason_bytes[0]) << 24) | - (((gpr_uint32)p->reason_bytes[1]) << 16) | - (((gpr_uint32)p->reason_bytes[2]) << 8) | - (((gpr_uint32)p->reason_bytes[3])); + stream_parsing->received_close = 1; + stream_parsing->saw_rst_stream = 1; + stream_parsing->rst_stream_reason = + (((gpr_uint32)p->reason_bytes[0]) << 24) | + (((gpr_uint32)p->reason_bytes[1]) << 16) | + (((gpr_uint32)p->reason_bytes[2]) << 8) | + (((gpr_uint32)p->reason_bytes[3])); } return GRPC_CHTTP2_PARSE_OK; diff --git a/src/core/transport/chttp2/frame_rst_stream.h b/src/core/transport/chttp2/frame_rst_stream.h index 07a3c98d03..ed69e588af 100644 --- a/src/core/transport/chttp2/frame_rst_stream.h +++ b/src/core/transport/chttp2/frame_rst_stream.h @@ -47,6 +47,7 @@ gpr_slice grpc_chttp2_rst_stream_create(gpr_uint32 stream_id, gpr_uint32 code); grpc_chttp2_parse_error grpc_chttp2_rst_stream_parser_begin_frame( grpc_chttp2_rst_stream_parser *parser, gpr_uint32 length, gpr_uint8 flags); grpc_chttp2_parse_error grpc_chttp2_rst_stream_parser_parse( - void *parser, grpc_chttp2_parse_state *state, gpr_slice slice, int is_last); + void *parser, grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last); -#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_FRAME_RST_STREAM_H */ +#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_FRAME_RST_STREAM_H */ diff --git a/src/core/transport/chttp2/frame_settings.c b/src/core/transport/chttp2/frame_settings.c index 2ffce730d5..d42bc000ae 100644 --- a/src/core/transport/chttp2/frame_settings.c +++ b/src/core/transport/chttp2/frame_settings.c @@ -32,6 +32,7 @@ */ #include "src/core/transport/chttp2/frame_settings.h" +#include "src/core/transport/chttp2/internal.h" #include <string.h> @@ -137,7 +138,8 @@ grpc_chttp2_parse_error grpc_chttp2_settings_parser_begin_frame( } grpc_chttp2_parse_error grpc_chttp2_settings_parser_parse( - void *p, grpc_chttp2_parse_state *state, gpr_slice slice, int is_last) { + void *p, grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) { grpc_chttp2_settings_parser *parser = p; const gpr_uint8 *cur = GPR_SLICE_START_PTR(slice); const gpr_uint8 *end = GPR_SLICE_END_PTR(slice); @@ -152,9 +154,11 @@ grpc_chttp2_parse_error grpc_chttp2_settings_parser_parse( if (cur == end) { parser->state = GRPC_CHTTP2_SPS_ID0; if (is_last) { + transport_parsing->settings_updated = 1; memcpy(parser->target_settings, parser->incoming_settings, GRPC_CHTTP2_NUM_SETTINGS * sizeof(gpr_uint32)); - state->ack_settings = 1; + gpr_slice_buffer_add(&transport_parsing->qbuf, + grpc_chttp2_settings_ack_create()); } return GRPC_CHTTP2_PARSE_OK; } @@ -220,15 +224,16 @@ grpc_chttp2_parse_error grpc_chttp2_settings_parser_parse( } if (parser->id == GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE && parser->incoming_settings[parser->id] != parser->value) { - state->initial_window_update = + transport_parsing->initial_window_update = (gpr_int64)parser->value - parser->incoming_settings[parser->id]; gpr_log(GPR_DEBUG, "adding %d for initial_window change", - (int)state->initial_window_update); + (int)transport_parsing->initial_window_update); } parser->incoming_settings[parser->id] = parser->value; if (grpc_http_trace) { - gpr_log(GPR_DEBUG, "CHTTP2: got setting %d = %d", parser->id, + gpr_log(GPR_DEBUG, "CHTTP2:%s: got setting %d = %d", + transport_parsing->is_client ? "CLI" : "SVR", parser->id, parser->value); } } else { diff --git a/src/core/transport/chttp2/frame_settings.h b/src/core/transport/chttp2/frame_settings.h index 18765631a6..0ac68a9fa8 100644 --- a/src/core/transport/chttp2/frame_settings.h +++ b/src/core/transport/chttp2/frame_settings.h @@ -94,6 +94,7 @@ grpc_chttp2_parse_error grpc_chttp2_settings_parser_begin_frame( grpc_chttp2_settings_parser *parser, gpr_uint32 length, gpr_uint8 flags, gpr_uint32 *settings); grpc_chttp2_parse_error grpc_chttp2_settings_parser_parse( - void *parser, grpc_chttp2_parse_state *state, gpr_slice slice, int is_last); + void *parser, grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last); -#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_FRAME_SETTINGS_H */ +#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_FRAME_SETTINGS_H */ diff --git a/src/core/transport/chttp2/frame_window_update.c b/src/core/transport/chttp2/frame_window_update.c index a8db7d6653..b817df7745 100644 --- a/src/core/transport/chttp2/frame_window_update.c +++ b/src/core/transport/chttp2/frame_window_update.c @@ -32,6 +32,7 @@ */ #include "src/core/transport/chttp2/frame_window_update.h" +#include "src/core/transport/chttp2/internal.h" #include <grpc/support/log.h> @@ -73,15 +74,15 @@ grpc_chttp2_parse_error grpc_chttp2_window_update_parser_begin_frame( } grpc_chttp2_parse_error grpc_chttp2_window_update_parser_parse( - void *parser, grpc_chttp2_parse_state *state, gpr_slice slice, - int is_last) { + void *parser, grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) { gpr_uint8 *const beg = GPR_SLICE_START_PTR(slice); gpr_uint8 *const end = GPR_SLICE_END_PTR(slice); gpr_uint8 *cur = beg; grpc_chttp2_window_update_parser *p = parser; while (p->byte != 4 && cur != end) { - p->amount |= ((gpr_uint32) * cur) << (8 * (3 - p->byte)); + p->amount |= ((gpr_uint32)*cur) << (8 * (3 - p->byte)); cur++; p->byte++; } @@ -92,7 +93,21 @@ grpc_chttp2_parse_error grpc_chttp2_window_update_parser_parse( return GRPC_CHTTP2_CONNECTION_ERROR; } GPR_ASSERT(is_last); - state->window_update = p->amount; + + if (transport_parsing->incoming_stream_id) { + if (stream_parsing) { + GRPC_CHTTP2_FLOWCTL_TRACE_STREAM("update", transport_parsing, + stream_parsing, outgoing_window_update, + p->amount); + stream_parsing->outgoing_window_update += p->amount; + grpc_chttp2_list_add_parsing_seen_stream(transport_parsing, + stream_parsing); + } + } else { + GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT("update", transport_parsing, + outgoing_window_update, p->amount); + transport_parsing->outgoing_window_update += p->amount; + } } return GRPC_CHTTP2_PARSE_OK; diff --git a/src/core/transport/chttp2/frame_window_update.h b/src/core/transport/chttp2/frame_window_update.h index 85475a8f9e..deba801d00 100644 --- a/src/core/transport/chttp2/frame_window_update.h +++ b/src/core/transport/chttp2/frame_window_update.h @@ -50,6 +50,7 @@ grpc_chttp2_parse_error grpc_chttp2_window_update_parser_begin_frame( grpc_chttp2_window_update_parser *parser, gpr_uint32 length, gpr_uint8 flags); grpc_chttp2_parse_error grpc_chttp2_window_update_parser_parse( - void *parser, grpc_chttp2_parse_state *state, gpr_slice slice, int is_last); + void *parser, grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last); -#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_FRAME_WINDOW_UPDATE_H */ +#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_FRAME_WINDOW_UPDATE_H */ diff --git a/src/core/transport/chttp2/gen_hpack_tables.c b/src/core/transport/chttp2/gen_hpack_tables.c deleted file mode 100644 index bdaa3cf094..0000000000 --- a/src/core/transport/chttp2/gen_hpack_tables.c +++ /dev/null @@ -1,362 +0,0 @@ -/* - * - * 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. - * - */ - -/* generates constant tables for hpack.c */ - -#include <stddef.h> -#include <stdio.h> -#include <string.h> -#include <assert.h> - -#include <grpc/support/log.h> -#include "src/core/transport/chttp2/huffsyms.h" - -/* - * first byte LUT generation - */ - -typedef struct { - const char *call; - /* bit prefix for the field type */ - unsigned char prefix; - /* length of the bit prefix for the field type */ - unsigned char prefix_length; - /* index value: 0 = all zeros, 2 = all ones, 1 otherwise */ - unsigned char index; -} spec; - -static const spec fields[] = {{"INDEXED_FIELD", 0X80, 1, 1}, - {"INDEXED_FIELD_X", 0X80, 1, 2}, - {"LITHDR_INCIDX", 0X40, 2, 1}, - {"LITHDR_INCIDX_X", 0X40, 2, 2}, - {"LITHDR_INCIDX_V", 0X40, 2, 0}, - {"LITHDR_NOTIDX", 0X00, 4, 1}, - {"LITHDR_NOTIDX_X", 0X00, 4, 2}, - {"LITHDR_NOTIDX_V", 0X00, 4, 0}, - {"LITHDR_NVRIDX", 0X10, 4, 1}, - {"LITHDR_NVRIDX_X", 0X10, 4, 2}, - {"LITHDR_NVRIDX_V", 0X10, 4, 0}, - {"MAX_TBL_SIZE", 0X20, 3, 1}, - {"MAX_TBL_SIZE_X", 0X20, 3, 2}, }; - -static const int num_fields = sizeof(fields) / sizeof(*fields); - -static unsigned char prefix_mask(unsigned char prefix_len) { - unsigned char i; - unsigned char out = 0; - for (i = 0; i < prefix_len; i++) { - out |= 1 << (7 - i); - } - return out; -} - -static unsigned char suffix_mask(unsigned char prefix_len) { - return ~prefix_mask(prefix_len); -} - -static void generate_first_byte_lut(void) { - int i, j, n; - const spec *chrspec; - unsigned char suffix; - - n = printf("static CALLTYPE first_byte[256] = {"); - /* for each potential first byte of a header */ - for (i = 0; i < 256; i++) { - /* find the field type that matches it */ - chrspec = NULL; - for (j = 0; j < num_fields; j++) { - if ((prefix_mask(fields[j].prefix_length) & i) == fields[j].prefix) { - suffix = suffix_mask(fields[j].prefix_length) & i; - if (suffix == suffix_mask(fields[j].prefix_length)) { - if (fields[j].index != 2) continue; - } else if (suffix == 0) { - if (fields[j].index != 0) continue; - } else { - if (fields[j].index != 1) continue; - } - GPR_ASSERT(chrspec == NULL); - chrspec = &fields[j]; - } - } - if (chrspec) { - n += printf("%s, ", chrspec->call); - } else { - n += printf("ILLEGAL, "); - } - /* make some small effort towards readable output */ - if (n > 70) { - printf("\n "); - n = 2; - } - } - printf("};\n"); -} - -/* - * Huffman decoder table generation - */ - -#define MAXHUFFSTATES 1024 - -/* represents a set of symbols as an array of booleans indicating inclusion */ -typedef struct { - char included[GRPC_CHTTP2_NUM_HUFFSYMS]; -} symset; -/* represents a lookup table indexed by a nibble */ -typedef struct { - int values[16]; -} nibblelut; - -/* returns a symset that includes all possible symbols */ -static symset symset_all(void) { - symset x; - memset(x.included, 1, sizeof(x.included)); - return x; -} - -/* returns a symset that includes no symbols */ -static symset symset_none(void) { - symset x; - memset(x.included, 0, sizeof(x.included)); - return x; -} - -/* returns an empty nibblelut */ -static nibblelut nibblelut_empty(void) { - nibblelut x; - int i; - for (i = 0; i < 16; i++) { - x.values[i] = -1; - } - return x; -} - -/* counts symbols in a symset - only used for debug builds */ -#ifndef NDEBUG -static int nsyms(symset s) { - int i; - int c = 0; - for (i = 0; i < GRPC_CHTTP2_NUM_HUFFSYMS; i++) { - c += s.included[i] != 0; - } - return c; -} -#endif - -/* global table of discovered huffman decoding states */ -static struct { - /* the bit offset that this state starts at */ - int bitofs; - /* the set of symbols that this state started with */ - symset syms; - - /* lookup table for the next state */ - nibblelut next; - /* lookup table for what to emit */ - nibblelut emit; -} huffstates[MAXHUFFSTATES]; -static int nhuffstates = 0; - -/* given a number of decoded bits and a set of symbols that are live, - return the index into the decoder table for this state. - set isnew to 1 if this state was previously undiscovered */ -static int state_index(int bitofs, symset syms, int *isnew) { - int i; - for (i = 0; i < nhuffstates; i++) { - if (huffstates[i].bitofs != bitofs) continue; - if (0 != memcmp(huffstates[i].syms.included, syms.included, - GRPC_CHTTP2_NUM_HUFFSYMS)) - continue; - *isnew = 0; - return i; - } - GPR_ASSERT(nhuffstates != MAXHUFFSTATES); - i = nhuffstates++; - huffstates[i].bitofs = bitofs; - huffstates[i].syms = syms; - huffstates[i].next = nibblelut_empty(); - huffstates[i].emit = nibblelut_empty(); - *isnew = 1; - return i; -} - -/* recursively build a decoding table - - state - the huffman state that we are trying to fill in - nibble - the current nibble - nibbits - the number of bits in the nibble that have been filled in - bitofs - the number of bits of symbol that have been decoded - emit - the symbol to emit on this nibble (or -1 if no symbol has been - found) - syms - the set of symbols that could be matched */ -static void build_dec_tbl(int state, int nibble, int nibbits, unsigned bitofs, - int emit, symset syms) { - int i; - unsigned bit; - - /* If we have four bits in the nibble we're looking at, then we can fill in - a slot in the lookup tables. */ - if (nibbits == 4) { - int isnew; - /* Find the state that we are in: this may be a new state, in which case - we recurse to fill it in, or we may have already seen this state, in - which case the recursion terminates */ - int st = state_index(bitofs, syms, &isnew); - GPR_ASSERT(huffstates[state].next.values[nibble] == -1); - huffstates[state].next.values[nibble] = st; - huffstates[state].emit.values[nibble] = emit; - if (isnew) { - build_dec_tbl(st, 0, 0, bitofs, -1, syms); - } - return; - } - - assert(nsyms(syms)); - - /* A bit can be 0 or 1 */ - for (bit = 0; bit < 2; bit++) { - /* walk over active symbols and see if they have this bit set */ - symset nextsyms = symset_none(); - for (i = 0; i < GRPC_CHTTP2_NUM_HUFFSYMS; i++) { - if (!syms.included[i]) continue; /* disregard inactive symbols */ - if (((grpc_chttp2_huffsyms[i].bits >> - (grpc_chttp2_huffsyms[i].length - bitofs - 1)) & - 1) == bit) { - /* the bit is set, include it in the next recursive set */ - if (grpc_chttp2_huffsyms[i].length == bitofs + 1) { - /* additionally, we've gotten to the end of a symbol - this is a - special recursion step: re-activate all the symbols, reset - bitofs to zero, and recurse */ - build_dec_tbl(state, (nibble << 1) | bit, nibbits + 1, 0, i, - symset_all()); - /* skip the remainder of this loop */ - goto next; - } - nextsyms.included[i] = 1; - } - } - /* recurse down for this bit */ - build_dec_tbl(state, (nibble << 1) | bit, nibbits + 1, bitofs + 1, emit, - nextsyms); - next: - ; - } -} - -static nibblelut ctbl[MAXHUFFSTATES]; -static int nctbl; - -static int ctbl_idx(nibblelut x) { - int i; - for (i = 0; i < nctbl; i++) { - if (0 == memcmp(&x, ctbl + i, sizeof(nibblelut))) return i; - } - ctbl[i] = x; - nctbl++; - return i; -} - -static void dump_ctbl(const char *name) { - int i, j; - printf("static const gpr_int16 %s[%d*16] = {\n", name, nctbl); - for (i = 0; i < nctbl; i++) { - for (j = 0; j < 16; j++) { - printf("%d,", ctbl[i].values[j]); - } - printf("\n"); - } - printf("};\n"); -} - -static void generate_huff_tables(void) { - int i; - build_dec_tbl(state_index(0, symset_all(), &i), 0, 0, 0, -1, symset_all()); - - nctbl = 0; - printf("static const gpr_uint8 next_tbl[%d] = {", nhuffstates); - for (i = 0; i < nhuffstates; i++) { - printf("%d,", ctbl_idx(huffstates[i].next)); - } - printf("};\n"); - dump_ctbl("next_sub_tbl"); - - nctbl = 0; - printf("static const gpr_uint16 emit_tbl[%d] = {", nhuffstates); - for (i = 0; i < nhuffstates; i++) { - printf("%d,", ctbl_idx(huffstates[i].emit)); - } - printf("};\n"); - dump_ctbl("emit_sub_tbl"); -} - -static void generate_base64_huff_encoder_table(void) { - static const char alphabet[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - int i; - - printf( - "static const struct { gpr_uint16 bits, gpr_uint8 length } " - "base64_syms[64] = {\n"); - for (i = 0; i < 64; i++) { - printf("{0x%x, %d},", grpc_chttp2_huffsyms[(unsigned char)alphabet[i]].bits, - grpc_chttp2_huffsyms[(unsigned char)alphabet[i]].length); - } - printf("};\n"); -} - -static void generate_base64_inverse_table(void) { - static const char alphabet[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; - unsigned char inverse[256]; - unsigned i; - - memset(inverse, 255, sizeof(inverse)); - for (i = 0; i < strlen(alphabet); i++) { - inverse[(unsigned char)alphabet[i]] = i; - } - - printf("static const gpr_uint8 inverse_base64[256] = {"); - for (i = 0; i < 256; i++) { - printf("%d,", inverse[i]); - } - printf("};\n"); -} - -int main(void) { - generate_huff_tables(); - generate_first_byte_lut(); - generate_base64_huff_encoder_table(); - generate_base64_inverse_table(); - - return 0; -} diff --git a/src/core/transport/chttp2/hpack_parser.c b/src/core/transport/chttp2/hpack_parser.c index a489543868..b8ab664db5 100644 --- a/src/core/transport/chttp2/hpack_parser.c +++ b/src/core/transport/chttp2/hpack_parser.c @@ -32,6 +32,7 @@ */ #include "src/core/transport/chttp2/hpack_parser.h" +#include "src/core/transport/chttp2/internal.h" #include <stddef.h> #include <string.h> @@ -149,10 +150,12 @@ typedef enum { /* jump table of parse state functions -- order must match first_byte_type above */ static const grpc_chttp2_hpack_parser_state first_byte_action[] = { - parse_indexed_field, parse_indexed_field_x, parse_lithdr_incidx, - parse_lithdr_incidx_x, parse_lithdr_incidx_v, parse_lithdr_notidx, - parse_lithdr_notidx_x, parse_lithdr_notidx_v, parse_lithdr_nvridx, - parse_lithdr_nvridx_x, parse_lithdr_nvridx_v, parse_max_tbl_size, + parse_indexed_field, parse_indexed_field_x, + parse_lithdr_incidx, parse_lithdr_incidx_x, + parse_lithdr_incidx_v, parse_lithdr_notidx, + parse_lithdr_notidx_x, parse_lithdr_notidx_v, + parse_lithdr_nvridx, parse_lithdr_nvridx_x, + parse_lithdr_nvridx_v, parse_max_tbl_size, parse_max_tbl_size_x, parse_error}; /* indexes the first byte to a parse state function - generated by @@ -221,7 +224,8 @@ static const gpr_uint8 first_byte_lut[256] = { INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, - INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD_X, }; + INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD_X, +}; /* state table for huffman decoding: given a state, gives an index/16 into next_sub_tbl. Taking that index and adding the value of the nibble being @@ -241,7 +245,8 @@ static const gpr_uint8 next_tbl[256] = { 38, 1, 1, 1, 1, 1, 1, 1, 15, 2, 2, 2, 2, 26, 3, 3, 39, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 7, 3, 3, 3, 40, 2, 41, 1, 1, 1, 42, 43, 1, 1, 44, 1, 1, 1, 1, 15, 2, 2, 2, 2, 2, 2, - 3, 3, 3, 45, 46, 1, 1, 2, 2, 2, 35, 3, 3, 18, 47, 2, }; + 3, 3, 3, 45, 46, 1, 1, 2, 2, 2, 35, 3, 3, 18, 47, 2, +}; /* next state, based upon current state and the current nibble: see above. generated by gen_hpack_tables.c */ static const gpr_int16 next_sub_tbl[48 * 16] = { @@ -296,7 +301,8 @@ static const gpr_int16 next_sub_tbl[48 * 16] = { 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 255, }; + 0, 0, 255, +}; /* emission table: indexed like next_tbl, ultimately gives the byte to be emitted, or -1 for no byte, or 256 for end of stream @@ -319,7 +325,8 @@ static const gpr_uint16 emit_tbl[256] = { 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 0, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, - 248, }; + 248, +}; /* generated by gen_hpack_tables.c */ static const gpr_int16 emit_sub_tbl[249 * 16] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, @@ -587,7 +594,8 @@ static const gpr_int16 emit_sub_tbl[249 * 16] = { 251, 251, 252, 252, 253, 253, 254, 254, 2, 3, 4, 5, 6, 7, 8, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 127, 220, 249, -1, 10, 10, 10, 10, 13, 13, 13, - 13, 22, 22, 22, 22, 256, 256, 256, 256, }; + 13, 22, 22, 22, 22, 256, 256, 256, 256, +}; static const gpr_uint8 inverse_base64[256] = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, @@ -607,7 +615,8 @@ static const gpr_uint8 inverse_base64[256] = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, }; + 255, +}; /* emission helpers */ static void on_hdr(grpc_chttp2_hpack_parser *p, grpc_mdelem *md, @@ -945,7 +954,7 @@ static int parse_value1(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur, return 1; } - *p->parsing.value += (((gpr_uint32) * cur) & 0x7f) << 7; + *p->parsing.value += (((gpr_uint32)*cur) & 0x7f) << 7; if ((*cur) & 0x80) { return parse_value2(p, cur + 1, end); @@ -963,7 +972,7 @@ static int parse_value2(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur, return 1; } - *p->parsing.value += (((gpr_uint32) * cur) & 0x7f) << 14; + *p->parsing.value += (((gpr_uint32)*cur) & 0x7f) << 14; if ((*cur) & 0x80) { return parse_value3(p, cur + 1, end); @@ -981,7 +990,7 @@ static int parse_value3(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur, return 1; } - *p->parsing.value += (((gpr_uint32) * cur) & 0x7f) << 21; + *p->parsing.value += (((gpr_uint32)*cur) & 0x7f) << 21; if ((*cur) & 0x80) { return parse_value4(p, cur + 1, end); @@ -1369,8 +1378,8 @@ int grpc_chttp2_hpack_parser_parse(grpc_chttp2_hpack_parser *p, } grpc_chttp2_parse_error grpc_chttp2_header_parser_parse( - void *hpack_parser, grpc_chttp2_parse_state *state, gpr_slice slice, - int is_last) { + void *hpack_parser, grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) { grpc_chttp2_hpack_parser *parser = hpack_parser; if (!grpc_chttp2_hpack_parser_parse(parser, GPR_SLICE_START_PTR(slice), GPR_SLICE_END_PTR(slice))) { @@ -1382,9 +1391,16 @@ grpc_chttp2_parse_error grpc_chttp2_header_parser_parse( "end of header frame not aligned with a hpack record boundary"); return GRPC_CHTTP2_CONNECTION_ERROR; } - state->metadata_boundary = parser->is_boundary; - state->end_of_stream = parser->is_eof; - state->need_flush_reads = parser->is_eof; + if (parser->is_boundary) { + grpc_chttp2_incoming_metadata_buffer_place_metadata_batch_into( + &stream_parsing->incoming_metadata, + &stream_parsing->data_parser.incoming_sopb); + grpc_chttp2_list_add_parsing_seen_stream(transport_parsing, + stream_parsing); + } + if (parser->is_eof) { + stream_parsing->received_close = 1; + } parser->on_header = on_header_not_set; parser->on_header_user_data = NULL; parser->is_boundary = 0xde; diff --git a/src/core/transport/chttp2/hpack_parser.h b/src/core/transport/chttp2/hpack_parser.h index bfc06b3980..c1768d9d5d 100644 --- a/src/core/transport/chttp2/hpack_parser.h +++ b/src/core/transport/chttp2/hpack_parser.h @@ -107,7 +107,7 @@ int grpc_chttp2_hpack_parser_parse(grpc_chttp2_hpack_parser *p, /* wraps grpc_chttp2_hpack_parser_parse to provide a frame level parser for the transport */ grpc_chttp2_parse_error grpc_chttp2_header_parser_parse( - void *hpack_parser, grpc_chttp2_parse_state *state, gpr_slice slice, - int is_last); + void *hpack_parser, grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last); -#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_HPACK_PARSER_H */ +#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_HPACK_PARSER_H */ diff --git a/src/core/transport/chttp2/hpack_table.c b/src/core/transport/chttp2/hpack_table.c index 2c0159260f..372e71d68f 100644 --- a/src/core/transport/chttp2/hpack_table.c +++ b/src/core/transport/chttp2/hpack_table.c @@ -43,68 +43,69 @@ static struct { const char *key; const char *value; } static_table[] = { - /* 0: */ {NULL, NULL}, - /* 1: */ {":authority", ""}, - /* 2: */ {":method", "GET"}, - /* 3: */ {":method", "POST"}, - /* 4: */ {":path", "/"}, - /* 5: */ {":path", "/index.html"}, - /* 6: */ {":scheme", "http"}, - /* 7: */ {":scheme", "https"}, - /* 8: */ {":status", "200"}, - /* 9: */ {":status", "204"}, - /* 10: */ {":status", "206"}, - /* 11: */ {":status", "304"}, - /* 12: */ {":status", "400"}, - /* 13: */ {":status", "404"}, - /* 14: */ {":status", "500"}, - /* 15: */ {"accept-charset", ""}, - /* 16: */ {"accept-encoding", "gzip, deflate"}, - /* 17: */ {"accept-language", ""}, - /* 18: */ {"accept-ranges", ""}, - /* 19: */ {"accept", ""}, - /* 20: */ {"access-control-allow-origin", ""}, - /* 21: */ {"age", ""}, - /* 22: */ {"allow", ""}, - /* 23: */ {"authorization", ""}, - /* 24: */ {"cache-control", ""}, - /* 25: */ {"content-disposition", ""}, - /* 26: */ {"content-encoding", ""}, - /* 27: */ {"content-language", ""}, - /* 28: */ {"content-length", ""}, - /* 29: */ {"content-location", ""}, - /* 30: */ {"content-range", ""}, - /* 31: */ {"content-type", ""}, - /* 32: */ {"cookie", ""}, - /* 33: */ {"date", ""}, - /* 34: */ {"etag", ""}, - /* 35: */ {"expect", ""}, - /* 36: */ {"expires", ""}, - /* 37: */ {"from", ""}, - /* 38: */ {"host", ""}, - /* 39: */ {"if-match", ""}, - /* 40: */ {"if-modified-since", ""}, - /* 41: */ {"if-none-match", ""}, - /* 42: */ {"if-range", ""}, - /* 43: */ {"if-unmodified-since", ""}, - /* 44: */ {"last-modified", ""}, - /* 45: */ {"link", ""}, - /* 46: */ {"location", ""}, - /* 47: */ {"max-forwards", ""}, - /* 48: */ {"proxy-authenticate", ""}, - /* 49: */ {"proxy-authorization", ""}, - /* 50: */ {"range", ""}, - /* 51: */ {"referer", ""}, - /* 52: */ {"refresh", ""}, - /* 53: */ {"retry-after", ""}, - /* 54: */ {"server", ""}, - /* 55: */ {"set-cookie", ""}, - /* 56: */ {"strict-transport-security", ""}, - /* 57: */ {"transfer-encoding", ""}, - /* 58: */ {"user-agent", ""}, - /* 59: */ {"vary", ""}, - /* 60: */ {"via", ""}, - /* 61: */ {"www-authenticate", ""}, }; + /* 0: */ {NULL, NULL}, + /* 1: */ {":authority", ""}, + /* 2: */ {":method", "GET"}, + /* 3: */ {":method", "POST"}, + /* 4: */ {":path", "/"}, + /* 5: */ {":path", "/index.html"}, + /* 6: */ {":scheme", "http"}, + /* 7: */ {":scheme", "https"}, + /* 8: */ {":status", "200"}, + /* 9: */ {":status", "204"}, + /* 10: */ {":status", "206"}, + /* 11: */ {":status", "304"}, + /* 12: */ {":status", "400"}, + /* 13: */ {":status", "404"}, + /* 14: */ {":status", "500"}, + /* 15: */ {"accept-charset", ""}, + /* 16: */ {"accept-encoding", "gzip, deflate"}, + /* 17: */ {"accept-language", ""}, + /* 18: */ {"accept-ranges", ""}, + /* 19: */ {"accept", ""}, + /* 20: */ {"access-control-allow-origin", ""}, + /* 21: */ {"age", ""}, + /* 22: */ {"allow", ""}, + /* 23: */ {"authorization", ""}, + /* 24: */ {"cache-control", ""}, + /* 25: */ {"content-disposition", ""}, + /* 26: */ {"content-encoding", ""}, + /* 27: */ {"content-language", ""}, + /* 28: */ {"content-length", ""}, + /* 29: */ {"content-location", ""}, + /* 30: */ {"content-range", ""}, + /* 31: */ {"content-type", ""}, + /* 32: */ {"cookie", ""}, + /* 33: */ {"date", ""}, + /* 34: */ {"etag", ""}, + /* 35: */ {"expect", ""}, + /* 36: */ {"expires", ""}, + /* 37: */ {"from", ""}, + /* 38: */ {"host", ""}, + /* 39: */ {"if-match", ""}, + /* 40: */ {"if-modified-since", ""}, + /* 41: */ {"if-none-match", ""}, + /* 42: */ {"if-range", ""}, + /* 43: */ {"if-unmodified-since", ""}, + /* 44: */ {"last-modified", ""}, + /* 45: */ {"link", ""}, + /* 46: */ {"location", ""}, + /* 47: */ {"max-forwards", ""}, + /* 48: */ {"proxy-authenticate", ""}, + /* 49: */ {"proxy-authorization", ""}, + /* 50: */ {"range", ""}, + /* 51: */ {"referer", ""}, + /* 52: */ {"refresh", ""}, + /* 53: */ {"retry-after", ""}, + /* 54: */ {"server", ""}, + /* 55: */ {"set-cookie", ""}, + /* 56: */ {"strict-transport-security", ""}, + /* 57: */ {"transfer-encoding", ""}, + /* 58: */ {"user-agent", ""}, + /* 59: */ {"vary", ""}, + /* 60: */ {"via", ""}, + /* 61: */ {"www-authenticate", ""}, +}; void grpc_chttp2_hptbl_init(grpc_chttp2_hptbl *tbl, grpc_mdctx *mdctx) { size_t i; diff --git a/src/core/transport/chttp2/hpack_table.h b/src/core/transport/chttp2/hpack_table.h index d3bf41bbc5..4f882e2e03 100644 --- a/src/core/transport/chttp2/hpack_table.h +++ b/src/core/transport/chttp2/hpack_table.h @@ -94,4 +94,4 @@ typedef struct { grpc_chttp2_hptbl_find_result grpc_chttp2_hptbl_find( const grpc_chttp2_hptbl *tbl, grpc_mdelem *md); -#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_HPACK_TABLE_H */ +#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_HPACK_TABLE_H */ diff --git a/src/core/transport/chttp2/http2_errors.h b/src/core/transport/chttp2/http2_errors.h index 4ab2ec0220..a4f309e056 100644 --- a/src/core/transport/chttp2/http2_errors.h +++ b/src/core/transport/chttp2/http2_errors.h @@ -53,4 +53,4 @@ typedef enum { GRPC_CHTTP2__ERROR_DO_NOT_USE = -1 } grpc_chttp2_error_code; -#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_HTTP2_ERRORS_H */ +#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_HTTP2_ERRORS_H */ diff --git a/src/core/transport/chttp2/huffsyms.c b/src/core/transport/chttp2/huffsyms.c index 0a926e7e35..6f5cf6a2a9 100644 --- a/src/core/transport/chttp2/huffsyms.c +++ b/src/core/transport/chttp2/huffsyms.c @@ -37,260 +37,69 @@ command: :%s/.* \([0-9a-f]\+\) \[ *\([0-9]\+\)\]/{0x\1, \2},/g */ const grpc_chttp2_huffsym grpc_chttp2_huffsyms[GRPC_CHTTP2_NUM_HUFFSYMS] = { - {0x1ff8, 13}, - {0x7fffd8, 23}, - {0xfffffe2, 28}, - {0xfffffe3, 28}, - {0xfffffe4, 28}, - {0xfffffe5, 28}, - {0xfffffe6, 28}, - {0xfffffe7, 28}, - {0xfffffe8, 28}, - {0xffffea, 24}, - {0x3ffffffc, 30}, - {0xfffffe9, 28}, - {0xfffffea, 28}, - {0x3ffffffd, 30}, - {0xfffffeb, 28}, - {0xfffffec, 28}, - {0xfffffed, 28}, - {0xfffffee, 28}, - {0xfffffef, 28}, - {0xffffff0, 28}, - {0xffffff1, 28}, - {0xffffff2, 28}, - {0x3ffffffe, 30}, - {0xffffff3, 28}, - {0xffffff4, 28}, - {0xffffff5, 28}, - {0xffffff6, 28}, - {0xffffff7, 28}, - {0xffffff8, 28}, - {0xffffff9, 28}, - {0xffffffa, 28}, - {0xffffffb, 28}, - {0x14, 6}, - {0x3f8, 10}, - {0x3f9, 10}, - {0xffa, 12}, - {0x1ff9, 13}, - {0x15, 6}, - {0xf8, 8}, - {0x7fa, 11}, - {0x3fa, 10}, - {0x3fb, 10}, - {0xf9, 8}, - {0x7fb, 11}, - {0xfa, 8}, - {0x16, 6}, - {0x17, 6}, - {0x18, 6}, - {0x0, 5}, - {0x1, 5}, - {0x2, 5}, - {0x19, 6}, - {0x1a, 6}, - {0x1b, 6}, - {0x1c, 6}, - {0x1d, 6}, - {0x1e, 6}, - {0x1f, 6}, - {0x5c, 7}, - {0xfb, 8}, - {0x7ffc, 15}, - {0x20, 6}, - {0xffb, 12}, - {0x3fc, 10}, - {0x1ffa, 13}, - {0x21, 6}, - {0x5d, 7}, - {0x5e, 7}, - {0x5f, 7}, - {0x60, 7}, - {0x61, 7}, - {0x62, 7}, - {0x63, 7}, - {0x64, 7}, - {0x65, 7}, - {0x66, 7}, - {0x67, 7}, - {0x68, 7}, - {0x69, 7}, - {0x6a, 7}, - {0x6b, 7}, - {0x6c, 7}, - {0x6d, 7}, - {0x6e, 7}, - {0x6f, 7}, - {0x70, 7}, - {0x71, 7}, - {0x72, 7}, - {0xfc, 8}, - {0x73, 7}, - {0xfd, 8}, - {0x1ffb, 13}, - {0x7fff0, 19}, - {0x1ffc, 13}, - {0x3ffc, 14}, - {0x22, 6}, - {0x7ffd, 15}, - {0x3, 5}, - {0x23, 6}, - {0x4, 5}, - {0x24, 6}, - {0x5, 5}, - {0x25, 6}, - {0x26, 6}, - {0x27, 6}, - {0x6, 5}, - {0x74, 7}, - {0x75, 7}, - {0x28, 6}, - {0x29, 6}, - {0x2a, 6}, - {0x7, 5}, - {0x2b, 6}, - {0x76, 7}, - {0x2c, 6}, - {0x8, 5}, - {0x9, 5}, - {0x2d, 6}, - {0x77, 7}, - {0x78, 7}, - {0x79, 7}, - {0x7a, 7}, - {0x7b, 7}, - {0x7ffe, 15}, - {0x7fc, 11}, - {0x3ffd, 14}, - {0x1ffd, 13}, - {0xffffffc, 28}, - {0xfffe6, 20}, - {0x3fffd2, 22}, - {0xfffe7, 20}, - {0xfffe8, 20}, - {0x3fffd3, 22}, - {0x3fffd4, 22}, - {0x3fffd5, 22}, - {0x7fffd9, 23}, - {0x3fffd6, 22}, - {0x7fffda, 23}, - {0x7fffdb, 23}, - {0x7fffdc, 23}, - {0x7fffdd, 23}, - {0x7fffde, 23}, - {0xffffeb, 24}, - {0x7fffdf, 23}, - {0xffffec, 24}, - {0xffffed, 24}, - {0x3fffd7, 22}, - {0x7fffe0, 23}, - {0xffffee, 24}, - {0x7fffe1, 23}, - {0x7fffe2, 23}, - {0x7fffe3, 23}, - {0x7fffe4, 23}, - {0x1fffdc, 21}, - {0x3fffd8, 22}, - {0x7fffe5, 23}, - {0x3fffd9, 22}, - {0x7fffe6, 23}, - {0x7fffe7, 23}, - {0xffffef, 24}, - {0x3fffda, 22}, - {0x1fffdd, 21}, - {0xfffe9, 20}, - {0x3fffdb, 22}, - {0x3fffdc, 22}, - {0x7fffe8, 23}, - {0x7fffe9, 23}, - {0x1fffde, 21}, - {0x7fffea, 23}, - {0x3fffdd, 22}, - {0x3fffde, 22}, - {0xfffff0, 24}, - {0x1fffdf, 21}, - {0x3fffdf, 22}, - {0x7fffeb, 23}, - {0x7fffec, 23}, - {0x1fffe0, 21}, - {0x1fffe1, 21}, - {0x3fffe0, 22}, - {0x1fffe2, 21}, - {0x7fffed, 23}, - {0x3fffe1, 22}, - {0x7fffee, 23}, - {0x7fffef, 23}, - {0xfffea, 20}, - {0x3fffe2, 22}, - {0x3fffe3, 22}, - {0x3fffe4, 22}, - {0x7ffff0, 23}, - {0x3fffe5, 22}, - {0x3fffe6, 22}, - {0x7ffff1, 23}, - {0x3ffffe0, 26}, - {0x3ffffe1, 26}, - {0xfffeb, 20}, - {0x7fff1, 19}, - {0x3fffe7, 22}, - {0x7ffff2, 23}, - {0x3fffe8, 22}, - {0x1ffffec, 25}, - {0x3ffffe2, 26}, - {0x3ffffe3, 26}, - {0x3ffffe4, 26}, - {0x7ffffde, 27}, - {0x7ffffdf, 27}, - {0x3ffffe5, 26}, - {0xfffff1, 24}, - {0x1ffffed, 25}, - {0x7fff2, 19}, - {0x1fffe3, 21}, - {0x3ffffe6, 26}, - {0x7ffffe0, 27}, - {0x7ffffe1, 27}, - {0x3ffffe7, 26}, - {0x7ffffe2, 27}, - {0xfffff2, 24}, - {0x1fffe4, 21}, - {0x1fffe5, 21}, - {0x3ffffe8, 26}, - {0x3ffffe9, 26}, - {0xffffffd, 28}, - {0x7ffffe3, 27}, - {0x7ffffe4, 27}, - {0x7ffffe5, 27}, - {0xfffec, 20}, - {0xfffff3, 24}, - {0xfffed, 20}, - {0x1fffe6, 21}, - {0x3fffe9, 22}, - {0x1fffe7, 21}, - {0x1fffe8, 21}, - {0x7ffff3, 23}, - {0x3fffea, 22}, - {0x3fffeb, 22}, - {0x1ffffee, 25}, - {0x1ffffef, 25}, - {0xfffff4, 24}, - {0xfffff5, 24}, - {0x3ffffea, 26}, - {0x7ffff4, 23}, - {0x3ffffeb, 26}, - {0x7ffffe6, 27}, - {0x3ffffec, 26}, - {0x3ffffed, 26}, - {0x7ffffe7, 27}, - {0x7ffffe8, 27}, - {0x7ffffe9, 27}, - {0x7ffffea, 27}, - {0x7ffffeb, 27}, - {0xffffffe, 28}, - {0x7ffffec, 27}, - {0x7ffffed, 27}, - {0x7ffffee, 27}, - {0x7ffffef, 27}, - {0x7fffff0, 27}, - {0x3ffffee, 26}, - {0x3fffffff, 30}, }; + {0x1ff8, 13}, {0x7fffd8, 23}, {0xfffffe2, 28}, {0xfffffe3, 28}, + {0xfffffe4, 28}, {0xfffffe5, 28}, {0xfffffe6, 28}, {0xfffffe7, 28}, + {0xfffffe8, 28}, {0xffffea, 24}, {0x3ffffffc, 30}, {0xfffffe9, 28}, + {0xfffffea, 28}, {0x3ffffffd, 30}, {0xfffffeb, 28}, {0xfffffec, 28}, + {0xfffffed, 28}, {0xfffffee, 28}, {0xfffffef, 28}, {0xffffff0, 28}, + {0xffffff1, 28}, {0xffffff2, 28}, {0x3ffffffe, 30}, {0xffffff3, 28}, + {0xffffff4, 28}, {0xffffff5, 28}, {0xffffff6, 28}, {0xffffff7, 28}, + {0xffffff8, 28}, {0xffffff9, 28}, {0xffffffa, 28}, {0xffffffb, 28}, + {0x14, 6}, {0x3f8, 10}, {0x3f9, 10}, {0xffa, 12}, + {0x1ff9, 13}, {0x15, 6}, {0xf8, 8}, {0x7fa, 11}, + {0x3fa, 10}, {0x3fb, 10}, {0xf9, 8}, {0x7fb, 11}, + {0xfa, 8}, {0x16, 6}, {0x17, 6}, {0x18, 6}, + {0x0, 5}, {0x1, 5}, {0x2, 5}, {0x19, 6}, + {0x1a, 6}, {0x1b, 6}, {0x1c, 6}, {0x1d, 6}, + {0x1e, 6}, {0x1f, 6}, {0x5c, 7}, {0xfb, 8}, + {0x7ffc, 15}, {0x20, 6}, {0xffb, 12}, {0x3fc, 10}, + {0x1ffa, 13}, {0x21, 6}, {0x5d, 7}, {0x5e, 7}, + {0x5f, 7}, {0x60, 7}, {0x61, 7}, {0x62, 7}, + {0x63, 7}, {0x64, 7}, {0x65, 7}, {0x66, 7}, + {0x67, 7}, {0x68, 7}, {0x69, 7}, {0x6a, 7}, + {0x6b, 7}, {0x6c, 7}, {0x6d, 7}, {0x6e, 7}, + {0x6f, 7}, {0x70, 7}, {0x71, 7}, {0x72, 7}, + {0xfc, 8}, {0x73, 7}, {0xfd, 8}, {0x1ffb, 13}, + {0x7fff0, 19}, {0x1ffc, 13}, {0x3ffc, 14}, {0x22, 6}, + {0x7ffd, 15}, {0x3, 5}, {0x23, 6}, {0x4, 5}, + {0x24, 6}, {0x5, 5}, {0x25, 6}, {0x26, 6}, + {0x27, 6}, {0x6, 5}, {0x74, 7}, {0x75, 7}, + {0x28, 6}, {0x29, 6}, {0x2a, 6}, {0x7, 5}, + {0x2b, 6}, {0x76, 7}, {0x2c, 6}, {0x8, 5}, + {0x9, 5}, {0x2d, 6}, {0x77, 7}, {0x78, 7}, + {0x79, 7}, {0x7a, 7}, {0x7b, 7}, {0x7ffe, 15}, + {0x7fc, 11}, {0x3ffd, 14}, {0x1ffd, 13}, {0xffffffc, 28}, + {0xfffe6, 20}, {0x3fffd2, 22}, {0xfffe7, 20}, {0xfffe8, 20}, + {0x3fffd3, 22}, {0x3fffd4, 22}, {0x3fffd5, 22}, {0x7fffd9, 23}, + {0x3fffd6, 22}, {0x7fffda, 23}, {0x7fffdb, 23}, {0x7fffdc, 23}, + {0x7fffdd, 23}, {0x7fffde, 23}, {0xffffeb, 24}, {0x7fffdf, 23}, + {0xffffec, 24}, {0xffffed, 24}, {0x3fffd7, 22}, {0x7fffe0, 23}, + {0xffffee, 24}, {0x7fffe1, 23}, {0x7fffe2, 23}, {0x7fffe3, 23}, + {0x7fffe4, 23}, {0x1fffdc, 21}, {0x3fffd8, 22}, {0x7fffe5, 23}, + {0x3fffd9, 22}, {0x7fffe6, 23}, {0x7fffe7, 23}, {0xffffef, 24}, + {0x3fffda, 22}, {0x1fffdd, 21}, {0xfffe9, 20}, {0x3fffdb, 22}, + {0x3fffdc, 22}, {0x7fffe8, 23}, {0x7fffe9, 23}, {0x1fffde, 21}, + {0x7fffea, 23}, {0x3fffdd, 22}, {0x3fffde, 22}, {0xfffff0, 24}, + {0x1fffdf, 21}, {0x3fffdf, 22}, {0x7fffeb, 23}, {0x7fffec, 23}, + {0x1fffe0, 21}, {0x1fffe1, 21}, {0x3fffe0, 22}, {0x1fffe2, 21}, + {0x7fffed, 23}, {0x3fffe1, 22}, {0x7fffee, 23}, {0x7fffef, 23}, + {0xfffea, 20}, {0x3fffe2, 22}, {0x3fffe3, 22}, {0x3fffe4, 22}, + {0x7ffff0, 23}, {0x3fffe5, 22}, {0x3fffe6, 22}, {0x7ffff1, 23}, + {0x3ffffe0, 26}, {0x3ffffe1, 26}, {0xfffeb, 20}, {0x7fff1, 19}, + {0x3fffe7, 22}, {0x7ffff2, 23}, {0x3fffe8, 22}, {0x1ffffec, 25}, + {0x3ffffe2, 26}, {0x3ffffe3, 26}, {0x3ffffe4, 26}, {0x7ffffde, 27}, + {0x7ffffdf, 27}, {0x3ffffe5, 26}, {0xfffff1, 24}, {0x1ffffed, 25}, + {0x7fff2, 19}, {0x1fffe3, 21}, {0x3ffffe6, 26}, {0x7ffffe0, 27}, + {0x7ffffe1, 27}, {0x3ffffe7, 26}, {0x7ffffe2, 27}, {0xfffff2, 24}, + {0x1fffe4, 21}, {0x1fffe5, 21}, {0x3ffffe8, 26}, {0x3ffffe9, 26}, + {0xffffffd, 28}, {0x7ffffe3, 27}, {0x7ffffe4, 27}, {0x7ffffe5, 27}, + {0xfffec, 20}, {0xfffff3, 24}, {0xfffed, 20}, {0x1fffe6, 21}, + {0x3fffe9, 22}, {0x1fffe7, 21}, {0x1fffe8, 21}, {0x7ffff3, 23}, + {0x3fffea, 22}, {0x3fffeb, 22}, {0x1ffffee, 25}, {0x1ffffef, 25}, + {0xfffff4, 24}, {0xfffff5, 24}, {0x3ffffea, 26}, {0x7ffff4, 23}, + {0x3ffffeb, 26}, {0x7ffffe6, 27}, {0x3ffffec, 26}, {0x3ffffed, 26}, + {0x7ffffe7, 27}, {0x7ffffe8, 27}, {0x7ffffe9, 27}, {0x7ffffea, 27}, + {0x7ffffeb, 27}, {0xffffffe, 28}, {0x7ffffec, 27}, {0x7ffffed, 27}, + {0x7ffffee, 27}, {0x7ffffef, 27}, {0x7fffff0, 27}, {0x3ffffee, 26}, + {0x3fffffff, 30}, +}; diff --git a/src/core/transport/chttp2/huffsyms.h b/src/core/transport/chttp2/huffsyms.h index f9c1447966..a3cdba8235 100644 --- a/src/core/transport/chttp2/huffsyms.h +++ b/src/core/transport/chttp2/huffsyms.h @@ -45,4 +45,4 @@ typedef struct { extern const grpc_chttp2_huffsym grpc_chttp2_huffsyms[GRPC_CHTTP2_NUM_HUFFSYMS]; -#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_HUFFSYMS_H */ +#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_HUFFSYMS_H */ diff --git a/src/core/transport/chttp2/incoming_metadata.c b/src/core/transport/chttp2/incoming_metadata.c new file mode 100644 index 0000000000..a4b7174329 --- /dev/null +++ b/src/core/transport/chttp2/incoming_metadata.c @@ -0,0 +1,181 @@ +/* + * + * 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 "src/core/transport/chttp2/incoming_metadata.h" + +#include <string.h> + +#include "src/core/transport/chttp2/internal.h" + +#include <grpc/support/alloc.h> +#include <grpc/support/log.h> + +void grpc_chttp2_incoming_metadata_buffer_init( + grpc_chttp2_incoming_metadata_buffer *buffer) { + buffer->deadline = gpr_inf_future; +} + +void grpc_chttp2_incoming_metadata_buffer_destroy( + grpc_chttp2_incoming_metadata_buffer *buffer) { + size_t i; + for (i = 0; i < buffer->count; i++) { + grpc_mdelem_unref(buffer->elems[i].md); + } + gpr_free(buffer->elems); +} + +void grpc_chttp2_incoming_metadata_buffer_add( + grpc_chttp2_incoming_metadata_buffer *buffer, grpc_mdelem *elem) { + if (buffer->capacity == buffer->count) { + buffer->capacity = GPR_MAX(8, 2 * buffer->capacity); + buffer->elems = + gpr_realloc(buffer->elems, sizeof(*buffer->elems) * buffer->capacity); + } + buffer->elems[buffer->count++].md = elem; +} + +void grpc_chttp2_incoming_metadata_buffer_set_deadline( + grpc_chttp2_incoming_metadata_buffer *buffer, gpr_timespec deadline) { + buffer->deadline = deadline; +} + +void grpc_chttp2_incoming_metadata_live_op_buffer_end( + grpc_chttp2_incoming_metadata_live_op_buffer *buffer) { + gpr_free(buffer->elems); + buffer->elems = NULL; +} + +void grpc_chttp2_incoming_metadata_buffer_place_metadata_batch_into( + grpc_chttp2_incoming_metadata_buffer *buffer, grpc_stream_op_buffer *sopb) { + grpc_metadata_batch b; + + b.list.head = NULL; + /* Store away the last element of the list, so that in patch_metadata_ops + we can reconstitute the list. + We can't do list building here as later incoming metadata may reallocate + the underlying array. */ + b.list.tail = (void *)(gpr_intptr)buffer->count; + b.garbage.head = b.garbage.tail = NULL; + b.deadline = buffer->deadline; + buffer->deadline = gpr_inf_future; + + grpc_sopb_add_metadata(sopb, b); +} + +void grpc_chttp2_incoming_metadata_buffer_swap( + grpc_chttp2_incoming_metadata_buffer *a, + grpc_chttp2_incoming_metadata_buffer *b) { + GPR_SWAP(grpc_chttp2_incoming_metadata_buffer, *a, *b); +} + +void grpc_incoming_metadata_buffer_move_to_referencing_sopb( + grpc_chttp2_incoming_metadata_buffer *src, + grpc_chttp2_incoming_metadata_buffer *dst, grpc_stream_op_buffer *sopb) { + size_t delta; + size_t i; + dst->deadline = gpr_time_min(src->deadline, dst->deadline); + + if (src->count == 0) { + return; + } + if (dst->count == 0) { + grpc_chttp2_incoming_metadata_buffer_swap(src, dst); + return; + } + delta = dst->count; + if (dst->capacity < src->count + dst->count) { + dst->capacity = GPR_MAX(dst->capacity * 2, src->count + dst->count); + dst->elems = gpr_realloc(dst->elems, dst->capacity * sizeof(*dst->elems)); + } + memcpy(dst->elems + dst->count, src->elems, src->count * sizeof(*src->elems)); + dst->count += src->count; + for (i = 0; i < sopb->nops; i++) { + if (sopb->ops[i].type != GRPC_OP_METADATA) continue; + sopb->ops[i].data.metadata.list.tail = + (void *)(delta + (gpr_intptr)sopb->ops[i].data.metadata.list.tail); + } +} + +void grpc_chttp2_incoming_metadata_buffer_postprocess_sopb_and_begin_live_op( + grpc_chttp2_incoming_metadata_buffer *buffer, grpc_stream_op_buffer *sopb, + grpc_chttp2_incoming_metadata_live_op_buffer *live_op_buffer) { + grpc_stream_op *ops = sopb->ops; + size_t nops = sopb->nops; + size_t i; + size_t j; + size_t mdidx = 0; + size_t last_mdidx; + int found_metadata = 0; + + /* rework the array of metadata into a linked list, making use + of the breadcrumbs we left in metadata batches during + add_metadata_batch */ + for (i = 0; i < nops; i++) { + grpc_stream_op *op = &ops[i]; + if (op->type != GRPC_OP_METADATA) continue; + found_metadata = 1; + /* we left a breadcrumb indicating where the end of this list is, + and since we add sequentially, we know from the end of the last + segment where this segment begins */ + last_mdidx = (size_t)(gpr_intptr)(op->data.metadata.list.tail); + GPR_ASSERT(last_mdidx > mdidx); + GPR_ASSERT(last_mdidx <= buffer->count); + /* turn the array into a doubly linked list */ + op->data.metadata.list.head = &buffer->elems[mdidx]; + op->data.metadata.list.tail = &buffer->elems[last_mdidx - 1]; + for (j = mdidx + 1; j < last_mdidx; j++) { + buffer->elems[j].prev = &buffer->elems[j - 1]; + buffer->elems[j - 1].next = &buffer->elems[j]; + } + buffer->elems[mdidx].prev = NULL; + buffer->elems[last_mdidx - 1].next = NULL; + /* track where we're up to */ + mdidx = last_mdidx; + } + if (found_metadata) { + live_op_buffer->elems = buffer->elems; + if (mdidx != buffer->count) { + /* we have a partially read metadata batch still in incoming_metadata */ + size_t new_count = buffer->count - mdidx; + size_t copy_bytes = sizeof(*buffer->elems) * new_count; + GPR_ASSERT(mdidx < buffer->count); + buffer->elems = gpr_malloc(copy_bytes); + memcpy(live_op_buffer->elems + mdidx, buffer->elems, copy_bytes); + buffer->count = buffer->capacity = new_count; + } else { + buffer->elems = NULL; + buffer->count = 0; + buffer->capacity = 0; + } + } +} diff --git a/src/core/transport/chttp2/incoming_metadata.h b/src/core/transport/chttp2/incoming_metadata.h new file mode 100644 index 0000000000..2f1de411ba --- /dev/null +++ b/src/core/transport/chttp2/incoming_metadata.h @@ -0,0 +1,80 @@ +/* + * + * 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. + * + */ + +#ifndef GRPC_INTERNAL_CORE_CHTTP2_INCOMING_METADATA_H +#define GRPC_INTERNAL_CORE_CHTTP2_INCOMING_METADATA_H + +#include "src/core/transport/transport.h" + +typedef struct { + grpc_linked_mdelem *elems; + size_t count; + size_t capacity; + gpr_timespec deadline; +} grpc_chttp2_incoming_metadata_buffer; + +typedef struct { + grpc_linked_mdelem *elems; +} grpc_chttp2_incoming_metadata_live_op_buffer; + +/** assumes everything initially zeroed */ +void grpc_chttp2_incoming_metadata_buffer_init( + grpc_chttp2_incoming_metadata_buffer *buffer); +void grpc_chttp2_incoming_metadata_buffer_destroy( + grpc_chttp2_incoming_metadata_buffer *buffer); +void grpc_chttp2_incoming_metadata_buffer_reset( + grpc_chttp2_incoming_metadata_buffer *buffer); + +void grpc_chttp2_incoming_metadata_buffer_add( + grpc_chttp2_incoming_metadata_buffer *buffer, grpc_mdelem *elem); +void grpc_chttp2_incoming_metadata_buffer_set_deadline( + grpc_chttp2_incoming_metadata_buffer *buffer, gpr_timespec deadline); + +/** extend sopb with a metadata batch; this must be post-processed by + grpc_chttp2_incoming_metadata_buffer_postprocess_sopb before being handed + out of the transport */ +void grpc_chttp2_incoming_metadata_buffer_place_metadata_batch_into( + grpc_chttp2_incoming_metadata_buffer *buffer, grpc_stream_op_buffer *sopb); + +void grpc_incoming_metadata_buffer_move_to_referencing_sopb( + grpc_chttp2_incoming_metadata_buffer *src, + grpc_chttp2_incoming_metadata_buffer *dst, grpc_stream_op_buffer *sopb); + +void grpc_chttp2_incoming_metadata_buffer_postprocess_sopb_and_begin_live_op( + grpc_chttp2_incoming_metadata_buffer *buffer, grpc_stream_op_buffer *sopb, + grpc_chttp2_incoming_metadata_live_op_buffer *live_op_buffer); + +void grpc_chttp2_incoming_metadata_live_op_buffer_end( + grpc_chttp2_incoming_metadata_live_op_buffer *live_op_buffer); + +#endif /* GRPC_INTERNAL_CORE_CHTTP2_INCOMING_METADATA_H */ diff --git a/src/core/transport/chttp2/internal.h b/src/core/transport/chttp2/internal.h new file mode 100644 index 0000000000..45addd8c2b --- /dev/null +++ b/src/core/transport/chttp2/internal.h @@ -0,0 +1,621 @@ +/* + * + * 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. + * + */ + +#ifndef GRPC_INTERNAL_CORE_CHTTP2_INTERNAL_H +#define GRPC_INTERNAL_CORE_CHTTP2_INTERNAL_H + +#include "src/core/transport/transport_impl.h" +#include "src/core/iomgr/endpoint.h" +#include "src/core/transport/chttp2/frame.h" +#include "src/core/transport/chttp2/frame_data.h" +#include "src/core/transport/chttp2/frame_goaway.h" +#include "src/core/transport/chttp2/frame_ping.h" +#include "src/core/transport/chttp2/frame_rst_stream.h" +#include "src/core/transport/chttp2/frame_settings.h" +#include "src/core/transport/chttp2/frame_window_update.h" +#include "src/core/transport/chttp2/hpack_parser.h" +#include "src/core/transport/chttp2/incoming_metadata.h" +#include "src/core/transport/chttp2/stream_encoder.h" +#include "src/core/transport/chttp2/stream_map.h" + +typedef struct grpc_chttp2_transport grpc_chttp2_transport; +typedef struct grpc_chttp2_stream grpc_chttp2_stream; + +/* streams are kept in various linked lists depending on what things need to + happen to them... this enum labels each list */ +typedef enum { + GRPC_CHTTP2_LIST_ALL_STREAMS, + GRPC_CHTTP2_LIST_READ_WRITE_STATE_CHANGED, + GRPC_CHTTP2_LIST_WRITABLE, + GRPC_CHTTP2_LIST_WRITING, + GRPC_CHTTP2_LIST_WRITTEN, + GRPC_CHTTP2_LIST_WRITABLE_WINDOW_UPDATE, + GRPC_CHTTP2_LIST_PARSING_SEEN, + GRPC_CHTTP2_LIST_CLOSED_WAITING_FOR_PARSING, + GRPC_CHTTP2_LIST_INCOMING_WINDOW_UPDATED, + /** streams that are waiting to start because there are too many concurrent + streams on the connection */ + GRPC_CHTTP2_LIST_WAITING_FOR_CONCURRENCY, + STREAM_LIST_COUNT /* must be last */ +} grpc_chttp2_stream_list_id; + +/* deframer state for the overall http2 stream of bytes */ +typedef enum { + /* prefix: one entry per http2 connection prefix byte */ + GRPC_DTS_CLIENT_PREFIX_0 = 0, + GRPC_DTS_CLIENT_PREFIX_1, + GRPC_DTS_CLIENT_PREFIX_2, + GRPC_DTS_CLIENT_PREFIX_3, + GRPC_DTS_CLIENT_PREFIX_4, + GRPC_DTS_CLIENT_PREFIX_5, + GRPC_DTS_CLIENT_PREFIX_6, + GRPC_DTS_CLIENT_PREFIX_7, + GRPC_DTS_CLIENT_PREFIX_8, + GRPC_DTS_CLIENT_PREFIX_9, + GRPC_DTS_CLIENT_PREFIX_10, + GRPC_DTS_CLIENT_PREFIX_11, + GRPC_DTS_CLIENT_PREFIX_12, + GRPC_DTS_CLIENT_PREFIX_13, + GRPC_DTS_CLIENT_PREFIX_14, + GRPC_DTS_CLIENT_PREFIX_15, + GRPC_DTS_CLIENT_PREFIX_16, + GRPC_DTS_CLIENT_PREFIX_17, + GRPC_DTS_CLIENT_PREFIX_18, + GRPC_DTS_CLIENT_PREFIX_19, + GRPC_DTS_CLIENT_PREFIX_20, + GRPC_DTS_CLIENT_PREFIX_21, + GRPC_DTS_CLIENT_PREFIX_22, + GRPC_DTS_CLIENT_PREFIX_23, + /* frame header byte 0... */ + /* must follow from the prefix states */ + GRPC_DTS_FH_0, + GRPC_DTS_FH_1, + GRPC_DTS_FH_2, + GRPC_DTS_FH_3, + GRPC_DTS_FH_4, + GRPC_DTS_FH_5, + GRPC_DTS_FH_6, + GRPC_DTS_FH_7, + /* ... frame header byte 8 */ + GRPC_DTS_FH_8, + /* inside a http2 frame */ + GRPC_DTS_FRAME +} grpc_chttp2_deframe_transport_state; + +typedef enum { + GRPC_WRITE_STATE_OPEN, + GRPC_WRITE_STATE_QUEUED_CLOSE, + GRPC_WRITE_STATE_SENT_CLOSE +} grpc_chttp2_write_state; + +typedef enum { + GRPC_DONT_SEND_CLOSED = 0, + GRPC_SEND_CLOSED, + GRPC_SEND_CLOSED_WITH_RST_STREAM +} grpc_chttp2_send_closed; + +typedef struct { + grpc_chttp2_stream *head; + grpc_chttp2_stream *tail; +} grpc_chttp2_stream_list; + +typedef struct { + grpc_chttp2_stream *next; + grpc_chttp2_stream *prev; +} grpc_chttp2_stream_link; + +typedef enum { + GRPC_CHTTP2_ERROR_STATE_NONE, + GRPC_CHTTP2_ERROR_STATE_SEEN, + GRPC_CHTTP2_ERROR_STATE_NOTIFIED +} grpc_chttp2_error_state; + +/* We keep several sets of connection wide parameters */ +typedef enum { + /* The settings our peer has asked for (and we have acked) */ + GRPC_PEER_SETTINGS = 0, + /* The settings we'd like to have */ + GRPC_LOCAL_SETTINGS, + /* The settings we've published to our peer */ + GRPC_SENT_SETTINGS, + /* The settings the peer has acked */ + GRPC_ACKED_SETTINGS, + GRPC_NUM_SETTING_SETS +} grpc_chttp2_setting_set; + +/* Outstanding ping request data */ +typedef struct grpc_chttp2_outstanding_ping { + gpr_uint8 id[8]; + grpc_iomgr_closure *on_recv; + struct grpc_chttp2_outstanding_ping *next; + struct grpc_chttp2_outstanding_ping *prev; +} grpc_chttp2_outstanding_ping; + +typedef struct { + /** data to write next write */ + gpr_slice_buffer qbuf; + /** queued callbacks */ + grpc_iomgr_closure *pending_closures; + + /** window available for us to send to peer */ + gpr_uint32 outgoing_window; + /** window available for peer to send to us - updated after parse */ + gpr_uint32 incoming_window; + /** how much window would we like to have for incoming_window */ + gpr_uint32 connection_window_target; + + /** is this transport a client? */ + gpr_uint8 is_client; + /** are the local settings dirty and need to be sent? */ + gpr_uint8 dirtied_local_settings; + /** have local settings been sent? */ + gpr_uint8 sent_local_settings; + /** bitmask of setting indexes to send out */ + gpr_uint32 force_send_settings; + /** settings values */ + gpr_uint32 settings[GRPC_NUM_SETTING_SETS][GRPC_CHTTP2_NUM_SETTINGS]; + + /** has there been a connection level error, and have we notified + anyone about it? */ + grpc_chttp2_error_state error_state; + + /** what is the next stream id to be allocated by this peer? + copied to next_stream_id in parsing when parsing commences */ + gpr_uint32 next_stream_id; + + /** last received stream id */ + gpr_uint32 last_incoming_stream_id; + + /** pings awaiting responses */ + grpc_chttp2_outstanding_ping pings; + /** next payload for an outgoing ping */ + gpr_uint64 ping_counter; + + /** concurrent stream count: updated when not parsing, + so this is a strict over-estimation on the client */ + gpr_uint32 concurrent_stream_count; + + /** is there a goaway available? (boolean) */ + grpc_chttp2_error_state goaway_state; + /** what is the debug text of the goaway? */ + gpr_slice goaway_text; + /** what is the status code of the goaway? */ + grpc_status_code goaway_error; +} grpc_chttp2_transport_global; + +typedef struct { + /** data to write now */ + gpr_slice_buffer outbuf; + /** hpack encoding */ + grpc_chttp2_hpack_compressor hpack_compressor; + /** is this a client? */ + gpr_uint8 is_client; +} grpc_chttp2_transport_writing; + +struct grpc_chttp2_transport_parsing { + /** is this transport a client? (boolean) */ + gpr_uint8 is_client; + + /** were settings updated? */ + gpr_uint8 settings_updated; + /** was a settings ack received? */ + gpr_uint8 settings_ack_received; + /** was a goaway frame received? */ + gpr_uint8 goaway_received; + + /** initial window change */ + gpr_int64 initial_window_update; + + /** data to write later - after parsing */ + gpr_slice_buffer qbuf; + /* metadata object cache */ + grpc_mdstr *str_grpc_timeout; + /** parser for headers */ + grpc_chttp2_hpack_parser hpack_parser; + /** simple one shot parsers */ + union { + grpc_chttp2_window_update_parser window_update; + grpc_chttp2_settings_parser settings; + grpc_chttp2_ping_parser ping; + grpc_chttp2_rst_stream_parser rst_stream; + } simple; + /** parser for goaway frames */ + grpc_chttp2_goaway_parser goaway_parser; + + /** window available for peer to send to us */ + gpr_uint32 incoming_window; + gpr_uint32 incoming_window_delta; + + /** next stream id available at the time of beginning parsing */ + gpr_uint32 next_stream_id; + gpr_uint32 last_incoming_stream_id; + + /* deframing */ + grpc_chttp2_deframe_transport_state deframe_state; + gpr_uint8 incoming_frame_type; + gpr_uint8 incoming_frame_flags; + gpr_uint8 header_eof; + gpr_uint32 expect_continuation_stream_id; + gpr_uint32 incoming_frame_size; + gpr_uint32 incoming_stream_id; + + /* active parser */ + void *parser_data; + grpc_chttp2_stream_parsing *incoming_stream; + grpc_chttp2_parse_error (*parser)( + void *parser_user_data, grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last); + + /* received settings */ + gpr_uint32 settings[GRPC_CHTTP2_NUM_SETTINGS]; + + /* goaway data */ + grpc_status_code goaway_error; + gpr_uint32 goaway_last_stream_index; + gpr_slice goaway_text; + + gpr_uint64 outgoing_window_update; + + /** pings awaiting responses */ + grpc_chttp2_outstanding_ping pings; +}; + +struct grpc_chttp2_transport { + grpc_transport base; /* must be first */ + grpc_endpoint *ep; + grpc_mdctx *metadata_context; + gpr_refcount refs; + + gpr_mu mu; + + /** is the transport destroying itself? */ + gpr_uint8 destroying; + /** has the upper layer closed the transport? */ + gpr_uint8 closed; + + /** is a thread currently writing */ + gpr_uint8 writing_active; + /** is a thread currently parsing */ + gpr_uint8 parsing_active; + + /** is there a read request to the endpoint outstanding? */ + gpr_uint8 endpoint_reading; + + /** various lists of streams */ + grpc_chttp2_stream_list lists[STREAM_LIST_COUNT]; + + /** global state for reading/writing */ + grpc_chttp2_transport_global global; + /** state only accessible by the chain of execution that + set writing_active=1 */ + grpc_chttp2_transport_writing writing; + /** state only accessible by the chain of execution that + set parsing_active=1 */ + grpc_chttp2_transport_parsing parsing; + + /** maps stream id to grpc_chttp2_stream objects; + owned by the parsing thread when parsing */ + grpc_chttp2_stream_map parsing_stream_map; + + /** streams created by the client (possibly during parsing); + merged with parsing_stream_map during unlock when no + parsing is occurring */ + grpc_chttp2_stream_map new_stream_map; + + /** closure to execute writing */ + grpc_iomgr_closure writing_action; + /** closure to start reading from the endpoint */ + grpc_iomgr_closure reading_action; + + /** address to place a newly accepted stream - set and unset by + grpc_chttp2_parsing_accept_stream; used by init_stream to + publish the accepted server stream */ + grpc_chttp2_stream **accepting_stream; + + struct { + /** is a thread currently performing channel callbacks */ + gpr_uint8 executing; + /** transport channel-level callback */ + const grpc_transport_callbacks *cb; + /** user data for cb calls */ + void *cb_user_data; + /** closure for notifying transport closure */ + grpc_iomgr_closure notify_closed; + } channel_callback; +}; + +typedef struct { + /** HTTP2 stream id for this stream, or zero if one has not been assigned */ + gpr_uint32 id; + + grpc_iomgr_closure *send_done_closure; + grpc_iomgr_closure *recv_done_closure; + + /** window available for us to send to peer */ + gpr_int64 outgoing_window; + /** The number of bytes the upper layers have offered to receive. + As the upper layer offers more bytes, this value increases. + As bytes are read, this value decreases. */ + gpr_uint32 max_recv_bytes; + /** The number of bytes the upper layer has offered to read but we have + not yet announced to HTTP2 flow control. + As the upper layers offer to read more bytes, this value increases. + As we advertise incoming flow control window, this value decreases. */ + gpr_uint32 unannounced_incoming_window; + /** The number of bytes of HTTP2 flow control we have advertised. + As we advertise incoming flow control window, this value increases. + As bytes are read, this value decreases. + Updated after parse. */ + gpr_uint32 incoming_window; + /** stream ops the transport user would like to send */ + grpc_stream_op_buffer *outgoing_sopb; + /** when the application requests writes be closed, the write_closed is + 'queued'; when the close is flow controlled into the send path, we are + 'sending' it; when the write has been performed it is 'sent' */ + grpc_chttp2_write_state write_state; + /** is this stream closed (boolean) */ + gpr_uint8 read_closed; + /** has this stream been cancelled? (boolean) */ + gpr_uint8 cancelled; + grpc_status_code cancelled_status; + /** have we told the upper layer that this stream is cancelled? */ + gpr_uint8 published_cancelled; + /** is this stream in the stream map? (boolean) */ + gpr_uint8 in_stream_map; + + /** stream state already published to the upper layer */ + grpc_stream_state published_state; + /** address to publish next stream state to */ + grpc_stream_state *publish_state; + /** pointer to sop buffer to fill in with new stream ops */ + grpc_stream_op_buffer *publish_sopb; + grpc_stream_op_buffer incoming_sopb; + + /** incoming metadata */ + grpc_chttp2_incoming_metadata_buffer incoming_metadata; + grpc_chttp2_incoming_metadata_live_op_buffer outstanding_metadata; +} grpc_chttp2_stream_global; + +typedef struct { + /** HTTP2 stream id for this stream, or zero if one has not been assigned */ + gpr_uint32 id; + /** sops that have passed flow control to be written */ + grpc_stream_op_buffer sopb; + /** how strongly should we indicate closure with the next write */ + grpc_chttp2_send_closed send_closed; +} grpc_chttp2_stream_writing; + +struct grpc_chttp2_stream_parsing { + /** HTTP2 stream id for this stream, or zero if one has not been assigned */ + gpr_uint32 id; + /** has this stream received a close */ + gpr_uint8 received_close; + /** saw a rst_stream */ + gpr_uint8 saw_rst_stream; + /** incoming_window has been reduced by this much during parsing */ + gpr_uint32 incoming_window_delta; + /** window available for peer to send to us */ + gpr_uint32 incoming_window; + /** parsing state for data frames */ + grpc_chttp2_data_parser data_parser; + /** reason give to rst_stream */ + gpr_uint32 rst_stream_reason; + /* amount of window given */ + gpr_uint64 outgoing_window_update; + + /** incoming metadata */ + grpc_chttp2_incoming_metadata_buffer incoming_metadata; +}; + +struct grpc_chttp2_stream { + grpc_chttp2_stream_global global; + grpc_chttp2_stream_writing writing; + grpc_chttp2_stream_parsing parsing; + + grpc_chttp2_stream_link links[STREAM_LIST_COUNT]; + gpr_uint8 included[STREAM_LIST_COUNT]; +}; + +/** Transport writing call flow: + chttp2_transport.c calls grpc_chttp2_unlocking_check_writes to see if writes + are required; + if they are, chttp2_transport.c calls grpc_chttp2_perform_writes to do the + writes. + Once writes have been completed (meaning another write could potentially be + started), + grpc_chttp2_terminate_writing is called. This will call + grpc_chttp2_cleanup_writing, at which + point the write phase is complete. */ + +/** Someone is unlocking the transport mutex: check to see if writes + are required, and schedule them if so */ +int grpc_chttp2_unlocking_check_writes(grpc_chttp2_transport_global *global, + grpc_chttp2_transport_writing *writing); +void grpc_chttp2_perform_writes( + grpc_chttp2_transport_writing *transport_writing, grpc_endpoint *endpoint); +void grpc_chttp2_terminate_writing( + grpc_chttp2_transport_writing *transport_writing, int success); +void grpc_chttp2_cleanup_writing(grpc_chttp2_transport_global *global, + grpc_chttp2_transport_writing *writing); + +void grpc_chttp2_prepare_to_read(grpc_chttp2_transport_global *global, + grpc_chttp2_transport_parsing *parsing); +/** Process one slice of incoming data; return 1 if the connection is still + viable after reading, or 0 if the connection should be torn down */ +int grpc_chttp2_perform_read(grpc_chttp2_transport_parsing *transport_parsing, + gpr_slice slice); +void grpc_chttp2_publish_reads(grpc_chttp2_transport_global *global, + grpc_chttp2_transport_parsing *parsing); + +/** Get a writable stream + returns non-zero if there was a stream available */ +void grpc_chttp2_list_add_writable_stream( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global *stream_global); +int grpc_chttp2_list_pop_writable_stream( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_transport_writing *transport_writing, + grpc_chttp2_stream_global **stream_global, + grpc_chttp2_stream_writing **stream_writing); + +void grpc_chttp2_list_add_incoming_window_updated( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global *stream_global); +int grpc_chttp2_list_pop_incoming_window_updated( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_global **stream_global, + grpc_chttp2_stream_parsing **stream_parsing); +void grpc_chttp2_list_remove_incoming_window_updated( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global *stream_global); + +void grpc_chttp2_list_add_writing_stream( + grpc_chttp2_transport_writing *transport_writing, + grpc_chttp2_stream_writing *stream_writing); +int grpc_chttp2_list_have_writing_streams( + grpc_chttp2_transport_writing *transport_writing); +int grpc_chttp2_list_pop_writing_stream( + grpc_chttp2_transport_writing *transport_writing, + grpc_chttp2_stream_writing **stream_writing); + +void grpc_chttp2_list_add_written_stream( + grpc_chttp2_transport_writing *transport_writing, + grpc_chttp2_stream_writing *stream_writing); +int grpc_chttp2_list_pop_written_stream( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_transport_writing *transport_writing, + grpc_chttp2_stream_global **stream_global, + grpc_chttp2_stream_writing **stream_writing); + +void grpc_chttp2_list_add_writable_window_update_stream( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global *stream_global); +int grpc_chttp2_list_pop_writable_window_update_stream( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global **stream_global); +void grpc_chttp2_list_remove_writable_window_update_stream( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global *stream_global); + +void grpc_chttp2_list_add_parsing_seen_stream( + grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_parsing *stream_parsing); +int grpc_chttp2_list_pop_parsing_seen_stream( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_global **stream_global, + grpc_chttp2_stream_parsing **stream_parsing); + +void grpc_chttp2_list_add_waiting_for_concurrency( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global *stream_global); +int grpc_chttp2_list_pop_waiting_for_concurrency( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global **stream_global); + +void grpc_chttp2_list_add_closed_waiting_for_parsing( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global *stream_global); +int grpc_chttp2_list_pop_closed_waiting_for_parsing( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global **stream_global); + +void grpc_chttp2_list_add_read_write_state_changed( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global *stream_global); +int grpc_chttp2_list_pop_read_write_state_changed( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global **stream_global); + +/** schedule a closure to run without the transport lock taken */ +void grpc_chttp2_schedule_closure( + grpc_chttp2_transport_global *transport_global, grpc_iomgr_closure *closure, + int success); + +grpc_chttp2_stream_parsing *grpc_chttp2_parsing_lookup_stream( + grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id); +grpc_chttp2_stream_parsing *grpc_chttp2_parsing_accept_stream( + grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id); + +void grpc_chttp2_add_incoming_goaway( + grpc_chttp2_transport_global *transport_global, gpr_uint32 goaway_error, + gpr_slice goaway_text); + +void grpc_chttp2_register_stream(grpc_chttp2_transport *t, + grpc_chttp2_stream *s); +void grpc_chttp2_unregister_stream(grpc_chttp2_transport *t, + grpc_chttp2_stream *s); +void grpc_chttp2_for_all_streams( + grpc_chttp2_transport_global *transport_global, void *user_data, + void (*cb)(grpc_chttp2_transport_global *transport_global, void *user_data, + grpc_chttp2_stream_global *stream_global)); + +void grpc_chttp2_parsing_become_skip_parser( + grpc_chttp2_transport_parsing *transport_parsing); + +#define GRPC_CHTTP2_CLIENT_CONNECT_STRING "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" +#define GRPC_CHTTP2_CLIENT_CONNECT_STRLEN \ + (sizeof(GRPC_CHTTP2_CLIENT_CONNECT_STRING) - 1) + +extern int grpc_http_trace; +extern int grpc_flowctl_trace; + +#define GRPC_CHTTP2_IF_TRACING(stmt) \ + if (!(grpc_http_trace)) \ + ; \ + else \ + stmt + +#define GRPC_CHTTP2_FLOWCTL_TRACE_STREAM(reason, transport, context, var, \ + delta) \ + if (!(grpc_flowctl_trace)) { \ + } else { \ + grpc_chttp2_flowctl_trace(__FILE__, __LINE__, reason, #context, #var, \ + transport->is_client, context->id, context->var, \ + delta); \ + } + +#define GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT(reason, context, var, delta) \ + if (!(grpc_flowctl_trace)) { \ + } else { \ + grpc_chttp2_flowctl_trace(__FILE__, __LINE__, reason, #context, #var, \ + context->is_client, 0, context->var, delta); \ + } + +void grpc_chttp2_flowctl_trace(const char *file, int line, const char *reason, + const char *context, const char *var, + int is_client, gpr_uint32 stream_id, + gpr_int64 current_value, gpr_int64 delta); + +#endif diff --git a/src/core/transport/chttp2/parsing.c b/src/core/transport/chttp2/parsing.c new file mode 100644 index 0000000000..bf6b04181b --- /dev/null +++ b/src/core/transport/chttp2/parsing.c @@ -0,0 +1,817 @@ +/* + * + * 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 "src/core/transport/chttp2/internal.h" + +#include <string.h> + +#include "src/core/transport/chttp2/http2_errors.h" +#include "src/core/transport/chttp2/status_conversion.h" +#include "src/core/transport/chttp2/timeout_encoding.h" + +#include <grpc/support/alloc.h> +#include <grpc/support/log.h> + +static int init_frame_parser(grpc_chttp2_transport_parsing *transport_parsing); +static int init_header_frame_parser( + grpc_chttp2_transport_parsing *transport_parsing, int is_continuation); +static int init_data_frame_parser( + grpc_chttp2_transport_parsing *transport_parsing); +static int init_rst_stream_parser( + grpc_chttp2_transport_parsing *transport_parsing); +static int init_settings_frame_parser( + grpc_chttp2_transport_parsing *transport_parsing); +static int init_window_update_frame_parser( + grpc_chttp2_transport_parsing *transport_parsing); +static int init_ping_parser(grpc_chttp2_transport_parsing *transport_parsing); +static int init_goaway_parser(grpc_chttp2_transport_parsing *transport_parsing); +static int init_skip_frame_parser( + grpc_chttp2_transport_parsing *transport_parsing, int is_header); + +static int parse_frame_slice(grpc_chttp2_transport_parsing *transport_parsing, + gpr_slice slice, int is_last); + +void grpc_chttp2_prepare_to_read( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_transport_parsing *transport_parsing) { + grpc_chttp2_stream_global *stream_global; + grpc_chttp2_stream_parsing *stream_parsing; + + transport_parsing->next_stream_id = transport_global->next_stream_id; + + /* update the parsing view of incoming window */ + if (transport_parsing->incoming_window != transport_global->incoming_window) { + GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT( + "parse", transport_parsing, incoming_window, + (gpr_int64)transport_global->incoming_window - + (gpr_int64)transport_parsing->incoming_window); + transport_parsing->incoming_window = transport_global->incoming_window; + } + while (grpc_chttp2_list_pop_incoming_window_updated( + transport_global, transport_parsing, &stream_global, &stream_parsing)) { + stream_parsing->id = stream_global->id; + if (stream_parsing->incoming_window != stream_global->incoming_window) { + GRPC_CHTTP2_FLOWCTL_TRACE_STREAM( + "parse", transport_parsing, stream_parsing, incoming_window, + (gpr_int64)stream_global->incoming_window - + (gpr_int64)stream_parsing->incoming_window); + stream_parsing->incoming_window = stream_global->incoming_window; + } + } +} + +void grpc_chttp2_publish_reads( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_transport_parsing *transport_parsing) { + grpc_chttp2_stream_global *stream_global; + grpc_chttp2_stream_parsing *stream_parsing; + + /* transport_parsing->last_incoming_stream_id is used as + last-grpc_chttp2_stream-id when + sending GOAWAY frame. + https://tools.ietf.org/html/draft-ietf-httpbis-http2-17#section-6.8 + says that last-grpc_chttp2_stream-id is peer-initiated grpc_chttp2_stream + ID. So, + since we don't have server pushed streams, client should send + GOAWAY last-grpc_chttp2_stream-id=0 in this case. */ + if (!transport_parsing->is_client) { + transport_global->last_incoming_stream_id = + transport_parsing->incoming_stream_id; + } + + /* copy parsing qbuf to global qbuf */ + gpr_slice_buffer_move_into(&transport_parsing->qbuf, &transport_global->qbuf); + + /* update global settings */ + if (transport_parsing->settings_updated) { + memcpy(transport_global->settings[GRPC_PEER_SETTINGS], + transport_parsing->settings, sizeof(transport_parsing->settings)); + transport_parsing->settings_updated = 0; + } + + /* update settings based on ack if received */ + if (transport_parsing->settings_ack_received) { + memcpy(transport_global->settings[GRPC_ACKED_SETTINGS], + transport_global->settings[GRPC_SENT_SETTINGS], + GRPC_CHTTP2_NUM_SETTINGS * sizeof(gpr_uint32)); + transport_parsing->settings_ack_received = 0; + } + + /* move goaway to the global state if we received one (it will be + published later */ + if (transport_parsing->goaway_received) { + grpc_chttp2_add_incoming_goaway(transport_global, + transport_parsing->goaway_error, + transport_parsing->goaway_text); + transport_parsing->goaway_text = gpr_empty_slice(); + transport_parsing->goaway_received = 0; + } + + /* propagate flow control tokens to global state */ + if (transport_parsing->outgoing_window_update) { + GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT( + "parsed", transport_global, outgoing_window, + transport_parsing->outgoing_window_update); + GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT( + "parsed", transport_parsing, outgoing_window_update, + -(gpr_int64)transport_parsing->outgoing_window_update); + transport_global->outgoing_window += + transport_parsing->outgoing_window_update; + transport_parsing->outgoing_window_update = 0; + } + + if (transport_parsing->incoming_window_delta) { + GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT( + "parsed", transport_global, incoming_window, + -(gpr_int64)transport_parsing->incoming_window_delta); + GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT( + "parsed", transport_parsing, incoming_window_delta, + -(gpr_int64)transport_parsing->incoming_window_delta); + transport_global->incoming_window -= + transport_parsing->incoming_window_delta; + transport_parsing->incoming_window_delta = 0; + } + + /* for each stream that saw an update, fixup global state */ + while (grpc_chttp2_list_pop_parsing_seen_stream( + transport_global, transport_parsing, &stream_global, &stream_parsing)) { + /* update incoming flow control window */ + if (stream_parsing->incoming_window_delta) { + GRPC_CHTTP2_FLOWCTL_TRACE_STREAM( + "parsed", transport_parsing, stream_global, incoming_window, + -(gpr_int64)stream_parsing->incoming_window_delta); + GRPC_CHTTP2_FLOWCTL_TRACE_STREAM( + "parsed", transport_parsing, stream_parsing, incoming_window_delta, + -(gpr_int64)stream_parsing->incoming_window_delta); + GRPC_CHTTP2_FLOWCTL_TRACE_STREAM( + "parsed", transport_parsing, stream_global, max_recv_bytes, + -(gpr_int64)stream_parsing->incoming_window_delta); + stream_global->incoming_window -= stream_parsing->incoming_window_delta; + GPR_ASSERT(stream_global->max_recv_bytes >= + stream_parsing->incoming_window_delta); + stream_global->max_recv_bytes -= + stream_parsing->incoming_window_delta; + stream_parsing->incoming_window_delta = 0; + grpc_chttp2_list_add_writable_window_update_stream(transport_global, + stream_global); + } + + /* update outgoing flow control window */ + if (stream_parsing->outgoing_window_update) { + int was_zero = stream_global->outgoing_window <= 0; + int is_zero; + GRPC_CHTTP2_FLOWCTL_TRACE_STREAM("parsed", transport_parsing, + stream_global, outgoing_window, + stream_parsing->outgoing_window_update); + GRPC_CHTTP2_FLOWCTL_TRACE_STREAM( + "parsed", transport_parsing, stream_parsing, outgoing_window_update, + -(gpr_int64)stream_parsing->outgoing_window_update); + stream_global->outgoing_window += stream_parsing->outgoing_window_update; + stream_parsing->outgoing_window_update = 0; + is_zero = stream_global->outgoing_window <= 0; + if (was_zero && !is_zero) { + grpc_chttp2_list_add_writable_stream(transport_global, stream_global); + } + } + + /* updating closed status */ + if (stream_parsing->received_close) { + stream_global->read_closed = 1; + grpc_chttp2_list_add_read_write_state_changed(transport_global, + stream_global); + } + if (stream_parsing->saw_rst_stream) { + stream_global->cancelled = 1; + stream_global->cancelled_status = grpc_chttp2_http2_error_to_grpc_status(stream_parsing->rst_stream_reason); + if (stream_parsing->rst_stream_reason == GRPC_CHTTP2_NO_ERROR) { + stream_global->published_cancelled = 1; + } + grpc_chttp2_list_add_read_write_state_changed(transport_global, + stream_global); + } + + /* publish incoming stream ops */ + if (stream_parsing->data_parser.incoming_sopb.nops > 0) { + grpc_incoming_metadata_buffer_move_to_referencing_sopb( + &stream_parsing->incoming_metadata, &stream_global->incoming_metadata, + &stream_parsing->data_parser.incoming_sopb); + grpc_sopb_move_to(&stream_parsing->data_parser.incoming_sopb, + &stream_global->incoming_sopb); + grpc_chttp2_list_add_read_write_state_changed(transport_global, + stream_global); + } + } +} + +int grpc_chttp2_perform_read(grpc_chttp2_transport_parsing *transport_parsing, + gpr_slice slice) { + gpr_uint8 *beg = GPR_SLICE_START_PTR(slice); + gpr_uint8 *end = GPR_SLICE_END_PTR(slice); + gpr_uint8 *cur = beg; + + if (cur == end) return 1; + + switch (transport_parsing->deframe_state) { + case GRPC_DTS_CLIENT_PREFIX_0: + case GRPC_DTS_CLIENT_PREFIX_1: + case GRPC_DTS_CLIENT_PREFIX_2: + case GRPC_DTS_CLIENT_PREFIX_3: + case GRPC_DTS_CLIENT_PREFIX_4: + case GRPC_DTS_CLIENT_PREFIX_5: + case GRPC_DTS_CLIENT_PREFIX_6: + case GRPC_DTS_CLIENT_PREFIX_7: + case GRPC_DTS_CLIENT_PREFIX_8: + case GRPC_DTS_CLIENT_PREFIX_9: + case GRPC_DTS_CLIENT_PREFIX_10: + case GRPC_DTS_CLIENT_PREFIX_11: + case GRPC_DTS_CLIENT_PREFIX_12: + case GRPC_DTS_CLIENT_PREFIX_13: + case GRPC_DTS_CLIENT_PREFIX_14: + case GRPC_DTS_CLIENT_PREFIX_15: + case GRPC_DTS_CLIENT_PREFIX_16: + case GRPC_DTS_CLIENT_PREFIX_17: + case GRPC_DTS_CLIENT_PREFIX_18: + case GRPC_DTS_CLIENT_PREFIX_19: + case GRPC_DTS_CLIENT_PREFIX_20: + case GRPC_DTS_CLIENT_PREFIX_21: + case GRPC_DTS_CLIENT_PREFIX_22: + case GRPC_DTS_CLIENT_PREFIX_23: + while (cur != end && transport_parsing->deframe_state != GRPC_DTS_FH_0) { + if (*cur != GRPC_CHTTP2_CLIENT_CONNECT_STRING[transport_parsing + ->deframe_state]) { + gpr_log(GPR_INFO, + "Connect string mismatch: expected '%c' (%d) got '%c' (%d) " + "at byte %d", + GRPC_CHTTP2_CLIENT_CONNECT_STRING[transport_parsing + ->deframe_state], + (int)(gpr_uint8)GRPC_CHTTP2_CLIENT_CONNECT_STRING + [transport_parsing->deframe_state], + *cur, (int)*cur, transport_parsing->deframe_state); + return 0; + } + ++cur; + ++transport_parsing->deframe_state; + } + if (cur == end) { + return 1; + } + /* fallthrough */ + dts_fh_0: + case GRPC_DTS_FH_0: + GPR_ASSERT(cur < end); + transport_parsing->incoming_frame_size = ((gpr_uint32)*cur) << 16; + if (++cur == end) { + transport_parsing->deframe_state = GRPC_DTS_FH_1; + return 1; + } + /* fallthrough */ + case GRPC_DTS_FH_1: + GPR_ASSERT(cur < end); + transport_parsing->incoming_frame_size |= ((gpr_uint32)*cur) << 8; + if (++cur == end) { + transport_parsing->deframe_state = GRPC_DTS_FH_2; + return 1; + } + /* fallthrough */ + case GRPC_DTS_FH_2: + GPR_ASSERT(cur < end); + transport_parsing->incoming_frame_size |= *cur; + if (++cur == end) { + transport_parsing->deframe_state = GRPC_DTS_FH_3; + return 1; + } + /* fallthrough */ + case GRPC_DTS_FH_3: + GPR_ASSERT(cur < end); + transport_parsing->incoming_frame_type = *cur; + if (++cur == end) { + transport_parsing->deframe_state = GRPC_DTS_FH_4; + return 1; + } + /* fallthrough */ + case GRPC_DTS_FH_4: + GPR_ASSERT(cur < end); + transport_parsing->incoming_frame_flags = *cur; + if (++cur == end) { + transport_parsing->deframe_state = GRPC_DTS_FH_5; + return 1; + } + /* fallthrough */ + case GRPC_DTS_FH_5: + GPR_ASSERT(cur < end); + transport_parsing->incoming_stream_id = (((gpr_uint32)*cur) & 0x7f) << 24; + if (++cur == end) { + transport_parsing->deframe_state = GRPC_DTS_FH_6; + return 1; + } + /* fallthrough */ + case GRPC_DTS_FH_6: + GPR_ASSERT(cur < end); + transport_parsing->incoming_stream_id |= ((gpr_uint32)*cur) << 16; + if (++cur == end) { + transport_parsing->deframe_state = GRPC_DTS_FH_7; + return 1; + } + /* fallthrough */ + case GRPC_DTS_FH_7: + GPR_ASSERT(cur < end); + transport_parsing->incoming_stream_id |= ((gpr_uint32)*cur) << 8; + if (++cur == end) { + transport_parsing->deframe_state = GRPC_DTS_FH_8; + return 1; + } + /* fallthrough */ + case GRPC_DTS_FH_8: + GPR_ASSERT(cur < end); + transport_parsing->incoming_stream_id |= ((gpr_uint32)*cur); + transport_parsing->deframe_state = GRPC_DTS_FRAME; + if (!init_frame_parser(transport_parsing)) { + return 0; + } + if (transport_parsing->incoming_stream_id) { + transport_parsing->last_incoming_stream_id = + transport_parsing->incoming_stream_id; + } + if (transport_parsing->incoming_frame_size == 0) { + if (!parse_frame_slice(transport_parsing, gpr_empty_slice(), 1)) { + return 0; + } + transport_parsing->incoming_stream = NULL; + if (++cur == end) { + transport_parsing->deframe_state = GRPC_DTS_FH_0; + return 1; + } + goto dts_fh_0; /* loop */ + } + if (++cur == end) { + return 1; + } + /* fallthrough */ + case GRPC_DTS_FRAME: + GPR_ASSERT(cur < end); + if ((gpr_uint32)(end - cur) == transport_parsing->incoming_frame_size) { + if (!parse_frame_slice( + transport_parsing, + gpr_slice_sub_no_ref(slice, cur - beg, end - beg), 1)) { + return 0; + } + transport_parsing->deframe_state = GRPC_DTS_FH_0; + transport_parsing->incoming_stream = NULL; + return 1; + } else if ((gpr_uint32)(end - cur) > + transport_parsing->incoming_frame_size) { + if (!parse_frame_slice( + transport_parsing, + gpr_slice_sub_no_ref( + slice, cur - beg, + cur + transport_parsing->incoming_frame_size - beg), + 1)) { + return 0; + } + cur += transport_parsing->incoming_frame_size; + transport_parsing->incoming_stream = NULL; + goto dts_fh_0; /* loop */ + } else { + if (!parse_frame_slice( + transport_parsing, + gpr_slice_sub_no_ref(slice, cur - beg, end - beg), 0)) { + return 0; + } + transport_parsing->incoming_frame_size -= (end - cur); + return 1; + } + gpr_log(GPR_ERROR, "should never reach here"); + abort(); + } + + gpr_log(GPR_ERROR, "should never reach here"); + abort(); + + return 0; +} + +static int init_frame_parser(grpc_chttp2_transport_parsing *transport_parsing) { + if (transport_parsing->expect_continuation_stream_id != 0) { + if (transport_parsing->incoming_frame_type != + GRPC_CHTTP2_FRAME_CONTINUATION) { + gpr_log(GPR_ERROR, "Expected CONTINUATION frame, got frame type %02x", + transport_parsing->incoming_frame_type); + return 0; + } + if (transport_parsing->expect_continuation_stream_id != + transport_parsing->incoming_stream_id) { + gpr_log(GPR_ERROR, + "Expected CONTINUATION frame for grpc_chttp2_stream %08x, got " + "grpc_chttp2_stream %08x", + transport_parsing->expect_continuation_stream_id, + transport_parsing->incoming_stream_id); + return 0; + } + return init_header_frame_parser(transport_parsing, 1); + } + switch (transport_parsing->incoming_frame_type) { + case GRPC_CHTTP2_FRAME_DATA: + return init_data_frame_parser(transport_parsing); + case GRPC_CHTTP2_FRAME_HEADER: + return init_header_frame_parser(transport_parsing, 0); + case GRPC_CHTTP2_FRAME_CONTINUATION: + gpr_log(GPR_ERROR, "Unexpected CONTINUATION frame"); + return 0; + case GRPC_CHTTP2_FRAME_RST_STREAM: + return init_rst_stream_parser(transport_parsing); + case GRPC_CHTTP2_FRAME_SETTINGS: + return init_settings_frame_parser(transport_parsing); + case GRPC_CHTTP2_FRAME_WINDOW_UPDATE: + return init_window_update_frame_parser(transport_parsing); + case GRPC_CHTTP2_FRAME_PING: + return init_ping_parser(transport_parsing); + case GRPC_CHTTP2_FRAME_GOAWAY: + return init_goaway_parser(transport_parsing); + default: + gpr_log(GPR_ERROR, "Unknown frame type %02x", + transport_parsing->incoming_frame_type); + return init_skip_frame_parser(transport_parsing, 0); + } +} + +static grpc_chttp2_parse_error skip_parser( + void *parser, grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) { + return GRPC_CHTTP2_PARSE_OK; +} + +static void skip_header(void *tp, grpc_mdelem *md) { grpc_mdelem_unref(md); } + +static int init_skip_frame_parser( + grpc_chttp2_transport_parsing *transport_parsing, int is_header) { + if (is_header) { + int is_eoh = transport_parsing->expect_continuation_stream_id != 0; + transport_parsing->parser = grpc_chttp2_header_parser_parse; + transport_parsing->parser_data = &transport_parsing->hpack_parser; + transport_parsing->hpack_parser.on_header = skip_header; + transport_parsing->hpack_parser.on_header_user_data = NULL; + transport_parsing->hpack_parser.is_boundary = is_eoh; + transport_parsing->hpack_parser.is_eof = + is_eoh ? transport_parsing->header_eof : 0; + } else { + transport_parsing->parser = skip_parser; + } + return 1; +} + +void grpc_chttp2_parsing_become_skip_parser( + grpc_chttp2_transport_parsing *transport_parsing) { + init_skip_frame_parser( + transport_parsing, + transport_parsing->parser == grpc_chttp2_header_parser_parse); +} + +static grpc_chttp2_parse_error update_incoming_window( + grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_parsing *stream_parsing) { + if (transport_parsing->incoming_frame_size > + transport_parsing->incoming_window) { + gpr_log(GPR_ERROR, "frame of size %d overflows incoming window of %d", + transport_parsing->incoming_frame_size, + transport_parsing->incoming_window); + return GRPC_CHTTP2_CONNECTION_ERROR; + } + + if (transport_parsing->incoming_frame_size > + stream_parsing->incoming_window) { + gpr_log(GPR_ERROR, "frame of size %d overflows incoming window of %d", + transport_parsing->incoming_frame_size, + stream_parsing->incoming_window); + return GRPC_CHTTP2_CONNECTION_ERROR; + } + + GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT( + "data", transport_parsing, incoming_window, + -(gpr_int64)transport_parsing->incoming_frame_size); + GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT("data", transport_parsing, + incoming_window_delta, + transport_parsing->incoming_frame_size); + GRPC_CHTTP2_FLOWCTL_TRACE_STREAM( + "data", transport_parsing, stream_parsing, incoming_window, + -(gpr_int64)transport_parsing->incoming_frame_size); + GRPC_CHTTP2_FLOWCTL_TRACE_STREAM("data", transport_parsing, stream_parsing, + incoming_window_delta, + transport_parsing->incoming_frame_size); + + transport_parsing->incoming_window -= transport_parsing->incoming_frame_size; + transport_parsing->incoming_window_delta += + transport_parsing->incoming_frame_size; + stream_parsing->incoming_window -= transport_parsing->incoming_frame_size; + stream_parsing->incoming_window_delta += + transport_parsing->incoming_frame_size; + grpc_chttp2_list_add_parsing_seen_stream(transport_parsing, stream_parsing); + + return GRPC_CHTTP2_PARSE_OK; +} + +static int init_data_frame_parser( + grpc_chttp2_transport_parsing *transport_parsing) { + grpc_chttp2_stream_parsing *stream_parsing = + grpc_chttp2_parsing_lookup_stream(transport_parsing, + transport_parsing->incoming_stream_id); + grpc_chttp2_parse_error err = GRPC_CHTTP2_PARSE_OK; + if (!stream_parsing || stream_parsing->received_close) + return init_skip_frame_parser(transport_parsing, 0); + if (err == GRPC_CHTTP2_PARSE_OK) { + err = update_incoming_window(transport_parsing, stream_parsing); + } + if (err == GRPC_CHTTP2_PARSE_OK) { + err = grpc_chttp2_data_parser_begin_frame( + &stream_parsing->data_parser, transport_parsing->incoming_frame_flags); + } + switch (err) { + case GRPC_CHTTP2_PARSE_OK: + transport_parsing->incoming_stream = stream_parsing; + transport_parsing->parser = grpc_chttp2_data_parser_parse; + transport_parsing->parser_data = &stream_parsing->data_parser; + return 1; + case GRPC_CHTTP2_STREAM_ERROR: + stream_parsing->received_close = 1; + stream_parsing->saw_rst_stream = 1; + stream_parsing->rst_stream_reason = GRPC_CHTTP2_PROTOCOL_ERROR; + gpr_slice_buffer_add( + &transport_parsing->qbuf, + grpc_chttp2_rst_stream_create(transport_parsing->incoming_stream_id, + GRPC_CHTTP2_PROTOCOL_ERROR)); + return init_skip_frame_parser(transport_parsing, 0); + case GRPC_CHTTP2_CONNECTION_ERROR: + return 0; + } + gpr_log(GPR_ERROR, "should never reach here"); + abort(); + return 0; +} + +static void free_timeout(void *p) { gpr_free(p); } + +static void on_header(void *tp, grpc_mdelem *md) { + grpc_chttp2_transport_parsing *transport_parsing = tp; + grpc_chttp2_stream_parsing *stream_parsing = + transport_parsing->incoming_stream; + + GPR_ASSERT(stream_parsing); + + GRPC_CHTTP2_IF_TRACING(gpr_log( + GPR_INFO, "HTTP:%d:HDR: %s: %s", stream_parsing->id, + transport_parsing->is_client ? "CLI" : "SVR", + grpc_mdstr_as_c_string(md->key), grpc_mdstr_as_c_string(md->value))); + + if (md->key == transport_parsing->str_grpc_timeout) { + gpr_timespec *cached_timeout = grpc_mdelem_get_user_data(md, free_timeout); + if (!cached_timeout) { + /* not already parsed: parse it now, and store the result away */ + cached_timeout = gpr_malloc(sizeof(gpr_timespec)); + if (!grpc_chttp2_decode_timeout(grpc_mdstr_as_c_string(md->value), + cached_timeout)) { + gpr_log(GPR_ERROR, "Ignoring bad timeout value '%s'", + grpc_mdstr_as_c_string(md->value)); + *cached_timeout = gpr_inf_future; + } + grpc_mdelem_set_user_data(md, free_timeout, cached_timeout); + } + grpc_chttp2_incoming_metadata_buffer_set_deadline( + &stream_parsing->incoming_metadata, + gpr_time_add(gpr_now(), *cached_timeout)); + grpc_mdelem_unref(md); + } else { + grpc_chttp2_incoming_metadata_buffer_add(&stream_parsing->incoming_metadata, + md); + } + + grpc_chttp2_list_add_parsing_seen_stream(transport_parsing, stream_parsing); +} + +static int init_header_frame_parser( + grpc_chttp2_transport_parsing *transport_parsing, int is_continuation) { + int is_eoh = (transport_parsing->incoming_frame_flags & + GRPC_CHTTP2_DATA_FLAG_END_HEADERS) != 0; + int via_accept = 0; + grpc_chttp2_stream_parsing *stream_parsing; + + if (is_eoh) { + transport_parsing->expect_continuation_stream_id = 0; + } else { + transport_parsing->expect_continuation_stream_id = + transport_parsing->incoming_stream_id; + } + + if (!is_continuation) { + transport_parsing->header_eof = (transport_parsing->incoming_frame_flags & + GRPC_CHTTP2_DATA_FLAG_END_STREAM) != 0; + } + + /* could be a new grpc_chttp2_stream or an existing grpc_chttp2_stream */ + stream_parsing = grpc_chttp2_parsing_lookup_stream( + transport_parsing, transport_parsing->incoming_stream_id); + if (stream_parsing == NULL) { + if (is_continuation) { + gpr_log(GPR_ERROR, + "grpc_chttp2_stream disbanded before CONTINUATION received"); + return init_skip_frame_parser(transport_parsing, 1); + } + if (transport_parsing->is_client) { + if ((transport_parsing->incoming_stream_id & 1) && + transport_parsing->incoming_stream_id < + transport_parsing->next_stream_id) { + /* this is an old (probably cancelled) grpc_chttp2_stream */ + } else { + gpr_log(GPR_ERROR, + "ignoring new grpc_chttp2_stream creation on client"); + } + return init_skip_frame_parser(transport_parsing, 1); + } else if (transport_parsing->last_incoming_stream_id > + transport_parsing->incoming_stream_id) { + gpr_log(GPR_ERROR, + "ignoring out of order new grpc_chttp2_stream request on server; " + "last grpc_chttp2_stream " + "id=%d, new grpc_chttp2_stream id=%d", + transport_parsing->last_incoming_stream_id, + transport_parsing->incoming_stream_id); + return init_skip_frame_parser(transport_parsing, 1); + } else if ((transport_parsing->incoming_stream_id & 1) == 0) { + gpr_log(GPR_ERROR, + "ignoring grpc_chttp2_stream with non-client generated index %d", + transport_parsing->incoming_stream_id); + return init_skip_frame_parser(transport_parsing, 1); + } + stream_parsing = transport_parsing->incoming_stream = + grpc_chttp2_parsing_accept_stream( + transport_parsing, transport_parsing->incoming_stream_id); + if (stream_parsing == NULL) { + gpr_log(GPR_ERROR, "grpc_chttp2_stream not accepted"); + return init_skip_frame_parser(transport_parsing, 1); + } + via_accept = 1; + } else { + transport_parsing->incoming_stream = stream_parsing; + } + GPR_ASSERT(stream_parsing != NULL && (via_accept == 0 || via_accept == 1)); + if (stream_parsing->received_close) { + gpr_log(GPR_ERROR, "skipping already closed grpc_chttp2_stream header"); + transport_parsing->incoming_stream = NULL; + return init_skip_frame_parser(transport_parsing, 1); + } + transport_parsing->parser = grpc_chttp2_header_parser_parse; + transport_parsing->parser_data = &transport_parsing->hpack_parser; + transport_parsing->hpack_parser.on_header = on_header; + transport_parsing->hpack_parser.on_header_user_data = transport_parsing; + transport_parsing->hpack_parser.is_boundary = is_eoh; + transport_parsing->hpack_parser.is_eof = + is_eoh ? transport_parsing->header_eof : 0; + if (!is_continuation && (transport_parsing->incoming_frame_flags & + GRPC_CHTTP2_FLAG_HAS_PRIORITY)) { + grpc_chttp2_hpack_parser_set_has_priority(&transport_parsing->hpack_parser); + } + return 1; +} + +static int init_window_update_frame_parser( + grpc_chttp2_transport_parsing *transport_parsing) { + int ok = GRPC_CHTTP2_PARSE_OK == grpc_chttp2_window_update_parser_begin_frame( + &transport_parsing->simple.window_update, + transport_parsing->incoming_frame_size, + transport_parsing->incoming_frame_flags); + if (transport_parsing->incoming_stream_id) { + transport_parsing->incoming_stream = grpc_chttp2_parsing_lookup_stream( + transport_parsing, transport_parsing->incoming_stream_id); + } + transport_parsing->parser = grpc_chttp2_window_update_parser_parse; + transport_parsing->parser_data = &transport_parsing->simple.window_update; + return ok; +} + +static int init_ping_parser(grpc_chttp2_transport_parsing *transport_parsing) { + int ok = GRPC_CHTTP2_PARSE_OK == grpc_chttp2_ping_parser_begin_frame( + &transport_parsing->simple.ping, + transport_parsing->incoming_frame_size, + transport_parsing->incoming_frame_flags); + transport_parsing->parser = grpc_chttp2_ping_parser_parse; + transport_parsing->parser_data = &transport_parsing->simple.ping; + return ok; +} + +static int init_rst_stream_parser( + grpc_chttp2_transport_parsing *transport_parsing) { + int ok = GRPC_CHTTP2_PARSE_OK == grpc_chttp2_rst_stream_parser_begin_frame( + &transport_parsing->simple.rst_stream, + transport_parsing->incoming_frame_size, + transport_parsing->incoming_frame_flags); + transport_parsing->incoming_stream = grpc_chttp2_parsing_lookup_stream( + transport_parsing, transport_parsing->incoming_stream_id); + if (!transport_parsing->incoming_stream) { + return init_skip_frame_parser(transport_parsing, 0); + } + transport_parsing->parser = grpc_chttp2_rst_stream_parser_parse; + transport_parsing->parser_data = &transport_parsing->simple.rst_stream; + return ok; +} + +static int init_goaway_parser( + grpc_chttp2_transport_parsing *transport_parsing) { + int ok = GRPC_CHTTP2_PARSE_OK == grpc_chttp2_goaway_parser_begin_frame( + &transport_parsing->goaway_parser, + transport_parsing->incoming_frame_size, + transport_parsing->incoming_frame_flags); + transport_parsing->parser = grpc_chttp2_goaway_parser_parse; + transport_parsing->parser_data = &transport_parsing->goaway_parser; + return ok; +} + +static int init_settings_frame_parser( + grpc_chttp2_transport_parsing *transport_parsing) { + int ok; + + if (transport_parsing->incoming_stream_id != 0) { + gpr_log(GPR_ERROR, "settings frame received for grpc_chttp2_stream %d", + transport_parsing->incoming_stream_id); + return 0; + } + + ok = GRPC_CHTTP2_PARSE_OK == grpc_chttp2_settings_parser_begin_frame( + &transport_parsing->simple.settings, + transport_parsing->incoming_frame_size, + transport_parsing->incoming_frame_flags, + transport_parsing->settings); + if (!ok) { + return 0; + } + if (transport_parsing->incoming_frame_flags & GRPC_CHTTP2_FLAG_ACK) { + transport_parsing->settings_ack_received = 1; + } + transport_parsing->parser = grpc_chttp2_settings_parser_parse; + transport_parsing->parser_data = &transport_parsing->simple.settings; + return ok; +} + +/* +static int is_window_update_legal(gpr_int64 window_update, gpr_int64 window) { + return window + window_update < MAX_WINDOW; +} +*/ + +static int parse_frame_slice(grpc_chttp2_transport_parsing *transport_parsing, + gpr_slice slice, int is_last) { + grpc_chttp2_stream_parsing *stream_parsing = + transport_parsing->incoming_stream; + switch (transport_parsing->parser(transport_parsing->parser_data, + transport_parsing, stream_parsing, slice, + is_last)) { + case GRPC_CHTTP2_PARSE_OK: + if (stream_parsing) { + grpc_chttp2_list_add_parsing_seen_stream(transport_parsing, + stream_parsing); + } + return 1; + case GRPC_CHTTP2_STREAM_ERROR: + grpc_chttp2_parsing_become_skip_parser(transport_parsing); + if (stream_parsing) { + stream_parsing->saw_rst_stream = 1; + stream_parsing->rst_stream_reason = GRPC_CHTTP2_PROTOCOL_ERROR; + gpr_slice_buffer_add( + &transport_parsing->qbuf, + grpc_chttp2_rst_stream_create(transport_parsing->incoming_stream_id, + GRPC_CHTTP2_PROTOCOL_ERROR)); + } + return 1; + case GRPC_CHTTP2_CONNECTION_ERROR: + return 0; + } + gpr_log(GPR_ERROR, "should never reach here"); + abort(); + return 0; +} diff --git a/src/core/transport/chttp2/status_conversion.h b/src/core/transport/chttp2/status_conversion.h index cf06c3576e..0ec5b560b8 100644 --- a/src/core/transport/chttp2/status_conversion.h +++ b/src/core/transport/chttp2/status_conversion.h @@ -47,4 +47,4 @@ grpc_status_code grpc_chttp2_http2_error_to_grpc_status( grpc_status_code grpc_chttp2_http2_status_to_grpc_status(int status); int grpc_chttp2_grpc_status_to_http2_status(grpc_status_code status); -#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_STATUS_CONVERSION_H */ +#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_STATUS_CONVERSION_H */ diff --git a/src/core/transport/chttp2/stream_encoder.h b/src/core/transport/chttp2/stream_encoder.h index 50c58ad5ca..db52f2a0f6 100644 --- a/src/core/transport/chttp2/stream_encoder.h +++ b/src/core/transport/chttp2/stream_encoder.h @@ -90,4 +90,4 @@ void grpc_chttp2_encode(grpc_stream_op *ops, size_t ops_count, int eof, grpc_chttp2_hpack_compressor *compressor, gpr_slice_buffer *output); -#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_STREAM_ENCODER_H */ +#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_STREAM_ENCODER_H */ diff --git a/src/core/transport/chttp2/stream_lists.c b/src/core/transport/chttp2/stream_lists.c new file mode 100644 index 0000000000..c6ba12fca8 --- /dev/null +++ b/src/core/transport/chttp2/stream_lists.c @@ -0,0 +1,352 @@ +/* + * + * 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 "src/core/transport/chttp2/internal.h" + +#include <grpc/support/log.h> + +#define TRANSPORT_FROM_GLOBAL(tg) \ + ((grpc_chttp2_transport *)((char *)(tg)-offsetof(grpc_chttp2_transport, \ + global))) + +#define STREAM_FROM_GLOBAL(sg) \ + ((grpc_chttp2_stream *)((char *)(sg)-offsetof(grpc_chttp2_stream, global))) + +#define TRANSPORT_FROM_WRITING(tw) \ + ((grpc_chttp2_transport *)((char *)(tw)-offsetof(grpc_chttp2_transport, \ + writing))) + +#define STREAM_FROM_WRITING(sw) \ + ((grpc_chttp2_stream *)((char *)(sw)-offsetof(grpc_chttp2_stream, writing))) + +#define TRANSPORT_FROM_PARSING(tp) \ + ((grpc_chttp2_transport *)((char *)(tp)-offsetof(grpc_chttp2_transport, \ + parsing))) + +#define STREAM_FROM_PARSING(sp) \ + ((grpc_chttp2_stream *)((char *)(sp)-offsetof(grpc_chttp2_stream, parsing))) + +/* core list management */ + +static int stream_list_empty(grpc_chttp2_transport *t, + grpc_chttp2_stream_list_id id) { + return t->lists[id].head == NULL; +} + +static int stream_list_pop(grpc_chttp2_transport *t, + grpc_chttp2_stream **stream, + grpc_chttp2_stream_list_id id) { + grpc_chttp2_stream *s = t->lists[id].head; + if (s) { + grpc_chttp2_stream *new_head = s->links[id].next; + GPR_ASSERT(s->included[id]); + if (new_head) { + t->lists[id].head = new_head; + new_head->links[id].prev = NULL; + } else { + t->lists[id].head = NULL; + t->lists[id].tail = NULL; + } + s->included[id] = 0; + } + *stream = s; + return s != 0; +} + +static void stream_list_remove(grpc_chttp2_transport *t, grpc_chttp2_stream *s, + grpc_chttp2_stream_list_id id) { + GPR_ASSERT(s->included[id]); + s->included[id] = 0; + if (s->links[id].prev) { + s->links[id].prev->links[id].next = s->links[id].next; + } else { + GPR_ASSERT(t->lists[id].head == s); + t->lists[id].head = s->links[id].next; + } + if (s->links[id].next) { + s->links[id].next->links[id].prev = s->links[id].prev; + } else { + t->lists[id].tail = s->links[id].prev; + } +} + +static void stream_list_maybe_remove(grpc_chttp2_transport *t, + grpc_chttp2_stream *s, + grpc_chttp2_stream_list_id id) { + if (s->included[id]) { + stream_list_remove(t, s, id); + } +} + +static void stream_list_add_tail(grpc_chttp2_transport *t, + grpc_chttp2_stream *s, + grpc_chttp2_stream_list_id id) { + grpc_chttp2_stream *old_tail; + GPR_ASSERT(!s->included[id]); + old_tail = t->lists[id].tail; + s->links[id].next = NULL; + s->links[id].prev = old_tail; + if (old_tail) { + old_tail->links[id].next = s; + } else { + s->links[id].prev = NULL; + t->lists[id].head = s; + } + t->lists[id].tail = s; + s->included[id] = 1; +} + +static void stream_list_add(grpc_chttp2_transport *t, grpc_chttp2_stream *s, + grpc_chttp2_stream_list_id id) { + if (s->included[id]) { + return; + } + stream_list_add_tail(t, s, id); +} + +/* wrappers for specializations */ + +void grpc_chttp2_list_add_writable_stream( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global *stream_global) { + stream_list_add(TRANSPORT_FROM_GLOBAL(transport_global), + STREAM_FROM_GLOBAL(stream_global), GRPC_CHTTP2_LIST_WRITABLE); +} + +int grpc_chttp2_list_pop_writable_stream( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_transport_writing *transport_writing, + grpc_chttp2_stream_global **stream_global, + grpc_chttp2_stream_writing **stream_writing) { + grpc_chttp2_stream *stream; + int r = stream_list_pop(TRANSPORT_FROM_GLOBAL(transport_global), &stream, + GRPC_CHTTP2_LIST_WRITABLE); + *stream_global = &stream->global; + *stream_writing = &stream->writing; + return r; +} + +void grpc_chttp2_list_add_writing_stream( + grpc_chttp2_transport_writing *transport_writing, + grpc_chttp2_stream_writing *stream_writing) { + stream_list_add(TRANSPORT_FROM_WRITING(transport_writing), + STREAM_FROM_WRITING(stream_writing), + GRPC_CHTTP2_LIST_WRITING); +} + +int grpc_chttp2_list_have_writing_streams( + grpc_chttp2_transport_writing *transport_writing) { + return !stream_list_empty(TRANSPORT_FROM_WRITING(transport_writing), + GRPC_CHTTP2_LIST_WRITING); +} + +int grpc_chttp2_list_pop_writing_stream( + grpc_chttp2_transport_writing *transport_writing, + grpc_chttp2_stream_writing **stream_writing) { + grpc_chttp2_stream *stream; + int r = stream_list_pop(TRANSPORT_FROM_WRITING(transport_writing), &stream, + GRPC_CHTTP2_LIST_WRITING); + *stream_writing = &stream->writing; + return r; +} + +void grpc_chttp2_list_add_written_stream( + grpc_chttp2_transport_writing *transport_writing, + grpc_chttp2_stream_writing *stream_writing) { + stream_list_add(TRANSPORT_FROM_WRITING(transport_writing), + STREAM_FROM_WRITING(stream_writing), + GRPC_CHTTP2_LIST_WRITTEN); +} + +int grpc_chttp2_list_pop_written_stream( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_transport_writing *transport_writing, + grpc_chttp2_stream_global **stream_global, + grpc_chttp2_stream_writing **stream_writing) { + grpc_chttp2_stream *stream; + int r = stream_list_pop(TRANSPORT_FROM_WRITING(transport_writing), &stream, + GRPC_CHTTP2_LIST_WRITTEN); + *stream_global = &stream->global; + *stream_writing = &stream->writing; + return r; +} + +void grpc_chttp2_list_add_writable_window_update_stream( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global *stream_global) { + stream_list_add(TRANSPORT_FROM_GLOBAL(transport_global), + STREAM_FROM_GLOBAL(stream_global), + GRPC_CHTTP2_LIST_WRITABLE_WINDOW_UPDATE); +} + +int grpc_chttp2_list_pop_writable_window_update_stream( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global **stream_global) { + grpc_chttp2_stream *stream; + int r = stream_list_pop(TRANSPORT_FROM_GLOBAL(transport_global), &stream, + GRPC_CHTTP2_LIST_WRITABLE_WINDOW_UPDATE); + *stream_global = &stream->global; + return r; +} + +void grpc_chttp2_list_remove_writable_window_update_stream( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global *stream_global) { + stream_list_maybe_remove(TRANSPORT_FROM_GLOBAL(transport_global), STREAM_FROM_GLOBAL(stream_global), GRPC_CHTTP2_LIST_WRITABLE_WINDOW_UPDATE); +} + +void grpc_chttp2_list_add_parsing_seen_stream( + grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_parsing *stream_parsing) { + stream_list_add(TRANSPORT_FROM_PARSING(transport_parsing), + STREAM_FROM_PARSING(stream_parsing), + GRPC_CHTTP2_LIST_PARSING_SEEN); +} + +int grpc_chttp2_list_pop_parsing_seen_stream( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_global **stream_global, + grpc_chttp2_stream_parsing **stream_parsing) { + grpc_chttp2_stream *stream; + int r = stream_list_pop(TRANSPORT_FROM_PARSING(transport_parsing), &stream, + GRPC_CHTTP2_LIST_PARSING_SEEN); + *stream_global = &stream->global; + *stream_parsing = &stream->parsing; + return r; +} + +void grpc_chttp2_list_add_waiting_for_concurrency( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global *stream_global) { + stream_list_add(TRANSPORT_FROM_GLOBAL(transport_global), + STREAM_FROM_GLOBAL(stream_global), + GRPC_CHTTP2_LIST_WAITING_FOR_CONCURRENCY); +} + +int grpc_chttp2_list_pop_waiting_for_concurrency( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global **stream_global) { + grpc_chttp2_stream *stream; + int r = stream_list_pop(TRANSPORT_FROM_GLOBAL(transport_global), &stream, + GRPC_CHTTP2_LIST_WAITING_FOR_CONCURRENCY); + *stream_global = &stream->global; + return r; +} + +void grpc_chttp2_list_add_closed_waiting_for_parsing( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global *stream_global) { + stream_list_add(TRANSPORT_FROM_GLOBAL(transport_global), + STREAM_FROM_GLOBAL(stream_global), + GRPC_CHTTP2_LIST_CLOSED_WAITING_FOR_PARSING); +} + +int grpc_chttp2_list_pop_closed_waiting_for_parsing( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global **stream_global) { + grpc_chttp2_stream *stream; + int r = stream_list_pop(TRANSPORT_FROM_GLOBAL(transport_global), &stream, + GRPC_CHTTP2_LIST_CLOSED_WAITING_FOR_PARSING); + *stream_global = &stream->global; + return r; +} + +void grpc_chttp2_list_add_incoming_window_updated( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global *stream_global) { + stream_list_add(TRANSPORT_FROM_GLOBAL(transport_global), + STREAM_FROM_GLOBAL(stream_global), + GRPC_CHTTP2_LIST_INCOMING_WINDOW_UPDATED); +} + +int grpc_chttp2_list_pop_incoming_window_updated( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_global **stream_global, + grpc_chttp2_stream_parsing **stream_parsing) { + grpc_chttp2_stream *stream; + int r = stream_list_pop(TRANSPORT_FROM_GLOBAL(transport_global), &stream, + GRPC_CHTTP2_LIST_INCOMING_WINDOW_UPDATED); + *stream_global = &stream->global; + *stream_parsing = &stream->parsing; + return r; +} + +void grpc_chttp2_list_remove_incoming_window_updated( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global *stream_global) { + stream_list_maybe_remove(TRANSPORT_FROM_GLOBAL(transport_global), + STREAM_FROM_GLOBAL(stream_global), + GRPC_CHTTP2_LIST_INCOMING_WINDOW_UPDATED); +} + +void grpc_chttp2_list_add_read_write_state_changed( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global *stream_global) { + stream_list_add(TRANSPORT_FROM_GLOBAL(transport_global), + STREAM_FROM_GLOBAL(stream_global), + GRPC_CHTTP2_LIST_READ_WRITE_STATE_CHANGED); +} + +int grpc_chttp2_list_pop_read_write_state_changed( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global **stream_global) { + grpc_chttp2_stream *stream; + int r = stream_list_pop(TRANSPORT_FROM_GLOBAL(transport_global), &stream, + GRPC_CHTTP2_LIST_READ_WRITE_STATE_CHANGED); + *stream_global = &stream->global; + return r; +} + +void grpc_chttp2_register_stream(grpc_chttp2_transport *t, + grpc_chttp2_stream *s) { + stream_list_add_tail(t, s, GRPC_CHTTP2_LIST_ALL_STREAMS); +} + +void grpc_chttp2_unregister_stream(grpc_chttp2_transport *t, + grpc_chttp2_stream *s) { + stream_list_remove(t, s, GRPC_CHTTP2_LIST_ALL_STREAMS); +} + +void grpc_chttp2_for_all_streams( + grpc_chttp2_transport_global *transport_global, void *user_data, + void (*cb)(grpc_chttp2_transport_global *transport_global, void *user_data, + grpc_chttp2_stream_global *stream_global)) { + grpc_chttp2_stream *s; + grpc_chttp2_transport *t = TRANSPORT_FROM_GLOBAL(transport_global); + for (s = t->lists[GRPC_CHTTP2_LIST_ALL_STREAMS].head; s != NULL; + s = s->links[GRPC_CHTTP2_LIST_ALL_STREAMS].next) { + cb(transport_global, user_data, &s->global); + } +} diff --git a/src/core/transport/chttp2/stream_map.c b/src/core/transport/chttp2/stream_map.c index 580e32c582..0ec2f27291 100644 --- a/src/core/transport/chttp2/stream_map.c +++ b/src/core/transport/chttp2/stream_map.c @@ -32,8 +32,12 @@ */ #include "src/core/transport/chttp2/stream_map.h" + +#include <string.h> + #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/useful.h> void grpc_chttp2_stream_map_init(grpc_chttp2_stream_map *map, size_t initial_capacity) { @@ -92,6 +96,41 @@ void grpc_chttp2_stream_map_add(grpc_chttp2_stream_map *map, gpr_uint32 key, map->count = count + 1; } +void grpc_chttp2_stream_map_move_into(grpc_chttp2_stream_map *src, + grpc_chttp2_stream_map *dst) { + /* if src is empty we dont need to do anything */ + if (src->count == src->free) { + return; + } + /* if dst is empty we simply need to swap */ + if (dst->count == dst->free) { + GPR_SWAP(grpc_chttp2_stream_map, *src, *dst); + return; + } + /* the first element of src must be greater than the last of dst... + * however the maps may need compacting for this property to hold */ + if (src->keys[0] <= dst->keys[dst->count - 1]) { + src->count = compact(src->keys, src->values, src->count); + src->free = 0; + dst->count = compact(dst->keys, dst->values, dst->count); + dst->free = 0; + } + GPR_ASSERT(src->keys[0] > dst->keys[dst->count - 1]); + /* if dst doesn't have capacity, resize */ + if (dst->count + src->count > dst->capacity) { + dst->capacity = GPR_MAX(dst->capacity * 3 / 2, dst->count + src->count); + dst->keys = gpr_realloc(dst->keys, dst->capacity * sizeof(gpr_uint32)); + dst->values = gpr_realloc(dst->values, dst->capacity * sizeof(void *)); + } + memcpy(dst->keys + dst->count, src->keys, src->count * sizeof(gpr_uint32)); + memcpy(dst->values + dst->count, src->values, + src->count * sizeof(void*)); + dst->count += src->count; + dst->free += src->free; + src->count = 0; + src->free = 0; +} + static void **find(grpc_chttp2_stream_map *map, gpr_uint32 key) { size_t min_idx = 0; size_t max_idx = map->count; @@ -127,6 +166,11 @@ void *grpc_chttp2_stream_map_delete(grpc_chttp2_stream_map *map, out = *pvalue; *pvalue = NULL; map->free += (out != NULL); + /* recognize complete emptyness and ensure we can skip + * defragmentation later */ + if (map->free == map->count) { + map->free = map->count = 0; + } } return out; } diff --git a/src/core/transport/chttp2/stream_map.h b/src/core/transport/chttp2/stream_map.h index d338d2f892..71b0582054 100644 --- a/src/core/transport/chttp2/stream_map.h +++ b/src/core/transport/chttp2/stream_map.h @@ -66,6 +66,10 @@ void grpc_chttp2_stream_map_add(grpc_chttp2_stream_map *map, gpr_uint32 key, void *grpc_chttp2_stream_map_delete(grpc_chttp2_stream_map *map, gpr_uint32 key); +/* Move all elements of src into dst */ +void grpc_chttp2_stream_map_move_into(grpc_chttp2_stream_map *src, + grpc_chttp2_stream_map *dst); + /* Return an existing key, or NULL if it does not exist */ void *grpc_chttp2_stream_map_find(grpc_chttp2_stream_map *map, gpr_uint32 key); @@ -78,4 +82,4 @@ void grpc_chttp2_stream_map_for_each(grpc_chttp2_stream_map *map, void *value), void *user_data); -#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_STREAM_MAP_H */ +#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_STREAM_MAP_H */ diff --git a/src/core/transport/chttp2/timeout_encoding.h b/src/core/transport/chttp2/timeout_encoding.h index e6664c6262..9d8756e799 100644 --- a/src/core/transport/chttp2/timeout_encoding.h +++ b/src/core/transport/chttp2/timeout_encoding.h @@ -44,4 +44,4 @@ void grpc_chttp2_encode_timeout(gpr_timespec timeout, char *buffer); int grpc_chttp2_decode_timeout(const char *buffer, gpr_timespec *timeout); -#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_TIMEOUT_ENCODING_H */ +#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_TIMEOUT_ENCODING_H */ diff --git a/src/core/transport/chttp2/varint.h b/src/core/transport/chttp2/varint.h index ee04ed7fb2..0a6fb55248 100644 --- a/src/core/transport/chttp2/varint.h +++ b/src/core/transport/chttp2/varint.h @@ -56,19 +56,18 @@ void grpc_chttp2_hpack_write_varint_tail(gpr_uint32 tail_value, ((n) < GRPC_CHTTP2_MAX_IN_PREFIX(prefix_bits) \ ? 1 \ : grpc_chttp2_hpack_varint_length( \ - (n) - GRPC_CHTTP2_MAX_IN_PREFIX(prefix_bits))) + (n)-GRPC_CHTTP2_MAX_IN_PREFIX(prefix_bits))) -#define GRPC_CHTTP2_WRITE_VARINT(n, prefix_bits, prefix_or, target, length) \ - do { \ - gpr_uint8* tgt = target; \ - if ((length) == 1) { \ - (tgt)[0] = (prefix_or) | (n); \ - } else { \ - (tgt)[0] = (prefix_or) | GRPC_CHTTP2_MAX_IN_PREFIX(prefix_bits); \ - grpc_chttp2_hpack_write_varint_tail( \ - (n) - GRPC_CHTTP2_MAX_IN_PREFIX(prefix_bits), (tgt) + 1, \ - (length) - 1); \ - } \ +#define GRPC_CHTTP2_WRITE_VARINT(n, prefix_bits, prefix_or, target, length) \ + do { \ + gpr_uint8* tgt = target; \ + if ((length) == 1) { \ + (tgt)[0] = (prefix_or) | (n); \ + } else { \ + (tgt)[0] = (prefix_or) | GRPC_CHTTP2_MAX_IN_PREFIX(prefix_bits); \ + grpc_chttp2_hpack_write_varint_tail( \ + (n)-GRPC_CHTTP2_MAX_IN_PREFIX(prefix_bits), (tgt) + 1, (length)-1); \ + } \ } while (0) -#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_VARINT_H */ +#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_VARINT_H */ diff --git a/src/core/transport/chttp2/writing.c b/src/core/transport/chttp2/writing.c new file mode 100644 index 0000000000..ab50c5639d --- /dev/null +++ b/src/core/transport/chttp2/writing.c @@ -0,0 +1,215 @@ +/* + * + * 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 "src/core/transport/chttp2/internal.h" +#include "src/core/transport/chttp2/http2_errors.h" + +#include <grpc/support/log.h> + +static void finalize_outbuf(grpc_chttp2_transport_writing *transport_writing); +static void finish_write_cb(void *tw, grpc_endpoint_cb_status write_status); + +int grpc_chttp2_unlocking_check_writes( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_transport_writing *transport_writing) { + grpc_chttp2_stream_global *stream_global; + grpc_chttp2_stream_writing *stream_writing; + gpr_uint32 window_delta; + + /* simple writes are queued to qbuf, and flushed here */ + gpr_slice_buffer_swap(&transport_global->qbuf, &transport_writing->outbuf); + GPR_ASSERT(transport_global->qbuf.count == 0); + + if (transport_global->dirtied_local_settings && + !transport_global->sent_local_settings) { + gpr_slice_buffer_add( + &transport_writing->outbuf, + grpc_chttp2_settings_create( + transport_global->settings[GRPC_SENT_SETTINGS], + transport_global->settings[GRPC_LOCAL_SETTINGS], + transport_global->force_send_settings, GRPC_CHTTP2_NUM_SETTINGS)); + transport_global->force_send_settings = 0; + transport_global->dirtied_local_settings = 0; + transport_global->sent_local_settings = 1; + } + + /* for each grpc_chttp2_stream that's become writable, frame it's data + (according to + available window sizes) and add to the output buffer */ + while (transport_global->outgoing_window && + grpc_chttp2_list_pop_writable_stream(transport_global, + transport_writing, &stream_global, + &stream_writing) && + stream_global->outgoing_window > 0) { + stream_writing->id = stream_global->id; + window_delta = grpc_chttp2_preencode( + stream_global->outgoing_sopb->ops, &stream_global->outgoing_sopb->nops, + GPR_MIN(transport_global->outgoing_window, + stream_global->outgoing_window), + &stream_writing->sopb); + GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT( + "write", transport_global, outgoing_window, -(gpr_int64)window_delta); + GRPC_CHTTP2_FLOWCTL_TRACE_STREAM("write", transport_global, stream_global, + outgoing_window, -(gpr_int64)window_delta); + transport_global->outgoing_window -= window_delta; + stream_global->outgoing_window -= window_delta; + + if (stream_global->write_state == GRPC_WRITE_STATE_QUEUED_CLOSE && + stream_global->outgoing_sopb->nops == 0) { + if (!transport_global->is_client && !stream_global->read_closed) { + stream_writing->send_closed = GRPC_SEND_CLOSED_WITH_RST_STREAM; + } else { + stream_writing->send_closed = GRPC_SEND_CLOSED; + } + } + if (stream_writing->sopb.nops > 0 || + stream_writing->send_closed != GRPC_DONT_SEND_CLOSED) { + grpc_chttp2_list_add_writing_stream(transport_writing, stream_writing); + } + + /* we should either exhaust window or have no ops left, but not both */ + if (stream_global->outgoing_sopb->nops == 0) { + stream_global->outgoing_sopb = NULL; + grpc_chttp2_schedule_closure(transport_global, + stream_global->send_done_closure, 1); + } else if (stream_global->outgoing_window > 0) { + grpc_chttp2_list_add_writable_stream(transport_global, stream_global); + } + } + + /* for each grpc_chttp2_stream that wants to update its window, add that + * window here */ + while (grpc_chttp2_list_pop_writable_window_update_stream(transport_global, + &stream_global)) { + if (!stream_global->read_closed && stream_global->unannounced_incoming_window > 0) { + gpr_slice_buffer_add( + &transport_writing->outbuf, + grpc_chttp2_window_update_create( + stream_global->id, stream_global->unannounced_incoming_window)); + GRPC_CHTTP2_FLOWCTL_TRACE_STREAM("write", transport_global, stream_global, + incoming_window, stream_global->unannounced_incoming_window); + GRPC_CHTTP2_FLOWCTL_TRACE_STREAM("write", transport_global, stream_global, + unannounced_incoming_window, -(gpr_int64)stream_global->unannounced_incoming_window); + stream_global->incoming_window += stream_global->unannounced_incoming_window; + stream_global->unannounced_incoming_window = 0; + grpc_chttp2_list_add_incoming_window_updated(transport_global, + stream_global); + } + } + + /* if the grpc_chttp2_transport is ready to send a window update, do so here + also; 3/4 is a magic number that will likely get tuned soon */ + if (transport_global->incoming_window < + transport_global->connection_window_target * 3 / 4) { + window_delta = transport_global->connection_window_target - + transport_global->incoming_window; + gpr_slice_buffer_add(&transport_writing->outbuf, + grpc_chttp2_window_update_create(0, window_delta)); + GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT("write", transport_global, + incoming_window, window_delta); + transport_global->incoming_window += window_delta; + } + + return transport_writing->outbuf.count > 0 || + grpc_chttp2_list_have_writing_streams(transport_writing); +} + +void grpc_chttp2_perform_writes( + grpc_chttp2_transport_writing *transport_writing, grpc_endpoint *endpoint) { + GPR_ASSERT(transport_writing->outbuf.count > 0 || + grpc_chttp2_list_have_writing_streams(transport_writing)); + + finalize_outbuf(transport_writing); + + GPR_ASSERT(transport_writing->outbuf.count > 0); + GPR_ASSERT(endpoint); + + switch (grpc_endpoint_write(endpoint, transport_writing->outbuf.slices, + transport_writing->outbuf.count, finish_write_cb, + transport_writing)) { + case GRPC_ENDPOINT_WRITE_DONE: + grpc_chttp2_terminate_writing(transport_writing, 1); + break; + case GRPC_ENDPOINT_WRITE_ERROR: + grpc_chttp2_terminate_writing(transport_writing, 0); + break; + case GRPC_ENDPOINT_WRITE_PENDING: + break; + } +} + +static void finalize_outbuf(grpc_chttp2_transport_writing *transport_writing) { + grpc_chttp2_stream_writing *stream_writing; + + while ( + grpc_chttp2_list_pop_writing_stream(transport_writing, &stream_writing)) { + grpc_chttp2_encode(stream_writing->sopb.ops, stream_writing->sopb.nops, + stream_writing->send_closed != GRPC_DONT_SEND_CLOSED, + stream_writing->id, &transport_writing->hpack_compressor, + &transport_writing->outbuf); + stream_writing->sopb.nops = 0; + if (stream_writing->send_closed == GRPC_SEND_CLOSED_WITH_RST_STREAM) { + gpr_slice_buffer_add(&transport_writing->outbuf, + grpc_chttp2_rst_stream_create(stream_writing->id, + GRPC_CHTTP2_NO_ERROR)); + } + grpc_chttp2_list_add_written_stream(transport_writing, stream_writing); + } +} + +static void finish_write_cb(void *tw, grpc_endpoint_cb_status write_status) { + grpc_chttp2_transport_writing *transport_writing = tw; + grpc_chttp2_terminate_writing(transport_writing, + write_status == GRPC_ENDPOINT_CB_OK); +} + +void grpc_chttp2_cleanup_writing( + grpc_chttp2_transport_global *transport_global, + grpc_chttp2_transport_writing *transport_writing) { + grpc_chttp2_stream_writing *stream_writing; + grpc_chttp2_stream_global *stream_global; + + while (grpc_chttp2_list_pop_written_stream( + transport_global, transport_writing, &stream_global, &stream_writing)) { + if (stream_writing->send_closed != GRPC_DONT_SEND_CLOSED) { + stream_global->write_state = GRPC_WRITE_STATE_SENT_CLOSE; + if (!transport_global->is_client) { + stream_global->read_closed = 1; + } + grpc_chttp2_list_add_read_write_state_changed(transport_global, + stream_global); + } + } + transport_writing->outbuf.count = 0; + transport_writing->outbuf.length = 0; +} diff --git a/src/core/transport/chttp2_transport.c b/src/core/transport/chttp2_transport.c index c85eb96a6f..0307cc3154 100644 --- a/src/core/transport/chttp2_transport.c +++ b/src/core/transport/chttp2_transport.c @@ -39,22 +39,15 @@ #include "src/core/profiling/timers.h" #include "src/core/support/string.h" -#include "src/core/transport/chttp2/frame_data.h" -#include "src/core/transport/chttp2/frame_goaway.h" -#include "src/core/transport/chttp2/frame_ping.h" -#include "src/core/transport/chttp2/frame_rst_stream.h" -#include "src/core/transport/chttp2/frame_settings.h" -#include "src/core/transport/chttp2/frame_window_update.h" -#include "src/core/transport/chttp2/hpack_parser.h" #include "src/core/transport/chttp2/http2_errors.h" #include "src/core/transport/chttp2/status_conversion.h" -#include "src/core/transport/chttp2/stream_encoder.h" -#include "src/core/transport/chttp2/stream_map.h" #include "src/core/transport/chttp2/timeout_encoding.h" +#include "src/core/transport/chttp2/internal.h" #include "src/core/transport/transport_impl.h" #include <grpc/support/alloc.h> #include <grpc/support/log.h> #include <grpc/support/slice_buffer.h> +#include <grpc/support/string_util.h> #include <grpc/support/useful.h> #define DEFAULT_WINDOW 65535 @@ -63,430 +56,156 @@ #define MAX_CLIENT_STREAM_ID 0x7fffffffu -#define CLIENT_CONNECT_STRING "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" -#define CLIENT_CONNECT_STRLEN 24 - int grpc_http_trace = 0; int grpc_flowctl_trace = 0; -typedef struct transport transport; -typedef struct stream stream; - -#define IF_TRACING(stmt) \ - if (!(grpc_http_trace)) \ - ; \ - else \ - stmt - -#define FLOWCTL_TRACE(t, obj, dir, id, delta) \ - if (!grpc_flowctl_trace) \ - ; \ - else \ - flowctl_trace(t, #dir, obj->dir##_window, id, delta) - -/* streams are kept in various linked lists depending on what things need to - happen to them... this enum labels each list */ -typedef enum { - /* streams that have pending writes */ - WRITABLE = 0, - /* streams that have been selected to be written */ - WRITING, - /* streams that have just been written, and included a close */ - WRITTEN_CLOSED, - /* streams that have been cancelled and have some pending state updates - to perform */ - CANCELLED, - /* streams that want to send window updates */ - WINDOW_UPDATE, - /* streams that are waiting to start because there are too many concurrent - streams on the connection */ - WAITING_FOR_CONCURRENCY, - /* streams that have finished reading: we wait until unlock to coalesce - all changes into one callback */ - FINISHED_READ_OP, - STREAM_LIST_COUNT /* must be last */ -} stream_list_id; - -/* deframer state for the overall http2 stream of bytes */ -typedef enum { - /* prefix: one entry per http2 connection prefix byte */ - DTS_CLIENT_PREFIX_0 = 0, - DTS_CLIENT_PREFIX_1, - DTS_CLIENT_PREFIX_2, - DTS_CLIENT_PREFIX_3, - DTS_CLIENT_PREFIX_4, - DTS_CLIENT_PREFIX_5, - DTS_CLIENT_PREFIX_6, - DTS_CLIENT_PREFIX_7, - DTS_CLIENT_PREFIX_8, - DTS_CLIENT_PREFIX_9, - DTS_CLIENT_PREFIX_10, - DTS_CLIENT_PREFIX_11, - DTS_CLIENT_PREFIX_12, - DTS_CLIENT_PREFIX_13, - DTS_CLIENT_PREFIX_14, - DTS_CLIENT_PREFIX_15, - DTS_CLIENT_PREFIX_16, - DTS_CLIENT_PREFIX_17, - DTS_CLIENT_PREFIX_18, - DTS_CLIENT_PREFIX_19, - DTS_CLIENT_PREFIX_20, - DTS_CLIENT_PREFIX_21, - DTS_CLIENT_PREFIX_22, - DTS_CLIENT_PREFIX_23, - /* frame header byte 0... */ - /* must follow from the prefix states */ - DTS_FH_0, - DTS_FH_1, - DTS_FH_2, - DTS_FH_3, - DTS_FH_4, - DTS_FH_5, - DTS_FH_6, - DTS_FH_7, - /* ... frame header byte 8 */ - DTS_FH_8, - /* inside a http2 frame */ - DTS_FRAME -} deframe_transport_state; - -typedef enum { - WRITE_STATE_OPEN, - WRITE_STATE_QUEUED_CLOSE, - WRITE_STATE_SENT_CLOSE -} write_state; - -typedef enum { - DONT_SEND_CLOSED = 0, - SEND_CLOSED, - SEND_CLOSED_WITH_RST_STREAM -} send_closed; - -typedef struct { - stream *head; - stream *tail; -} stream_list; - -typedef struct { - stream *next; - stream *prev; -} stream_link; - -typedef enum { - ERROR_STATE_NONE, - ERROR_STATE_SEEN, - ERROR_STATE_NOTIFIED -} error_state; - -/* We keep several sets of connection wide parameters */ -typedef enum { - /* The settings our peer has asked for (and we have acked) */ - PEER_SETTINGS = 0, - /* The settings we'd like to have */ - LOCAL_SETTINGS, - /* The settings we've published to our peer */ - SENT_SETTINGS, - /* The settings the peer has acked */ - ACKED_SETTINGS, - NUM_SETTING_SETS -} setting_set; - -/* Outstanding ping request data */ -typedef struct { - gpr_uint8 id[8]; - void (*cb)(void *user_data); - void *user_data; -} outstanding_ping; +#define TRANSPORT_FROM_WRITING(tw) \ + ((grpc_chttp2_transport *)((char *)(tw)-offsetof(grpc_chttp2_transport, \ + writing))) -typedef struct { - grpc_status_code status; - gpr_slice debug; -} pending_goaway; +#define TRANSPORT_FROM_PARSING(tw) \ + ((grpc_chttp2_transport *)((char *)(tw)-offsetof(grpc_chttp2_transport, \ + parsing))) -typedef struct { - void (*cb)(void *user_data, int success); - void *user_data; - int success; -} op_closure; +#define TRANSPORT_FROM_GLOBAL(tg) \ + ((grpc_chttp2_transport *)((char *)(tg)-offsetof(grpc_chttp2_transport, \ + global))) -typedef struct { - op_closure *callbacks; - size_t count; - size_t capacity; -} op_closure_array; - -struct transport { - grpc_transport base; /* must be first */ - const grpc_transport_callbacks *cb; - void *cb_user_data; - grpc_endpoint *ep; - grpc_mdctx *metadata_context; - gpr_refcount refs; - gpr_uint8 is_client; - - gpr_mu mu; - gpr_cv cv; - - /* basic state management - what are we doing at the moment? */ - gpr_uint8 reading; - gpr_uint8 writing; - gpr_uint8 calling_back; - gpr_uint8 destroying; - gpr_uint8 closed; - error_state error_state; - - /* queued callbacks */ - op_closure_array pending_callbacks; - op_closure_array executing_callbacks; - - /* stream indexing */ - gpr_uint32 next_stream_id; - gpr_uint32 last_incoming_stream_id; - - /* settings */ - gpr_uint32 settings[NUM_SETTING_SETS][GRPC_CHTTP2_NUM_SETTINGS]; - gpr_uint32 force_send_settings; /* bitmask of setting indexes to send out */ - gpr_uint8 sent_local_settings; /* have local settings been sent? */ - gpr_uint8 dirtied_local_settings; /* are the local settings dirty? */ - - /* window management */ - gpr_uint32 outgoing_window; - gpr_uint32 incoming_window; - gpr_uint32 connection_window_target; - - /* deframing */ - deframe_transport_state deframe_state; - gpr_uint8 incoming_frame_type; - gpr_uint8 incoming_frame_flags; - gpr_uint8 header_eof; - gpr_uint32 expect_continuation_stream_id; - gpr_uint32 incoming_frame_size; - gpr_uint32 incoming_stream_id; - - /* hpack encoding */ - grpc_chttp2_hpack_compressor hpack_compressor; - - /* various parsers */ - grpc_chttp2_hpack_parser hpack_parser; - /* simple one shot parsers */ - union { - grpc_chttp2_window_update_parser window_update; - grpc_chttp2_settings_parser settings; - grpc_chttp2_ping_parser ping; - grpc_chttp2_rst_stream_parser rst_stream; - } simple_parsers; - - /* goaway */ - grpc_chttp2_goaway_parser goaway_parser; - pending_goaway *pending_goaways; - size_t num_pending_goaways; - size_t cap_pending_goaways; - - /* state for a stream that's not yet been created */ - grpc_stream_op_buffer new_stream_sopb; - - /* stream ops that need to be destroyed, but outside of the lock */ - grpc_stream_op_buffer nuke_later_sopb; - - /* active parser */ - void *parser_data; - stream *incoming_stream; - grpc_chttp2_parse_error (*parser)(void *parser_user_data, - grpc_chttp2_parse_state *state, - gpr_slice slice, int is_last); - - gpr_slice_buffer outbuf; - gpr_slice_buffer qbuf; - - stream_list lists[STREAM_LIST_COUNT]; - grpc_chttp2_stream_map stream_map; - - /* metadata object cache */ - grpc_mdstr *str_grpc_timeout; - - /* pings */ - outstanding_ping *pings; - size_t ping_count; - size_t ping_capacity; - gpr_int64 ping_counter; -}; - -struct stream { - gpr_uint32 id; - - /** The number of bytes the upper layers have offered to receive. - As the upper layer offers more bytes, this value increases. - As bytes are read, this value decreases. */ - gpr_uint32 max_recv_bytes; - /** The number of bytes the upper layer has offered to read but we have - not yet announced to HTTP2 flow control. - As the upper layers offer to read more bytes, this value increases. - As we advertise incoming flow control window, this value decreases. */ - gpr_uint32 unannounced_incoming_window; - /** The number of bytes of HTTP2 flow control we have advertised. - As we advertise incoming flow control window, this value increases. - As bytes are read, this value decreases. */ - gpr_uint32 incoming_window; - gpr_int64 outgoing_window; - /* when the application requests writes be closed, the write_closed is - 'queued'; when the close is flow controlled into the send path, we are - 'sending' it; when the write has been performed it is 'sent' */ - write_state write_state; - send_closed send_closed; - gpr_uint8 read_closed; - gpr_uint8 cancelled; - - op_closure send_done_closure; - op_closure recv_done_closure; - - stream_link links[STREAM_LIST_COUNT]; - gpr_uint8 included[STREAM_LIST_COUNT]; - - /* incoming metadata */ - grpc_linked_mdelem *incoming_metadata; - size_t incoming_metadata_count; - size_t incoming_metadata_capacity; - grpc_linked_mdelem *old_incoming_metadata; - gpr_timespec incoming_deadline; - - /* sops from application */ - grpc_stream_op_buffer *outgoing_sopb; - grpc_stream_op_buffer *incoming_sopb; - grpc_stream_state *publish_state; - grpc_stream_state published_state; - /* sops that have passed flow control to be written */ - grpc_stream_op_buffer writing_sopb; - - grpc_chttp2_data_parser parser; - - grpc_stream_state callback_state; - grpc_stream_op_buffer callback_sopb; -}; +#define STREAM_FROM_GLOBAL(sg) \ + ((grpc_chttp2_stream *)((char *)(sg)-offsetof(grpc_chttp2_stream, global))) static const grpc_transport_vtable vtable; -static void push_setting(transport *t, grpc_chttp2_setting_id id, - gpr_uint32 value); +static void lock(grpc_chttp2_transport *t); +static void unlock(grpc_chttp2_transport *t); -static int prepare_callbacks(transport *t); -static void run_callbacks(transport *t, const grpc_transport_callbacks *cb); -static void call_cb_closed(transport *t, const grpc_transport_callbacks *cb); +static void unlock_check_channel_callbacks(grpc_chttp2_transport *t); +static void unlock_check_read_write_state(grpc_chttp2_transport *t); -static int prepare_write(transport *t); -static void perform_write(transport *t, grpc_endpoint *ep); +/* forward declarations of various callbacks that we'll build closures around */ +static void writing_action(void *t, int iomgr_success_ignored); +static void reading_action(void *t, int iomgr_success_ignored); +static void notify_closed(void *t, int iomgr_success_ignored); -static void lock(transport *t); -static void unlock(transport *t); +/** Set a transport level setting, and push it to our peer */ +static void push_setting(grpc_chttp2_transport *t, grpc_chttp2_setting_id id, + gpr_uint32 value); -static void drop_connection(transport *t); -static void end_all_the_calls(transport *t); +/** Endpoint callback to process incoming data */ +static void recv_data(void *tp, gpr_slice *slices, size_t nslices, + grpc_endpoint_cb_status error); -static stream *stream_list_remove_head(transport *t, stream_list_id id); -static void stream_list_remove(transport *t, stream *s, stream_list_id id); -static void stream_list_add_tail(transport *t, stream *s, stream_list_id id); -static void stream_list_join(transport *t, stream *s, stream_list_id id); +/** Start disconnection chain */ +static void drop_connection(grpc_chttp2_transport *t); -static void cancel_stream_id(transport *t, gpr_uint32 id, - grpc_status_code local_status, - grpc_chttp2_error_code error_code, int send_rst); -static void cancel_stream(transport *t, stream *s, - grpc_status_code local_status, - grpc_chttp2_error_code error_code, - grpc_mdstr *optional_message, int send_rst); -static void finalize_cancellations(transport *t); -static stream *lookup_stream(transport *t, gpr_uint32 id); -static void remove_from_stream_map(transport *t, stream *s); -static void maybe_start_some_streams(transport *t); +/** Perform a transport_op */ +static void perform_op_locked(grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global *stream_global, + grpc_transport_op *op); -static void become_skip_parser(transport *t); +/** Cancel a stream: coming from the transport API */ +static void cancel_from_api(grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global *stream_global, + grpc_status_code status); -static void recv_data(void *tp, gpr_slice *slices, size_t nslices, - grpc_endpoint_cb_status error); +/** Add endpoint from this transport to pollset */ +static void add_to_pollset_locked(grpc_chttp2_transport *t, + grpc_pollset *pollset); -static void schedule_cb(transport *t, op_closure closure, int success); -static void maybe_finish_read(transport *t, stream *s); -static void maybe_join_window_updates(transport *t, stream *s); -static void finish_reads(transport *t); -static void add_to_pollset_locked(transport *t, grpc_pollset *pollset); -static void perform_op_locked(transport *t, stream *s, grpc_transport_op *op); -static void add_metadata_batch(transport *t, stream *s); - -static void flowctl_trace(transport *t, const char *flow, gpr_int32 window, - gpr_uint32 id, gpr_int32 delta) { - gpr_log(GPR_DEBUG, "HTTP:FLOW:%p:%d:%s: %d + %d = %d", t, id, flow, window, - delta, window + delta); -} +/** Start new streams that have been created if we can */ +static void maybe_start_some_streams( + grpc_chttp2_transport_global *transport_global); /* * CONSTRUCTION/DESTRUCTION/REFCOUNTING */ -static void destruct_transport(transport *t) { +static void destruct_transport(grpc_chttp2_transport *t) { size_t i; gpr_mu_lock(&t->mu); GPR_ASSERT(t->ep == NULL); - gpr_slice_buffer_destroy(&t->outbuf); - gpr_slice_buffer_destroy(&t->qbuf); - grpc_chttp2_hpack_parser_destroy(&t->hpack_parser); - grpc_chttp2_hpack_compressor_destroy(&t->hpack_compressor); - grpc_chttp2_goaway_parser_destroy(&t->goaway_parser); + gpr_slice_buffer_destroy(&t->global.qbuf); + + gpr_slice_buffer_destroy(&t->writing.outbuf); + grpc_chttp2_hpack_compressor_destroy(&t->writing.hpack_compressor); + + gpr_slice_buffer_destroy(&t->parsing.qbuf); + grpc_chttp2_hpack_parser_destroy(&t->parsing.hpack_parser); + grpc_chttp2_goaway_parser_destroy(&t->parsing.goaway_parser); - grpc_mdstr_unref(t->str_grpc_timeout); + grpc_mdstr_unref(t->parsing.str_grpc_timeout); for (i = 0; i < STREAM_LIST_COUNT; i++) { GPR_ASSERT(t->lists[i].head == NULL); GPR_ASSERT(t->lists[i].tail == NULL); } - GPR_ASSERT(grpc_chttp2_stream_map_size(&t->stream_map) == 0); + GPR_ASSERT(grpc_chttp2_stream_map_size(&t->parsing_stream_map) == 0); + GPR_ASSERT(grpc_chttp2_stream_map_size(&t->new_stream_map) == 0); - grpc_chttp2_stream_map_destroy(&t->stream_map); + grpc_chttp2_stream_map_destroy(&t->parsing_stream_map); + grpc_chttp2_stream_map_destroy(&t->new_stream_map); gpr_mu_unlock(&t->mu); gpr_mu_destroy(&t->mu); - gpr_cv_destroy(&t->cv); /* callback remaining pings: they're not allowed to call into the transpot, and maybe they hold resources that need to be freed */ - for (i = 0; i < t->ping_count; i++) { - t->pings[i].cb(t->pings[i].user_data); + while (t->global.pings.next != &t->global.pings) { + grpc_chttp2_outstanding_ping *ping = t->global.pings.next; + grpc_iomgr_add_delayed_callback(ping->on_recv, 0); + ping->next->prev = ping->prev; + ping->prev->next = ping->next; + gpr_free(ping); } - gpr_free(t->pings); - - gpr_free(t->pending_callbacks.callbacks); - gpr_free(t->executing_callbacks.callbacks); - - for (i = 0; i < t->num_pending_goaways; i++) { - gpr_slice_unref(t->pending_goaways[i].debug); - } - gpr_free(t->pending_goaways); - - grpc_sopb_destroy(&t->nuke_later_sopb); grpc_mdctx_unref(t->metadata_context); gpr_free(t); } -static void unref_transport(transport *t) { +#ifdef REFCOUNTING_DEBUG +#define REF_TRANSPORT(t, r) ref_transport(t, r, __FILE__, __LINE__) +#define UNREF_TRANSPORT(t, r) unref_transport(t, r, __FILE__, __LINE__) +static void unref_transport(grpc_chttp2_transport *t, const char *reason, + const char *file, int line) { + gpr_log(GPR_DEBUG, "chttp2:unref:%p %d->%d %s [%s:%d]", t, t->refs.count, + t->refs.count - 1, reason, file, line); if (!gpr_unref(&t->refs)) return; destruct_transport(t); } -static void ref_transport(transport *t) { gpr_ref(&t->refs); } +static void ref_transport(grpc_chttp2_transport *t, const char *reason, + const char *file, int line) { + gpr_log(GPR_DEBUG, "chttp2: ref:%p %d->%d %s [%s:%d]", t, t->refs.count, + t->refs.count + 1, reason, file, line); + gpr_ref(&t->refs); +} +#else +#define REF_TRANSPORT(t, r) ref_transport(t) +#define UNREF_TRANSPORT(t, r) unref_transport(t) +static void unref_transport(grpc_chttp2_transport *t) { + if (!gpr_unref(&t->refs)) return; + destruct_transport(t); +} -static void init_transport(transport *t, grpc_transport_setup_callback setup, - void *arg, const grpc_channel_args *channel_args, +static void ref_transport(grpc_chttp2_transport *t) { gpr_ref(&t->refs); } +#endif + +static void init_transport(grpc_chttp2_transport *t, + grpc_transport_setup_callback setup, void *arg, + const grpc_channel_args *channel_args, grpc_endpoint *ep, gpr_slice *slices, size_t nslices, grpc_mdctx *mdctx, int is_client) { size_t i; int j; grpc_transport_setup_result sr; - GPR_ASSERT(strlen(CLIENT_CONNECT_STRING) == CLIENT_CONNECT_STRLEN); + GPR_ASSERT(strlen(GRPC_CHTTP2_CLIENT_CONNECT_STRING) == + GRPC_CHTTP2_CLIENT_CONNECT_STRLEN); memset(t, 0, sizeof(*t)); @@ -495,51 +214,65 @@ static void init_transport(transport *t, grpc_transport_setup_callback setup, /* one ref is for destroy, the other for when ep becomes NULL */ gpr_ref_init(&t->refs, 2); gpr_mu_init(&t->mu); - gpr_cv_init(&t->cv); grpc_mdctx_ref(mdctx); t->metadata_context = mdctx; - t->str_grpc_timeout = + t->endpoint_reading = 1; + t->global.error_state = GRPC_CHTTP2_ERROR_STATE_NONE; + t->global.next_stream_id = is_client ? 1 : 2; + t->global.is_client = is_client; + t->global.outgoing_window = DEFAULT_WINDOW; + t->global.incoming_window = DEFAULT_WINDOW; + t->global.connection_window_target = DEFAULT_CONNECTION_WINDOW_TARGET; + t->global.ping_counter = 1; + t->global.pings.next = t->global.pings.prev = &t->global.pings; + t->parsing.is_client = is_client; + t->parsing.str_grpc_timeout = grpc_mdstr_from_string(t->metadata_context, "grpc-timeout"); - t->reading = 1; - t->error_state = ERROR_STATE_NONE; - t->next_stream_id = is_client ? 1 : 2; - t->is_client = is_client; - t->outgoing_window = DEFAULT_WINDOW; - t->incoming_window = DEFAULT_WINDOW; - t->connection_window_target = DEFAULT_CONNECTION_WINDOW_TARGET; - t->deframe_state = is_client ? DTS_FH_0 : DTS_CLIENT_PREFIX_0; - t->ping_counter = gpr_now().tv_nsec; - grpc_chttp2_hpack_compressor_init(&t->hpack_compressor, mdctx); - grpc_chttp2_goaway_parser_init(&t->goaway_parser); - gpr_slice_buffer_init(&t->outbuf); - gpr_slice_buffer_init(&t->qbuf); - grpc_sopb_init(&t->nuke_later_sopb); - grpc_chttp2_hpack_parser_init(&t->hpack_parser, t->metadata_context); + t->parsing.deframe_state = + is_client ? GRPC_DTS_FH_0 : GRPC_DTS_CLIENT_PREFIX_0; + t->writing.is_client = is_client; + + gpr_slice_buffer_init(&t->global.qbuf); + + gpr_slice_buffer_init(&t->writing.outbuf); + grpc_chttp2_hpack_compressor_init(&t->writing.hpack_compressor, mdctx); + grpc_iomgr_closure_init(&t->writing_action, writing_action, t); + grpc_iomgr_closure_init(&t->reading_action, reading_action, t); + + gpr_slice_buffer_init(&t->parsing.qbuf); + grpc_chttp2_goaway_parser_init(&t->parsing.goaway_parser); + grpc_chttp2_hpack_parser_init(&t->parsing.hpack_parser, t->metadata_context); + + grpc_iomgr_closure_init(&t->channel_callback.notify_closed, notify_closed, t); if (is_client) { - gpr_slice_buffer_add(&t->qbuf, - gpr_slice_from_copied_string(CLIENT_CONNECT_STRING)); + gpr_slice_buffer_add( + &t->global.qbuf, + gpr_slice_from_copied_string(GRPC_CHTTP2_CLIENT_CONNECT_STRING)); } /* 8 is a random stab in the dark as to a good initial size: it's small enough that it shouldn't waste memory for infrequently used connections, yet large enough that the exponential growth should happen nicely when it's needed. TODO(ctiller): tune this */ - grpc_chttp2_stream_map_init(&t->stream_map, 8); + grpc_chttp2_stream_map_init(&t->parsing_stream_map, 8); + grpc_chttp2_stream_map_init(&t->new_stream_map, 8); /* copy in initial settings to all setting sets */ - for (i = 0; i < NUM_SETTING_SETS; i++) { - for (j = 0; j < GRPC_CHTTP2_NUM_SETTINGS; j++) { - t->settings[i][j] = grpc_chttp2_settings_parameters[j].default_value; + for (i = 0; i < GRPC_CHTTP2_NUM_SETTINGS; i++) { + t->parsing.settings[i] = grpc_chttp2_settings_parameters[i].default_value; + for (j = 0; j < GRPC_NUM_SETTING_SETS; j++) { + t->global.settings[j][i] = + grpc_chttp2_settings_parameters[i].default_value; } } - t->dirtied_local_settings = 1; + t->global.dirtied_local_settings = 1; /* Hack: it's common for implementations to assume 65536 bytes initial send window -- this should by rights be 0 */ - t->force_send_settings = 1 << GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE; - t->sent_local_settings = 0; + t->global.force_send_settings = 1 << GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE; + t->global.sent_local_settings = 0; /* configure http2 the way we like it */ - if (t->is_client) { + if (is_client) { push_setting(t, GRPC_CHTTP2_SETTINGS_ENABLE_PUSH, 0); push_setting(t, GRPC_CHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 0); } @@ -549,7 +282,7 @@ static void init_transport(transport *t, grpc_transport_setup_callback setup, for (i = 0; i < channel_args->num_args; i++) { if (0 == strcmp(channel_args->args[i].key, GRPC_ARG_MAX_CONCURRENT_STREAMS)) { - if (t->is_client) { + if (is_client) { gpr_log(GPR_ERROR, "%s: is ignored on the client", GRPC_ARG_MAX_CONCURRENT_STREAMS); } else if (channel_args->args[i].type != GRPC_ARG_INTEGER) { @@ -564,69 +297,50 @@ static void init_transport(transport *t, grpc_transport_setup_callback setup, if (channel_args->args[i].type != GRPC_ARG_INTEGER) { gpr_log(GPR_ERROR, "%s: must be an integer", GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER); - } else if ((t->next_stream_id & 1) != + } else if ((t->global.next_stream_id & 1) != (channel_args->args[i].value.integer & 1)) { gpr_log(GPR_ERROR, "%s: low bit must be %d on %s", - GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER, t->next_stream_id & 1, - t->is_client ? "client" : "server"); + GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER, + t->global.next_stream_id & 1, + is_client ? "client" : "server"); } else { - t->next_stream_id = channel_args->args[i].value.integer; + t->global.next_stream_id = channel_args->args[i].value.integer; } } } } gpr_mu_lock(&t->mu); - t->calling_back = 1; - ref_transport(t); /* matches unref at end of this function */ + t->channel_callback.executing = 1; + REF_TRANSPORT(t, "init"); /* matches unref at end of this function */ gpr_mu_unlock(&t->mu); sr = setup(arg, &t->base, t->metadata_context); lock(t); - t->cb = sr.callbacks; - t->cb_user_data = sr.user_data; - t->calling_back = 0; - if (t->destroying) gpr_cv_signal(&t->cv); + t->channel_callback.cb = sr.callbacks; + t->channel_callback.cb_user_data = sr.user_data; + t->channel_callback.executing = 0; unlock(t); - ref_transport(t); /* matches unref inside recv_data */ + REF_TRANSPORT(t, "recv_data"); /* matches unref inside recv_data */ recv_data(t, slices, nslices, GRPC_ENDPOINT_CB_OK); - unref_transport(t); + UNREF_TRANSPORT(t, "init"); } static void destroy_transport(grpc_transport *gt) { - transport *t = (transport *)gt; + grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt; lock(t); t->destroying = 1; - /* Wait for pending stuff to finish. - We need to be not calling back to ensure that closed() gets a chance to - trigger if needed during unlock() before we die. - We need to be not writing as cancellation finalization may produce some - callbacks that NEED to be made to close out some streams when t->writing - becomes 0. */ - while (t->calling_back || t->writing) { - gpr_cv_wait(&t->cv, &t->mu, gpr_inf_future); - } drop_connection(t); unlock(t); - /* The drop_connection() above puts the transport into an error state, and - the follow-up unlock should then (as part of the cleanup work it does) - ensure that cb is NULL, and therefore not call back anything further. - This check validates this very subtle behavior. - It's shutdown path, so I don't believe an extra lock pair is going to be - problematic for performance. */ - lock(t); - GPR_ASSERT(!t->cb); - unlock(t); - - unref_transport(t); + UNREF_TRANSPORT(t, "destroy"); } -static void close_transport_locked(transport *t) { +static void close_transport_locked(grpc_chttp2_transport *t) { if (!t->closed) { t->closed = 1; if (t->ep) { @@ -636,7 +350,7 @@ static void close_transport_locked(transport *t) { } static void close_transport(grpc_transport *gt) { - transport *t = (transport *)gt; + grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt; gpr_mu_lock(&t->mu); close_transport_locked(t); gpr_mu_unlock(&t->mu); @@ -644,277 +358,153 @@ static void close_transport(grpc_transport *gt) { static void goaway(grpc_transport *gt, grpc_status_code status, gpr_slice debug_data) { - transport *t = (transport *)gt; + grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt; lock(t); - grpc_chttp2_goaway_append(t->last_incoming_stream_id, + grpc_chttp2_goaway_append(t->global.last_incoming_stream_id, grpc_chttp2_grpc_status_to_http2_error(status), - debug_data, &t->qbuf); + debug_data, &t->global.qbuf); unlock(t); } static int init_stream(grpc_transport *gt, grpc_stream *gs, const void *server_data, grpc_transport_op *initial_op) { - transport *t = (transport *)gt; - stream *s = (stream *)gs; + grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt; + grpc_chttp2_stream *s = (grpc_chttp2_stream *)gs; memset(s, 0, sizeof(*s)); - ref_transport(t); - - if (!server_data) { - lock(t); - s->id = 0; - s->outgoing_window = 0; - s->incoming_window = 0; - } else { - /* already locked */ - s->id = (gpr_uint32)(gpr_uintptr)server_data; - s->outgoing_window = - t->settings[PEER_SETTINGS][GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE]; - s->max_recv_bytes = s->incoming_window = - t->settings[SENT_SETTINGS][GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE]; - t->incoming_stream = s; - grpc_chttp2_stream_map_add(&t->stream_map, s->id, s); - } + grpc_chttp2_incoming_metadata_buffer_init(&s->parsing.incoming_metadata); + grpc_chttp2_incoming_metadata_buffer_init(&s->global.incoming_metadata); + grpc_sopb_init(&s->writing.sopb); + grpc_sopb_init(&s->global.incoming_sopb); + grpc_chttp2_data_parser_init(&s->parsing.data_parser); - s->incoming_deadline = gpr_inf_future; - grpc_sopb_init(&s->writing_sopb); - grpc_sopb_init(&s->callback_sopb); - grpc_chttp2_data_parser_init(&s->parser); + REF_TRANSPORT(t, "stream"); - if (initial_op) perform_op_locked(t, s, initial_op); - - if (!server_data) { - unlock(t); - } + lock(t); + grpc_chttp2_register_stream(t, s); + if (server_data) { + GPR_ASSERT(t->parsing_active); + s->global.id = (gpr_uint32)(gpr_uintptr)server_data; + s->global.outgoing_window = + t->global.settings[GRPC_PEER_SETTINGS] + [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE]; + s->global.max_recv_bytes = + s->parsing.incoming_window = + s->global.incoming_window = + t->global.settings[GRPC_SENT_SETTINGS] + [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE]; + *t->accepting_stream = s; + grpc_chttp2_stream_map_add(&t->parsing_stream_map, s->global.id, s); + s->global.in_stream_map = 1; + } + + if (initial_op) perform_op_locked(&t->global, &s->global, initial_op); + unlock(t); return 0; } -static void schedule_nuke_sopb(transport *t, grpc_stream_op_buffer *sopb) { - grpc_sopb_append(&t->nuke_later_sopb, sopb->ops, sopb->nops); - sopb->nops = 0; -} - static void destroy_stream(grpc_transport *gt, grpc_stream *gs) { - transport *t = (transport *)gt; - stream *s = (stream *)gs; - size_t i; + grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt; + grpc_chttp2_stream *s = (grpc_chttp2_stream *)gs; + int i; gpr_mu_lock(&t->mu); - /* stop parsing if we're currently parsing this stream */ - if (t->deframe_state == DTS_FRAME && t->incoming_stream_id == s->id && - s->id != 0) { - become_skip_parser(t); + GPR_ASSERT(s->global.published_state == GRPC_STREAM_CLOSED || + s->global.id == 0); + GPR_ASSERT(!s->global.in_stream_map); + grpc_chttp2_unregister_stream(t, s); + if (!t->parsing_active && s->global.id) { + GPR_ASSERT(grpc_chttp2_stream_map_find(&t->parsing_stream_map, + s->global.id) == NULL); } - for (i = 0; i < STREAM_LIST_COUNT; i++) { - stream_list_remove(t, s, i); - } - remove_from_stream_map(t, s); + grpc_chttp2_list_remove_incoming_window_updated(&t->global, &s->global); + grpc_chttp2_list_remove_writable_window_update_stream(&t->global, &s->global); gpr_mu_unlock(&t->mu); - GPR_ASSERT(s->outgoing_sopb == NULL); - GPR_ASSERT(s->incoming_sopb == NULL); - grpc_sopb_destroy(&s->writing_sopb); - grpc_sopb_destroy(&s->callback_sopb); - grpc_chttp2_data_parser_destroy(&s->parser); - for (i = 0; i < s->incoming_metadata_count; i++) { - grpc_mdelem_unref(s->incoming_metadata[i].md); - } - gpr_free(s->incoming_metadata); - gpr_free(s->old_incoming_metadata); - - unref_transport(t); -} - -/* - * LIST MANAGEMENT - */ - -static int stream_list_empty(transport *t, stream_list_id id) { - return t->lists[id].head == NULL; -} - -static stream *stream_list_remove_head(transport *t, stream_list_id id) { - stream *s = t->lists[id].head; - if (s) { - stream *new_head = s->links[id].next; - GPR_ASSERT(s->included[id]); - if (new_head) { - t->lists[id].head = new_head; - new_head->links[id].prev = NULL; - } else { - t->lists[id].head = NULL; - t->lists[id].tail = NULL; - } - s->included[id] = 0; + for (i = 0; i < STREAM_LIST_COUNT; i++) { + GPR_ASSERT(!s->included[i]); } - return s; -} -static void stream_list_remove(transport *t, stream *s, stream_list_id id) { - if (!s->included[id]) return; - s->included[id] = 0; - if (s->links[id].prev) { - s->links[id].prev->links[id].next = s->links[id].next; - } else { - GPR_ASSERT(t->lists[id].head == s); - t->lists[id].head = s->links[id].next; - } - if (s->links[id].next) { - s->links[id].next->links[id].prev = s->links[id].prev; - } else { - t->lists[id].tail = s->links[id].prev; - } -} + GPR_ASSERT(s->global.outgoing_sopb == NULL); + GPR_ASSERT(s->global.publish_sopb == NULL); + grpc_sopb_destroy(&s->writing.sopb); + grpc_sopb_destroy(&s->global.incoming_sopb); + grpc_chttp2_data_parser_destroy(&s->parsing.data_parser); + grpc_chttp2_incoming_metadata_buffer_destroy(&s->parsing.incoming_metadata); + grpc_chttp2_incoming_metadata_buffer_destroy(&s->global.incoming_metadata); + grpc_chttp2_incoming_metadata_live_op_buffer_end( + &s->global.outstanding_metadata); -static void stream_list_add_tail(transport *t, stream *s, stream_list_id id) { - stream *old_tail; - GPR_ASSERT(!s->included[id]); - old_tail = t->lists[id].tail; - s->links[id].next = NULL; - s->links[id].prev = old_tail; - if (old_tail) { - old_tail->links[id].next = s; - } else { - s->links[id].prev = NULL; - t->lists[id].head = s; - } - t->lists[id].tail = s; - s->included[id] = 1; + UNREF_TRANSPORT(t, "stream"); } -static void stream_list_join(transport *t, stream *s, stream_list_id id) { - if (s->included[id]) { - return; - } - stream_list_add_tail(t, s, id); +grpc_chttp2_stream_parsing *grpc_chttp2_parsing_lookup_stream( + grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id) { + grpc_chttp2_transport *t = TRANSPORT_FROM_PARSING(transport_parsing); + grpc_chttp2_stream *s = + grpc_chttp2_stream_map_find(&t->parsing_stream_map, id); + return s ? &s->parsing : NULL; } -static void remove_from_stream_map(transport *t, stream *s) { - if (s->id == 0) return; - IF_TRACING(gpr_log(GPR_DEBUG, "HTTP:%s: Removing stream %d", - t->is_client ? "CLI" : "SVR", s->id)); - if (grpc_chttp2_stream_map_delete(&t->stream_map, s->id)) { - maybe_start_some_streams(t); - } +grpc_chttp2_stream_parsing *grpc_chttp2_parsing_accept_stream( + grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id) { + grpc_chttp2_stream *accepting; + grpc_chttp2_transport *t = TRANSPORT_FROM_PARSING(transport_parsing); + GPR_ASSERT(t->accepting_stream == NULL); + t->accepting_stream = &accepting; + t->channel_callback.cb->accept_stream(t->channel_callback.cb_user_data, + &t->base, (void *)(gpr_uintptr)id); + t->accepting_stream = NULL; + return &accepting->parsing; } /* * LOCK MANAGEMENT */ -/* We take a transport-global lock in response to calls coming in from above, +/* We take a grpc_chttp2_transport-global lock in response to calls coming in + from above, and in response to data being received from below. New data to be written is always queued, as are callbacks to process data. During unlock() we check our todo lists and initiate callbacks and flush writes. */ -static void lock(transport *t) { gpr_mu_lock(&t->mu); } - -static void unlock(transport *t) { - int start_write = 0; - int perform_callbacks = 0; - int call_closed = 0; - int num_goaways = 0; - int i; - pending_goaway *goaways = NULL; - grpc_endpoint *ep = t->ep; - grpc_stream_op_buffer nuke_now; - const grpc_transport_callbacks *cb = t->cb; - - GRPC_TIMER_BEGIN(GRPC_PTAG_HTTP2_UNLOCK, 0); - - grpc_sopb_init(&nuke_now); - if (t->nuke_later_sopb.nops) { - grpc_sopb_swap(&nuke_now, &t->nuke_later_sopb); - } - - /* see if we need to trigger a write - and if so, get the data ready */ - if (ep && !t->writing) { - t->writing = start_write = prepare_write(t); - if (start_write) { - ref_transport(t); - } - } +static void lock(grpc_chttp2_transport *t) { gpr_mu_lock(&t->mu); } - if (!t->writing) { - finalize_cancellations(t); - } +static void unlock(grpc_chttp2_transport *t) { + grpc_iomgr_closure *run_closures; - finish_reads(t); - - /* gather any callbacks that need to be made */ - if (!t->calling_back) { - t->calling_back = perform_callbacks = prepare_callbacks(t); - if (cb) { - if (t->error_state == ERROR_STATE_SEEN && !t->writing) { - call_closed = 1; - t->calling_back = 1; - t->cb = NULL; /* no more callbacks */ - t->error_state = ERROR_STATE_NOTIFIED; - } - if (t->num_pending_goaways) { - goaways = t->pending_goaways; - num_goaways = t->num_pending_goaways; - t->pending_goaways = NULL; - t->num_pending_goaways = 0; - t->cap_pending_goaways = 0; - t->calling_back = 1; - } - } + unlock_check_read_write_state(t); + if (!t->writing_active && t->global.error_state == GRPC_CHTTP2_ERROR_STATE_NONE && + grpc_chttp2_unlocking_check_writes(&t->global, &t->writing)) { + t->writing_active = 1; + REF_TRANSPORT(t, "writing"); + grpc_chttp2_schedule_closure(&t->global, &t->writing_action, 1); } + /* unlock_check_parser(t); */ + unlock_check_channel_callbacks(t); - if (perform_callbacks || call_closed || num_goaways) { - ref_transport(t); - } + run_closures = t->global.pending_closures; + t->global.pending_closures = NULL; - /* finally unlock */ gpr_mu_unlock(&t->mu); - GRPC_TIMER_MARK(GRPC_PTAG_HTTP2_UNLOCK_CLEANUP, 0); - - /* perform some callbacks if necessary */ - for (i = 0; i < num_goaways; i++) { - cb->goaway(t->cb_user_data, &t->base, goaways[i].status, goaways[i].debug); - } - - if (perform_callbacks) { - run_callbacks(t, cb); + while (run_closures) { + grpc_iomgr_closure *next = run_closures->next; + run_closures->cb(run_closures->cb_arg, run_closures->success); + run_closures = next; } - - if (call_closed) { - call_cb_closed(t, cb); - } - - /* write some bytes if necessary */ - if (start_write) { - /* ultimately calls unref_transport(t); and clears t->writing */ - perform_write(t, ep); - } - - if (perform_callbacks || call_closed || num_goaways) { - lock(t); - t->calling_back = 0; - if (t->destroying) gpr_cv_signal(&t->cv); - unlock(t); - unref_transport(t); - } - - grpc_sopb_destroy(&nuke_now); - - gpr_free(goaways); - - GRPC_TIMER_END(GRPC_PTAG_HTTP2_UNLOCK, 0); } /* * OUTPUT PROCESSING */ -static void push_setting(transport *t, grpc_chttp2_setting_id id, +static void push_setting(grpc_chttp2_transport *t, grpc_chttp2_setting_id id, gpr_uint32 value) { const grpc_chttp2_setting_parameters *sp = &grpc_chttp2_settings_parameters[id]; @@ -923,304 +513,209 @@ static void push_setting(transport *t, grpc_chttp2_setting_id id, gpr_log(GPR_INFO, "Requested parameter %s clamped from %d to %d", sp->name, value, use_value); } - if (use_value != t->settings[LOCAL_SETTINGS][id]) { - t->settings[LOCAL_SETTINGS][id] = use_value; - t->dirtied_local_settings = 1; + if (use_value != t->global.settings[GRPC_LOCAL_SETTINGS][id]) { + t->global.settings[GRPC_LOCAL_SETTINGS][id] = use_value; + t->global.dirtied_local_settings = 1; } } -static int prepare_write(transport *t) { - stream *s; - gpr_uint32 window_delta; - - /* simple writes are queued to qbuf, and flushed here */ - gpr_slice_buffer_swap(&t->qbuf, &t->outbuf); - GPR_ASSERT(t->qbuf.count == 0); - - if (t->dirtied_local_settings && !t->sent_local_settings) { - gpr_slice_buffer_add( - &t->outbuf, grpc_chttp2_settings_create( - t->settings[SENT_SETTINGS], t->settings[LOCAL_SETTINGS], - t->force_send_settings, GRPC_CHTTP2_NUM_SETTINGS)); - t->force_send_settings = 0; - t->dirtied_local_settings = 0; - t->sent_local_settings = 1; - } - - /* for each stream that's become writable, frame it's data (according to - available window sizes) and add to the output buffer */ - while (t->outgoing_window && (s = stream_list_remove_head(t, WRITABLE)) && - s->outgoing_window > 0) { - window_delta = grpc_chttp2_preencode( - s->outgoing_sopb->ops, &s->outgoing_sopb->nops, - GPR_MIN(t->outgoing_window, s->outgoing_window), &s->writing_sopb); - FLOWCTL_TRACE(t, t, outgoing, 0, -(gpr_int64)window_delta); - FLOWCTL_TRACE(t, s, outgoing, s->id, -(gpr_int64)window_delta); - t->outgoing_window -= window_delta; - s->outgoing_window -= window_delta; - - if (s->write_state == WRITE_STATE_QUEUED_CLOSE && - s->outgoing_sopb->nops == 0) { - if (!t->is_client && !s->read_closed) { - s->send_closed = SEND_CLOSED_WITH_RST_STREAM; - } else { - s->send_closed = SEND_CLOSED; - } - } - if (s->writing_sopb.nops > 0 || s->send_closed) { - stream_list_join(t, s, WRITING); - } - - /* we should either exhaust window or have no ops left, but not both */ - if (s->outgoing_sopb->nops == 0) { - s->outgoing_sopb = NULL; - schedule_cb(t, s->send_done_closure, 1); - } else if (s->outgoing_window) { - stream_list_add_tail(t, s, WRITABLE); - } - } - - /* for each stream that wants to update its window, add that window here */ - while ((s = stream_list_remove_head(t, WINDOW_UPDATE))) { - if (!s->read_closed && s->unannounced_incoming_window > 0) { - gpr_slice_buffer_add(&t->outbuf, - grpc_chttp2_window_update_create( - s->id, s->unannounced_incoming_window)); - FLOWCTL_TRACE(t, s, incoming, s->id, s->unannounced_incoming_window); - s->incoming_window += s->unannounced_incoming_window; - s->unannounced_incoming_window = 0; - } - } - - /* if the transport is ready to send a window update, do so here also */ - if (t->incoming_window < t->connection_window_target * 3 / 4) { - window_delta = t->connection_window_target - t->incoming_window; - gpr_slice_buffer_add(&t->outbuf, - grpc_chttp2_window_update_create(0, window_delta)); - FLOWCTL_TRACE(t, t, incoming, 0, window_delta); - t->incoming_window += window_delta; - } - - return t->outbuf.length > 0 || !stream_list_empty(t, WRITING); -} - -static void finalize_outbuf(transport *t) { - stream *s; - - while ((s = stream_list_remove_head(t, WRITING))) { - grpc_chttp2_encode(s->writing_sopb.ops, s->writing_sopb.nops, - s->send_closed != DONT_SEND_CLOSED, s->id, &t->hpack_compressor, &t->outbuf); - s->writing_sopb.nops = 0; - if (s->send_closed == SEND_CLOSED_WITH_RST_STREAM) { - gpr_slice_buffer_add(&t->outbuf, grpc_chttp2_rst_stream_create(s->id, GRPC_CHTTP2_NO_ERROR)); - } - if (s->send_closed != DONT_SEND_CLOSED) { - stream_list_join(t, s, WRITTEN_CLOSED); - } - } -} - -static void finish_write_common(transport *t, int success) { - stream *s; +void grpc_chttp2_terminate_writing( + grpc_chttp2_transport_writing *transport_writing, int success) { + grpc_chttp2_transport *t = TRANSPORT_FROM_WRITING(transport_writing); lock(t); + if (!success) { drop_connection(t); } - while ((s = stream_list_remove_head(t, WRITTEN_CLOSED))) { - s->write_state = WRITE_STATE_SENT_CLOSE; - if (!t->is_client) { - s->read_closed = 1; - } - maybe_finish_read(t, s); - } - t->outbuf.count = 0; - t->outbuf.length = 0; + + /* cleanup writing related jazz */ + grpc_chttp2_cleanup_writing(&t->global, &t->writing); + /* leave the writing flag up on shutdown to prevent further writes in unlock() from starting */ - t->writing = 0; - if (t->destroying) { - gpr_cv_signal(&t->cv); - } - if (!t->reading) { + t->writing_active = 0; + if (t->ep && !t->endpoint_reading) { grpc_endpoint_destroy(t->ep); t->ep = NULL; - unref_transport(t); /* safe because we'll still have the ref for write */ + UNREF_TRANSPORT( + t, "disconnect"); /* safe because we'll still have the ref for write */ } - unlock(t); - unref_transport(t); -} - -static void finish_write(void *tp, grpc_endpoint_cb_status error) { - transport *t = tp; - finish_write_common(t, error == GRPC_ENDPOINT_CB_OK); -} - -static void perform_write(transport *t, grpc_endpoint *ep) { - finalize_outbuf(t); - - GPR_ASSERT(t->outbuf.count > 0); + unlock(t); - switch (grpc_endpoint_write(ep, t->outbuf.slices, t->outbuf.count, - finish_write, t)) { - case GRPC_ENDPOINT_WRITE_DONE: - finish_write_common(t, 1); - break; - case GRPC_ENDPOINT_WRITE_ERROR: - finish_write_common(t, 0); - break; - case GRPC_ENDPOINT_WRITE_PENDING: - break; - } + UNREF_TRANSPORT(t, "writing"); } -static void add_goaway(transport *t, gpr_uint32 goaway_error, gpr_slice goaway_text) { - if (t->num_pending_goaways == t->cap_pending_goaways) { - t->cap_pending_goaways = GPR_MAX(1, t->cap_pending_goaways * 2); - t->pending_goaways = - gpr_realloc(t->pending_goaways, - sizeof(pending_goaway) * t->cap_pending_goaways); - } - t->pending_goaways[t->num_pending_goaways].status = - grpc_chttp2_http2_error_to_grpc_status(goaway_error); - t->pending_goaways[t->num_pending_goaways].debug = goaway_text; - t->num_pending_goaways++; +static void writing_action(void *gt, int iomgr_success_ignored) { + grpc_chttp2_transport *t = gt; + grpc_chttp2_perform_writes(&t->writing, t->ep); } - -static void maybe_start_some_streams(transport *t) { - /* start streams where we have free stream ids and free concurrency */ - while ( - t->next_stream_id <= MAX_CLIENT_STREAM_ID && - grpc_chttp2_stream_map_size(&t->stream_map) < - t->settings[PEER_SETTINGS][GRPC_CHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS]) { - stream *s = stream_list_remove_head(t, WAITING_FOR_CONCURRENCY); - if (!s) return; - - IF_TRACING(gpr_log(GPR_DEBUG, "HTTP:%s: Allocating new stream %p to id %d", - t->is_client ? "CLI" : "SVR", s, t->next_stream_id)); - - if (t->next_stream_id == MAX_CLIENT_STREAM_ID) { - add_goaway(t, GRPC_CHTTP2_NO_ERROR, gpr_slice_from_copied_string("Exceeded sequence number limit")); +void grpc_chttp2_add_incoming_goaway( + grpc_chttp2_transport_global *transport_global, gpr_uint32 goaway_error, + gpr_slice goaway_text) { + char *msg = gpr_hexdump((char*)GPR_SLICE_START_PTR(goaway_text), GPR_SLICE_LENGTH(goaway_text), GPR_HEXDUMP_PLAINTEXT); + gpr_free(msg); + if (transport_global->goaway_state == GRPC_CHTTP2_ERROR_STATE_NONE) { + transport_global->goaway_state = GRPC_CHTTP2_ERROR_STATE_SEEN; + transport_global->goaway_text = goaway_text; + transport_global->goaway_error = goaway_error; + } else { + gpr_slice_unref(goaway_text); + } +} + +static void maybe_start_some_streams( + grpc_chttp2_transport_global *transport_global) { + grpc_chttp2_stream_global *stream_global; + /* start streams where we have free grpc_chttp2_stream ids and free + * concurrency */ + while (transport_global->next_stream_id <= MAX_CLIENT_STREAM_ID && + transport_global->concurrent_stream_count < + transport_global + ->settings[GRPC_PEER_SETTINGS] + [GRPC_CHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS] && + grpc_chttp2_list_pop_waiting_for_concurrency(transport_global, + &stream_global)) { + GRPC_CHTTP2_IF_TRACING(gpr_log( + GPR_DEBUG, "HTTP:%s: Allocating new grpc_chttp2_stream %p to id %d", + transport_global->is_client ? "CLI" : "SVR", stream_global, + transport_global->next_stream_id)); + + GPR_ASSERT(stream_global->id == 0); + stream_global->id = transport_global->next_stream_id; + transport_global->next_stream_id += 2; + + if (transport_global->next_stream_id >= MAX_CLIENT_STREAM_ID) { + grpc_chttp2_add_incoming_goaway( + transport_global, GRPC_CHTTP2_NO_ERROR, + gpr_slice_from_copied_string("Exceeded sequence number limit")); } - GPR_ASSERT(s->id == 0); - s->id = t->next_stream_id; - t->next_stream_id += 2; - s->outgoing_window = - t->settings[PEER_SETTINGS][GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE]; - s->incoming_window = - t->settings[SENT_SETTINGS][GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE]; - s->max_recv_bytes = GPR_MAX(s->incoming_window, s->max_recv_bytes); - grpc_chttp2_stream_map_add(&t->stream_map, s->id, s); - stream_list_join(t, s, WRITABLE); - maybe_join_window_updates(t, s); + stream_global->outgoing_window = + transport_global->settings[GRPC_PEER_SETTINGS] + [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE]; + stream_global->incoming_window = + transport_global->settings[GRPC_SENT_SETTINGS] + [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE]; + stream_global->max_recv_bytes = + GPR_MAX(stream_global->incoming_window, stream_global->max_recv_bytes); + grpc_chttp2_stream_map_add( + &TRANSPORT_FROM_GLOBAL(transport_global)->new_stream_map, + stream_global->id, STREAM_FROM_GLOBAL(stream_global)); + stream_global->in_stream_map = 1; + transport_global->concurrent_stream_count++; + grpc_chttp2_list_add_incoming_window_updated(transport_global, + stream_global); + grpc_chttp2_list_add_writable_stream(transport_global, stream_global); + grpc_chttp2_list_add_writable_window_update_stream(transport_global, + stream_global); + } /* cancel out streams that will never be started */ - while (t->next_stream_id > MAX_CLIENT_STREAM_ID) { - stream *s = stream_list_remove_head(t, WAITING_FOR_CONCURRENCY); - if (!s) return; - - cancel_stream(t, s, GRPC_STATUS_UNAVAILABLE, grpc_chttp2_grpc_status_to_http2_error(GRPC_STATUS_UNAVAILABLE), NULL, 0); + while (transport_global->next_stream_id >= MAX_CLIENT_STREAM_ID && + grpc_chttp2_list_pop_waiting_for_concurrency(transport_global, + &stream_global)) { + cancel_from_api(transport_global, stream_global, GRPC_STATUS_UNAVAILABLE); } } -static void perform_op_locked(transport *t, stream *s, grpc_transport_op *op) { +static void perform_op_locked(grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global *stream_global, + grpc_transport_op *op) { if (op->cancel_with_status != GRPC_STATUS_OK) { - cancel_stream( - t, s, op->cancel_with_status, - grpc_chttp2_grpc_status_to_http2_error(op->cancel_with_status), - op->cancel_message, 1); + cancel_from_api(transport_global, stream_global, op->cancel_with_status); } if (op->send_ops) { - GPR_ASSERT(s->outgoing_sopb == NULL); - s->send_done_closure.cb = op->on_done_send; - s->send_done_closure.user_data = op->send_user_data; - if (!s->cancelled) { - s->outgoing_sopb = op->send_ops; - if (op->is_last_send && s->write_state == WRITE_STATE_OPEN) { - s->write_state = WRITE_STATE_QUEUED_CLOSE; + GPR_ASSERT(stream_global->outgoing_sopb == NULL); + stream_global->send_done_closure = op->on_done_send; + if (!stream_global->cancelled) { + stream_global->outgoing_sopb = op->send_ops; + if (op->is_last_send && + stream_global->write_state == GRPC_WRITE_STATE_OPEN) { + stream_global->write_state = GRPC_WRITE_STATE_QUEUED_CLOSE; } - if (s->id == 0) { - IF_TRACING(gpr_log(GPR_DEBUG, - "HTTP:%s: New stream %p waiting for concurrency", - t->is_client ? "CLI" : "SVR", s)); - stream_list_join(t, s, WAITING_FOR_CONCURRENCY); - maybe_start_some_streams(t); - } else if (s->outgoing_window > 0) { - stream_list_join(t, s, WRITABLE); + if (stream_global->id == 0) { + GRPC_CHTTP2_IF_TRACING(gpr_log( + GPR_DEBUG, + "HTTP:%s: New grpc_chttp2_stream %p waiting for concurrency", + transport_global->is_client ? "CLI" : "SVR", stream_global)); + grpc_chttp2_list_add_waiting_for_concurrency(transport_global, + stream_global); + maybe_start_some_streams(transport_global); + } else if (stream_global->outgoing_window > 0) { + grpc_chttp2_list_add_writable_stream(transport_global, stream_global); } } else { - schedule_nuke_sopb(t, op->send_ops); - schedule_cb(t, s->send_done_closure, 0); + grpc_sopb_reset(op->send_ops); + grpc_chttp2_schedule_closure(transport_global, + stream_global->send_done_closure, 0); } } if (op->recv_ops) { - GPR_ASSERT(s->incoming_sopb == NULL); - GPR_ASSERT(s->published_state != GRPC_STREAM_CLOSED); - s->recv_done_closure.cb = op->on_done_recv; - s->recv_done_closure.user_data = op->recv_user_data; - s->incoming_sopb = op->recv_ops; - s->incoming_sopb->nops = 0; - s->publish_state = op->recv_state; - if (s->max_recv_bytes < op->max_recv_bytes) { - s->unannounced_incoming_window += op->max_recv_bytes - s->max_recv_bytes; - s->max_recv_bytes = op->max_recv_bytes; + GPR_ASSERT(stream_global->publish_sopb == NULL); + GPR_ASSERT(stream_global->published_state != GRPC_STREAM_CLOSED); + stream_global->recv_done_closure = op->on_done_recv; + stream_global->publish_sopb = op->recv_ops; + stream_global->publish_sopb->nops = 0; + stream_global->publish_state = op->recv_state; + if (stream_global->max_recv_bytes < op->max_recv_bytes) { + GRPC_CHTTP2_FLOWCTL_TRACE_STREAM("op", transport_global, stream_global, + unannounced_incoming_window, op->max_recv_bytes - stream_global->max_recv_bytes); + GRPC_CHTTP2_FLOWCTL_TRACE_STREAM("op", transport_global, stream_global, + max_recv_bytes, op->max_recv_bytes - stream_global->max_recv_bytes); + stream_global->unannounced_incoming_window += op->max_recv_bytes - stream_global->max_recv_bytes; + stream_global->max_recv_bytes = op->max_recv_bytes; } - gpr_free(s->old_incoming_metadata); - s->old_incoming_metadata = NULL; - maybe_finish_read(t, s); - maybe_join_window_updates(t, s); + grpc_chttp2_incoming_metadata_live_op_buffer_end( + &stream_global->outstanding_metadata); + grpc_chttp2_list_add_read_write_state_changed(transport_global, + stream_global); + grpc_chttp2_list_add_writable_window_update_stream(transport_global, + stream_global); } if (op->bind_pollset) { - add_to_pollset_locked(t, op->bind_pollset); + add_to_pollset_locked(TRANSPORT_FROM_GLOBAL(transport_global), + op->bind_pollset); } if (op->on_consumed) { - op_closure c; - c.cb = op->on_consumed; - c.user_data = op->on_consumed_user_data; - schedule_cb(t, c, 1); + grpc_chttp2_schedule_closure(transport_global, op->on_consumed, 1); } } static void perform_op(grpc_transport *gt, grpc_stream *gs, grpc_transport_op *op) { - transport *t = (transport *)gt; - stream *s = (stream *)gs; + grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt; + grpc_chttp2_stream *s = (grpc_chttp2_stream *)gs; lock(t); - perform_op_locked(t, s, op); + perform_op_locked(&t->global, &s->global, op); unlock(t); } -static void send_ping(grpc_transport *gt, void (*cb)(void *user_data), - void *user_data) { - transport *t = (transport *)gt; - outstanding_ping *p; +static void send_ping(grpc_transport *gt, grpc_iomgr_closure *on_recv) { + grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt; + grpc_chttp2_outstanding_ping *p = gpr_malloc(sizeof(*p)); lock(t); - if (t->ping_capacity == t->ping_count) { - t->ping_capacity = GPR_MAX(1, t->ping_capacity * 3 / 2); - t->pings = - gpr_realloc(t->pings, sizeof(outstanding_ping) * t->ping_capacity); - } - p = &t->pings[t->ping_count++]; - p->id[0] = (t->ping_counter >> 56) & 0xff; - p->id[1] = (t->ping_counter >> 48) & 0xff; - p->id[2] = (t->ping_counter >> 40) & 0xff; - p->id[3] = (t->ping_counter >> 32) & 0xff; - p->id[4] = (t->ping_counter >> 24) & 0xff; - p->id[5] = (t->ping_counter >> 16) & 0xff; - p->id[6] = (t->ping_counter >> 8) & 0xff; - p->id[7] = t->ping_counter & 0xff; - p->cb = cb; - p->user_data = user_data; - gpr_slice_buffer_add(&t->qbuf, grpc_chttp2_ping_create(0, p->id)); + p->next = &t->global.pings; + p->prev = p->next->prev; + p->prev->next = p->next->prev = p; + p->id[0] = (t->global.ping_counter >> 56) & 0xff; + p->id[1] = (t->global.ping_counter >> 48) & 0xff; + p->id[2] = (t->global.ping_counter >> 40) & 0xff; + p->id[3] = (t->global.ping_counter >> 32) & 0xff; + p->id[4] = (t->global.ping_counter >> 24) & 0xff; + p->id[5] = (t->global.ping_counter >> 16) & 0xff; + p->id[6] = (t->global.ping_counter >> 8) & 0xff; + p->id[7] = t->global.ping_counter & 0xff; + p->on_recv = on_recv; + gpr_slice_buffer_add(&t->global.qbuf, grpc_chttp2_ping_create(0, p->id)); unlock(t); } @@ -1228,754 +723,174 @@ static void send_ping(grpc_transport *gt, void (*cb)(void *user_data), * INPUT PROCESSING */ -static void finalize_cancellations(transport *t) { - stream *s; - - while ((s = stream_list_remove_head(t, CANCELLED))) { - s->read_closed = 1; - s->write_state = WRITE_STATE_SENT_CLOSE; - maybe_finish_read(t, s); - } -} - -static void add_incoming_metadata(transport *t, stream *s, grpc_mdelem *elem) { - if (s->incoming_metadata_capacity == s->incoming_metadata_count) { - s->incoming_metadata_capacity = - GPR_MAX(8, 2 * s->incoming_metadata_capacity); - s->incoming_metadata = - gpr_realloc(s->incoming_metadata, sizeof(*s->incoming_metadata) * - s->incoming_metadata_capacity); - } - s->incoming_metadata[s->incoming_metadata_count++].md = elem; -} - -static void cancel_stream_inner(transport *t, stream *s, gpr_uint32 id, - grpc_status_code local_status, - grpc_chttp2_error_code error_code, - grpc_mdstr *optional_message, int send_rst) { - int had_outgoing; - char buffer[GPR_LTOA_MIN_BUFSIZE]; - - if (s) { - /* clear out any unreported input & output: nobody cares anymore */ - had_outgoing = s->outgoing_sopb && s->outgoing_sopb->nops != 0; - if (error_code != GRPC_CHTTP2_NO_ERROR) { - schedule_nuke_sopb(t, &s->parser.incoming_sopb); - if (s->outgoing_sopb) { - schedule_nuke_sopb(t, s->outgoing_sopb); - s->outgoing_sopb = NULL; - stream_list_remove(t, s, WRITABLE); - schedule_cb(t, s->send_done_closure, 0); - } - } - if (s->cancelled) { - send_rst = 0; - } else if (!s->read_closed || s->write_state != WRITE_STATE_SENT_CLOSE || - had_outgoing) { - s->cancelled = 1; - stream_list_join(t, s, CANCELLED); - - if (error_code != GRPC_CHTTP2_NO_ERROR) { - /* synthesize a status if we don't believe we'll get one */ - gpr_ltoa(local_status, buffer); - add_incoming_metadata( - t, s, - grpc_mdelem_from_strings(t->metadata_context, "grpc-status", buffer)); - if (!optional_message) { - switch (local_status) { - case GRPC_STATUS_CANCELLED: - add_incoming_metadata( - t, s, grpc_mdelem_from_strings(t->metadata_context, - "grpc-message", "Cancelled")); - break; - default: - break; - } - } else { - add_incoming_metadata( - t, s, - grpc_mdelem_from_metadata_strings( - t->metadata_context, - grpc_mdstr_from_string(t->metadata_context, "grpc-message"), - grpc_mdstr_ref(optional_message))); - } - add_metadata_batch(t, s); - } - } - maybe_finish_read(t, s); - } - if (!id) send_rst = 0; - if (send_rst) { - gpr_slice_buffer_add(&t->qbuf, - grpc_chttp2_rst_stream_create(id, error_code)); - } - if (optional_message) { - grpc_mdstr_unref(optional_message); - } -} - -static void cancel_stream_id(transport *t, gpr_uint32 id, - grpc_status_code local_status, - grpc_chttp2_error_code error_code, int send_rst) { - cancel_stream_inner(t, lookup_stream(t, id), id, local_status, error_code, - NULL, send_rst); -} - -static void cancel_stream(transport *t, stream *s, - grpc_status_code local_status, - grpc_chttp2_error_code error_code, - grpc_mdstr *optional_message, int send_rst) { - cancel_stream_inner(t, s, s->id, local_status, error_code, optional_message, - send_rst); -} - -static void cancel_stream_cb(void *user_data, gpr_uint32 id, void *stream) { - cancel_stream(user_data, stream, GRPC_STATUS_UNAVAILABLE, - GRPC_CHTTP2_INTERNAL_ERROR, NULL, 0); -} - -static void end_all_the_calls(transport *t) { - grpc_chttp2_stream_map_for_each(&t->stream_map, cancel_stream_cb, t); -} - -static void drop_connection(transport *t) { - if (t->error_state == ERROR_STATE_NONE) { - t->error_state = ERROR_STATE_SEEN; - } - close_transport_locked(t); - end_all_the_calls(t); -} - -static void maybe_finish_read(transport *t, stream *s) { - if (s->incoming_sopb) { - stream_list_join(t, s, FINISHED_READ_OP); - } -} - -static void maybe_join_window_updates(transport *t, stream *s) { - if (s->incoming_sopb != NULL && - s->unannounced_incoming_window > - t->settings[LOCAL_SETTINGS] - [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE] / - 4) { - stream_list_join(t, s, WINDOW_UPDATE); - } -} - -static grpc_chttp2_parse_error update_incoming_window(transport *t, stream *s) { - if (t->incoming_frame_size > t->incoming_window) { - gpr_log(GPR_ERROR, "frame of size %d overflows incoming window of %d", - t->incoming_frame_size, t->incoming_window); - return GRPC_CHTTP2_CONNECTION_ERROR; - } - - if (t->incoming_frame_size > s->incoming_window) { - gpr_log(GPR_ERROR, "frame of size %d overflows incoming window of %d", - t->incoming_frame_size, s->incoming_window); - return GRPC_CHTTP2_CONNECTION_ERROR; - } - - FLOWCTL_TRACE(t, t, incoming, 0, -(gpr_int64)t->incoming_frame_size); - FLOWCTL_TRACE(t, s, incoming, s->id, -(gpr_int64)t->incoming_frame_size); - t->incoming_window -= t->incoming_frame_size; - s->incoming_window -= t->incoming_frame_size; - GPR_ASSERT(s->max_recv_bytes > t->incoming_frame_size); - s->max_recv_bytes -= t->incoming_frame_size; - - /* if the stream incoming window is getting low, schedule an update */ - maybe_join_window_updates(t, s); - - return GRPC_CHTTP2_PARSE_OK; -} - -static stream *lookup_stream(transport *t, gpr_uint32 id) { - return grpc_chttp2_stream_map_find(&t->stream_map, id); -} - -static grpc_chttp2_parse_error skip_parser(void *parser, - grpc_chttp2_parse_state *st, - gpr_slice slice, int is_last) { - return GRPC_CHTTP2_PARSE_OK; -} - -static void skip_header(void *tp, grpc_mdelem *md) { grpc_mdelem_unref(md); } - -static int init_skip_frame(transport *t, int is_header) { - if (is_header) { - int is_eoh = t->expect_continuation_stream_id != 0; - t->parser = grpc_chttp2_header_parser_parse; - t->parser_data = &t->hpack_parser; - t->hpack_parser.on_header = skip_header; - t->hpack_parser.on_header_user_data = NULL; - t->hpack_parser.is_boundary = is_eoh; - t->hpack_parser.is_eof = is_eoh ? t->header_eof : 0; - } else { - t->parser = skip_parser; - } - return 1; -} - -static void become_skip_parser(transport *t) { - init_skip_frame(t, t->parser == grpc_chttp2_header_parser_parse); +static grpc_stream_state compute_state(gpr_uint8 write_closed, + gpr_uint8 read_closed) { + if (write_closed && read_closed) return GRPC_STREAM_CLOSED; + if (write_closed) return GRPC_STREAM_SEND_CLOSED; + if (read_closed) return GRPC_STREAM_RECV_CLOSED; + return GRPC_STREAM_OPEN; } -static int init_data_frame_parser(transport *t) { - stream *s = lookup_stream(t, t->incoming_stream_id); - grpc_chttp2_parse_error err = GRPC_CHTTP2_PARSE_OK; - if (!s || s->read_closed) return init_skip_frame(t, 0); - if (err == GRPC_CHTTP2_PARSE_OK) { - err = update_incoming_window(t, s); - } - if (err == GRPC_CHTTP2_PARSE_OK) { - err = grpc_chttp2_data_parser_begin_frame(&s->parser, - t->incoming_frame_flags); - } - switch (err) { - case GRPC_CHTTP2_PARSE_OK: - t->incoming_stream = s; - t->parser = grpc_chttp2_data_parser_parse; - t->parser_data = &s->parser; - return 1; - case GRPC_CHTTP2_STREAM_ERROR: - cancel_stream(t, s, grpc_chttp2_http2_error_to_grpc_status( - GRPC_CHTTP2_INTERNAL_ERROR), - GRPC_CHTTP2_INTERNAL_ERROR, NULL, 1); - return init_skip_frame(t, 0); - case GRPC_CHTTP2_CONNECTION_ERROR: - drop_connection(t); - return 0; +static void remove_stream(grpc_chttp2_transport *t, gpr_uint32 id) { + size_t new_stream_count; + grpc_chttp2_stream *s = + grpc_chttp2_stream_map_delete(&t->parsing_stream_map, id); + if (!s) { + s = grpc_chttp2_stream_map_delete(&t->new_stream_map, id); } - gpr_log(GPR_ERROR, "should never reach here"); - abort(); - return 0; -} - -static void free_timeout(void *p) { gpr_free(p); } - -static void on_header(void *tp, grpc_mdelem *md) { - transport *t = tp; - stream *s = t->incoming_stream; - GPR_ASSERT(s); - - IF_TRACING(gpr_log( - GPR_INFO, "HTTP:%d:%s:HDR: %s: %s", s->id, t->is_client ? "CLI" : "SVR", - grpc_mdstr_as_c_string(md->key), grpc_mdstr_as_c_string(md->value))); - - if (md->key == t->str_grpc_timeout) { - gpr_timespec *cached_timeout = grpc_mdelem_get_user_data(md, free_timeout); - if (!cached_timeout) { - /* not already parsed: parse it now, and store the result away */ - cached_timeout = gpr_malloc(sizeof(gpr_timespec)); - if (!grpc_chttp2_decode_timeout(grpc_mdstr_as_c_string(md->value), - cached_timeout)) { - gpr_log(GPR_ERROR, "Ignoring bad timeout value '%s'", - grpc_mdstr_as_c_string(md->value)); - *cached_timeout = gpr_inf_future; - } - grpc_mdelem_set_user_data(md, free_timeout, cached_timeout); + s->global.in_stream_map = 0; + if (t->parsing.incoming_stream == &s->parsing) { + t->parsing.incoming_stream = NULL; + grpc_chttp2_parsing_become_skip_parser(&t->parsing); + } + + new_stream_count = + grpc_chttp2_stream_map_size(&t->parsing_stream_map) + + grpc_chttp2_stream_map_size(&t->new_stream_map); + if (new_stream_count != t->global.concurrent_stream_count) { + t->global.concurrent_stream_count = new_stream_count; + maybe_start_some_streams(&t->global); + } +} + +static void unlock_check_read_write_state(grpc_chttp2_transport *t) { + grpc_chttp2_transport_global *transport_global = &t->global; + grpc_chttp2_stream_global *stream_global; + grpc_stream_state state; + + if (!t->parsing_active) { + /* if a stream is in the stream map, and gets cancelled, we need to ensure + we are not parsing before continuing the cancellation to keep things in + a sane state */ + while (grpc_chttp2_list_pop_closed_waiting_for_parsing(transport_global, + &stream_global)) { + GPR_ASSERT(stream_global->in_stream_map); + GPR_ASSERT(stream_global->write_state != GRPC_WRITE_STATE_OPEN); + GPR_ASSERT(stream_global->read_closed); + remove_stream(t, stream_global->id); + grpc_chttp2_list_add_read_write_state_changed(transport_global, + stream_global); } - s->incoming_deadline = gpr_time_add(gpr_now(), *cached_timeout); - grpc_mdelem_unref(md); - } else { - add_incoming_metadata(t, s, md); } - maybe_finish_read(t, s); -} -static int init_header_frame_parser(transport *t, int is_continuation) { - int is_eoh = - (t->incoming_frame_flags & GRPC_CHTTP2_DATA_FLAG_END_HEADERS) != 0; - stream *s; - - if (is_eoh) { - t->expect_continuation_stream_id = 0; - } else { - t->expect_continuation_stream_id = t->incoming_stream_id; - } - - if (!is_continuation) { - t->header_eof = - (t->incoming_frame_flags & GRPC_CHTTP2_DATA_FLAG_END_STREAM) != 0; - } - - /* could be a new stream or an existing stream */ - s = lookup_stream(t, t->incoming_stream_id); - if (!s) { - if (is_continuation) { - gpr_log(GPR_ERROR, "stream disbanded before CONTINUATION received"); - return init_skip_frame(t, 1); + while (grpc_chttp2_list_pop_read_write_state_changed(transport_global, + &stream_global)) { + if (stream_global->cancelled) { + stream_global->write_state = GRPC_WRITE_STATE_SENT_CLOSE; + stream_global->read_closed = 1; + if (!stream_global->published_cancelled) { + char buffer[GPR_LTOA_MIN_BUFSIZE]; + gpr_ltoa(stream_global->cancelled_status, buffer); + grpc_chttp2_incoming_metadata_buffer_add(&stream_global->incoming_metadata, + grpc_mdelem_from_strings(t->metadata_context, "grpc-status", buffer)); + grpc_chttp2_incoming_metadata_buffer_place_metadata_batch_into( + &stream_global->incoming_metadata, + &stream_global->incoming_sopb); + stream_global->published_cancelled = 1; + } } - if (t->is_client) { - if ((t->incoming_stream_id & 1) && - t->incoming_stream_id < t->next_stream_id) { - /* this is an old (probably cancelled) stream */ + if (stream_global->write_state == GRPC_WRITE_STATE_SENT_CLOSE && + stream_global->read_closed && stream_global->in_stream_map) { + if (t->parsing_active) { + grpc_chttp2_list_add_closed_waiting_for_parsing(transport_global, + stream_global); } else { - gpr_log(GPR_ERROR, "ignoring new stream creation on client"); + remove_stream(t, stream_global->id); } - return init_skip_frame(t, 1); - } else if (t->last_incoming_stream_id > t->incoming_stream_id) { - gpr_log(GPR_ERROR, - "ignoring out of order new stream request on server; last stream " - "id=%d, new stream id=%d", - t->last_incoming_stream_id, t->incoming_stream_id); - return init_skip_frame(t, 1); - } else if ((t->incoming_stream_id & 1) == 0) { - gpr_log(GPR_ERROR, "ignoring stream with non-client generated index %d", t->incoming_stream_id); - return init_skip_frame(t, 1); } - t->incoming_stream = NULL; - /* if stream is accepted, we set incoming_stream in init_stream */ - t->cb->accept_stream(t->cb_user_data, &t->base, - (void *)(gpr_uintptr)t->incoming_stream_id); - s = t->incoming_stream; - if (!s) { - gpr_log(GPR_ERROR, "stream not accepted"); - return init_skip_frame(t, 1); + if (!stream_global->publish_sopb) { + continue; } - } else { - t->incoming_stream = s; - } - if (t->incoming_stream->read_closed) { - gpr_log(GPR_ERROR, "skipping already closed stream header"); - t->incoming_stream = NULL; - return init_skip_frame(t, 1); - } - t->parser = grpc_chttp2_header_parser_parse; - t->parser_data = &t->hpack_parser; - t->hpack_parser.on_header = on_header; - t->hpack_parser.on_header_user_data = t; - t->hpack_parser.is_boundary = is_eoh; - t->hpack_parser.is_eof = is_eoh ? t->header_eof : 0; - if (!is_continuation && - (t->incoming_frame_flags & GRPC_CHTTP2_FLAG_HAS_PRIORITY)) { - grpc_chttp2_hpack_parser_set_has_priority(&t->hpack_parser); - } - return 1; -} - -static int init_window_update_frame_parser(transport *t) { - int ok = GRPC_CHTTP2_PARSE_OK == grpc_chttp2_window_update_parser_begin_frame( - &t->simple_parsers.window_update, - t->incoming_frame_size, - t->incoming_frame_flags); - if (!ok) { - drop_connection(t); - } - t->parser = grpc_chttp2_window_update_parser_parse; - t->parser_data = &t->simple_parsers.window_update; - return ok; -} - -static int init_ping_parser(transport *t) { - int ok = GRPC_CHTTP2_PARSE_OK == - grpc_chttp2_ping_parser_begin_frame(&t->simple_parsers.ping, - t->incoming_frame_size, - t->incoming_frame_flags); - if (!ok) { - drop_connection(t); + /* FIXME(ctiller): we include in_stream_map in our computation of + whether the stream is write-closed. This is completely bogus, + but has the effect of delaying stream-closed until the stream + is indeed evicted from the stream map, making it safe to delete. + To fix this will require having an edge after stream-closed + indicating that the stream is closed AND safe to delete. */ + state = compute_state( + stream_global->write_state == GRPC_WRITE_STATE_SENT_CLOSE && + !stream_global->in_stream_map, + stream_global->read_closed); + if (stream_global->incoming_sopb.nops == 0 && + state == stream_global->published_state) { + continue; + } + grpc_chttp2_incoming_metadata_buffer_postprocess_sopb_and_begin_live_op( + &stream_global->incoming_metadata, &stream_global->incoming_sopb, + &stream_global->outstanding_metadata); + grpc_sopb_swap(stream_global->publish_sopb, &stream_global->incoming_sopb); + stream_global->published_state = *stream_global->publish_state = state; + grpc_chttp2_schedule_closure(transport_global, + stream_global->recv_done_closure, 1); + stream_global->recv_done_closure = NULL; + stream_global->publish_sopb = NULL; + stream_global->publish_state = NULL; } - t->parser = grpc_chttp2_ping_parser_parse; - t->parser_data = &t->simple_parsers.ping; - return ok; } -static int init_rst_stream_parser(transport *t) { - int ok = GRPC_CHTTP2_PARSE_OK == - grpc_chttp2_rst_stream_parser_begin_frame(&t->simple_parsers.rst_stream, - t->incoming_frame_size, - t->incoming_frame_flags); - if (!ok) { - drop_connection(t); +static void cancel_from_api(grpc_chttp2_transport_global *transport_global, + grpc_chttp2_stream_global *stream_global, + grpc_status_code status) { + stream_global->cancelled = 1; + stream_global->cancelled_status = status; + if (stream_global->id != 0) { + gpr_slice_buffer_add(&transport_global->qbuf, + grpc_chttp2_rst_stream_create( + stream_global->id, + grpc_chttp2_grpc_status_to_http2_error(status))); } - t->parser = grpc_chttp2_rst_stream_parser_parse; - t->parser_data = &t->simple_parsers.rst_stream; - return ok; + grpc_chttp2_list_add_read_write_state_changed(transport_global, + stream_global); } -static int init_goaway_parser(transport *t) { - int ok = - GRPC_CHTTP2_PARSE_OK == - grpc_chttp2_goaway_parser_begin_frame( - &t->goaway_parser, t->incoming_frame_size, t->incoming_frame_flags); - if (!ok) { - drop_connection(t); - } - t->parser = grpc_chttp2_goaway_parser_parse; - t->parser_data = &t->goaway_parser; - return ok; +static void cancel_stream_cb(grpc_chttp2_transport_global *transport_global, + void *user_data, + grpc_chttp2_stream_global *stream_global) { + cancel_from_api(transport_global, stream_global, GRPC_STATUS_UNAVAILABLE); } -static int init_settings_frame_parser(transport *t) { - int ok = GRPC_CHTTP2_PARSE_OK == - grpc_chttp2_settings_parser_begin_frame( - &t->simple_parsers.settings, t->incoming_frame_size, - t->incoming_frame_flags, t->settings[PEER_SETTINGS]); - if (!ok) { - drop_connection(t); - } - if (t->incoming_frame_flags & GRPC_CHTTP2_FLAG_ACK) { - memcpy(t->settings[ACKED_SETTINGS], t->settings[SENT_SETTINGS], - GRPC_CHTTP2_NUM_SETTINGS * sizeof(gpr_uint32)); - } - t->parser = grpc_chttp2_settings_parser_parse; - t->parser_data = &t->simple_parsers.settings; - return ok; +static void end_all_the_calls(grpc_chttp2_transport *t) { + grpc_chttp2_for_all_streams(&t->global, NULL, cancel_stream_cb); } -static int init_frame_parser(transport *t) { - if (t->expect_continuation_stream_id != 0) { - if (t->incoming_frame_type != GRPC_CHTTP2_FRAME_CONTINUATION) { - gpr_log(GPR_ERROR, "Expected CONTINUATION frame, got frame type %02x", - t->incoming_frame_type); - return 0; - } - if (t->expect_continuation_stream_id != t->incoming_stream_id) { - gpr_log(GPR_ERROR, - "Expected CONTINUATION frame for stream %08x, got stream %08x", - t->expect_continuation_stream_id, t->incoming_stream_id); - return 0; - } - return init_header_frame_parser(t, 1); - } - switch (t->incoming_frame_type) { - case GRPC_CHTTP2_FRAME_DATA: - return init_data_frame_parser(t); - case GRPC_CHTTP2_FRAME_HEADER: - return init_header_frame_parser(t, 0); - case GRPC_CHTTP2_FRAME_CONTINUATION: - gpr_log(GPR_ERROR, "Unexpected CONTINUATION frame"); - return 0; - case GRPC_CHTTP2_FRAME_RST_STREAM: - return init_rst_stream_parser(t); - case GRPC_CHTTP2_FRAME_SETTINGS: - return init_settings_frame_parser(t); - case GRPC_CHTTP2_FRAME_WINDOW_UPDATE: - return init_window_update_frame_parser(t); - case GRPC_CHTTP2_FRAME_PING: - return init_ping_parser(t); - case GRPC_CHTTP2_FRAME_GOAWAY: - return init_goaway_parser(t); - default: - gpr_log(GPR_ERROR, "Unknown frame type %02x", t->incoming_frame_type); - return init_skip_frame(t, 0); +static void drop_connection(grpc_chttp2_transport *t) { + if (t->global.error_state == GRPC_CHTTP2_ERROR_STATE_NONE) { + t->global.error_state = GRPC_CHTTP2_ERROR_STATE_SEEN; } + close_transport_locked(t); + end_all_the_calls(t); } -static int is_window_update_legal(gpr_int64 window_update, gpr_int64 window) { - return window + window_update < MAX_WINDOW; -} - -static void add_metadata_batch(transport *t, stream *s) { - grpc_metadata_batch b; - - b.list.head = NULL; - /* Store away the last element of the list, so that in patch_metadata_ops - we can reconstitute the list. - We can't do list building here as later incoming metadata may reallocate - the underlying array. */ - b.list.tail = (void*)(gpr_intptr)s->incoming_metadata_count; - b.garbage.head = b.garbage.tail = NULL; - b.deadline = s->incoming_deadline; - s->incoming_deadline = gpr_inf_future; - - grpc_sopb_add_metadata(&s->parser.incoming_sopb, b); -} +/** update window from a settings change */ +static void update_global_window(void *args, gpr_uint32 id, void *stream) { + grpc_chttp2_transport *t = args; + grpc_chttp2_stream *s = stream; + grpc_chttp2_transport_global *transport_global = &t->global; + grpc_chttp2_stream_global *stream_global = &s->global; -static int parse_frame_slice(transport *t, gpr_slice slice, int is_last) { - grpc_chttp2_parse_state st; - size_t i; - memset(&st, 0, sizeof(st)); - switch (t->parser(t->parser_data, &st, slice, is_last)) { - case GRPC_CHTTP2_PARSE_OK: - if (st.end_of_stream) { - t->incoming_stream->read_closed = 1; - maybe_finish_read(t, t->incoming_stream); - } - if (st.need_flush_reads) { - maybe_finish_read(t, t->incoming_stream); - } - if (st.metadata_boundary) { - add_metadata_batch(t, t->incoming_stream); - maybe_finish_read(t, t->incoming_stream); - } - if (st.ack_settings) { - gpr_slice_buffer_add(&t->qbuf, grpc_chttp2_settings_ack_create()); - maybe_start_some_streams(t); - } - if (st.send_ping_ack) { - gpr_slice_buffer_add( - &t->qbuf, - grpc_chttp2_ping_create(1, t->simple_parsers.ping.opaque_8bytes)); - } - if (st.goaway) { - add_goaway(t, st.goaway_error, st.goaway_text); - } - if (st.rst_stream) { - cancel_stream_id( - t, t->incoming_stream_id, - grpc_chttp2_http2_error_to_grpc_status(st.rst_stream_reason), - st.rst_stream_reason, 0); - } - if (st.process_ping_reply) { - for (i = 0; i < t->ping_count; i++) { - if (0 == - memcmp(t->pings[i].id, t->simple_parsers.ping.opaque_8bytes, 8)) { - t->pings[i].cb(t->pings[i].user_data); - memmove(&t->pings[i], &t->pings[i + 1], - (t->ping_count - i - 1) * sizeof(outstanding_ping)); - t->ping_count--; - break; - } - } - } - if (st.initial_window_update) { - for (i = 0; i < t->stream_map.count; i++) { - stream *s = (stream *)(t->stream_map.values[i]); - int was_window_empty = s->outgoing_window <= 0; - FLOWCTL_TRACE(t, s, outgoing, s->id, st.initial_window_update); - s->outgoing_window += st.initial_window_update; - if (was_window_empty && s->outgoing_window > 0 && s->outgoing_sopb && - s->outgoing_sopb->nops > 0) { - stream_list_join(t, s, WRITABLE); - } - } - } - if (st.window_update) { - if (t->incoming_stream_id) { - /* if there was a stream id, this is for some stream */ - stream *s = lookup_stream(t, t->incoming_stream_id); - if (s) { - int was_window_empty = s->outgoing_window <= 0; - if (!is_window_update_legal(st.window_update, s->outgoing_window)) { - cancel_stream(t, s, grpc_chttp2_http2_error_to_grpc_status( - GRPC_CHTTP2_FLOW_CONTROL_ERROR), - GRPC_CHTTP2_FLOW_CONTROL_ERROR, NULL, 1); - } else { - FLOWCTL_TRACE(t, s, outgoing, s->id, st.window_update); - s->outgoing_window += st.window_update; - /* if this window update makes outgoing ops writable again, - flag that */ - if (was_window_empty && s->outgoing_sopb && - s->outgoing_sopb->nops > 0) { - stream_list_join(t, s, WRITABLE); - } - } - } - } else { - /* transport level window update */ - if (!is_window_update_legal(st.window_update, t->outgoing_window)) { - drop_connection(t); - } else { - FLOWCTL_TRACE(t, t, outgoing, 0, st.window_update); - t->outgoing_window += st.window_update; - } - } - } - return 1; - case GRPC_CHTTP2_STREAM_ERROR: - become_skip_parser(t); - cancel_stream_id( - t, t->incoming_stream_id, - grpc_chttp2_http2_error_to_grpc_status(GRPC_CHTTP2_INTERNAL_ERROR), - GRPC_CHTTP2_INTERNAL_ERROR, 1); - return 1; - case GRPC_CHTTP2_CONNECTION_ERROR: - drop_connection(t); - return 0; - } - gpr_log(GPR_ERROR, "should never reach here"); - abort(); - return 0; + GRPC_CHTTP2_FLOWCTL_TRACE_STREAM("settings", transport_global, stream_global, + outgoing_window, + t->parsing.initial_window_update); + stream_global->outgoing_window += t->parsing.initial_window_update; } -static int process_read(transport *t, gpr_slice slice) { - gpr_uint8 *beg = GPR_SLICE_START_PTR(slice); - gpr_uint8 *end = GPR_SLICE_END_PTR(slice); - gpr_uint8 *cur = beg; - - if (cur == end) return 1; - - switch (t->deframe_state) { - case DTS_CLIENT_PREFIX_0: - case DTS_CLIENT_PREFIX_1: - case DTS_CLIENT_PREFIX_2: - case DTS_CLIENT_PREFIX_3: - case DTS_CLIENT_PREFIX_4: - case DTS_CLIENT_PREFIX_5: - case DTS_CLIENT_PREFIX_6: - case DTS_CLIENT_PREFIX_7: - case DTS_CLIENT_PREFIX_8: - case DTS_CLIENT_PREFIX_9: - case DTS_CLIENT_PREFIX_10: - case DTS_CLIENT_PREFIX_11: - case DTS_CLIENT_PREFIX_12: - case DTS_CLIENT_PREFIX_13: - case DTS_CLIENT_PREFIX_14: - case DTS_CLIENT_PREFIX_15: - case DTS_CLIENT_PREFIX_16: - case DTS_CLIENT_PREFIX_17: - case DTS_CLIENT_PREFIX_18: - case DTS_CLIENT_PREFIX_19: - case DTS_CLIENT_PREFIX_20: - case DTS_CLIENT_PREFIX_21: - case DTS_CLIENT_PREFIX_22: - case DTS_CLIENT_PREFIX_23: - while (cur != end && t->deframe_state != DTS_FH_0) { - if (*cur != CLIENT_CONNECT_STRING[t->deframe_state]) { - gpr_log(GPR_ERROR, - "Connect string mismatch: expected '%c' (%d) got '%c' (%d) " - "at byte %d", - CLIENT_CONNECT_STRING[t->deframe_state], - (int)(gpr_uint8)CLIENT_CONNECT_STRING[t->deframe_state], *cur, - (int)*cur, t->deframe_state); - drop_connection(t); - return 0; - } - ++cur; - ++t->deframe_state; - } - if (cur == end) { - return 1; - } - /* fallthrough */ - dts_fh_0: - case DTS_FH_0: - GPR_ASSERT(cur < end); - t->incoming_frame_size = ((gpr_uint32)*cur) << 16; - if (++cur == end) { - t->deframe_state = DTS_FH_1; - return 1; - } - /* fallthrough */ - case DTS_FH_1: - GPR_ASSERT(cur < end); - t->incoming_frame_size |= ((gpr_uint32)*cur) << 8; - if (++cur == end) { - t->deframe_state = DTS_FH_2; - return 1; - } - /* fallthrough */ - case DTS_FH_2: - GPR_ASSERT(cur < end); - t->incoming_frame_size |= *cur; - if (++cur == end) { - t->deframe_state = DTS_FH_3; - return 1; - } - /* fallthrough */ - case DTS_FH_3: - GPR_ASSERT(cur < end); - t->incoming_frame_type = *cur; - if (++cur == end) { - t->deframe_state = DTS_FH_4; - return 1; - } - /* fallthrough */ - case DTS_FH_4: - GPR_ASSERT(cur < end); - t->incoming_frame_flags = *cur; - if (++cur == end) { - t->deframe_state = DTS_FH_5; - return 1; - } - /* fallthrough */ - case DTS_FH_5: - GPR_ASSERT(cur < end); - t->incoming_stream_id = (((gpr_uint32)*cur) & 0x7f) << 24; - if (++cur == end) { - t->deframe_state = DTS_FH_6; - return 1; - } - /* fallthrough */ - case DTS_FH_6: - GPR_ASSERT(cur < end); - t->incoming_stream_id |= ((gpr_uint32)*cur) << 16; - if (++cur == end) { - t->deframe_state = DTS_FH_7; - return 1; - } - /* fallthrough */ - case DTS_FH_7: - GPR_ASSERT(cur < end); - t->incoming_stream_id |= ((gpr_uint32)*cur) << 8; - if (++cur == end) { - t->deframe_state = DTS_FH_8; - return 1; - } - /* fallthrough */ - case DTS_FH_8: - GPR_ASSERT(cur < end); - t->incoming_stream_id |= ((gpr_uint32)*cur); - t->deframe_state = DTS_FRAME; - if (!init_frame_parser(t)) { - return 0; - } - /* t->last_incoming_stream_id is used as last-stream-id when - sending GOAWAY frame. - https://tools.ietf.org/html/draft-ietf-httpbis-http2-17#section-6.8 - says that last-stream-id is peer-initiated stream ID. So, - since we don't have server pushed streams, client should send - GOAWAY last-stream-id=0 in this case. */ - if (!t->is_client) { - t->last_incoming_stream_id = t->incoming_stream_id; - } - if (t->incoming_frame_size == 0) { - if (!parse_frame_slice(t, gpr_empty_slice(), 1)) { - return 0; - } - if (++cur == end) { - t->deframe_state = DTS_FH_0; - return 1; - } - goto dts_fh_0; /* loop */ - } - if (++cur == end) { - return 1; - } - /* fallthrough */ - case DTS_FRAME: - GPR_ASSERT(cur < end); - if ((gpr_uint32)(end - cur) == t->incoming_frame_size) { - if (!parse_frame_slice( - t, gpr_slice_sub_no_ref(slice, cur - beg, end - beg), 1)) { - return 0; - } - t->deframe_state = DTS_FH_0; - return 1; - } else if ((gpr_uint32)(end - cur) > t->incoming_frame_size) { - if (!parse_frame_slice( - t, gpr_slice_sub_no_ref(slice, cur - beg, - cur + t->incoming_frame_size - beg), - 1)) { - return 0; - } - cur += t->incoming_frame_size; - goto dts_fh_0; /* loop */ - } else { - if (!parse_frame_slice( - t, gpr_slice_sub_no_ref(slice, cur - beg, end - beg), 0)) { - return 0; - } - t->incoming_frame_size -= (end - cur); - return 1; - } - gpr_log(GPR_ERROR, "should never reach here"); - abort(); +static void read_error_locked(grpc_chttp2_transport *t) { + t->endpoint_reading = 0; + if (!t->writing_active && t->ep) { + grpc_endpoint_destroy(t->ep); + t->ep = NULL; + /* safe as we still have a ref for read */ + UNREF_TRANSPORT(t, "disconnect"); } - - gpr_log(GPR_ERROR, "should never reach here"); - abort(); - - return 0; } /* tcp read callback */ static void recv_data(void *tp, gpr_slice *slices, size_t nslices, grpc_endpoint_cb_status error) { - transport *t = tp; + grpc_chttp2_transport *t = tp; size_t i; - int keep_reading = 0; + int unref = 0; switch (error) { case GRPC_ENDPOINT_CB_SHUTDOWN: @@ -1983,185 +898,201 @@ static void recv_data(void *tp, gpr_slice *slices, size_t nslices, case GRPC_ENDPOINT_CB_ERROR: lock(t); drop_connection(t); - t->reading = 0; - if (!t->writing && t->ep) { - grpc_endpoint_destroy(t->ep); - t->ep = NULL; - unref_transport(t); /* safe as we still have a ref for read */ - } + read_error_locked(t); unlock(t); - unref_transport(t); + unref = 1; + for (i = 0; i < nslices; i++) gpr_slice_unref(slices[i]); break; case GRPC_ENDPOINT_CB_OK: lock(t); - if (t->cb) { - for (i = 0; i < nslices && process_read(t, slices[i]); i++) - ; + i = 0; + GPR_ASSERT(!t->parsing_active); + if (t->global.error_state == GRPC_CHTTP2_ERROR_STATE_NONE) { + t->parsing_active = 1; + /* merge stream lists */ + grpc_chttp2_stream_map_move_into(&t->new_stream_map, + &t->parsing_stream_map); + grpc_chttp2_prepare_to_read(&t->global, &t->parsing); + gpr_mu_unlock(&t->mu); + for (; i < nslices && grpc_chttp2_perform_read(&t->parsing, slices[i]); + i++) { + gpr_slice_unref(slices[i]); + } + gpr_mu_lock(&t->mu); + if (i != nslices) { + drop_connection(t); + } + /* merge stream lists */ + grpc_chttp2_stream_map_move_into(&t->new_stream_map, + &t->parsing_stream_map); + t->global.concurrent_stream_count = grpc_chttp2_stream_map_size(&t->parsing_stream_map); + if (t->parsing.initial_window_update != 0) { + grpc_chttp2_stream_map_for_each(&t->parsing_stream_map, + update_global_window, t); + t->parsing.initial_window_update = 0; + } + /* handle higher level things */ + grpc_chttp2_publish_reads(&t->global, &t->parsing); + t->parsing_active = 0; + } + if (i == nslices) { + grpc_chttp2_schedule_closure(&t->global, &t->reading_action, 1); + } else { + read_error_locked(t); + unref = 1; } unlock(t); - keep_reading = 1; + for (; i < nslices; i++) gpr_slice_unref(slices[i]); break; } - - for (i = 0; i < nslices; i++) gpr_slice_unref(slices[i]); - - if (keep_reading) { - grpc_endpoint_notify_on_read(t->ep, recv_data, t); + if (unref) { + UNREF_TRANSPORT(t, "recv_data"); } } +static void reading_action(void *pt, int iomgr_success_ignored) { + grpc_chttp2_transport *t = pt; + grpc_endpoint_notify_on_read(t->ep, recv_data, t); +} + /* * CALLBACK LOOP */ -static grpc_stream_state compute_state(gpr_uint8 write_closed, - gpr_uint8 read_closed) { - if (write_closed && read_closed) return GRPC_STREAM_CLOSED; - if (write_closed) return GRPC_STREAM_SEND_CLOSED; - if (read_closed) return GRPC_STREAM_RECV_CLOSED; - return GRPC_STREAM_OPEN; -} +typedef struct { + grpc_chttp2_transport *t; + gpr_uint32 error; + gpr_slice text; + grpc_iomgr_closure closure; +} notify_goaways_args; -static void patch_metadata_ops(stream *s) { - grpc_stream_op *ops = s->incoming_sopb->ops; - size_t nops = s->incoming_sopb->nops; - size_t i; - size_t j; - size_t mdidx = 0; - size_t last_mdidx; - int found_metadata = 0; - - /* rework the array of metadata into a linked list, making use - of the breadcrumbs we left in metadata batches during - add_metadata_batch */ - for (i = 0; i < nops; i++) { - grpc_stream_op *op = &ops[i]; - if (op->type != GRPC_OP_METADATA) continue; - found_metadata = 1; - /* we left a breadcrumb indicating where the end of this list is, - and since we add sequentially, we know from the end of the last - segment where this segment begins */ - last_mdidx = (size_t)(gpr_intptr)(op->data.metadata.list.tail); - GPR_ASSERT(last_mdidx > mdidx); - GPR_ASSERT(last_mdidx <= s->incoming_metadata_count); - /* turn the array into a doubly linked list */ - op->data.metadata.list.head = &s->incoming_metadata[mdidx]; - op->data.metadata.list.tail = &s->incoming_metadata[last_mdidx - 1]; - for (j = mdidx + 1; j < last_mdidx; j++) { - s->incoming_metadata[j].prev = &s->incoming_metadata[j-1]; - s->incoming_metadata[j-1].next = &s->incoming_metadata[j]; - } - s->incoming_metadata[mdidx].prev = NULL; - s->incoming_metadata[last_mdidx-1].next = NULL; - /* track where we're up to */ - mdidx = last_mdidx; - } - if (found_metadata) { - s->old_incoming_metadata = s->incoming_metadata; - if (mdidx != s->incoming_metadata_count) { - /* we have a partially read metadata batch still in incoming_metadata */ - size_t new_count = s->incoming_metadata_count - mdidx; - size_t copy_bytes = sizeof(*s->incoming_metadata) * new_count; - GPR_ASSERT(mdidx < s->incoming_metadata_count); - s->incoming_metadata = gpr_malloc(copy_bytes); - memcpy(s->old_incoming_metadata + mdidx, s->incoming_metadata, copy_bytes); - s->incoming_metadata_count = s->incoming_metadata_capacity = new_count; - } else { - s->incoming_metadata = NULL; - s->incoming_metadata_count = 0; - s->incoming_metadata_capacity = 0; - } - } -} +static void notify_goaways(void *p, int iomgr_success_ignored) { + notify_goaways_args *a = p; + grpc_chttp2_transport *t = a->t; -static void finish_reads(transport *t) { - stream *s; - - while ((s = stream_list_remove_head(t, FINISHED_READ_OP)) != NULL) { - int publish = 0; - GPR_ASSERT(s->incoming_sopb); - *s->publish_state = - compute_state(s->write_state == WRITE_STATE_SENT_CLOSE, s->read_closed); - if (*s->publish_state != s->published_state) { - s->published_state = *s->publish_state; - publish = 1; - if (s->published_state == GRPC_STREAM_CLOSED) { - remove_from_stream_map(t, s); - } - } - if (s->parser.incoming_sopb.nops > 0) { - grpc_sopb_swap(s->incoming_sopb, &s->parser.incoming_sopb); - publish = 1; - } - if (publish) { - if (s->incoming_metadata_count > 0) { - patch_metadata_ops(s); - } - s->incoming_sopb = NULL; - schedule_cb(t, s->recv_done_closure, 1); - } - } + t->channel_callback.cb->goaway(t->channel_callback.cb_user_data, &t->base, + a->error, a->text); -} + gpr_free(a); -static void schedule_cb(transport *t, op_closure closure, int success) { - if (t->pending_callbacks.capacity == t->pending_callbacks.count) { - t->pending_callbacks.capacity = - GPR_MAX(t->pending_callbacks.capacity * 2, 8); - t->pending_callbacks.callbacks = - gpr_realloc(t->pending_callbacks.callbacks, - t->pending_callbacks.capacity * - sizeof(*t->pending_callbacks.callbacks)); - } - closure.success = success; - t->pending_callbacks.callbacks[t->pending_callbacks.count++] = closure; + lock(t); + t->channel_callback.executing = 0; + unlock(t); + + UNREF_TRANSPORT(t, "notify_goaways"); } -static int prepare_callbacks(transport *t) { - op_closure_array temp = t->pending_callbacks; - t->pending_callbacks = t->executing_callbacks; - t->executing_callbacks = temp; - return t->executing_callbacks.count > 0; +static void notify_closed(void *gt, int iomgr_success_ignored) { + grpc_chttp2_transport *t = gt; + t->channel_callback.cb->closed(t->channel_callback.cb_user_data, &t->base); + + lock(t); + t->channel_callback.executing = 0; + unlock(t); + + UNREF_TRANSPORT(t, "notify_closed"); } -static void run_callbacks(transport *t, const grpc_transport_callbacks *cb) { - size_t i; - for (i = 0; i < t->executing_callbacks.count; i++) { - op_closure c = t->executing_callbacks.callbacks[i]; - c.cb(c.user_data, c.success); +static void unlock_check_channel_callbacks(grpc_chttp2_transport *t) { + if (t->channel_callback.executing) { + return; + } + if (t->global.goaway_state != GRPC_CHTTP2_ERROR_STATE_NONE) { + if (t->global.goaway_state == GRPC_CHTTP2_ERROR_STATE_SEEN && + t->global.error_state != GRPC_CHTTP2_ERROR_STATE_NOTIFIED) { + notify_goaways_args *a = gpr_malloc(sizeof(*a)); + a->t = t; + a->error = t->global.goaway_error; + a->text = t->global.goaway_text; + t->global.goaway_state = GRPC_CHTTP2_ERROR_STATE_NOTIFIED; + t->channel_callback.executing = 1; + grpc_iomgr_closure_init(&a->closure, notify_goaways, a); + REF_TRANSPORT(t, "notify_goaways"); + grpc_chttp2_schedule_closure(&t->global, &a->closure, 1); + return; + } else if (t->global.goaway_state != GRPC_CHTTP2_ERROR_STATE_NOTIFIED) { + return; + } + } + if (t->global.error_state == GRPC_CHTTP2_ERROR_STATE_SEEN) { + t->global.error_state = GRPC_CHTTP2_ERROR_STATE_NOTIFIED; + t->channel_callback.executing = 1; + REF_TRANSPORT(t, "notify_closed"); + grpc_chttp2_schedule_closure(&t->global, &t->channel_callback.notify_closed, + 1); } - t->executing_callbacks.count = 0; } -static void call_cb_closed(transport *t, const grpc_transport_callbacks *cb) { - cb->closed(t->cb_user_data, &t->base); +void grpc_chttp2_schedule_closure( + grpc_chttp2_transport_global *transport_global, grpc_iomgr_closure *closure, + int success) { + closure->success = success; + closure->next = transport_global->pending_closures; + transport_global->pending_closures = closure; } /* * POLLSET STUFF */ -static void add_to_pollset_locked(transport *t, grpc_pollset *pollset) { +static void add_to_pollset_locked(grpc_chttp2_transport *t, + grpc_pollset *pollset) { if (t->ep) { grpc_endpoint_add_to_pollset(t->ep, pollset); } } static void add_to_pollset(grpc_transport *gt, grpc_pollset *pollset) { - transport *t = (transport *)gt; + grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt; lock(t); add_to_pollset_locked(t, pollset); unlock(t); } /* + * TRACING + */ + +void grpc_chttp2_flowctl_trace(const char *file, int line, const char *reason, + const char *context, const char *var, + int is_client, gpr_uint32 stream_id, + gpr_int64 current_value, gpr_int64 delta) { + char *identifier; + char *context_scope; + char *context_thread; + char *underscore_pos = strchr(context, '_'); + GPR_ASSERT(underscore_pos); + context_thread = gpr_strdup(underscore_pos + 1); + context_scope = gpr_strdup(context); + context_scope[underscore_pos - context] = 0; + if (stream_id) { + gpr_asprintf(&identifier, "%s[%d]", context_scope, stream_id); + } else { + identifier = gpr_strdup(context_scope); + } + gpr_log(GPR_INFO, + "FLOWCTL: %s %-10s %8s %-23s %8lld %c %8lld = %8lld %-10s [%s:%d]", + is_client ? "client" : "server", identifier, context_thread, var, + current_value, delta < 0 ? '-' : '+', delta < 0 ? -delta : delta, + current_value + delta, reason, file, line); + gpr_free(identifier); + gpr_free(context_thread); + gpr_free(context_scope); +} + +/* * INTEGRATION GLUE */ -static const grpc_transport_vtable vtable = { - sizeof(stream), init_stream, perform_op, - add_to_pollset, destroy_stream, goaway, - close_transport, send_ping, destroy_transport}; +static const grpc_transport_vtable vtable = {sizeof(grpc_chttp2_stream), + init_stream, + perform_op, + add_to_pollset, + destroy_stream, + goaway, + close_transport, + send_ping, + destroy_transport}; void grpc_create_chttp2_transport(grpc_transport_setup_callback setup, void *arg, @@ -2169,7 +1100,7 @@ void grpc_create_chttp2_transport(grpc_transport_setup_callback setup, grpc_endpoint *ep, gpr_slice *slices, size_t nslices, grpc_mdctx *mdctx, int is_client) { - transport *t = gpr_malloc(sizeof(transport)); + grpc_chttp2_transport *t = gpr_malloc(sizeof(grpc_chttp2_transport)); init_transport(t, setup, arg, channel_args, ep, slices, nslices, mdctx, is_client); } diff --git a/src/core/transport/chttp2_transport.h b/src/core/transport/chttp2_transport.h index fad714fabf..18e19f03af 100644 --- a/src/core/transport/chttp2_transport.h +++ b/src/core/transport/chttp2_transport.h @@ -47,4 +47,4 @@ void grpc_create_chttp2_transport(grpc_transport_setup_callback setup, size_t nslices, grpc_mdctx *metadata_context, int is_client); -#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_TRANSPORT_H */ +#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_TRANSPORT_H */ diff --git a/src/core/transport/stream_op.c b/src/core/transport/stream_op.c index 8996ecac35..81df5455f6 100644 --- a/src/core/transport/stream_op.c +++ b/src/core/transport/stream_op.c @@ -65,7 +65,7 @@ void grpc_sopb_swap(grpc_stream_op_buffer *a, grpc_stream_op_buffer *b) { if (a->ops == a->inlined_ops) { if (b->ops == b->inlined_ops) { /* swap contents of inlined buffer */ - gpr_slice temp[GRPC_SOPB_INLINE_ELEMENTS]; + grpc_stream_op temp[GRPC_SOPB_INLINE_ELEMENTS]; memcpy(temp, a->ops, b->nops * sizeof(grpc_stream_op)); memcpy(a->ops, b->ops, a->nops * sizeof(grpc_stream_op)); memcpy(b->ops, temp, b->nops * sizeof(grpc_stream_op)); @@ -163,6 +163,18 @@ void grpc_sopb_append(grpc_stream_op_buffer *sopb, grpc_stream_op *ops, sopb->nops = new_nops; } +void grpc_sopb_move_to(grpc_stream_op_buffer *src, grpc_stream_op_buffer *dst) { + if (src->nops == 0) { + return; + } + if (dst->nops == 0) { + grpc_sopb_swap(src, dst); + return; + } + grpc_sopb_append(dst, src->ops, src->nops); + src->nops = 0; +} + static void assert_valid_list(grpc_mdelem_list *list) { #ifndef NDEBUG grpc_linked_mdelem *l; @@ -184,34 +196,34 @@ static void assert_valid_list(grpc_mdelem_list *list) { } #ifndef NDEBUG -void grpc_metadata_batch_assert_ok(grpc_metadata_batch *comd) { - assert_valid_list(&comd->list); - assert_valid_list(&comd->garbage); +void grpc_metadata_batch_assert_ok(grpc_metadata_batch *batch) { + assert_valid_list(&batch->list); + assert_valid_list(&batch->garbage); } #endif /* NDEBUG */ -void grpc_metadata_batch_init(grpc_metadata_batch *comd) { - comd->list.head = comd->list.tail = comd->garbage.head = comd->garbage.tail = +void grpc_metadata_batch_init(grpc_metadata_batch *batch) { + batch->list.head = batch->list.tail = batch->garbage.head = batch->garbage.tail = NULL; - comd->deadline = gpr_inf_future; + batch->deadline = gpr_inf_future; } -void grpc_metadata_batch_destroy(grpc_metadata_batch *comd) { +void grpc_metadata_batch_destroy(grpc_metadata_batch *batch) { grpc_linked_mdelem *l; - for (l = comd->list.head; l; l = l->next) { + for (l = batch->list.head; l; l = l->next) { grpc_mdelem_unref(l->md); } - for (l = comd->garbage.head; l; l = l->next) { + for (l = batch->garbage.head; l; l = l->next) { grpc_mdelem_unref(l->md); } } -void grpc_metadata_batch_add_head(grpc_metadata_batch *comd, +void grpc_metadata_batch_add_head(grpc_metadata_batch *batch, grpc_linked_mdelem *storage, grpc_mdelem *elem_to_add) { GPR_ASSERT(elem_to_add); storage->md = elem_to_add; - grpc_metadata_batch_link_head(comd, storage); + grpc_metadata_batch_link_head(batch, storage); } static void link_head(grpc_mdelem_list *list, grpc_linked_mdelem *storage) { @@ -228,17 +240,17 @@ static void link_head(grpc_mdelem_list *list, grpc_linked_mdelem *storage) { assert_valid_list(list); } -void grpc_metadata_batch_link_head(grpc_metadata_batch *comd, +void grpc_metadata_batch_link_head(grpc_metadata_batch *batch, grpc_linked_mdelem *storage) { - link_head(&comd->list, storage); + link_head(&batch->list, storage); } -void grpc_metadata_batch_add_tail(grpc_metadata_batch *comd, +void grpc_metadata_batch_add_tail(grpc_metadata_batch *batch, grpc_linked_mdelem *storage, grpc_mdelem *elem_to_add) { GPR_ASSERT(elem_to_add); storage->md = elem_to_add; - grpc_metadata_batch_link_tail(comd, storage); + grpc_metadata_batch_link_tail(batch, storage); } static void link_tail(grpc_mdelem_list *list, grpc_linked_mdelem *storage) { @@ -255,9 +267,9 @@ static void link_tail(grpc_mdelem_list *list, grpc_linked_mdelem *storage) { assert_valid_list(list); } -void grpc_metadata_batch_link_tail(grpc_metadata_batch *comd, +void grpc_metadata_batch_link_tail(grpc_metadata_batch *batch, grpc_linked_mdelem *storage) { - link_tail(&comd->list, storage); + link_tail(&batch->list, storage); } void grpc_metadata_batch_merge(grpc_metadata_batch *target, @@ -274,16 +286,16 @@ void grpc_metadata_batch_merge(grpc_metadata_batch *target, } } -void grpc_metadata_batch_filter(grpc_metadata_batch *comd, +void grpc_metadata_batch_filter(grpc_metadata_batch *batch, grpc_mdelem *(*filter)(void *user_data, grpc_mdelem *elem), void *user_data) { grpc_linked_mdelem *l; grpc_linked_mdelem *next; - assert_valid_list(&comd->list); - assert_valid_list(&comd->garbage); - for (l = comd->list.head; l; l = next) { + assert_valid_list(&batch->list); + assert_valid_list(&batch->garbage); + for (l = batch->list.head; l; l = next) { grpc_mdelem *orig = l->md; grpc_mdelem *filt = filter(user_data, orig); next = l->next; @@ -294,19 +306,19 @@ void grpc_metadata_batch_filter(grpc_metadata_batch *comd, if (l->next) { l->next->prev = l->prev; } - if (comd->list.head == l) { - comd->list.head = l->next; + if (batch->list.head == l) { + batch->list.head = l->next; } - if (comd->list.tail == l) { - comd->list.tail = l->prev; + if (batch->list.tail == l) { + batch->list.tail = l->prev; } - assert_valid_list(&comd->list); - link_head(&comd->garbage, l); + assert_valid_list(&batch->list); + link_head(&batch->garbage, l); } else if (filt != orig) { grpc_mdelem_unref(orig); l->md = filt; } } - assert_valid_list(&comd->list); - assert_valid_list(&comd->garbage); + assert_valid_list(&batch->list); + assert_valid_list(&batch->garbage); } diff --git a/src/core/transport/stream_op.h b/src/core/transport/stream_op.h index 95497a3cc8..842fc932b9 100644 --- a/src/core/transport/stream_op.h +++ b/src/core/transport/stream_op.h @@ -58,11 +58,18 @@ typedef enum grpc_stream_op_code { GRPC_OP_SLICE } grpc_stream_op_code; -/* Arguments for GRPC_OP_BEGIN */ +/** Internal bit flag for grpc_begin_message's \a flags signaling the use of + * compression for the message */ +#define GRPC_WRITE_INTERNAL_COMPRESS (0x80000000u) +/** Mask of all valid internal flags. */ +#define GRPC_WRITE_INTERNAL_USED_MASK (GRPC_WRITE_INTERNAL_COMPRESS) + +/* Arguments for GRPC_OP_BEGIN_MESSAGE */ typedef struct grpc_begin_message { /* How many bytes of data will this message contain */ gpr_uint32 length; - /* Write flags for the message: see grpc.h GRPC_WRITE_xxx */ + /* Write flags for the message: see grpc.h GRPC_WRITE_* for the public bits, + * GRPC_WRITE_INTERNAL_* for the internal ones. */ gpr_uint32 flags; } grpc_begin_message; @@ -78,29 +85,62 @@ typedef struct grpc_mdelem_list { } grpc_mdelem_list; typedef struct grpc_metadata_batch { + /** Metadata elements in this batch */ grpc_mdelem_list list; + /** Elements that have been removed from the batch, but have + not yet been unreffed - used to allow collecting garbage + under a single metadata context lock */ grpc_mdelem_list garbage; + /** Used to calculate grpc-timeout at the point of sending, + or gpr_inf_future if this batch does not need to send a + grpc-timeout */ gpr_timespec deadline; } grpc_metadata_batch; -void grpc_metadata_batch_init(grpc_metadata_batch *comd); -void grpc_metadata_batch_destroy(grpc_metadata_batch *comd); +void grpc_metadata_batch_init(grpc_metadata_batch *batch); +void grpc_metadata_batch_destroy(grpc_metadata_batch *batch); void grpc_metadata_batch_merge(grpc_metadata_batch *target, grpc_metadata_batch *add); -void grpc_metadata_batch_link_head(grpc_metadata_batch *comd, +/** Add \a storage to the beginning of \a batch. storage->md is + assumed to be valid. + \a storage is owned by the caller and must survive for the + lifetime of batch. This usually means it should be around + for the lifetime of the call. */ +void grpc_metadata_batch_link_head(grpc_metadata_batch *batch, grpc_linked_mdelem *storage); -void grpc_metadata_batch_link_tail(grpc_metadata_batch *comd, +/** Add \a storage to the end of \a batch. storage->md is + assumed to be valid. + \a storage is owned by the caller and must survive for the + lifetime of batch. This usually means it should be around + for the lifetime of the call. */ +void grpc_metadata_batch_link_tail(grpc_metadata_batch *batch, grpc_linked_mdelem *storage); -void grpc_metadata_batch_add_head(grpc_metadata_batch *comd, +/** Add \a elem_to_add as the first element in \a batch, using + \a storage as backing storage for the linked list element. + \a storage is owned by the caller and must survive for the + lifetime of batch. This usually means it should be around + for the lifetime of the call. + Takes ownership of \a elem_to_add */ +void grpc_metadata_batch_add_head(grpc_metadata_batch *batch, grpc_linked_mdelem *storage, grpc_mdelem *elem_to_add); -void grpc_metadata_batch_add_tail(grpc_metadata_batch *comd, +/** Add \a elem_to_add as the last element in \a batch, using + \a storage as backing storage for the linked list element. + \a storage is owned by the caller and must survive for the + lifetime of batch. This usually means it should be around + for the lifetime of the call. + Takes ownership of \a elem_to_add */ +void grpc_metadata_batch_add_tail(grpc_metadata_batch *batch, grpc_linked_mdelem *storage, grpc_mdelem *elem_to_add); -void grpc_metadata_batch_filter(grpc_metadata_batch *comd, +/** For each element in \a batch, execute \a filter. + The return value from \a filter will be substituted for the + grpc_mdelem passed to \a filter. If \a filter returns NULL, + the element will be moved to the garbage list. */ +void grpc_metadata_batch_filter(grpc_metadata_batch *batch, grpc_mdelem *(*filter)(void *user_data, grpc_mdelem *elem), void *user_data); @@ -126,10 +166,8 @@ typedef struct grpc_stream_op { } data; } grpc_stream_op; -/* A stream op buffer is a wrapper around stream operations that is dynamically - extendable. - TODO(ctiller): inline a few elements into the struct, to avoid common case - per-call allocations. */ +/** A stream op buffer is a wrapper around stream operations that is + * dynamically extendable. */ typedef struct grpc_stream_op_buffer { grpc_stream_op *ops; size_t nops; @@ -161,6 +199,8 @@ void grpc_sopb_add_slice(grpc_stream_op_buffer *sopb, gpr_slice slice); void grpc_sopb_append(grpc_stream_op_buffer *sopb, grpc_stream_op *ops, size_t nops); +void grpc_sopb_move_to(grpc_stream_op_buffer *src, grpc_stream_op_buffer *dst); + char *grpc_sopb_string(grpc_stream_op_buffer *sopb); #endif /* GRPC_INTERNAL_CORE_TRANSPORT_STREAM_OP_H */ diff --git a/src/core/transport/transport.c b/src/core/transport/transport.c index a9948cd4b2..39d7b701f2 100644 --- a/src/core/transport/transport.c +++ b/src/core/transport/transport.c @@ -73,9 +73,8 @@ void grpc_transport_destroy_stream(grpc_transport *transport, transport->vtable->destroy_stream(transport, stream); } -void grpc_transport_ping(grpc_transport *transport, void (*cb)(void *user_data), - void *user_data) { - transport->vtable->ping(transport, cb, user_data); +void grpc_transport_ping(grpc_transport *transport, grpc_iomgr_closure *cb) { + transport->vtable->ping(transport, cb); } void grpc_transport_setup_cancel(grpc_transport_setup *setup) { @@ -98,13 +97,13 @@ void grpc_transport_setup_del_interested_party(grpc_transport_setup *setup, void grpc_transport_op_finish_with_failure(grpc_transport_op *op) { if (op->send_ops) { - op->on_done_send(op->send_user_data, 0); + op->on_done_send->cb(op->on_done_send->cb_arg, 0); } if (op->recv_ops) { - op->on_done_recv(op->recv_user_data, 0); + op->on_done_recv->cb(op->on_done_recv->cb_arg, 0); } if (op->on_consumed) { - op->on_consumed(op->on_consumed_user_data, 0); + op->on_consumed->cb(op->on_consumed->cb_arg, 0); } } @@ -113,8 +112,8 @@ void grpc_transport_op_add_cancellation(grpc_transport_op *op, grpc_mdstr *message) { if (op->cancel_with_status == GRPC_STATUS_OK) { op->cancel_with_status = status; - op->cancel_message = message; - } else if (message) { + } + if (message) { grpc_mdstr_unref(message); } } diff --git a/src/core/transport/transport.h b/src/core/transport/transport.h index 98fcbe752e..07c25dfbbf 100644 --- a/src/core/transport/transport.h +++ b/src/core/transport/transport.h @@ -64,13 +64,11 @@ typedef enum grpc_stream_state { /* Transport op: a set of operations to perform on a transport */ typedef struct grpc_transport_op { - void (*on_consumed)(void *user_data, int success); - void *on_consumed_user_data; + grpc_iomgr_closure *on_consumed; grpc_stream_op_buffer *send_ops; int is_last_send; - void (*on_done_send)(void *user_data, int success); - void *send_user_data; + grpc_iomgr_closure *on_done_send; grpc_stream_op_buffer *recv_ops; grpc_stream_state *recv_state; @@ -80,13 +78,11 @@ typedef struct grpc_transport_op { Offers are not retractable: if 5 bytes are offered and no bytes are read, a later offer of 3 bytes still implies that 5 have been offered. */ gpr_uint32 max_recv_bytes; - void (*on_done_recv)(void *user_data, int success); - void *recv_user_data; + grpc_iomgr_closure *on_done_recv; grpc_pollset *bind_pollset; grpc_status_code cancel_with_status; - grpc_mdstr *cancel_message; /* Indexes correspond to grpc_context_index enum values */ grpc_call_context_element *context; @@ -173,11 +169,8 @@ void grpc_transport_perform_op(grpc_transport *transport, grpc_stream *stream, /* Send a ping on a transport - Calls cb with user data when a response is received. - cb *MAY* be called with arbitrary transport level locks held. It is not safe - to call into the transport during cb. */ -void grpc_transport_ping(grpc_transport *transport, void (*cb)(void *user_data), - void *user_data); + Calls cb with user data when a response is received. */ +void grpc_transport_ping(grpc_transport *transport, grpc_iomgr_closure *cb); /* Advise peer of pending connection termination. */ void grpc_transport_goaway(grpc_transport *transport, grpc_status_code status, diff --git a/src/core/transport/transport_impl.h b/src/core/transport/transport_impl.h index 479e15338f..c51951b7a7 100644 --- a/src/core/transport/transport_impl.h +++ b/src/core/transport/transport_impl.h @@ -63,8 +63,7 @@ typedef struct grpc_transport_vtable { void (*close)(grpc_transport *self); /* implementation of grpc_transport_ping */ - void (*ping)(grpc_transport *self, void (*cb)(void *user_data), - void *user_data); + void (*ping)(grpc_transport *self, grpc_iomgr_closure *cb); /* implementation of grpc_transport_destroy */ void (*destroy)(grpc_transport *self); diff --git a/src/core/transport/transport_op_string.c b/src/core/transport/transport_op_string.c index 11d5a9d780..699ffad602 100644 --- a/src/core/transport/transport_op_string.c +++ b/src/core/transport/transport_op_string.c @@ -39,6 +39,7 @@ #include "src/core/support/string.h" #include <grpc/support/alloc.h> +#include <grpc/support/string_util.h> #include <grpc/support/useful.h> /* These routines are here to facilitate debugging - they produce string @@ -144,11 +145,6 @@ char *grpc_transport_op_string(grpc_transport_op *op) { first = 0; gpr_asprintf(&tmp, "CANCEL:%d", op->cancel_with_status); gpr_strvec_add(&b, tmp); - if (op->cancel_message) { - gpr_asprintf(&tmp, ";msg='%s'", - grpc_mdstr_as_c_string(op->cancel_message)); - gpr_strvec_add(&b, tmp); - } } out = gpr_strvec_flatten(&b, NULL); diff --git a/src/core/tsi/ssl_transport_security.c b/src/core/tsi/ssl_transport_security.c index 63b4c42131..6156a39d09 100644 --- a/src/core/tsi/ssl_transport_security.c +++ b/src/core/tsi/ssl_transport_security.c @@ -54,8 +54,16 @@ #define TSI_SSL_MAX_PROTECTED_FRAME_SIZE_UPPER_BOUND 16384 #define TSI_SSL_MAX_PROTECTED_FRAME_SIZE_LOWER_BOUND 1024 + +/* Putting a macro like this and littering the source file with #if is really + bad practice. + TODO(jboeuf): refactor all the #if / #endif in a separate module. */ +#ifndef TSI_OPENSSL_ALPN_SUPPORT +#define TSI_OPENSSL_ALPN_SUPPORT 1 +#endif + /* TODO(jboeuf): I have not found a way to get this number dynamically from the - * SSL structure. This is what we would ultimately want though... */ + SSL structure. This is what we would ultimately want though... */ #define TSI_SSL_MAX_PROTECTION_OVERHEAD 100 /* --- Structure definitions. ---*/ @@ -70,6 +78,8 @@ struct tsi_ssl_handshaker_factory { typedef struct { tsi_ssl_handshaker_factory base; SSL_CTX* ssl_context; + unsigned char* alpn_protocol_list; + size_t alpn_protocol_list_length; } tsi_ssl_client_handshaker_factory; typedef struct { @@ -841,7 +851,7 @@ static tsi_result ssl_handshaker_process_bytes_from_peer( static tsi_result ssl_handshaker_extract_peer(tsi_handshaker* self, tsi_peer* peer) { tsi_result result = TSI_OK; - const unsigned char* alpn_selected; + const unsigned char* alpn_selected = NULL; unsigned int alpn_selected_len; tsi_ssl_handshaker* impl = (tsi_ssl_handshaker*)self; X509* peer_cert = SSL_get_peer_certificate(impl->ssl); @@ -850,7 +860,14 @@ static tsi_result ssl_handshaker_extract_peer(tsi_handshaker* self, X509_free(peer_cert); if (result != TSI_OK) return result; } +#if TSI_OPENSSL_ALPN_SUPPORT SSL_get0_alpn_selected(impl->ssl, &alpn_selected, &alpn_selected_len); +#endif /* TSI_OPENSSL_ALPN_SUPPORT */ + if (alpn_selected == NULL) { + /* Try npn. */ + SSL_get0_next_proto_negotiated(impl->ssl, &alpn_selected, + &alpn_selected_len); + } if (alpn_selected != NULL) { size_t i; tsi_peer_property* new_properties = @@ -1012,6 +1029,32 @@ static tsi_result create_tsi_ssl_handshaker(SSL_CTX* ctx, int is_client, return TSI_OK; } +static int select_protocol_list(const unsigned char** out, + unsigned char* outlen, + const unsigned char* client_list, + unsigned int client_list_len, + const unsigned char* server_list, + unsigned int server_list_len) { + const unsigned char* client_current = client_list; + while ((unsigned int)(client_current - client_list) < client_list_len) { + unsigned char client_current_len = *(client_current++); + const unsigned char* server_current = server_list; + while ((server_current >= server_list) && + (gpr_uintptr)(server_current - server_list) < server_list_len) { + unsigned char server_current_len = *(server_current++); + if ((client_current_len == server_current_len) && + !memcmp(client_current, server_current, server_current_len)) { + *out = server_current; + *outlen = server_current_len; + return SSL_TLSEXT_ERR_OK; + } + server_current += server_current_len; + } + client_current += client_current_len; + } + return SSL_TLSEXT_ERR_NOACK; +} + /* --- tsi_ssl__client_handshaker_factory methods implementation. --- */ static tsi_result ssl_client_handshaker_factory_create_handshaker( @@ -1027,10 +1070,21 @@ static void ssl_client_handshaker_factory_destroy( tsi_ssl_handshaker_factory* self) { tsi_ssl_client_handshaker_factory* impl = (tsi_ssl_client_handshaker_factory*)self; - SSL_CTX_free(impl->ssl_context); + if (impl->ssl_context != NULL) SSL_CTX_free(impl->ssl_context); + if (impl->alpn_protocol_list != NULL) free(impl->alpn_protocol_list); free(impl); } +static int client_handshaker_factory_npn_callback( + SSL* ssl, unsigned char** out, unsigned char* outlen, + const unsigned char* in, unsigned int inlen, void* arg) { + tsi_ssl_client_handshaker_factory* factory = + (tsi_ssl_client_handshaker_factory*)arg; + return select_protocol_list((const unsigned char**)out, outlen, + factory->alpn_protocol_list, + factory->alpn_protocol_list_length, in, inlen); +} + /* --- tsi_ssl_server_handshaker_factory methods implementation. --- */ static tsi_result ssl_server_handshaker_factory_create_handshaker( @@ -1134,30 +1188,25 @@ static int ssl_server_handshaker_factory_servername_callback(SSL* ssl, int* ap, return SSL_TLSEXT_ERR_ALERT_WARNING; } +#if TSI_OPENSSL_ALPN_SUPPORT static int server_handshaker_factory_alpn_callback( SSL* ssl, const unsigned char** out, unsigned char* outlen, const unsigned char* in, unsigned int inlen, void* arg) { tsi_ssl_server_handshaker_factory* factory = (tsi_ssl_server_handshaker_factory*)arg; - const unsigned char* client_current = in; - while ((unsigned int)(client_current - in) < inlen) { - unsigned char client_current_len = *(client_current++); - const unsigned char* server_current = factory->alpn_protocol_list; - while ((server_current >= factory->alpn_protocol_list) && - (gpr_uintptr)(server_current - factory->alpn_protocol_list) < - factory->alpn_protocol_list_length) { - unsigned char server_current_len = *(server_current++); - if ((client_current_len == server_current_len) && - !memcmp(client_current, server_current, server_current_len)) { - *out = server_current; - *outlen = server_current_len; - return SSL_TLSEXT_ERR_OK; - } - server_current += server_current_len; - } - client_current += client_current_len; - } - return SSL_TLSEXT_ERR_NOACK; + return select_protocol_list(out, outlen, in, inlen, + factory->alpn_protocol_list, + factory->alpn_protocol_list_length); +} +#endif /* TSI_OPENSSL_ALPN_SUPPORT */ + +static int server_handshaker_factory_npn_advertised_callback( + SSL* ssl, const unsigned char** out, unsigned int* outlen, void* arg) { + tsi_ssl_server_handshaker_factory* factory = + (tsi_ssl_server_handshaker_factory*)arg; + *out = factory->alpn_protocol_list; + *outlen = factory->alpn_protocol_list_length; + return SSL_TLSEXT_ERR_OK; } /* --- tsi_ssl_handshaker_factory constructors. --- */ @@ -1184,6 +1233,14 @@ tsi_result tsi_create_ssl_client_handshaker_factory( gpr_log(GPR_ERROR, "Could not create ssl context."); return TSI_INVALID_ARGUMENT; } + + impl = calloc(1, sizeof(tsi_ssl_client_handshaker_factory)); + if (impl == NULL) { + SSL_CTX_free(ssl_context); + return TSI_OUT_OF_RESOURCES; + } + impl->ssl_context = ssl_context; + do { result = populate_ssl_context(ssl_context, pem_private_key, pem_private_key_size, @@ -1197,41 +1254,33 @@ tsi_result tsi_create_ssl_client_handshaker_factory( } if (num_alpn_protocols != 0) { - unsigned char* alpn_protocol_list = NULL; - size_t alpn_protocol_list_length = 0; - int ssl_failed; result = build_alpn_protocol_name_list( alpn_protocols, alpn_protocols_lengths, num_alpn_protocols, - &alpn_protocol_list, &alpn_protocol_list_length); + &impl->alpn_protocol_list, &impl->alpn_protocol_list_length); if (result != TSI_OK) { gpr_log(GPR_ERROR, "Building alpn list failed with error %s.", tsi_result_to_string(result)); - free(alpn_protocol_list); break; } - ssl_failed = SSL_CTX_set_alpn_protos(ssl_context, alpn_protocol_list, - alpn_protocol_list_length); - free(alpn_protocol_list); - if (ssl_failed) { +#if TSI_OPENSSL_ALPN_SUPPORT + if (SSL_CTX_set_alpn_protos(ssl_context, impl->alpn_protocol_list, + impl->alpn_protocol_list_length)) { gpr_log(GPR_ERROR, "Could not set alpn protocol list to context."); result = TSI_INVALID_ARGUMENT; break; } +#endif /* TSI_OPENSSL_ALPN_SUPPORT */ + SSL_CTX_set_next_proto_select_cb( + ssl_context, client_handshaker_factory_npn_callback, impl); } } while (0); if (result != TSI_OK) { - SSL_CTX_free(ssl_context); + ssl_client_handshaker_factory_destroy(&impl->base); return result; } SSL_CTX_set_verify(ssl_context, SSL_VERIFY_PEER, NULL); /* TODO(jboeuf): Add revocation verification. */ - impl = calloc(1, sizeof(tsi_ssl_client_handshaker_factory)); - if (impl == NULL) { - SSL_CTX_free(ssl_context); - return TSI_OUT_OF_RESOURCES; - } - impl->ssl_context = ssl_context; impl->base.create_handshaker = ssl_client_handshaker_factory_create_handshaker; impl->base.destroy = ssl_client_handshaker_factory_destroy; @@ -1322,8 +1371,13 @@ tsi_result tsi_create_ssl_server_handshaker_factory( impl->ssl_contexts[i], ssl_server_handshaker_factory_servername_callback); SSL_CTX_set_tlsext_servername_arg(impl->ssl_contexts[i], impl); +#if TSI_OPENSSL_ALPN_SUPPORT SSL_CTX_set_alpn_select_cb(impl->ssl_contexts[i], server_handshaker_factory_alpn_callback, impl); +#endif /* TSI_OPENSSL_ALPN_SUPPORT */ + SSL_CTX_set_next_protos_advertised_cb( + impl->ssl_contexts[i], + server_handshaker_factory_npn_advertised_callback, impl); } while (0); if (result != TSI_OK) { diff --git a/src/cpp/client/channel.cc b/src/cpp/client/channel.cc index 475a20d883..72593f877e 100644 --- a/src/cpp/client/channel.cc +++ b/src/cpp/client/channel.cc @@ -36,12 +36,10 @@ #include <memory> #include <grpc/grpc.h> -#include <grpc/grpc_security.h> #include <grpc/support/log.h> #include <grpc/support/slice.h> #include "src/core/profiling/timers.h" -#include "src/cpp/proto/proto_utils.h" #include <grpc++/channel_arguments.h> #include <grpc++/client_context.h> #include <grpc++/completion_queue.h> @@ -75,14 +73,14 @@ Call Channel::CreateCall(const RpcMethod& method, ClientContext* context, return Call(c_call, this, cq); } -void Channel::PerformOpsOnCall(CallOpBuffer* buf, Call* call) { +void Channel::PerformOpsOnCall(CallOpSetInterface* ops, Call* call) { static const size_t MAX_OPS = 8; - size_t nops = MAX_OPS; - grpc_op ops[MAX_OPS]; + size_t nops = 0; + grpc_op cops[MAX_OPS]; GRPC_TIMER_BEGIN(GRPC_PTAG_CPP_PERFORM_OPS, call->call()); - buf->FillOps(ops, &nops); + ops->FillOps(cops, &nops); GPR_ASSERT(GRPC_CALL_OK == - grpc_call_start_batch(call->call(), ops, nops, buf)); + grpc_call_start_batch(call->call(), cops, nops, ops)); GRPC_TIMER_END(GRPC_PTAG_CPP_PERFORM_OPS, call->call()); } diff --git a/src/cpp/client/channel.h b/src/cpp/client/channel.h index cd239247c8..9108713c58 100644 --- a/src/cpp/client/channel.h +++ b/src/cpp/client/channel.h @@ -44,22 +44,22 @@ struct grpc_channel; namespace grpc { class Call; -class CallOpBuffer; +class CallOpSetInterface; class ChannelArguments; class CompletionQueue; class Credentials; class StreamContextInterface; -class Channel GRPC_FINAL : public GrpcLibrary, - public ChannelInterface { +class Channel GRPC_FINAL : public GrpcLibrary, public ChannelInterface { public: Channel(const grpc::string& target, grpc_channel* c_channel); ~Channel() GRPC_OVERRIDE; - virtual void *RegisterMethod(const char *method) GRPC_OVERRIDE; + virtual void* RegisterMethod(const char* method) GRPC_OVERRIDE; virtual Call CreateCall(const RpcMethod& method, ClientContext* context, CompletionQueue* cq) GRPC_OVERRIDE; - virtual void PerformOpsOnCall(CallOpBuffer* ops, Call* call) GRPC_OVERRIDE; + virtual void PerformOpsOnCall(CallOpSetInterface* ops, + Call* call) GRPC_OVERRIDE; private: const grpc::string target_; diff --git a/src/cpp/client/channel_arguments.cc b/src/cpp/client/channel_arguments.cc index 87f8349eef..b271650673 100644 --- a/src/cpp/client/channel_arguments.cc +++ b/src/cpp/client/channel_arguments.cc @@ -33,21 +33,12 @@ #include <grpc++/channel_arguments.h> -#include <grpc/grpc_security.h> +#include "src/core/channel/channel_args.h" namespace grpc { -void ChannelArguments::SetSslTargetNameOverride(const grpc::string& name) { - SetString(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG, name); -} - -grpc::string ChannelArguments::GetSslTargetNameOverride() const { - for (unsigned int i = 0; i < args_.size(); i++) { - if (grpc::string(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG) == args_[i].key) { - return args_[i].value.string; - } - } - return ""; +void ChannelArguments::SetCompressionLevel(grpc_compression_level level) { + SetInt(GRPC_COMPRESSION_LEVEL_ARG, level); } void ChannelArguments::SetInt(const grpc::string& key, int value) { diff --git a/src/cpp/proto/proto_utils.h b/src/cpp/client/secure_channel_arguments.cc index 67a775b3ca..d89df999ad 100644 --- a/src/cpp/proto/proto_utils.h +++ b/src/cpp/client/secure_channel_arguments.cc @@ -31,25 +31,24 @@ * */ -#ifndef GRPC_INTERNAL_CPP_PROTO_PROTO_UTILS_H -#define GRPC_INTERNAL_CPP_PROTO_PROTO_UTILS_H +#include <grpc++/channel_arguments.h> +#include <grpc/grpc_security.h> -#include <grpc++/config.h> - -struct grpc_byte_buffer; +#include "src/core/channel/channel_args.h" namespace grpc { -// Serialize the msg into a buffer created inside the function. The caller -// should destroy the returned buffer when done with it. If serialization fails, -// false is returned and buffer is left unchanged. -bool SerializeProto(const grpc::protobuf::Message& msg, - grpc_byte_buffer** buffer); +void ChannelArguments::SetSslTargetNameOverride(const grpc::string& name) { + SetString(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG, name); +} -// The caller keeps ownership of buffer and msg. -bool DeserializeProto(grpc_byte_buffer* buffer, grpc::protobuf::Message* msg, - int max_message_size); +grpc::string ChannelArguments::GetSslTargetNameOverride() const { + for (unsigned int i = 0; i < args_.size(); i++) { + if (grpc::string(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG) == args_[i].key) { + return args_[i].value.string; + } + } + return ""; +} } // namespace grpc - -#endif // GRPC_INTERNAL_CPP_PROTO_PROTO_UTILS_H diff --git a/src/cpp/common/call.cc b/src/cpp/common/call.cc index 1068111e3f..0a5c976e01 100644 --- a/src/cpp/common/call.cc +++ b/src/cpp/common/call.cc @@ -39,107 +39,32 @@ #include <grpc++/channel_interface.h> #include "src/core/profiling/timers.h" -#include "src/cpp/proto/proto_utils.h" namespace grpc { -CallOpBuffer::CallOpBuffer() - : return_tag_(this), - send_initial_metadata_(false), - initial_metadata_count_(0), - initial_metadata_(nullptr), - recv_initial_metadata_(nullptr), - send_message_(nullptr), - send_message_buffer_(nullptr), - send_buf_(nullptr), - recv_message_(nullptr), - recv_message_buffer_(nullptr), - recv_buf_(nullptr), - max_message_size_(-1), - client_send_close_(false), - recv_trailing_metadata_(nullptr), - recv_status_(nullptr), - status_code_(GRPC_STATUS_OK), - status_details_(nullptr), - status_details_capacity_(0), - send_status_available_(false), - send_status_code_(GRPC_STATUS_OK), - trailing_metadata_count_(0), - trailing_metadata_(nullptr), - cancelled_buf_(0), - recv_closed_(nullptr) { - memset(&recv_trailing_metadata_arr_, 0, sizeof(recv_trailing_metadata_arr_)); - memset(&recv_initial_metadata_arr_, 0, sizeof(recv_initial_metadata_arr_)); - recv_trailing_metadata_arr_.metadata = nullptr; - recv_initial_metadata_arr_.metadata = nullptr; -} - -void CallOpBuffer::Reset(void* next_return_tag) { - return_tag_ = next_return_tag; - - send_initial_metadata_ = false; - initial_metadata_count_ = 0; - gpr_free(initial_metadata_); - - recv_initial_metadata_ = nullptr; - recv_initial_metadata_arr_.count = 0; - - if (send_buf_ && send_message_) { - grpc_byte_buffer_destroy(send_buf_); - } - send_message_ = nullptr; - send_message_buffer_ = nullptr; - send_buf_ = nullptr; - - got_message = false; - if (recv_buf_ && recv_message_) { - grpc_byte_buffer_destroy(recv_buf_); - } - recv_message_ = nullptr; - recv_message_buffer_ = nullptr; - recv_buf_ = nullptr; - - client_send_close_ = false; - - recv_trailing_metadata_ = nullptr; - recv_status_ = nullptr; - recv_trailing_metadata_arr_.count = 0; - - status_code_ = GRPC_STATUS_OK; - - send_status_available_ = false; - send_status_code_ = GRPC_STATUS_OK; - send_status_details_.clear(); - trailing_metadata_count_ = 0; - trailing_metadata_ = nullptr; - - recv_closed_ = nullptr; -} - -CallOpBuffer::~CallOpBuffer() { - gpr_free(status_details_); - gpr_free(recv_initial_metadata_arr_.metadata); - gpr_free(recv_trailing_metadata_arr_.metadata); - if (recv_buf_ && recv_message_) { - grpc_byte_buffer_destroy(recv_buf_); - } - if (send_buf_ && send_message_) { - grpc_byte_buffer_destroy(send_buf_); +void FillMetadataMap(grpc_metadata_array* arr, + std::multimap<grpc::string, grpc::string>* metadata) { + for (size_t i = 0; i < arr->count; i++) { + // TODO(yangg) handle duplicates? + metadata->insert(std::pair<grpc::string, grpc::string>( + arr->metadata[i].key, + grpc::string(arr->metadata[i].value, arr->metadata[i].value_length))); } + grpc_metadata_array_destroy(arr); + grpc_metadata_array_init(arr); } -namespace { // TODO(yangg) if the map is changed before we send, the pointers will be a // mess. Make sure it does not happen. grpc_metadata* FillMetadataArray( - std::multimap<grpc::string, grpc::string>* metadata) { - if (metadata->empty()) { + const std::multimap<grpc::string, grpc::string>& metadata) { + if (metadata.empty()) { return nullptr; } grpc_metadata* metadata_array = - (grpc_metadata*)gpr_malloc(metadata->size() * sizeof(grpc_metadata)); + (grpc_metadata*)gpr_malloc(metadata.size() * sizeof(grpc_metadata)); size_t i = 0; - for (auto iter = metadata->cbegin(); iter != metadata->cend(); ++iter, ++i) { + for (auto iter = metadata.cbegin(); iter != metadata.cend(); ++iter, ++i) { metadata_array[i].key = iter->first.c_str(); metadata_array[i].value = iter->second.c_str(); metadata_array[i].value_length = iter->second.size(); @@ -147,198 +72,6 @@ grpc_metadata* FillMetadataArray( return metadata_array; } -void FillMetadataMap(grpc_metadata_array* arr, - std::multimap<grpc::string, grpc::string>* metadata) { - for (size_t i = 0; i < arr->count; i++) { - // TODO(yangg) handle duplicates? - metadata->insert(std::pair<grpc::string, grpc::string>( - arr->metadata[i].key, - grpc::string(arr->metadata[i].value, arr->metadata[i].value_length))); - } - grpc_metadata_array_destroy(arr); - grpc_metadata_array_init(arr); -} -} // namespace - -void CallOpBuffer::AddSendInitialMetadata( - std::multimap<grpc::string, grpc::string>* metadata) { - send_initial_metadata_ = true; - initial_metadata_count_ = metadata->size(); - initial_metadata_ = FillMetadataArray(metadata); -} - -void CallOpBuffer::AddRecvInitialMetadata(ClientContext* ctx) { - ctx->initial_metadata_received_ = true; - recv_initial_metadata_ = &ctx->recv_initial_metadata_; -} - -void CallOpBuffer::AddSendInitialMetadata(ClientContext* ctx) { - AddSendInitialMetadata(&ctx->send_initial_metadata_); -} - -void CallOpBuffer::AddSendMessage(const grpc::protobuf::Message& message) { - send_message_ = &message; -} - -void CallOpBuffer::AddSendMessage(const ByteBuffer& message) { - send_message_buffer_ = &message; -} - -void CallOpBuffer::AddRecvMessage(grpc::protobuf::Message* message) { - recv_message_ = message; - recv_message_->Clear(); -} - -void CallOpBuffer::AddRecvMessage(ByteBuffer* message) { - recv_message_buffer_ = message; - recv_message_buffer_->Clear(); -} - -void CallOpBuffer::AddClientSendClose() { client_send_close_ = true; } - -void CallOpBuffer::AddServerRecvClose(bool* cancelled) { - recv_closed_ = cancelled; -} - -void CallOpBuffer::AddClientRecvStatus(ClientContext* context, Status* status) { - recv_trailing_metadata_ = &context->trailing_metadata_; - recv_status_ = status; -} - -void CallOpBuffer::AddServerSendStatus( - std::multimap<grpc::string, grpc::string>* metadata, const Status& status) { - if (metadata != NULL) { - trailing_metadata_count_ = metadata->size(); - trailing_metadata_ = FillMetadataArray(metadata); - } else { - trailing_metadata_count_ = 0; - } - send_status_available_ = true; - send_status_code_ = static_cast<grpc_status_code>(status.code()); - send_status_details_ = status.details(); -} - -void CallOpBuffer::FillOps(grpc_op* ops, size_t* nops) { - *nops = 0; - if (send_initial_metadata_) { - ops[*nops].op = GRPC_OP_SEND_INITIAL_METADATA; - ops[*nops].data.send_initial_metadata.count = initial_metadata_count_; - ops[*nops].data.send_initial_metadata.metadata = initial_metadata_; - (*nops)++; - } - if (recv_initial_metadata_) { - ops[*nops].op = GRPC_OP_RECV_INITIAL_METADATA; - ops[*nops].data.recv_initial_metadata = &recv_initial_metadata_arr_; - (*nops)++; - } - if (send_message_ || send_message_buffer_) { - if (send_message_) { - GRPC_TIMER_BEGIN(GRPC_PTAG_PROTO_SERIALIZE, 0); - bool success = SerializeProto(*send_message_, &send_buf_); - if (!success) { - abort(); - // TODO handle parse failure - } - GRPC_TIMER_END(GRPC_PTAG_PROTO_SERIALIZE, 0); - } else { - send_buf_ = send_message_buffer_->buffer(); - } - ops[*nops].op = GRPC_OP_SEND_MESSAGE; - ops[*nops].data.send_message = send_buf_; - (*nops)++; - } - if (recv_message_ || recv_message_buffer_) { - ops[*nops].op = GRPC_OP_RECV_MESSAGE; - ops[*nops].data.recv_message = &recv_buf_; - (*nops)++; - } - if (client_send_close_) { - ops[*nops].op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; - (*nops)++; - } - if (recv_status_) { - ops[*nops].op = GRPC_OP_RECV_STATUS_ON_CLIENT; - ops[*nops].data.recv_status_on_client.trailing_metadata = - &recv_trailing_metadata_arr_; - ops[*nops].data.recv_status_on_client.status = &status_code_; - ops[*nops].data.recv_status_on_client.status_details = &status_details_; - ops[*nops].data.recv_status_on_client.status_details_capacity = - &status_details_capacity_; - (*nops)++; - } - if (send_status_available_) { - ops[*nops].op = GRPC_OP_SEND_STATUS_FROM_SERVER; - ops[*nops].data.send_status_from_server.trailing_metadata_count = - trailing_metadata_count_; - ops[*nops].data.send_status_from_server.trailing_metadata = - trailing_metadata_; - ops[*nops].data.send_status_from_server.status = send_status_code_; - ops[*nops].data.send_status_from_server.status_details = - send_status_details_.empty() ? nullptr : send_status_details_.c_str(); - (*nops)++; - } - if (recv_closed_) { - ops[*nops].op = GRPC_OP_RECV_CLOSE_ON_SERVER; - ops[*nops].data.recv_close_on_server.cancelled = &cancelled_buf_; - (*nops)++; - } -} - -bool CallOpBuffer::FinalizeResult(void** tag, bool* status) { - // Release send buffers. - if (send_buf_ && send_message_) { - if (send_message_) { - grpc_byte_buffer_destroy(send_buf_); - } - send_buf_ = nullptr; - } - if (initial_metadata_) { - gpr_free(initial_metadata_); - initial_metadata_ = nullptr; - } - if (trailing_metadata_count_) { - gpr_free(trailing_metadata_); - trailing_metadata_ = nullptr; - } - // Set user-facing tag. - *tag = return_tag_; - // Process received initial metadata - if (recv_initial_metadata_) { - FillMetadataMap(&recv_initial_metadata_arr_, recv_initial_metadata_); - } - // Parse received message if any. - if (recv_message_ || recv_message_buffer_) { - if (recv_buf_) { - got_message = *status; - if (recv_message_) { - GRPC_TIMER_BEGIN(GRPC_PTAG_PROTO_DESERIALIZE, 0); - *status = *status && - DeserializeProto(recv_buf_, recv_message_, max_message_size_); - grpc_byte_buffer_destroy(recv_buf_); - GRPC_TIMER_END(GRPC_PTAG_PROTO_DESERIALIZE, 0); - } else { - recv_message_buffer_->set_buffer(recv_buf_); - } - recv_buf_ = nullptr; - } else { - // Read failed - got_message = false; - *status = false; - } - } - // Parse received status. - if (recv_status_) { - FillMetadataMap(&recv_trailing_metadata_arr_, recv_trailing_metadata_); - *recv_status_ = Status( - static_cast<StatusCode>(status_code_), - status_details_ ? grpc::string(status_details_) : grpc::string()); - } - if (recv_closed_) { - *recv_closed_ = cancelled_buf_ != 0; - } - return true; -} - Call::Call(grpc_call* call, CallHook* call_hook, CompletionQueue* cq) : call_hook_(call_hook), cq_(cq), call_(call), max_message_size_(-1) {} @@ -349,11 +82,11 @@ Call::Call(grpc_call* call, CallHook* call_hook, CompletionQueue* cq, call_(call), max_message_size_(max_message_size) {} -void Call::PerformOps(CallOpBuffer* buffer) { +void Call::PerformOps(CallOpSetInterface* ops) { if (max_message_size_ > 0) { - buffer->set_max_message_size(max_message_size_); + ops->set_max_message_size(max_message_size_); } - call_hook_->PerformOpsOnCall(buffer, this); + call_hook_->PerformOpsOnCall(ops, this); } } // namespace grpc diff --git a/src/cpp/proto/proto_utils.cc b/src/cpp/proto/proto_utils.cc index b9554c4bb7..7b2a65e99b 100644 --- a/src/cpp/proto/proto_utils.cc +++ b/src/cpp/proto/proto_utils.cc @@ -31,11 +31,12 @@ * */ -#include "src/cpp/proto/proto_utils.h" +#include <grpc++/impl/proto_utils.h> #include <grpc++/config.h> #include <grpc/grpc.h> #include <grpc/byte_buffer.h> +#include <grpc/byte_buffer_reader.h> #include <grpc/support/slice.h> #include <grpc/support/slice_buffer.h> #include <grpc/support/port_platform.h> @@ -48,8 +49,8 @@ class GrpcBufferWriter GRPC_FINAL explicit GrpcBufferWriter(grpc_byte_buffer** bp, int block_size = kMaxBufferLength) : block_size_(block_size), byte_count_(0), have_backup_(false) { - *bp = grpc_byte_buffer_create(NULL, 0); - slice_buffer_ = &(*bp)->data.slice_buffer; + *bp = grpc_raw_byte_buffer_create(NULL, 0); + slice_buffer_ = &(*bp)->data.raw.slice_buffer; } ~GrpcBufferWriter() GRPC_OVERRIDE { @@ -66,7 +67,7 @@ class GrpcBufferWriter GRPC_FINAL slice_ = gpr_slice_malloc(block_size_); } *data = GPR_SLICE_START_PTR(slice_); - byte_count_ += *size = GPR_SLICE_LENGTH(slice_); + byte_count_ += * size = GPR_SLICE_LENGTH(slice_); gpr_slice_buffer_add(slice_buffer_, slice_); return true; } @@ -100,11 +101,9 @@ class GrpcBufferReader GRPC_FINAL public: explicit GrpcBufferReader(grpc_byte_buffer* buffer) : byte_count_(0), backup_count_(0) { - reader_ = grpc_byte_buffer_reader_create(buffer); - } - ~GrpcBufferReader() GRPC_OVERRIDE { - grpc_byte_buffer_reader_destroy(reader_); + grpc_byte_buffer_reader_init(&reader_, buffer); } + ~GrpcBufferReader() GRPC_OVERRIDE {} bool Next(const void** data, int* size) GRPC_OVERRIDE { if (backup_count_ > 0) { @@ -114,12 +113,12 @@ class GrpcBufferReader GRPC_FINAL backup_count_ = 0; return true; } - if (!grpc_byte_buffer_reader_next(reader_, &slice_)) { + if (!grpc_byte_buffer_reader_next(&reader_, &slice_)) { return false; } gpr_slice_unref(slice_); *data = GPR_SLICE_START_PTR(slice_); - byte_count_ += *size = GPR_SLICE_LENGTH(slice_); + byte_count_ += * size = GPR_SLICE_LENGTH(slice_); return true; } @@ -147,26 +146,38 @@ class GrpcBufferReader GRPC_FINAL private: gpr_int64 byte_count_; gpr_int64 backup_count_; - grpc_byte_buffer_reader* reader_; + grpc_byte_buffer_reader reader_; gpr_slice slice_; }; namespace grpc { -bool SerializeProto(const grpc::protobuf::Message& msg, grpc_byte_buffer** bp) { +Status SerializeProto(const grpc::protobuf::Message& msg, grpc_byte_buffer** bp) { GrpcBufferWriter writer(bp); - return msg.SerializeToZeroCopyStream(&writer); + return msg.SerializeToZeroCopyStream(&writer) + ? Status::OK + : Status(StatusCode::INVALID_ARGUMENT, + "Failed to serialize message"); } -bool DeserializeProto(grpc_byte_buffer* buffer, grpc::protobuf::Message* msg, - int max_message_size) { - if (!buffer) return false; +Status DeserializeProto(grpc_byte_buffer* buffer, grpc::protobuf::Message* msg, + int max_message_size) { + if (!buffer) { + return Status(StatusCode::INVALID_ARGUMENT, "No payload"); + } GrpcBufferReader reader(buffer); ::grpc::protobuf::io::CodedInputStream decoder(&reader); if (max_message_size > 0) { decoder.SetTotalBytesLimit(max_message_size, max_message_size); } - return msg->ParseFromCodedStream(&decoder) && decoder.ConsumedEntireMessage(); + if (!msg->ParseFromCodedStream(&decoder)) { + return Status(StatusCode::INVALID_ARGUMENT, + msg->InitializationErrorString()); + } + if (!decoder.ConsumedEntireMessage()) { + return Status(StatusCode::INVALID_ARGUMENT, "Did not read entire message"); + } + return Status::OK; } } // namespace grpc diff --git a/src/core/compression/algorithm.h b/src/cpp/server/create_default_thread_pool.cc index 9dd9f57b56..89c1d7e929 100644 --- a/src/core/compression/algorithm.h +++ b/src/cpp/server/create_default_thread_pool.cc @@ -31,19 +31,19 @@ * */ -#ifndef GRPC_INTERNAL_CORE_COMPRESSION_ALGORITHM_H -#define GRPC_INTERNAL_CORE_COMPRESSION_ALGORITHM_H +#include <grpc/support/cpu.h> +#include "src/cpp/server/thread_pool.h" -/* The various compression algorithms supported by GRPC */ -typedef enum { - GRPC_COMPRESS_NONE = 0, - GRPC_COMPRESS_DEFLATE, - GRPC_COMPRESS_GZIP, - /* TODO(ctiller): snappy */ - GRPC_COMPRESS_ALGORITHMS_COUNT -} grpc_compression_algorithm; +#ifndef GRPC_CUSTOM_DEFAULT_THREAD_POOL -const char *grpc_compression_algorithm_name( - grpc_compression_algorithm algorithm); +namespace grpc { -#endif /* GRPC_INTERNAL_CORE_COMPRESSION_ALGORITHM_H */ +ThreadPoolInterface* CreateDefaultThreadPool() { + int cores = gpr_cpu_num_cores(); + if (!cores) cores = 4; + return new ThreadPool(cores); +} + +} // namespace grpc + +#endif // !GRPC_CUSTOM_DEFAULT_THREAD_POOL diff --git a/src/cpp/server/insecure_server_credentials.cc b/src/cpp/server/insecure_server_credentials.cc index 55dd90d7a7..aca3568e59 100644 --- a/src/cpp/server/insecure_server_credentials.cc +++ b/src/cpp/server/insecure_server_credentials.cc @@ -31,9 +31,10 @@ * */ -#include <grpc/grpc_security.h> #include <grpc++/server_credentials.h> +#include <grpc/grpc.h> + namespace grpc { namespace { class InsecureServerCredentialsImpl GRPC_FINAL : public ServerCredentials { diff --git a/src/cpp/server/server.cc b/src/cpp/server/server.cc index dbd88c5b8c..1437b2dea7 100644 --- a/src/cpp/server/server.cc +++ b/src/cpp/server/server.cc @@ -35,7 +35,6 @@ #include <utility> #include <grpc/grpc.h> -#include <grpc/grpc_security.h> #include <grpc/support/alloc.h> #include <grpc/support/log.h> #include <grpc++/completion_queue.h> @@ -48,7 +47,6 @@ #include <grpc++/time.h> #include "src/core/profiling/timers.h" -#include "src/cpp/proto/proto_utils.h" namespace grpc { @@ -69,15 +67,11 @@ class Server::SyncRequest GRPC_FINAL : public CompletionQueueTag { has_request_payload_(method->method_type() == RpcMethod::NORMAL_RPC || method->method_type() == RpcMethod::SERVER_STREAMING), - has_response_payload_(method->method_type() == RpcMethod::NORMAL_RPC || - method->method_type() == - RpcMethod::CLIENT_STREAMING) { + cq_(nullptr) { grpc_metadata_array_init(&request_metadata_); } - ~SyncRequest() { - grpc_metadata_array_destroy(&request_metadata_); - } + ~SyncRequest() { grpc_metadata_array_destroy(&request_metadata_); } static SyncRequest* Wait(CompletionQueue* cq, bool* ok) { void* tag = nullptr; @@ -90,10 +84,16 @@ class Server::SyncRequest GRPC_FINAL : public CompletionQueueTag { return mrd; } + void SetupRequest() { cq_ = grpc_completion_queue_create(); } + + void TeardownRequest() { + grpc_completion_queue_destroy(cq_); + cq_ = nullptr; + } + void Request(grpc_server* server, grpc_completion_queue* notify_cq) { - GPR_ASSERT(!in_flight_); + GPR_ASSERT(cq_ && !in_flight_); in_flight_ = true; - cq_ = grpc_completion_queue_create(); GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_registered_call( server, tag_, &call_, &deadline_, &request_metadata_, @@ -116,7 +116,6 @@ class Server::SyncRequest GRPC_FINAL : public CompletionQueueTag { ctx_(mrd->deadline_, mrd->request_metadata_.metadata, mrd->request_metadata_.count), has_request_payload_(mrd->has_request_payload_), - has_response_payload_(mrd->has_response_payload_), request_payload_(mrd->request_payload_), method_(mrd->method_) { ctx_.call_ = mrd->call_; @@ -133,35 +132,10 @@ class Server::SyncRequest GRPC_FINAL : public CompletionQueueTag { } void Run() { - std::unique_ptr<grpc::protobuf::Message> req; - std::unique_ptr<grpc::protobuf::Message> res; - if (has_request_payload_) { - GRPC_TIMER_BEGIN(GRPC_PTAG_PROTO_DESERIALIZE, call_.call()); - req.reset(method_->AllocateRequestProto()); - if (!DeserializeProto(request_payload_, req.get(), - call_.max_message_size())) { - // FIXME(yangg) deal with deserialization failure - cq_.Shutdown(); - return; - } - GRPC_TIMER_END(GRPC_PTAG_PROTO_DESERIALIZE, call_.call()); - } - if (has_response_payload_) { - res.reset(method_->AllocateResponseProto()); - } ctx_.BeginCompletionOp(&call_); - auto status = method_->handler()->RunHandler( - MethodHandler::HandlerParameter(&call_, &ctx_, req.get(), res.get())); - CallOpBuffer buf; - if (!ctx_.sent_initial_metadata_) { - buf.AddSendInitialMetadata(&ctx_.initial_metadata_); - } - if (has_response_payload_) { - buf.AddSendMessage(*res); - } - buf.AddServerSendStatus(&ctx_.trailing_metadata_, status); - call_.PerformOps(&buf); - cq_.Pluck(&buf); /* status ignored */ + method_->handler()->RunHandler(MethodHandler::HandlerParameter( + &call_, &ctx_, request_payload_, call_.max_message_size())); + request_payload_ = nullptr; void* ignored_tag; bool ignored_ok; cq_.Shutdown(); @@ -173,7 +147,6 @@ class Server::SyncRequest GRPC_FINAL : public CompletionQueueTag { Call call_; ServerContext ctx_; const bool has_request_payload_; - const bool has_response_payload_; grpc_byte_buffer* request_payload_; RpcServiceMethod* const method_; }; @@ -183,7 +156,6 @@ class Server::SyncRequest GRPC_FINAL : public CompletionQueueTag { void* const tag_; bool in_flight_; const bool has_request_payload_; - const bool has_response_payload_; grpc_call* call_; gpr_timespec deadline_; grpc_metadata_array request_metadata_; @@ -251,9 +223,9 @@ bool Server::RegisterService(RpcService* service) { } bool Server::RegisterAsyncService(AsynchronousService* service) { - GPR_ASSERT(service->dispatch_impl_ == nullptr && + GPR_ASSERT(service->server_ == nullptr && "Can only register an asynchronous service against one server."); - service->dispatch_impl_ = this; + service->server_ = this; service->request_args_ = new void*[service->method_count_]; for (size_t i = 0; i < service->method_count_; ++i) { void* tag = grpc_server_register_method(server_, service->method_names_[i], @@ -288,6 +260,7 @@ bool Server::Start() { // Start processing rpcs. if (!sync_methods_->empty()) { for (auto m = sync_methods_->begin(); m != sync_methods_->end(); m++) { + m->SetupRequest(); m->Request(server_, cq_.cq()); } @@ -318,141 +291,90 @@ void Server::Wait() { } } -void Server::PerformOpsOnCall(CallOpBuffer* buf, Call* call) { +void Server::PerformOpsOnCall(CallOpSetInterface* ops, Call* call) { static const size_t MAX_OPS = 8; - size_t nops = MAX_OPS; - grpc_op ops[MAX_OPS]; - buf->FillOps(ops, &nops); + size_t nops = 0; + grpc_op cops[MAX_OPS]; + ops->FillOps(cops, &nops); GPR_ASSERT(GRPC_CALL_OK == - grpc_call_start_batch(call->call(), ops, nops, buf)); + grpc_call_start_batch(call->call(), cops, nops, ops)); } -class Server::AsyncRequest GRPC_FINAL : public CompletionQueueTag { - public: - AsyncRequest(Server* server, void* registered_method, ServerContext* ctx, - grpc::protobuf::Message* request, - ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, - ServerCompletionQueue* notification_cq, void* tag) - : tag_(tag), - request_(request), - stream_(stream), - call_cq_(call_cq), - ctx_(ctx), - generic_ctx_(nullptr), - server_(server), - call_(nullptr), - payload_(nullptr) { - memset(&array_, 0, sizeof(array_)); - grpc_call_details_init(&call_details_); - GPR_ASSERT(notification_cq); - GPR_ASSERT(call_cq); - grpc_server_request_registered_call( - server->server_, registered_method, &call_, &call_details_.deadline, - &array_, request ? &payload_ : nullptr, call_cq->cq(), - notification_cq->cq(), this); - } +Server::BaseAsyncRequest::BaseAsyncRequest( + Server* server, ServerContext* context, + ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, void* tag) + : server_(server), + context_(context), + stream_(stream), + call_cq_(call_cq), + tag_(tag), + call_(nullptr) { + memset(&initial_metadata_array_, 0, sizeof(initial_metadata_array_)); +} - AsyncRequest(Server* server, GenericServerContext* ctx, - ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, - ServerCompletionQueue* notification_cq, void* tag) - : tag_(tag), - request_(nullptr), - stream_(stream), - call_cq_(call_cq), - ctx_(nullptr), - generic_ctx_(ctx), - server_(server), - call_(nullptr), - payload_(nullptr) { - memset(&array_, 0, sizeof(array_)); - grpc_call_details_init(&call_details_); - GPR_ASSERT(notification_cq); - GPR_ASSERT(call_cq); - grpc_server_request_call(server->server_, &call_, &call_details_, &array_, - call_cq->cq(), notification_cq->cq(), this); - } +Server::BaseAsyncRequest::~BaseAsyncRequest() {} - ~AsyncRequest() { - if (payload_) { - grpc_byte_buffer_destroy(payload_); +bool Server::BaseAsyncRequest::FinalizeResult(void** tag, bool* status) { + if (*status) { + for (size_t i = 0; i < initial_metadata_array_.count; i++) { + context_->client_metadata_.insert(std::make_pair( + grpc::string(initial_metadata_array_.metadata[i].key), + grpc::string(initial_metadata_array_.metadata[i].value, + initial_metadata_array_.metadata[i].value + + initial_metadata_array_.metadata[i].value_length))); } - grpc_metadata_array_destroy(&array_); } - - bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE { - *tag = tag_; - bool orig_status = *status; - if (*status && request_) { - if (payload_) { - GRPC_TIMER_BEGIN(GRPC_PTAG_PROTO_DESERIALIZE, call_); - *status = - DeserializeProto(payload_, request_, server_->max_message_size_); - GRPC_TIMER_END(GRPC_PTAG_PROTO_DESERIALIZE, call_); - } else { - *status = false; - } - } - ServerContext* ctx = ctx_ ? ctx_ : generic_ctx_; - GPR_ASSERT(ctx); - if (*status) { - ctx->deadline_ = call_details_.deadline; - for (size_t i = 0; i < array_.count; i++) { - ctx->client_metadata_.insert(std::make_pair( - grpc::string(array_.metadata[i].key), - grpc::string( - array_.metadata[i].value, - array_.metadata[i].value + array_.metadata[i].value_length))); - } - if (generic_ctx_) { - // TODO(yangg) remove the copy here. - generic_ctx_->method_ = call_details_.method; - generic_ctx_->host_ = call_details_.host; - gpr_free(call_details_.method); - gpr_free(call_details_.host); - } - } - ctx->call_ = call_; - ctx->cq_ = call_cq_; - Call call(call_, server_, call_cq_, server_->max_message_size_); - if (orig_status && call_) { - ctx->BeginCompletionOp(&call); - } - // just the pointers inside call are copied here - stream_->BindCall(&call); - delete this; - return true; + grpc_metadata_array_destroy(&initial_metadata_array_); + context_->call_ = call_; + context_->cq_ = call_cq_; + Call call(call_, server_, call_cq_, server_->max_message_size_); + if (*status && call_) { + context_->BeginCompletionOp(&call); } + // just the pointers inside call are copied here + stream_->BindCall(&call); + *tag = tag_; + delete this; + return true; +} - private: - void* const tag_; - grpc::protobuf::Message* const request_; - ServerAsyncStreamingInterface* const stream_; - CompletionQueue* const call_cq_; - ServerContext* const ctx_; - GenericServerContext* const generic_ctx_; - Server* const server_; - grpc_call* call_; - grpc_call_details call_details_; - grpc_metadata_array array_; - grpc_byte_buffer* payload_; -}; +Server::RegisteredAsyncRequest::RegisteredAsyncRequest( + Server* server, ServerContext* context, + ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, void* tag) + : BaseAsyncRequest(server, context, stream, call_cq, tag) {} + +void Server::RegisteredAsyncRequest::IssueRequest( + void* registered_method, grpc_byte_buffer** payload, + ServerCompletionQueue* notification_cq) { + grpc_server_request_registered_call( + server_->server_, registered_method, &call_, &context_->deadline_, + &initial_metadata_array_, payload, call_cq_->cq(), notification_cq->cq(), + this); +} -void Server::RequestAsyncCall(void* registered_method, ServerContext* context, - grpc::protobuf::Message* request, - ServerAsyncStreamingInterface* stream, - CompletionQueue* call_cq, - ServerCompletionQueue* notification_cq, - void* tag) { - new AsyncRequest(this, registered_method, context, request, stream, call_cq, - notification_cq, tag); +Server::GenericAsyncRequest::GenericAsyncRequest( + Server* server, GenericServerContext* context, + ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, + ServerCompletionQueue* notification_cq, void* tag) + : BaseAsyncRequest(server, context, stream, call_cq, tag) { + grpc_call_details_init(&call_details_); + GPR_ASSERT(notification_cq); + GPR_ASSERT(call_cq); + grpc_server_request_call(server->server_, &call_, &call_details_, + &initial_metadata_array_, call_cq->cq(), + notification_cq->cq(), this); } -void Server::RequestAsyncGenericCall(GenericServerContext* context, - ServerAsyncStreamingInterface* stream, - CompletionQueue* call_cq, - ServerCompletionQueue* notification_cq, - void* tag) { - new AsyncRequest(this, context, stream, call_cq, notification_cq, tag); +bool Server::GenericAsyncRequest::FinalizeResult(void** tag, bool* status) { + // TODO(yangg) remove the copy here. + if (*status) { + static_cast<GenericServerContext*>(context_)->method_ = + call_details_.method; + static_cast<GenericServerContext*>(context_)->host_ = call_details_.host; + } + gpr_free(call_details_.method); + gpr_free(call_details_.host); + return BaseAsyncRequest::FinalizeResult(tag, status); } void Server::ScheduleCallback() { @@ -472,9 +394,13 @@ void Server::RunRpc() { if (ok) { SyncRequest::CallData cd(this, mrd); { + mrd->SetupRequest(); grpc::unique_lock<grpc::mutex> lock(mu_); if (!shutdown_) { mrd->Request(server_, cq_.cq()); + } else { + // destroy the structure that was created + mrd->TeardownRequest(); } } cd.Run(); diff --git a/src/cpp/server/server_builder.cc b/src/cpp/server/server_builder.cc index 4bcbd82952..3ee1d54e76 100644 --- a/src/cpp/server/server_builder.cc +++ b/src/cpp/server/server_builder.cc @@ -87,9 +87,7 @@ std::unique_ptr<Server> ServerBuilder::BuildAndStart() { return nullptr; } if (!thread_pool_ && !services_.empty()) { - int cores = gpr_cpu_num_cores(); - if (!cores) cores = 4; - thread_pool_ = new ThreadPool(cores); + thread_pool_ = CreateDefaultThreadPool(); thread_pool_owned = true; } std::unique_ptr<Server> server( diff --git a/src/cpp/server/server_context.cc b/src/cpp/server/server_context.cc index 6b5e41d0a8..699895a3cf 100644 --- a/src/cpp/server/server_context.cc +++ b/src/cpp/server/server_context.cc @@ -43,12 +43,12 @@ namespace grpc { // CompletionOp -class ServerContext::CompletionOp GRPC_FINAL : public CallOpBuffer { +class ServerContext::CompletionOp GRPC_FINAL : public CallOpSetInterface { public: // initial refs: one in the server context, one in the cq - CompletionOp() : refs_(2), finalized_(false), cancelled_(false) { - AddServerRecvClose(&cancelled_); - } + CompletionOp() : refs_(2), finalized_(false), cancelled_(0) {} + + void FillOps(grpc_op* ops, size_t* nops) GRPC_OVERRIDE; bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE; bool CheckCancelled(CompletionQueue* cq); @@ -59,7 +59,7 @@ class ServerContext::CompletionOp GRPC_FINAL : public CallOpBuffer { grpc::mutex mu_; int refs_; bool finalized_; - bool cancelled_; + int cancelled_; }; void ServerContext::CompletionOp::Unref() { @@ -73,14 +73,20 @@ void ServerContext::CompletionOp::Unref() { bool ServerContext::CompletionOp::CheckCancelled(CompletionQueue* cq) { cq->TryPluck(this); grpc::lock_guard<grpc::mutex> g(mu_); - return finalized_ ? cancelled_ : false; + return finalized_ ? cancelled_ != 0 : false; +} + +void ServerContext::CompletionOp::FillOps(grpc_op* ops, size_t* nops) { + ops->op = GRPC_OP_RECV_CLOSE_ON_SERVER; + ops->data.recv_close_on_server.cancelled = &cancelled_; + ops->flags = 0; + *nops = 1; } bool ServerContext::CompletionOp::FinalizeResult(void** tag, bool* status) { - GPR_ASSERT(CallOpBuffer::FinalizeResult(tag, status)); grpc::unique_lock<grpc::mutex> lock(mu_); finalized_ = true; - if (!*status) cancelled_ = true; + if (!*status) cancelled_ = 1; if (--refs_ == 0) { lock.unlock(); delete this; diff --git a/src/cpp/server/server_credentials.cc b/src/cpp/server/server_credentials.cc index 6bdb465baa..be3a7425e0 100644 --- a/src/cpp/server/server_credentials.cc +++ b/src/cpp/server/server_credentials.cc @@ -31,8 +31,6 @@ * */ -#include <grpc/grpc_security.h> - #include <grpc++/server_credentials.h> namespace grpc { diff --git a/src/cpp/server/thread_pool.h b/src/cpp/server/thread_pool.h index 26f25611b5..3b70249bf9 100644 --- a/src/cpp/server/thread_pool.h +++ b/src/cpp/server/thread_pool.h @@ -62,6 +62,8 @@ class ThreadPool GRPC_FINAL : public ThreadPoolInterface { void ThreadFunc(); }; +ThreadPoolInterface* CreateDefaultThreadPool(); + } // namespace grpc #endif // GRPC_INTERNAL_CPP_SERVER_THREAD_POOL_H diff --git a/src/cpp/util/byte_buffer.cc b/src/cpp/util/byte_buffer.cc index ac2657472c..a66c92c3e1 100644 --- a/src/cpp/util/byte_buffer.cc +++ b/src/cpp/util/byte_buffer.cc @@ -31,17 +31,18 @@ * */ +#include <grpc/byte_buffer_reader.h> #include <grpc++/byte_buffer.h> namespace grpc { -ByteBuffer::ByteBuffer(Slice* slices, size_t nslices) { +ByteBuffer::ByteBuffer(const Slice* slices, size_t nslices) { // TODO(yangg) maybe expose some core API to simplify this std::vector<gpr_slice> c_slices(nslices); for (size_t i = 0; i < nslices; i++) { c_slices[i] = slices[i].slice_; } - buffer_ = grpc_byte_buffer_create(c_slices.data(), nslices); + buffer_ = grpc_raw_byte_buffer_create(c_slices.data(), nslices); } void ByteBuffer::Clear() { @@ -51,20 +52,20 @@ void ByteBuffer::Clear() { } } -void ByteBuffer::Dump(std::vector<Slice>* slices) { +void ByteBuffer::Dump(std::vector<Slice>* slices) const { slices->clear(); if (!buffer_) { return; } - grpc_byte_buffer_reader* reader = grpc_byte_buffer_reader_create(buffer_); + grpc_byte_buffer_reader reader; + grpc_byte_buffer_reader_init(&reader, buffer_); gpr_slice s; - while (grpc_byte_buffer_reader_next(reader, &s)) { + while (grpc_byte_buffer_reader_next(&reader, &s)) { slices->push_back(Slice(s, Slice::STEAL_REF)); } - grpc_byte_buffer_reader_destroy(reader); } -size_t ByteBuffer::Length() { +size_t ByteBuffer::Length() const { if (buffer_) { return grpc_byte_buffer_length(buffer_); } else { diff --git a/src/cpp/util/status.cc b/src/cpp/util/status.cc index b694a513e7..5bb9eda3d9 100644 --- a/src/cpp/util/status.cc +++ b/src/cpp/util/status.cc @@ -36,6 +36,6 @@ namespace grpc { const Status& Status::OK = Status(); -const Status& Status::Cancelled = Status(StatusCode::CANCELLED); +const Status& Status::CANCELLED = Status(StatusCode::CANCELLED, ""); } // namespace grpc diff --git a/src/cpp/util/time.cc b/src/cpp/util/time.cc index 1fef2a56de..fd94d00b32 100644 --- a/src/cpp/util/time.cc +++ b/src/cpp/util/time.cc @@ -42,6 +42,7 @@ using std::chrono::duration_cast; using std::chrono::nanoseconds; using std::chrono::seconds; using std::chrono::system_clock; +using std::chrono::high_resolution_clock; namespace grpc { @@ -59,6 +60,20 @@ void Timepoint2Timespec(const system_clock::time_point& from, to->tv_nsec = nsecs.count(); } +void TimepointHR2Timespec(const high_resolution_clock::time_point& from, + gpr_timespec* to) { + high_resolution_clock::duration deadline = from.time_since_epoch(); + seconds secs = duration_cast<seconds>(deadline); + if (from == high_resolution_clock::time_point::max() || + secs.count() >= gpr_inf_future.tv_sec || secs.count() < 0) { + *to = gpr_inf_future; + return; + } + nanoseconds nsecs = duration_cast<nanoseconds>(deadline - secs); + to->tv_sec = secs.count(); + to->tv_nsec = nsecs.count(); +} + system_clock::time_point Timespec2Timepoint(gpr_timespec t) { if (gpr_time_cmp(t, gpr_inf_future) == 0) { return system_clock::time_point::max(); diff --git a/src/csharp/Grpc.Auth/Grpc.Auth.nuspec b/src/csharp/Grpc.Auth/Grpc.Auth.nuspec index 171259d18d..e7a538b21b 100644 --- a/src/csharp/Grpc.Auth/Grpc.Auth.nuspec +++ b/src/csharp/Grpc.Auth/Grpc.Auth.nuspec @@ -5,19 +5,19 @@ <title>gRPC C# Auth</title> <summary>Auth library for C# implementation of gRPC - an RPC library and framework</summary> <description>Auth library for C# implementation of gRPC - an RPC library and framework. See project site for more info.</description> - <version>0.5.0</version> + <version>0.5.1</version> <authors>Google Inc.</authors> <owners>grpc-packages</owners> <licenseUrl>https://github.com/grpc/grpc/blob/master/LICENSE</licenseUrl> <projectUrl>https://github.com/grpc/grpc</projectUrl> <requireLicenseAcceptance>false</requireLicenseAcceptance> - <releaseNotes>Release 0.5.0 of gRPC C#</releaseNotes> + <releaseNotes>Release 0.5.1 of gRPC C#</releaseNotes> <copyright>Copyright 2015, Google Inc.</copyright> <tags>gRPC RPC Protocol HTTP/2 Auth OAuth2</tags> <dependencies> <dependency id="BouncyCastle" version="1.7.0" /> <dependency id="Google.Apis.Auth" version="1.9.1" /> - <dependency id="Grpc.Core" version="0.5.0" /> + <dependency id="Grpc.Core" version="0.5.1" /> </dependencies> </metadata> <files> diff --git a/src/csharp/Grpc.Core.Tests/ChannelOptionsTest.cs b/src/csharp/Grpc.Core.Tests/ChannelOptionsTest.cs new file mode 100644 index 0000000000..df09857efe --- /dev/null +++ b/src/csharp/Grpc.Core.Tests/ChannelOptionsTest.cs @@ -0,0 +1,105 @@ +#region Copyright notice and license + +// 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. + +#endregion + +using System; +using System.Collections.Generic; +using Grpc.Core; +using Grpc.Core.Internal; +using Grpc.Core.Utils; +using NUnit.Framework; + +namespace Grpc.Core.Internal.Tests +{ + public class ChannelOptionsTest + { + [Test] + public void IntOption() + { + var option = new ChannelOption("somename", 1); + + Assert.AreEqual(ChannelOption.OptionType.Integer, option.Type); + Assert.AreEqual("somename", option.Name); + Assert.AreEqual(1, option.IntValue); + Assert.Throws(typeof(InvalidOperationException), () => { var s = option.StringValue; }); + } + + [Test] + public void StringOption() + { + var option = new ChannelOption("somename", "ABCDEF"); + + Assert.AreEqual(ChannelOption.OptionType.String, option.Type); + Assert.AreEqual("somename", option.Name); + Assert.AreEqual("ABCDEF", option.StringValue); + Assert.Throws(typeof(InvalidOperationException), () => { var s = option.IntValue; }); + } + + [Test] + public void ConstructorPreconditions() + { + Assert.Throws(typeof(NullReferenceException), () => { new ChannelOption(null, "abc"); }); + Assert.Throws(typeof(NullReferenceException), () => { new ChannelOption(null, 1); }); + Assert.Throws(typeof(NullReferenceException), () => { new ChannelOption("abc", null); }); + } + + [Test] + public void CreateChannelArgsNull() + { + var channelArgs = ChannelOptions.CreateChannelArgs(null); + Assert.IsTrue(channelArgs.IsInvalid); + } + + [Test] + public void CreateChannelArgsEmpty() + { + var options = new List<ChannelOption>(); + var channelArgs = ChannelOptions.CreateChannelArgs(options); + channelArgs.Dispose(); + } + + [Test] + public void CreateChannelArgs() + { + var options = new List<ChannelOption> + { + new ChannelOption("ABC", "XYZ"), + new ChannelOption("somename", "IJKLM"), + new ChannelOption("intoption", 12345), + new ChannelOption("GHIJK", 12345), + }; + + var channelArgs = ChannelOptions.CreateChannelArgs(options); + channelArgs.Dispose(); + } + } +} diff --git a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs index b69b933aba..21f94d3cf5 100644 --- a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs +++ b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs @@ -86,7 +86,7 @@ namespace Grpc.Core.Tests server.AddServiceDefinition(ServiceDefinition); int port = server.AddListeningPort(Host, Server.PickUnusedPort); server.Start(); - channel = new Channel(Host + ":" + port); + channel = new Channel(Host, port); } [TearDown] @@ -204,7 +204,7 @@ namespace Grpc.Core.Tests BenchmarkUtil.RunBenchmark(100, 100, () => { Calls.BlockingUnaryCall(call, "ABC", default(CancellationToken)); }); } - + [Test] public void UnknownMethodHandler() { diff --git a/src/csharp/Grpc.Core.Tests/Grpc.Core.Tests.csproj b/src/csharp/Grpc.Core.Tests/Grpc.Core.Tests.csproj index 62cb443272..92e28b7d74 100644 --- a/src/csharp/Grpc.Core.Tests/Grpc.Core.Tests.csproj +++ b/src/csharp/Grpc.Core.Tests/Grpc.Core.Tests.csproj @@ -3,8 +3,6 @@ <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> - <ProductVersion>10.0.0</ProductVersion> - <SchemaVersion>2.0</SchemaVersion> <ProjectGuid>{86EC5CB4-4EA2-40A2-8057-86542A0353BB}</ProjectGuid> <OutputType>Library</OutputType> <RootNamespace>Grpc.Core.Tests</RootNamespace> @@ -46,6 +44,10 @@ <Compile Include="TimespecTest.cs" /> <Compile Include="PInvokeTest.cs" /> <Compile Include="Internal\MetadataArraySafeHandleTest.cs" /> + <Compile Include="Internal\CompletionQueueSafeHandleTest.cs" /> + <Compile Include="Internal\CompletionQueueEventTest.cs" /> + <Compile Include="Internal\ChannelArgsSafeHandleTest.cs" /> + <Compile Include="ChannelOptionsTest.cs" /> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <ItemGroup> @@ -61,4 +63,4 @@ <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" /> </ItemGroup> <ItemGroup /> -</Project>
\ No newline at end of file +</Project> diff --git a/src/csharp/Grpc.Core.Tests/Internal/ChannelArgsSafeHandleTest.cs b/src/csharp/Grpc.Core.Tests/Internal/ChannelArgsSafeHandleTest.cs new file mode 100644 index 0000000000..af0aaa5f01 --- /dev/null +++ b/src/csharp/Grpc.Core.Tests/Internal/ChannelArgsSafeHandleTest.cs @@ -0,0 +1,75 @@ +#region Copyright notice and license + +// 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. + +#endregion + +using System; +using Grpc.Core; +using Grpc.Core.Internal; +using Grpc.Core.Utils; +using NUnit.Framework; + +namespace Grpc.Core.Internal.Tests +{ + public class ChannelArgsSafeHandleTest + { + [Test] + public void CreateEmptyAndDestroy() + { + var channelArgs = ChannelArgsSafeHandle.Create(0); + channelArgs.Dispose(); + } + + [Test] + public void CreateNonEmptyAndDestroy() + { + var channelArgs = ChannelArgsSafeHandle.Create(5); + channelArgs.Dispose(); + } + + [Test] + public void CreateNullAndDestroy() + { + var channelArgs = ChannelArgsSafeHandle.CreateNull(); + channelArgs.Dispose(); + } + + [Test] + public void CreateFillAndDestroy() + { + var channelArgs = ChannelArgsSafeHandle.Create(3); + channelArgs.SetInteger(0, "somekey", 12345); + channelArgs.SetString(1, "somekey", "abcdefghijkl"); + channelArgs.SetString(2, "somekey", "XYZ"); + channelArgs.Dispose(); + } + } +} diff --git a/src/csharp/Grpc.Core.Tests/Internal/CompletionQueueEventTest.cs b/src/csharp/Grpc.Core.Tests/Internal/CompletionQueueEventTest.cs new file mode 100644 index 0000000000..188c6406a2 --- /dev/null +++ b/src/csharp/Grpc.Core.Tests/Internal/CompletionQueueEventTest.cs @@ -0,0 +1,52 @@ +#region Copyright notice and license + +// 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. + +#endregion + +using System; +using System.Runtime.InteropServices; +using System.Threading.Tasks; +using Grpc.Core; +using Grpc.Core.Internal; +using Grpc.Core.Utils; +using NUnit.Framework; + +namespace Grpc.Core.Internal.Tests +{ + public class CompletionQueueEventTest + { + [Test] + public void CreateAndDestroy() + { + Assert.AreEqual(CompletionQueueEvent.NativeSize, Marshal.SizeOf(typeof(CompletionQueueEvent))); + } + } +} diff --git a/src/csharp/Grpc.Core.Tests/Internal/CompletionQueueSafeHandleTest.cs b/src/csharp/Grpc.Core.Tests/Internal/CompletionQueueSafeHandleTest.cs new file mode 100644 index 0000000000..a2ee183272 --- /dev/null +++ b/src/csharp/Grpc.Core.Tests/Internal/CompletionQueueSafeHandleTest.cs @@ -0,0 +1,64 @@ +#region Copyright notice and license + +// 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. + +#endregion + +using System; +using System.Threading.Tasks; +using Grpc.Core; +using Grpc.Core.Internal; +using Grpc.Core.Utils; +using NUnit.Framework; + +namespace Grpc.Core.Internal.Tests +{ + public class CompletionQueueSafeHandleTest + { + [Test] + public void CreateAndDestroy() + { + var cq = CompletionQueueSafeHandle.Create(); + cq.Dispose(); + } + + [Test] + public void CreateAndShutdown() + { + var cq = CompletionQueueSafeHandle.Create(); + cq.Shutdown(); + var ev = cq.Next(); + cq.Dispose(); + Assert.AreEqual(GRPCCompletionType.Shutdown, ev.type); + Assert.AreNotEqual(IntPtr.Zero, ev.success); + Assert.AreEqual(IntPtr.Zero, ev.tag); + } + } +} diff --git a/src/csharp/Grpc.Core.Tests/PInvokeTest.cs b/src/csharp/Grpc.Core.Tests/PInvokeTest.cs index 26f87660df..8b3c910251 100644 --- a/src/csharp/Grpc.Core.Tests/PInvokeTest.cs +++ b/src/csharp/Grpc.Core.Tests/PInvokeTest.cs @@ -48,7 +48,7 @@ namespace Grpc.Core.Tests int counter; [DllImport("grpc_csharp_ext.dll")] - static extern GRPCCallError grpcsharp_test_callback([MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback); + static extern GRPCCallError grpcsharp_test_callback([MarshalAs(UnmanagedType.FunctionPtr)] OpCompletionDelegate callback); [DllImport("grpc_csharp_ext.dll")] static extern IntPtr grpcsharp_test_nop(IntPtr ptr); @@ -88,7 +88,7 @@ namespace Grpc.Core.Tests [Test] public void NativeCallbackBenchmark() { - CompletionCallbackDelegate handler = Handler; + OpCompletionDelegate handler = Handler; counter = 0; BenchmarkUtil.RunBenchmark( @@ -114,7 +114,7 @@ namespace Grpc.Core.Tests 10000, 10000, () => { - grpcsharp_test_callback(new CompletionCallbackDelegate(Handler)); + grpcsharp_test_callback(new OpCompletionDelegate(Handler)); }); Assert.AreNotEqual(0, counter); } @@ -134,7 +134,7 @@ namespace Grpc.Core.Tests }); } - private void Handler(bool success, IntPtr ptr) + private void Handler(bool success) { counter++; } diff --git a/src/csharp/Grpc.Core/Channel.cs b/src/csharp/Grpc.Core/Channel.cs index b47d810672..d6bfbb7bc4 100644 --- a/src/csharp/Grpc.Core/Channel.cs +++ b/src/csharp/Grpc.Core/Channel.cs @@ -29,6 +29,7 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion using System; +using System.Collections.Generic; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -45,33 +46,41 @@ namespace Grpc.Core readonly string target; /// <summary> - /// Creates a channel. + /// Creates a channel that connects to a specific host. + /// Port will default to 80 for an unsecure channel and to 443 a secure channel. /// </summary> - public Channel(string target, Credentials credentials = null, ChannelArgs channelArgs = null) + /// <param name="host">The DNS name of IP address of the host.</param> + /// <param name="credentials">Optional credentials to create a secure channel.</param> + /// <param name="options">Channel options.</param> + public Channel(string host, Credentials credentials = null, IEnumerable<ChannelOption> options = null) { - using (ChannelArgsSafeHandle nativeChannelArgs = CreateNativeChannelArgs(channelArgs)) + using (ChannelArgsSafeHandle nativeChannelArgs = ChannelOptions.CreateChannelArgs(options)) { if (credentials != null) { using (CredentialsSafeHandle nativeCredentials = credentials.ToNativeCredentials()) { - this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, target, nativeChannelArgs); + this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, host, nativeChannelArgs); } } else { - this.handle = ChannelSafeHandle.Create(target, nativeChannelArgs); + this.handle = ChannelSafeHandle.Create(host, nativeChannelArgs); } } - this.target = GetOverridenTarget(target, channelArgs); + this.target = GetOverridenTarget(host, options); } - public string Target + /// <summary> + /// Creates a channel that connects to a specific host and port. + /// </summary> + /// <param name="host">DNS name or IP address</param> + /// <param name="port">the port</param> + /// <param name="credentials">Optional credentials to create a secure channel.</param> + /// <param name="options">Channel options.</param> + public Channel(string host, int port, Credentials credentials = null, IEnumerable<ChannelOption> options = null) : + this(string.Format("{0}:{1}", host, port), credentials, options) { - get - { - return this.target; - } } public void Dispose() @@ -80,6 +89,14 @@ namespace Grpc.Core GC.SuppressFinalize(this); } + internal string Target + { + get + { + return target; + } + } + internal ChannelSafeHandle Handle { get @@ -96,22 +113,25 @@ namespace Grpc.Core } } - private static string GetOverridenTarget(string target, ChannelArgs args) + /// <summary> + /// Look for SslTargetNameOverride option and return its value instead of originalTarget + /// if found. + /// </summary> + private static string GetOverridenTarget(string originalTarget, IEnumerable<ChannelOption> options) { - if (args != null && !string.IsNullOrEmpty(args.GetSslTargetNameOverride())) + if (options == null) { - return args.GetSslTargetNameOverride(); + return originalTarget; } - return target; - } - - private static ChannelArgsSafeHandle CreateNativeChannelArgs(ChannelArgs args) - { - if (args == null) + foreach (var option in options) { - return ChannelArgsSafeHandle.CreateNull(); + if (option.Type == ChannelOption.OptionType.String + && option.Name == ChannelOptions.SslTargetNameOverride) + { + return option.StringValue; + } } - return args.ToNativeChannelArgs(); + return originalTarget; } } } diff --git a/src/csharp/Grpc.Core/ChannelArgs.cs b/src/csharp/Grpc.Core/ChannelArgs.cs deleted file mode 100644 index 74ab310e44..0000000000 --- a/src/csharp/Grpc.Core/ChannelArgs.cs +++ /dev/null @@ -1,115 +0,0 @@ -#region Copyright notice and license -// 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. -#endregion -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Runtime.InteropServices; -using System.Threading; -using System.Threading.Tasks; -using Grpc.Core.Internal; - -namespace Grpc.Core -{ - /// <summary> - /// gRPC channel options. - /// </summary> - public class ChannelArgs - { - public const string SslTargetNameOverrideKey = "grpc.ssl_target_name_override"; - - readonly ImmutableDictionary<string, string> stringArgs; - - private ChannelArgs(ImmutableDictionary<string, string> stringArgs) - { - this.stringArgs = stringArgs; - } - - public string GetSslTargetNameOverride() - { - string result; - if (stringArgs.TryGetValue(SslTargetNameOverrideKey, out result)) - { - return result; - } - return null; - } - - public static Builder CreateBuilder() - { - return new Builder(); - } - - public class Builder - { - readonly Dictionary<string, string> stringArgs = new Dictionary<string, string>(); - - // TODO: AddInteger not supported yet. - public Builder AddString(string key, string value) - { - stringArgs.Add(key, value); - return this; - } - - public ChannelArgs Build() - { - return new ChannelArgs(stringArgs.ToImmutableDictionary()); - } - } - - /// <summary> - /// Creates native object for the channel arguments. - /// </summary> - /// <returns>The native channel arguments.</returns> - internal ChannelArgsSafeHandle ToNativeChannelArgs() - { - ChannelArgsSafeHandle nativeArgs = null; - try - { - nativeArgs = ChannelArgsSafeHandle.Create(stringArgs.Count); - int i = 0; - foreach (var entry in stringArgs) - { - nativeArgs.SetString(i, entry.Key, entry.Value); - i++; - } - return nativeArgs; - } - catch (Exception) - { - if (nativeArgs != null) - { - nativeArgs.Dispose(); - } - throw; - } - } - } -} diff --git a/src/csharp/Grpc.Core/ChannelOptions.cs b/src/csharp/Grpc.Core/ChannelOptions.cs new file mode 100644 index 0000000000..bc23bb59b1 --- /dev/null +++ b/src/csharp/Grpc.Core/ChannelOptions.cs @@ -0,0 +1,178 @@ +#region Copyright notice and license +// 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. +#endregion +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Runtime.InteropServices; +using System.Threading; +using System.Threading.Tasks; +using Grpc.Core.Internal; +using Grpc.Core.Utils; + +namespace Grpc.Core +{ + /// <summary> + /// Channel option specified when creating a channel. + /// Corresponds to grpc_channel_args from grpc/grpc.h. + /// </summary> + public sealed class ChannelOption + { + public enum OptionType + { + Integer, + String + } + + private readonly OptionType type; + private readonly string name; + private readonly int intValue; + private readonly string stringValue; + + /// <summary> + /// Creates a channel option with a string value. + /// </summary> + /// <param name="name">Name.</param> + /// <param name="stringValue">String value.</param> + public ChannelOption(string name, string stringValue) + { + this.type = OptionType.String; + this.name = Preconditions.CheckNotNull(name); + this.stringValue = Preconditions.CheckNotNull(stringValue); + } + + /// <summary> + /// Creates a channel option with an integer value. + /// </summary> + /// <param name="name">Name.</param> + /// <param name="stringValue">String value.</param> + public ChannelOption(string name, int intValue) + { + this.type = OptionType.Integer; + this.name = Preconditions.CheckNotNull(name); + this.intValue = intValue; + } + + public OptionType Type + { + get + { + return type; + } + } + + public string Name + { + get + { + return name; + } + } + + public int IntValue + { + get + { + Preconditions.CheckState(type == OptionType.Integer); + return intValue; + } + } + + public string StringValue + { + get + { + Preconditions.CheckState(type == OptionType.String); + return stringValue; + } + } + } + + public static class ChannelOptions + { + // Override SSL target check. Only to be used for testing. + public const string SslTargetNameOverride = "grpc.ssl_target_name_override"; + + // Enable census for tracing and stats collection + public const string Census = "grpc.census"; + + // Maximum number of concurrent incoming streams to allow on a http2 connection + public const string MaxConcurrentStreams = "grpc.max_concurrent_streams"; + + // Maximum message length that the channel can receive + public const string MaxMessageLength = "grpc.max_message_length"; + + // Initial sequence number for http2 transports + public const string Http2InitialSequenceNumber = "grpc.http2.initial_sequence_number"; + + /// <summary> + /// Creates native object for a collection of channel options. + /// </summary> + /// <returns>The native channel arguments.</returns> + internal static ChannelArgsSafeHandle CreateChannelArgs(IEnumerable<ChannelOption> options) + { + if (options == null) + { + return ChannelArgsSafeHandle.CreateNull(); + } + var optionList = new List<ChannelOption>(options); // It's better to do defensive copy + ChannelArgsSafeHandle nativeArgs = null; + try + { + nativeArgs = ChannelArgsSafeHandle.Create(optionList.Count); + for (int i = 0; i < optionList.Count; i++) + { + var option = optionList[i]; + if (option.Type == ChannelOption.OptionType.Integer) + { + nativeArgs.SetInteger(i, option.Name, option.IntValue); + } + else if (option.Type == ChannelOption.OptionType.String) + { + nativeArgs.SetString(i, option.Name, option.StringValue); + } + else + { + throw new InvalidOperationException("Unknown option type"); + } + } + return nativeArgs; + } + catch (Exception) + { + if (nativeArgs != null) + { + nativeArgs.Dispose(); + } + throw; + } + } + } +} diff --git a/src/csharp/Grpc.Core/Grpc.Core.csproj b/src/csharp/Grpc.Core/Grpc.Core.csproj index fe2d446a35..a36a6a5acc 100644 --- a/src/csharp/Grpc.Core/Grpc.Core.csproj +++ b/src/csharp/Grpc.Core/Grpc.Core.csproj @@ -5,8 +5,6 @@ <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> - <ProductVersion>8.0.30703</ProductVersion> - <SchemaVersion>2.0</SchemaVersion> <ProjectGuid>{CCC4440E-49F7-4790-B0AF-FEABB0837AE7}</ProjectGuid> <OutputType>Library</OutputType> <RootNamespace>Grpc.Core</RootNamespace> @@ -73,13 +71,11 @@ <Compile Include="Marshaller.cs" /> <Compile Include="ServerServiceDefinition.cs" /> <Compile Include="Utils\AsyncStreamExtensions.cs" /> - <Compile Include="Internal\BatchContextSafeHandleNotOwned.cs" /> <Compile Include="Utils\BenchmarkUtil.cs" /> <Compile Include="Utils\ExceptionHelper.cs" /> <Compile Include="Internal\CredentialsSafeHandle.cs" /> <Compile Include="Credentials.cs" /> <Compile Include="Internal\ChannelArgsSafeHandle.cs" /> - <Compile Include="ChannelArgs.cs" /> <Compile Include="Internal\AsyncCompletion.cs" /> <Compile Include="Internal\AsyncCallBase.cs" /> <Compile Include="Internal\AsyncCallServer.cs" /> @@ -101,6 +97,10 @@ <Compile Include="Internal\AtomicCounter.cs" /> <Compile Include="Internal\DebugStats.cs" /> <Compile Include="ServerCallContext.cs" /> + <Compile Include="Internal\CompletionQueueEvent.cs" /> + <Compile Include="Internal\CompletionRegistry.cs" /> + <Compile Include="Internal\BatchContextSafeHandle.cs" /> + <Compile Include="ChannelOptions.cs" /> </ItemGroup> <ItemGroup> <None Include="packages.config" /> @@ -130,4 +130,4 @@ </Target> <Import Project="..\packages\grpc.dependencies.openssl.redist.1.0.2.2\build\portable-net45\grpc.dependencies.openssl.redist.targets" Condition="Exists('..\packages\grpc.dependencies.openssl.redist.1.0.2.2\build\portable-net45\grpc.dependencies.openssl.redist.targets')" /> <Import Project="..\packages\grpc.dependencies.zlib.redist.1.2.8.9\build\portable-net45\grpc.dependencies.zlib.redist.targets" Condition="Exists('..\packages\grpc.dependencies.zlib.redist.1.2.8.9\build\portable-net45\grpc.dependencies.zlib.redist.targets')" /> -</Project>
\ No newline at end of file +</Project> diff --git a/src/csharp/Grpc.Core/Grpc.Core.nuspec b/src/csharp/Grpc.Core/Grpc.Core.nuspec index 42eb90c9a3..629b978fdf 100644 --- a/src/csharp/Grpc.Core/Grpc.Core.nuspec +++ b/src/csharp/Grpc.Core/Grpc.Core.nuspec @@ -5,19 +5,19 @@ <title>gRPC C# Core</title> <summary>Core C# implementation of gRPC - an RPC library and framework</summary> <description>Core C# implementation of gRPC - an RPC library and framework. See project site for more info.</description> - <version>0.5.0</version> + <version>0.5.1</version> <authors>Google Inc.</authors> <owners>grpc-packages</owners> <licenseUrl>https://github.com/grpc/grpc/blob/master/LICENSE</licenseUrl> <projectUrl>https://github.com/grpc/grpc</projectUrl> <requireLicenseAcceptance>false</requireLicenseAcceptance> - <releaseNotes>Release 0.5.0 of gRPC C#</releaseNotes> + <releaseNotes>Release 0.5.1 of gRPC C#</releaseNotes> <copyright>Copyright 2015, Google Inc.</copyright> <tags>gRPC RPC Protocol HTTP/2</tags> <dependencies> <dependency id="Microsoft.Bcl.Immutable" version="1.0.34" /> <dependency id="Ix-Async" version="1.2.3" /> - <dependency id="grpc.native.csharp_ext" version="0.9.0.0" /> + <dependency id="grpc.native.csharp_ext" version="0.9.1" /> </dependencies> </metadata> <files> diff --git a/src/csharp/Grpc.Core/GrpcEnvironment.cs b/src/csharp/Grpc.Core/GrpcEnvironment.cs index 2e9e5a2ef6..30ff289714 100644 --- a/src/csharp/Grpc.Core/GrpcEnvironment.cs +++ b/src/csharp/Grpc.Core/GrpcEnvironment.cs @@ -54,6 +54,7 @@ namespace Grpc.Core static volatile GrpcEnvironment instance; readonly GrpcThreadPool threadPool; + readonly CompletionRegistry completionRegistry; bool isClosed; /// <summary> @@ -105,6 +106,19 @@ namespace Grpc.Core } } + internal static CompletionRegistry CompletionRegistry + { + get + { + var inst = instance; + if (inst == null) + { + throw new InvalidOperationException("GRPC environment not initialized"); + } + return inst.completionRegistry; + } + } + /// <summary> /// Creates gRPC environment. /// </summary> @@ -112,6 +126,7 @@ namespace Grpc.Core { GrpcLog.RedirectNativeLogs(Console.Error); grpcsharp_init(); + completionRegistry = new CompletionRegistry(); threadPool = new GrpcThreadPool(THREAD_POOL_SIZE); threadPool.Start(); // TODO: use proper logging here @@ -139,14 +154,24 @@ namespace Grpc.Core { var remainingClientCalls = DebugStats.ActiveClientCalls.Count; if (remainingClientCalls != 0) - { - Console.WriteLine("Warning: Detected {0} client calls that weren't disposed properly.", remainingClientCalls); + { + DebugWarning(string.Format("Detected {0} client calls that weren't disposed properly.", remainingClientCalls)); } var remainingServerCalls = DebugStats.ActiveServerCalls.Count; if (remainingServerCalls != 0) { - Console.WriteLine("Warning: Detected {0} server calls that weren't disposed properly.", remainingServerCalls); + DebugWarning(string.Format("Detected {0} server calls that weren't disposed properly.", remainingServerCalls)); + } + var pendingBatchCompletions = DebugStats.PendingBatchCompletions.Count; + if (pendingBatchCompletions != 0) + { + DebugWarning(string.Format("Detected {0} pending batch completions.", pendingBatchCompletions)); } } + + private static void DebugWarning(string message) + { + throw new Exception("Shutdown check: " + message); + } } } diff --git a/src/csharp/Grpc.Core/Internal/AsyncCall.cs b/src/csharp/Grpc.Core/Internal/AsyncCall.cs index 9bb918d53d..d350f45da6 100644 --- a/src/csharp/Grpc.Core/Internal/AsyncCall.cs +++ b/src/csharp/Grpc.Core/Internal/AsyncCall.cs @@ -47,9 +47,6 @@ namespace Grpc.Core.Internal /// </summary> internal class AsyncCall<TRequest, TResponse> : AsyncCallBase<TRequest, TResponse> { - readonly CompletionCallbackDelegate unaryResponseHandler; - readonly CompletionCallbackDelegate finishedHandler; - // Completion of a pending unary response if not null. TaskCompletionSource<TResponse> unaryResponseTcs; @@ -60,8 +57,6 @@ namespace Grpc.Core.Internal public AsyncCall(Func<TRequest, byte[]> serializer, Func<byte[], TResponse> deserializer) : base(serializer, deserializer) { - this.unaryResponseHandler = CreateBatchCompletionCallback(HandleUnaryResponse); - this.finishedHandler = CreateBatchCompletionCallback(HandleFinished); } public void Initialize(Channel channel, CompletionQueueSafeHandle cq, string methodName) @@ -96,7 +91,21 @@ namespace Grpc.Core.Internal using (var metadataArray = MetadataArraySafeHandle.Create(headers)) { - call.BlockingUnary(cq, payload, unaryResponseHandler, metadataArray); + using (var ctx = BatchContextSafeHandle.Create()) + { + call.StartUnary(payload, ctx, metadataArray); + var ev = cq.Pluck(ctx.Handle); + + bool success = (ev.success != 0); + try + { + HandleUnaryResponse(success, ctx); + } + catch (Exception e) + { + Console.WriteLine("Exception occured while invoking completion delegate: " + e); + } + } } try @@ -129,7 +138,7 @@ namespace Grpc.Core.Internal unaryResponseTcs = new TaskCompletionSource<TResponse>(); using (var metadataArray = MetadataArraySafeHandle.Create(headers)) { - call.StartUnary(payload, unaryResponseHandler, metadataArray); + call.StartUnary(payload, HandleUnaryResponse, metadataArray); } return unaryResponseTcs.Task; } @@ -151,7 +160,7 @@ namespace Grpc.Core.Internal unaryResponseTcs = new TaskCompletionSource<TResponse>(); using (var metadataArray = MetadataArraySafeHandle.Create(headers)) { - call.StartClientStreaming(unaryResponseHandler, metadataArray); + call.StartClientStreaming(HandleUnaryResponse, metadataArray); } return unaryResponseTcs.Task; @@ -175,7 +184,7 @@ namespace Grpc.Core.Internal using (var metadataArray = MetadataArraySafeHandle.Create(headers)) { - call.StartServerStreaming(payload, finishedHandler, metadataArray); + call.StartServerStreaming(payload, HandleFinished, metadataArray); } } } @@ -194,7 +203,7 @@ namespace Grpc.Core.Internal using (var metadataArray = MetadataArraySafeHandle.Create(headers)) { - call.StartDuplexStreaming(finishedHandler, metadataArray); + call.StartDuplexStreaming(HandleFinished, metadataArray); } } } @@ -229,7 +238,7 @@ namespace Grpc.Core.Internal Preconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null"); CheckSendingAllowed(); - call.StartSendCloseFromClient(halfclosedHandler); + call.StartSendCloseFromClient(HandleHalfclosed); halfcloseRequested = true; sendCompletionDelegate = completionDelegate; @@ -274,7 +283,7 @@ namespace Grpc.Core.Internal /// <summary> /// Handler for unary response completion. /// </summary> - private void HandleUnaryResponse(bool success, BatchContextSafeHandleNotOwned ctx) + private void HandleUnaryResponse(bool success, BatchContextSafeHandle ctx) { lock (myLock) { @@ -307,7 +316,7 @@ namespace Grpc.Core.Internal /// <summary> /// Handles receive status completion for calls with streaming response. /// </summary> - private void HandleFinished(bool success, BatchContextSafeHandleNotOwned ctx) + private void HandleFinished(bool success, BatchContextSafeHandle ctx) { var status = ctx.GetReceivedStatus(); diff --git a/src/csharp/Grpc.Core/Internal/AsyncCallBase.cs b/src/csharp/Grpc.Core/Internal/AsyncCallBase.cs index b4f4edb17a..64713c8c52 100644 --- a/src/csharp/Grpc.Core/Internal/AsyncCallBase.cs +++ b/src/csharp/Grpc.Core/Internal/AsyncCallBase.cs @@ -51,13 +51,8 @@ namespace Grpc.Core.Internal readonly Func<TWrite, byte[]> serializer; readonly Func<byte[], TRead> deserializer; - protected readonly CompletionCallbackDelegate sendFinishedHandler; - protected readonly CompletionCallbackDelegate readFinishedHandler; - protected readonly CompletionCallbackDelegate halfclosedHandler; - protected readonly object myLock = new object(); - protected GCHandle gchandle; protected CallSafeHandle call; protected bool disposed; @@ -77,10 +72,6 @@ namespace Grpc.Core.Internal { this.serializer = Preconditions.CheckNotNull(serializer); this.deserializer = Preconditions.CheckNotNull(deserializer); - - this.sendFinishedHandler = CreateBatchCompletionCallback(HandleSendFinished); - this.readFinishedHandler = CreateBatchCompletionCallback(HandleReadFinished); - this.halfclosedHandler = CreateBatchCompletionCallback(HandleHalfclosed); } /// <summary> @@ -121,9 +112,6 @@ namespace Grpc.Core.Internal { lock (myLock) { - // Make sure this object and the delegated held by it will not be garbage collected - // before we release this handle. - gchandle = GCHandle.Alloc(this); this.call = call; } } @@ -141,7 +129,7 @@ namespace Grpc.Core.Internal Preconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null"); CheckSendingAllowed(); - call.StartSendMessage(payload, sendFinishedHandler); + call.StartSendMessage(payload, HandleSendFinished); sendCompletionDelegate = completionDelegate; } } @@ -157,7 +145,7 @@ namespace Grpc.Core.Internal Preconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null"); CheckReadingAllowed(); - call.StartReceiveMessage(readFinishedHandler); + call.StartReceiveMessage(HandleReadFinished); readCompletionDelegate = completionDelegate; } } @@ -197,7 +185,6 @@ namespace Grpc.Core.Internal { call.Dispose(); } - gchandle.Free(); disposed = true; } @@ -282,29 +269,9 @@ namespace Grpc.Core.Internal } /// <summary> - /// Creates completion callback delegate that wraps the batch completion handler in a try catch block to - /// prevent propagating exceptions accross managed/unmanaged boundary. - /// </summary> - protected CompletionCallbackDelegate CreateBatchCompletionCallback(Action<bool, BatchContextSafeHandleNotOwned> handler) - { - return new CompletionCallbackDelegate((success, batchContextPtr) => - { - try - { - var ctx = new BatchContextSafeHandleNotOwned(batchContextPtr); - handler(success, ctx); - } - catch (Exception e) - { - Console.WriteLine("Caught exception in a native handler: " + e); - } - }); - } - - /// <summary> /// Handles send completion. /// </summary> - private void HandleSendFinished(bool success, BatchContextSafeHandleNotOwned ctx) + protected void HandleSendFinished(bool success, BatchContextSafeHandle ctx) { AsyncCompletionDelegate<object> origCompletionDelegate = null; lock (myLock) @@ -328,7 +295,7 @@ namespace Grpc.Core.Internal /// <summary> /// Handles halfclose completion. /// </summary> - private void HandleHalfclosed(bool success, BatchContextSafeHandleNotOwned ctx) + protected void HandleHalfclosed(bool success, BatchContextSafeHandle ctx) { AsyncCompletionDelegate<object> origCompletionDelegate = null; lock (myLock) @@ -353,7 +320,7 @@ namespace Grpc.Core.Internal /// <summary> /// Handles streaming read completion. /// </summary> - private void HandleReadFinished(bool success, BatchContextSafeHandleNotOwned ctx) + protected void HandleReadFinished(bool success, BatchContextSafeHandle ctx) { var payload = ctx.GetReceivedMessage(); diff --git a/src/csharp/Grpc.Core/Internal/AsyncCallServer.cs b/src/csharp/Grpc.Core/Internal/AsyncCallServer.cs index 1f0335e4e6..4f510ba40a 100644 --- a/src/csharp/Grpc.Core/Internal/AsyncCallServer.cs +++ b/src/csharp/Grpc.Core/Internal/AsyncCallServer.cs @@ -47,12 +47,10 @@ namespace Grpc.Core.Internal /// </summary> internal class AsyncCallServer<TRequest, TResponse> : AsyncCallBase<TResponse, TRequest> { - readonly CompletionCallbackDelegate finishedServersideHandler; readonly TaskCompletionSource<object> finishedServersideTcs = new TaskCompletionSource<object>(); public AsyncCallServer(Func<TResponse, byte[]> serializer, Func<byte[], TRequest> deserializer) : base(serializer, deserializer) { - this.finishedServersideHandler = CreateBatchCompletionCallback(HandleFinishedServerside); } public void Initialize(CallSafeHandle call) @@ -72,7 +70,7 @@ namespace Grpc.Core.Internal started = true; - call.StartServerSide(finishedServersideHandler); + call.StartServerSide(HandleFinishedServerside); return finishedServersideTcs.Task; } } @@ -107,8 +105,9 @@ namespace Grpc.Core.Internal Preconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null"); CheckSendingAllowed(); - call.StartSendStatusFromServer(status, halfclosedHandler); + call.StartSendStatusFromServer(status, HandleHalfclosed); halfcloseRequested = true; + readingDone = true; sendCompletionDelegate = completionDelegate; } } @@ -121,7 +120,7 @@ namespace Grpc.Core.Internal /// <summary> /// Handles the server side close completion. /// </summary> - private void HandleFinishedServerside(bool success, BatchContextSafeHandleNotOwned ctx) + private void HandleFinishedServerside(bool success, BatchContextSafeHandle ctx) { bool cancelled = ctx.GetReceivedCloseOnServerCancelled(); diff --git a/src/csharp/Grpc.Core/Internal/BatchContextSafeHandleNotOwned.cs b/src/csharp/Grpc.Core/Internal/BatchContextSafeHandle.cs index b562abaa7a..861cbbe4c6 100644 --- a/src/csharp/Grpc.Core/Internal/BatchContextSafeHandleNotOwned.cs +++ b/src/csharp/Grpc.Core/Internal/BatchContextSafeHandle.cs @@ -41,32 +41,50 @@ namespace Grpc.Core.Internal /// Not owned version of /// grpcsharp_batch_context /// </summary> - internal class BatchContextSafeHandleNotOwned : SafeHandleZeroIsInvalid + internal class BatchContextSafeHandle : SafeHandleZeroIsInvalid { [DllImport("grpc_csharp_ext.dll")] - static extern IntPtr grpcsharp_batch_context_recv_message_length(BatchContextSafeHandleNotOwned ctx); + static extern BatchContextSafeHandle grpcsharp_batch_context_create(); [DllImport("grpc_csharp_ext.dll")] - static extern void grpcsharp_batch_context_recv_message_to_buffer(BatchContextSafeHandleNotOwned ctx, byte[] buffer, UIntPtr bufferLen); + static extern IntPtr grpcsharp_batch_context_recv_message_length(BatchContextSafeHandle ctx); [DllImport("grpc_csharp_ext.dll")] - static extern StatusCode grpcsharp_batch_context_recv_status_on_client_status(BatchContextSafeHandleNotOwned ctx); + static extern void grpcsharp_batch_context_recv_message_to_buffer(BatchContextSafeHandle ctx, byte[] buffer, UIntPtr bufferLen); [DllImport("grpc_csharp_ext.dll")] - static extern IntPtr grpcsharp_batch_context_recv_status_on_client_details(BatchContextSafeHandleNotOwned ctx); // returns const char* + static extern StatusCode grpcsharp_batch_context_recv_status_on_client_status(BatchContextSafeHandle ctx); [DllImport("grpc_csharp_ext.dll")] - static extern CallSafeHandle grpcsharp_batch_context_server_rpc_new_call(BatchContextSafeHandleNotOwned ctx); + static extern IntPtr grpcsharp_batch_context_recv_status_on_client_details(BatchContextSafeHandle ctx); // returns const char* [DllImport("grpc_csharp_ext.dll")] - static extern IntPtr grpcsharp_batch_context_server_rpc_new_method(BatchContextSafeHandleNotOwned ctx); // returns const char* + static extern CallSafeHandle grpcsharp_batch_context_server_rpc_new_call(BatchContextSafeHandle ctx); [DllImport("grpc_csharp_ext.dll")] - static extern int grpcsharp_batch_context_recv_close_on_server_cancelled(BatchContextSafeHandleNotOwned ctx); + static extern IntPtr grpcsharp_batch_context_server_rpc_new_method(BatchContextSafeHandle ctx); // returns const char* - public BatchContextSafeHandleNotOwned(IntPtr handle) : base(false) + [DllImport("grpc_csharp_ext.dll")] + static extern int grpcsharp_batch_context_recv_close_on_server_cancelled(BatchContextSafeHandle ctx); + + [DllImport("grpc_csharp_ext.dll")] + static extern void grpcsharp_batch_context_destroy(IntPtr ctx); + + private BatchContextSafeHandle() + { + } + + public static BatchContextSafeHandle Create() { - SetHandle(handle); + return grpcsharp_batch_context_create(); + } + + public IntPtr Handle + { + get + { + return handle; + } } public Status GetReceivedStatus() @@ -102,5 +120,11 @@ namespace Grpc.Core.Internal { return grpcsharp_batch_context_recv_close_on_server_cancelled(this) != 0; } + + protected override bool ReleaseHandle() + { + grpcsharp_batch_context_destroy(handle); + return true; + } } }
\ No newline at end of file diff --git a/src/csharp/Grpc.Core/Internal/CallSafeHandle.cs b/src/csharp/Grpc.Core/Internal/CallSafeHandle.cs index 491b8414ec..ef92b44402 100644 --- a/src/csharp/Grpc.Core/Internal/CallSafeHandle.cs +++ b/src/csharp/Grpc.Core/Internal/CallSafeHandle.cs @@ -37,8 +37,6 @@ using Grpc.Core.Utils; namespace Grpc.Core.Internal { - internal delegate void CompletionCallbackDelegate(bool success, IntPtr batchContextPtr); - /// <summary> /// grpc_call from <grpc/grpc.h> /// </summary> @@ -57,49 +55,40 @@ namespace Grpc.Core.Internal [DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError grpcsharp_call_start_unary(CallSafeHandle call, - [MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback, - byte[] send_buffer, UIntPtr send_buffer_len, MetadataArraySafeHandle metadataArray); - - [DllImport("grpc_csharp_ext.dll")] - static extern void grpcsharp_call_blocking_unary(CallSafeHandle call, CompletionQueueSafeHandle dedicatedCq, - [MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback, - byte[] send_buffer, UIntPtr send_buffer_len, MetadataArraySafeHandle metadataArray); + BatchContextSafeHandle ctx, byte[] send_buffer, UIntPtr send_buffer_len, MetadataArraySafeHandle metadataArray); [DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError grpcsharp_call_start_client_streaming(CallSafeHandle call, - [MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback, - MetadataArraySafeHandle metadataArray); + BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray); [DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError grpcsharp_call_start_server_streaming(CallSafeHandle call, - [MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback, - byte[] send_buffer, UIntPtr send_buffer_len, - MetadataArraySafeHandle metadataArray); + BatchContextSafeHandle ctx, byte[] send_buffer, UIntPtr send_buffer_len, + MetadataArraySafeHandle metadataArray); [DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError grpcsharp_call_start_duplex_streaming(CallSafeHandle call, - [MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback, - MetadataArraySafeHandle metadataArray); + BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray); [DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError grpcsharp_call_send_message(CallSafeHandle call, - [MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback, - byte[] send_buffer, UIntPtr send_buffer_len); + BatchContextSafeHandle ctx, byte[] send_buffer, UIntPtr send_buffer_len); [DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError grpcsharp_call_send_close_from_client(CallSafeHandle call, - [MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback); + BatchContextSafeHandle ctx); [DllImport("grpc_csharp_ext.dll")] - static extern GRPCCallError grpcsharp_call_send_status_from_server(CallSafeHandle call, [MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback, StatusCode statusCode, string statusMessage); + static extern GRPCCallError grpcsharp_call_send_status_from_server(CallSafeHandle call, + BatchContextSafeHandle ctx, StatusCode statusCode, string statusMessage); [DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError grpcsharp_call_recv_message(CallSafeHandle call, - [MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback); + BatchContextSafeHandle ctx); [DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError grpcsharp_call_start_serverside(CallSafeHandle call, - [MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback); + BatchContextSafeHandle ctx); [DllImport("grpc_csharp_ext.dll")] static extern void grpcsharp_call_destroy(IntPtr call); @@ -113,64 +102,84 @@ namespace Grpc.Core.Internal return grpcsharp_channel_create_call(channel, cq, method, host, deadline); } - public void StartUnary(byte[] payload, CompletionCallbackDelegate callback, MetadataArraySafeHandle metadataArray) + public void StartUnary(byte[] payload, BatchCompletionDelegate callback, MetadataArraySafeHandle metadataArray) { - AssertCallOk(grpcsharp_call_start_unary(this, callback, payload, new UIntPtr((ulong)payload.Length), metadataArray)); + var ctx = BatchContextSafeHandle.Create(); + GrpcEnvironment.CompletionRegistry.RegisterBatchCompletion(ctx, callback); + grpcsharp_call_start_unary(this, ctx, payload, new UIntPtr((ulong)payload.Length), metadataArray) + .CheckOk(); } - public void BlockingUnary(CompletionQueueSafeHandle dedicatedCq, byte[] payload, CompletionCallbackDelegate callback, MetadataArraySafeHandle metadataArray) + public void StartUnary(byte[] payload, BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray) { - grpcsharp_call_blocking_unary(this, dedicatedCq, callback, payload, new UIntPtr((ulong)payload.Length), metadataArray); + grpcsharp_call_start_unary(this, ctx, payload, new UIntPtr((ulong)payload.Length), metadataArray) + .CheckOk(); } - public void StartClientStreaming(CompletionCallbackDelegate callback, MetadataArraySafeHandle metadataArray) + public void StartClientStreaming(BatchCompletionDelegate callback, MetadataArraySafeHandle metadataArray) { - AssertCallOk(grpcsharp_call_start_client_streaming(this, callback, metadataArray)); + var ctx = BatchContextSafeHandle.Create(); + GrpcEnvironment.CompletionRegistry.RegisterBatchCompletion(ctx, callback); + grpcsharp_call_start_client_streaming(this, ctx, metadataArray).CheckOk(); } - public void StartServerStreaming(byte[] payload, CompletionCallbackDelegate callback, MetadataArraySafeHandle metadataArray) + public void StartServerStreaming(byte[] payload, BatchCompletionDelegate callback, MetadataArraySafeHandle metadataArray) { - AssertCallOk(grpcsharp_call_start_server_streaming(this, callback, payload, new UIntPtr((ulong)payload.Length), metadataArray)); + var ctx = BatchContextSafeHandle.Create(); + GrpcEnvironment.CompletionRegistry.RegisterBatchCompletion(ctx, callback); + grpcsharp_call_start_server_streaming(this, ctx, payload, new UIntPtr((ulong)payload.Length), metadataArray).CheckOk(); } - public void StartDuplexStreaming(CompletionCallbackDelegate callback, MetadataArraySafeHandle metadataArray) + public void StartDuplexStreaming(BatchCompletionDelegate callback, MetadataArraySafeHandle metadataArray) { - AssertCallOk(grpcsharp_call_start_duplex_streaming(this, callback, metadataArray)); + var ctx = BatchContextSafeHandle.Create(); + GrpcEnvironment.CompletionRegistry.RegisterBatchCompletion(ctx, callback); + grpcsharp_call_start_duplex_streaming(this, ctx, metadataArray).CheckOk(); } - public void StartSendMessage(byte[] payload, CompletionCallbackDelegate callback) + public void StartSendMessage(byte[] payload, BatchCompletionDelegate callback) { - AssertCallOk(grpcsharp_call_send_message(this, callback, payload, new UIntPtr((ulong)payload.Length))); + var ctx = BatchContextSafeHandle.Create(); + GrpcEnvironment.CompletionRegistry.RegisterBatchCompletion(ctx, callback); + grpcsharp_call_send_message(this, ctx, payload, new UIntPtr((ulong)payload.Length)).CheckOk(); } - public void StartSendCloseFromClient(CompletionCallbackDelegate callback) + public void StartSendCloseFromClient(BatchCompletionDelegate callback) { - AssertCallOk(grpcsharp_call_send_close_from_client(this, callback)); + var ctx = BatchContextSafeHandle.Create(); + GrpcEnvironment.CompletionRegistry.RegisterBatchCompletion(ctx, callback); + grpcsharp_call_send_close_from_client(this, ctx).CheckOk(); } - public void StartSendStatusFromServer(Status status, CompletionCallbackDelegate callback) + public void StartSendStatusFromServer(Status status, BatchCompletionDelegate callback) { - AssertCallOk(grpcsharp_call_send_status_from_server(this, callback, status.StatusCode, status.Detail)); + var ctx = BatchContextSafeHandle.Create(); + GrpcEnvironment.CompletionRegistry.RegisterBatchCompletion(ctx, callback); + grpcsharp_call_send_status_from_server(this, ctx, status.StatusCode, status.Detail).CheckOk(); } - public void StartReceiveMessage(CompletionCallbackDelegate callback) + public void StartReceiveMessage(BatchCompletionDelegate callback) { - AssertCallOk(grpcsharp_call_recv_message(this, callback)); + var ctx = BatchContextSafeHandle.Create(); + GrpcEnvironment.CompletionRegistry.RegisterBatchCompletion(ctx, callback); + grpcsharp_call_recv_message(this, ctx).CheckOk(); } - public void StartServerSide(CompletionCallbackDelegate callback) + public void StartServerSide(BatchCompletionDelegate callback) { - AssertCallOk(grpcsharp_call_start_serverside(this, callback)); + var ctx = BatchContextSafeHandle.Create(); + GrpcEnvironment.CompletionRegistry.RegisterBatchCompletion(ctx, callback); + grpcsharp_call_start_serverside(this, ctx).CheckOk(); } public void Cancel() { - AssertCallOk(grpcsharp_call_cancel(this)); + grpcsharp_call_cancel(this).CheckOk(); } public void CancelWithStatus(Status status) { - AssertCallOk(grpcsharp_call_cancel_with_status(this, status.StatusCode, status.Detail)); + grpcsharp_call_cancel_with_status(this, status.StatusCode, status.Detail).CheckOk(); } protected override bool ReleaseHandle() @@ -179,11 +188,6 @@ namespace Grpc.Core.Internal return true; } - private static void AssertCallOk(GRPCCallError callError) - { - Preconditions.CheckState(callError == GRPCCallError.GRPC_CALL_OK, "Status not GRPC_CALL_OK"); - } - private static uint GetFlags(bool buffered) { return buffered ? 0 : GRPC_WRITE_BUFFER_HINT; diff --git a/src/csharp/Grpc.Core/Internal/ChannelArgsSafeHandle.cs b/src/csharp/Grpc.Core/Internal/ChannelArgsSafeHandle.cs index c69f1a0d02..c12aec5a3a 100644 --- a/src/csharp/Grpc.Core/Internal/ChannelArgsSafeHandle.cs +++ b/src/csharp/Grpc.Core/Internal/ChannelArgsSafeHandle.cs @@ -45,6 +45,9 @@ namespace Grpc.Core.Internal [DllImport("grpc_csharp_ext.dll", CharSet = CharSet.Ansi)] static extern void grpcsharp_channel_args_set_string(ChannelArgsSafeHandle args, UIntPtr index, string key, string value); + [DllImport("grpc_csharp_ext.dll", CharSet = CharSet.Ansi)] + static extern void grpcsharp_channel_args_set_integer(ChannelArgsSafeHandle args, UIntPtr index, string key, int value); + [DllImport("grpc_csharp_ext.dll")] static extern void grpcsharp_channel_args_destroy(IntPtr args); @@ -67,6 +70,11 @@ namespace Grpc.Core.Internal grpcsharp_channel_args_set_string(this, new UIntPtr((uint)index), key, value); } + public void SetInteger(int index, string key, int value) + { + grpcsharp_channel_args_set_integer(this, new UIntPtr((uint)index), key, value); + } + protected override bool ReleaseHandle() { grpcsharp_channel_args_destroy(handle); diff --git a/src/csharp/Grpc.Core/Internal/CompletionQueueEvent.cs b/src/csharp/Grpc.Core/Internal/CompletionQueueEvent.cs new file mode 100644 index 0000000000..3f517514a3 --- /dev/null +++ b/src/csharp/Grpc.Core/Internal/CompletionQueueEvent.cs @@ -0,0 +1,60 @@ +#region Copyright notice and license + +// 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. + +#endregion + +using System; +using System.Runtime.InteropServices; + +namespace Grpc.Core.Internal +{ + /// <summary> + /// grpc_event from grpc/grpc.h + /// </summary> + [StructLayout(LayoutKind.Sequential)] + internal struct CompletionQueueEvent + { + [DllImport("grpc_csharp_ext.dll")] + static extern int grpcsharp_sizeof_grpc_event(); + + public GRPCCompletionType type; + public int success; + public IntPtr tag; + + internal static int NativeSize + { + get + { + return grpcsharp_sizeof_grpc_event(); + } + } + } +} diff --git a/src/csharp/Grpc.Core/Internal/CompletionQueueSafeHandle.cs b/src/csharp/Grpc.Core/Internal/CompletionQueueSafeHandle.cs index 600d1fc87c..f64f3d4175 100644 --- a/src/csharp/Grpc.Core/Internal/CompletionQueueSafeHandle.cs +++ b/src/csharp/Grpc.Core/Internal/CompletionQueueSafeHandle.cs @@ -46,7 +46,10 @@ namespace Grpc.Core.Internal static extern void grpcsharp_completion_queue_shutdown(CompletionQueueSafeHandle cq); [DllImport("grpc_csharp_ext.dll")] - static extern GRPCCompletionType grpcsharp_completion_queue_next_with_callback(CompletionQueueSafeHandle cq); + static extern CompletionQueueEvent grpcsharp_completion_queue_next(CompletionQueueSafeHandle cq); + + [DllImport("grpc_csharp_ext.dll")] + static extern CompletionQueueEvent grpcsharp_completion_queue_pluck(CompletionQueueSafeHandle cq, IntPtr tag); [DllImport("grpc_csharp_ext.dll")] static extern void grpcsharp_completion_queue_destroy(IntPtr cq); @@ -60,9 +63,14 @@ namespace Grpc.Core.Internal return grpcsharp_completion_queue_create(); } - public GRPCCompletionType NextWithCallback() + public CompletionQueueEvent Next() + { + return grpcsharp_completion_queue_next(this); + } + + public CompletionQueueEvent Pluck(IntPtr tag) { - return grpcsharp_completion_queue_next_with_callback(this); + return grpcsharp_completion_queue_pluck(this, tag); } public void Shutdown() diff --git a/src/csharp/Grpc.Core/Internal/CompletionRegistry.cs b/src/csharp/Grpc.Core/Internal/CompletionRegistry.cs new file mode 100644 index 0000000000..80f006ae50 --- /dev/null +++ b/src/csharp/Grpc.Core/Internal/CompletionRegistry.cs @@ -0,0 +1,89 @@ +#region Copyright notice and license + +// 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. + +#endregion + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using Grpc.Core.Utils; + +namespace Grpc.Core.Internal +{ + internal delegate void OpCompletionDelegate(bool success); + + internal delegate void BatchCompletionDelegate(bool success, BatchContextSafeHandle ctx); + + internal class CompletionRegistry + { + readonly ConcurrentDictionary<IntPtr, OpCompletionDelegate> dict = new ConcurrentDictionary<IntPtr, OpCompletionDelegate>(); + + public void Register(IntPtr key, OpCompletionDelegate callback) + { + DebugStats.PendingBatchCompletions.Increment(); + Preconditions.CheckState(dict.TryAdd(key, callback)); + } + + public void RegisterBatchCompletion(BatchContextSafeHandle ctx, BatchCompletionDelegate callback) + { + OpCompletionDelegate opCallback = ((success) => HandleBatchCompletion(success, ctx, callback)); + Register(ctx.Handle, opCallback); + } + + public OpCompletionDelegate Extract(IntPtr key) + { + OpCompletionDelegate value; + Preconditions.CheckState(dict.TryRemove(key, out value)); + DebugStats.PendingBatchCompletions.Decrement(); + return value; + } + + private static void HandleBatchCompletion(bool success, BatchContextSafeHandle ctx, BatchCompletionDelegate callback) + { + try + { + callback(success, ctx); + } + catch (Exception e) + { + Console.WriteLine("Exception occured while invoking completion delegate: " + e); + } + finally + { + if (ctx != null) + { + ctx.Dispose(); + } + } + } + } +} diff --git a/src/csharp/Grpc.Core/Internal/DebugStats.cs b/src/csharp/Grpc.Core/Internal/DebugStats.cs index 476914f751..ef9d9afe11 100644 --- a/src/csharp/Grpc.Core/Internal/DebugStats.cs +++ b/src/csharp/Grpc.Core/Internal/DebugStats.cs @@ -41,5 +41,7 @@ namespace Grpc.Core.Internal public static readonly AtomicCounter ActiveClientCalls = new AtomicCounter(); public static readonly AtomicCounter ActiveServerCalls = new AtomicCounter(); + + public static readonly AtomicCounter PendingBatchCompletions = new AtomicCounter(); } } diff --git a/src/csharp/Grpc.Core/Internal/Enums.cs b/src/csharp/Grpc.Core/Internal/Enums.cs index 2b4f6cae0c..af11b5b9f3 100644 --- a/src/csharp/Grpc.Core/Internal/Enums.cs +++ b/src/csharp/Grpc.Core/Internal/Enums.cs @@ -33,35 +33,47 @@ using System; using System.Runtime.InteropServices; +using Grpc.Core.Utils; namespace Grpc.Core.Internal { /// <summary> - /// from grpc/grpc.h + /// grpc_call_error from grpc/grpc.h /// </summary> internal enum GRPCCallError { /* everything went ok */ - GRPC_CALL_OK = 0, + OK = 0, /* something failed, we don't know what */ - GRPC_CALL_ERROR, + Error, /* this method is not available on the server */ - GRPC_CALL_ERROR_NOT_ON_SERVER, + NotOnServer, /* this method is not available on the client */ - GRPC_CALL_ERROR_NOT_ON_CLIENT, + NotOnClient, /* this method must be called before server_accept */ - GRPC_CALL_ERROR_ALREADY_ACCEPTED, + AlreadyAccepted, /* this method must be called before invoke */ - GRPC_CALL_ERROR_ALREADY_INVOKED, + AlreadyInvoked, /* this method must be called after invoke */ - GRPC_CALL_ERROR_NOT_INVOKED, + NotInvoked, /* this call is already finished (writes_done or write_status has already been called) */ - GRPC_CALL_ERROR_ALREADY_FINISHED, + AlreadyFinished, /* there is already an outstanding read/write operation on the call */ - GRPC_CALL_ERROR_TOO_MANY_OPERATIONS, + TooManyOperations, /* the flags value was illegal for this call */ - GRPC_CALL_ERROR_INVALID_FLAGS + InvalidFlags + } + + internal static class CallErrorExtensions + { + /// <summary> + /// Checks the call API invocation's result is OK. + /// </summary> + public static void CheckOk(this GRPCCallError callError) + { + Preconditions.CheckState(callError == GRPCCallError.OK, "Call error: " + callError); + } } /// <summary> @@ -70,12 +82,12 @@ namespace Grpc.Core.Internal internal enum GRPCCompletionType { /* Shutting down */ - GRPC_QUEUE_SHUTDOWN, + Shutdown, /* No event before timeout */ - GRPC_QUEUE_TIMEOUT, + Timeout, /* operation completion */ - GRPC_OP_COMPLETE + OpComplete } } diff --git a/src/csharp/Grpc.Core/Internal/GrpcThreadPool.cs b/src/csharp/Grpc.Core/Internal/GrpcThreadPool.cs index f4224668f1..89b44a4e2b 100644 --- a/src/csharp/Grpc.Core/Internal/GrpcThreadPool.cs +++ b/src/csharp/Grpc.Core/Internal/GrpcThreadPool.cs @@ -112,12 +112,26 @@ namespace Grpc.Core.Internal /// </summary> private void RunHandlerLoop() { - GRPCCompletionType completionType; + CompletionQueueEvent ev; do { - completionType = cq.NextWithCallback(); + ev = cq.Next(); + if (ev.type == GRPCCompletionType.OpComplete) + { + bool success = (ev.success != 0); + IntPtr tag = ev.tag; + try + { + var callback = GrpcEnvironment.CompletionRegistry.Extract(tag); + callback(success); + } + catch (Exception e) + { + Console.WriteLine("Exception occured while invoking completion delegate: " + e); + } + } } - while (completionType != GRPCCompletionType.GRPC_QUEUE_SHUTDOWN); + while (ev.type != GRPCCompletionType.Shutdown); Console.WriteLine("Completion queue has shutdown successfully, thread " + Thread.CurrentThread.Name + " exiting."); } } diff --git a/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs b/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs index f494d9e0ff..c0e5bae13f 100644 --- a/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs +++ b/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs @@ -267,8 +267,6 @@ namespace Grpc.Core.Internal var responseStream = new ServerResponseStream<byte[], byte[]>(asyncCall); await responseStream.WriteStatusAsync(new Status(StatusCode.Unimplemented, "No such method.")); - // TODO(jtattermusch): if we don't read what client has sent, the server call never gets disposed. - await requestStream.ToList(); await finishedTask; } } diff --git a/src/csharp/Grpc.Core/Internal/ServerSafeHandle.cs b/src/csharp/Grpc.Core/Internal/ServerSafeHandle.cs index c44ee87bad..83dbb910aa 100644 --- a/src/csharp/Grpc.Core/Internal/ServerSafeHandle.cs +++ b/src/csharp/Grpc.Core/Internal/ServerSafeHandle.cs @@ -45,10 +45,7 @@ namespace Grpc.Core.Internal internal sealed class ServerSafeHandle : SafeHandleZeroIsInvalid { [DllImport("grpc_csharp_ext.dll")] - static extern GRPCCallError grpcsharp_server_request_call(ServerSafeHandle server, CompletionQueueSafeHandle cq, [MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback); - - [DllImport("grpc_csharp_ext.dll")] - static extern ServerSafeHandle grpcsharp_server_create(CompletionQueueSafeHandle cq, IntPtr args); + static extern ServerSafeHandle grpcsharp_server_create(CompletionQueueSafeHandle cq, ChannelArgsSafeHandle args); [DllImport("grpc_csharp_ext.dll")] static extern int grpcsharp_server_add_http2_port(ServerSafeHandle server, string addr); @@ -60,19 +57,22 @@ namespace Grpc.Core.Internal static extern void grpcsharp_server_start(ServerSafeHandle server); [DllImport("grpc_csharp_ext.dll")] - static extern void grpcsharp_server_shutdown_and_notify_callback(ServerSafeHandle server, CompletionQueueSafeHandle cq, [MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback); + static extern GRPCCallError grpcsharp_server_request_call(ServerSafeHandle server, CompletionQueueSafeHandle cq, BatchContextSafeHandle ctx); [DllImport("grpc_csharp_ext.dll")] static extern void grpcsharp_server_cancel_all_calls(ServerSafeHandle server); [DllImport("grpc_csharp_ext.dll")] + static extern void grpcsharp_server_shutdown_and_notify_callback(ServerSafeHandle server, CompletionQueueSafeHandle cq, BatchContextSafeHandle ctx); + + [DllImport("grpc_csharp_ext.dll")] static extern void grpcsharp_server_destroy(IntPtr server); private ServerSafeHandle() { } - public static ServerSafeHandle NewServer(CompletionQueueSafeHandle cq, IntPtr args) + public static ServerSafeHandle NewServer(CompletionQueueSafeHandle cq, ChannelArgsSafeHandle args) { return grpcsharp_server_create(cq, args); } @@ -91,15 +91,19 @@ namespace Grpc.Core.Internal { grpcsharp_server_start(this); } - - public void ShutdownAndNotify(CompletionQueueSafeHandle cq, CompletionCallbackDelegate callback) + + public void ShutdownAndNotify(CompletionQueueSafeHandle cq, BatchCompletionDelegate callback) { - grpcsharp_server_shutdown_and_notify_callback(this, cq, callback); + var ctx = BatchContextSafeHandle.Create(); + GrpcEnvironment.CompletionRegistry.RegisterBatchCompletion(ctx, callback); + grpcsharp_server_shutdown_and_notify_callback(this, cq, ctx); } - public void RequestCall(CompletionQueueSafeHandle cq, CompletionCallbackDelegate callback) + public void RequestCall(CompletionQueueSafeHandle cq, BatchCompletionDelegate callback) { - AssertCallOk(grpcsharp_server_request_call(this, cq, callback)); + var ctx = BatchContextSafeHandle.Create(); + GrpcEnvironment.CompletionRegistry.RegisterBatchCompletion(ctx, callback); + grpcsharp_server_request_call(this, cq, ctx).CheckOk(); } protected override bool ReleaseHandle() @@ -113,10 +117,5 @@ namespace Grpc.Core.Internal { grpcsharp_server_cancel_all_calls(this); } - - private static void AssertCallOk(GRPCCallError callError) - { - Preconditions.CheckState(callError == GRPCCallError.GRPC_CALL_OK, "Status not GRPC_CALL_OK"); - } } } diff --git a/src/csharp/Grpc.Core/Internal/Timespec.cs b/src/csharp/Grpc.Core/Internal/Timespec.cs index 94d48c2c49..775af27db9 100644 --- a/src/csharp/Grpc.Core/Internal/Timespec.cs +++ b/src/csharp/Grpc.Core/Internal/Timespec.cs @@ -51,7 +51,7 @@ namespace Grpc.Core.Internal [DllImport("grpc_csharp_ext.dll")] static extern int gprsharp_sizeof_timespec(); - // TODO: revisit this. + // NOTE: on linux 64bit sizeof(gpr_timespec) = 16, on windows 32bit sizeof(gpr_timespec) = 8 // so IntPtr seems to have the right size to work on both. public System.IntPtr tv_sec; diff --git a/src/csharp/Grpc.Core/Server.cs b/src/csharp/Grpc.Core/Server.cs index 0f4d77eaf3..8e818885d1 100644 --- a/src/csharp/Grpc.Core/Server.cs +++ b/src/csharp/Grpc.Core/Server.cs @@ -52,11 +52,6 @@ namespace Grpc.Core /// </summary> public const int PickUnusedPort = 0; - // TODO(jtattermusch) : make sure the delegate doesn't get garbage collected while - // native callbacks are in the completion queue. - readonly CompletionCallbackDelegate serverShutdownHandler; - readonly CompletionCallbackDelegate newServerRpcHandler; - readonly ServerSafeHandle handle; readonly object myLock = new object(); @@ -66,11 +61,16 @@ namespace Grpc.Core bool startRequested; bool shutdownRequested; - public Server() + /// <summary> + /// Create a new server. + /// </summary> + /// <param name="options">Channel options.</param> + public Server(IEnumerable<ChannelOption> options = null) { - this.handle = ServerSafeHandle.NewServer(GetCompletionQueue(), IntPtr.Zero); - this.newServerRpcHandler = HandleNewServerRpc; - this.serverShutdownHandler = HandleServerShutdown; + using (var channelArgs = ChannelOptions.CreateChannelArgs(options)) + { + this.handle = ServerSafeHandle.NewServer(GetCompletionQueue(), channelArgs); + } } /// <summary> @@ -108,7 +108,7 @@ namespace Grpc.Core /// </summary> /// <returns>The port on which server will be listening.</returns> /// <param name="host">the host</param> - /// <param name="port">the port. If zero, , an unused port is chosen automatically.</param> + /// <param name="port">the port. If zero, an unused port is chosen automatically.</param> public int AddListeningPort(string host, int port, ServerCredentials credentials) { Preconditions.CheckNotNull(credentials); @@ -144,7 +144,7 @@ namespace Grpc.Core shutdownRequested = true; } - handle.ShutdownAndNotify(GetCompletionQueue(), serverShutdownHandler); + handle.ShutdownAndNotify(GetCompletionQueue(), HandleServerShutdown); await shutdownTcs.Task; handle.Dispose(); } @@ -173,7 +173,7 @@ namespace Grpc.Core shutdownRequested = true; } - handle.ShutdownAndNotify(GetCompletionQueue(), serverShutdownHandler); + handle.ShutdownAndNotify(GetCompletionQueue(), HandleServerShutdown); handle.CancelAllCalls(); await shutdownTcs.Task; handle.Dispose(); @@ -208,7 +208,7 @@ namespace Grpc.Core { if (!shutdownRequested) { - handle.RequestCall(GetCompletionQueue(), newServerRpcHandler); + handle.RequestCall(GetCompletionQueue(), HandleNewServerRpc); } } } @@ -236,44 +236,28 @@ namespace Grpc.Core /// <summary> /// Handles the native callback. /// </summary> - private void HandleNewServerRpc(bool success, IntPtr batchContextPtr) + private void HandleNewServerRpc(bool success, BatchContextSafeHandle ctx) { - try - { - var ctx = new BatchContextSafeHandleNotOwned(batchContextPtr); - - // TODO: handle error + // TODO: handle error - CallSafeHandle call = ctx.GetServerRpcNewCall(); - string method = ctx.GetServerRpcNewMethod(); - - // after server shutdown, the callback returns with null call - if (!call.IsInvalid) - { - Task.Run(async () => await InvokeCallHandler(call, method)); - } + CallSafeHandle call = ctx.GetServerRpcNewCall(); + string method = ctx.GetServerRpcNewMethod(); - AllowOneRpc(); - } - catch (Exception e) + // after server shutdown, the callback returns with null call + if (!call.IsInvalid) { - Console.WriteLine("Caught exception in a native handler: " + e); + Task.Run(async () => await InvokeCallHandler(call, method)); } + + AllowOneRpc(); } /// <summary> /// Handles native callback. /// </summary> - private void HandleServerShutdown(bool success, IntPtr batchContextPtr) + private void HandleServerShutdown(bool success, BatchContextSafeHandle ctx) { - try - { - shutdownTcs.SetResult(null); - } - catch (Exception e) - { - Console.WriteLine("Caught exception in a native handler: " + e); - } + shutdownTcs.SetResult(null); } private static CompletionQueueSafeHandle GetCompletionQueue() diff --git a/src/csharp/Grpc.Examples.MathClient/MathClient.cs b/src/csharp/Grpc.Examples.MathClient/MathClient.cs index 85d9cdc7a6..360fe928dd 100644 --- a/src/csharp/Grpc.Examples.MathClient/MathClient.cs +++ b/src/csharp/Grpc.Examples.MathClient/MathClient.cs @@ -41,7 +41,7 @@ namespace math { GrpcEnvironment.Initialize(); - using (Channel channel = new Channel("127.0.0.1:23456")) + using (Channel channel = new Channel("127.0.0.1", 23456)) { Math.IMathClient stub = new Math.MathClient(channel); MathExamples.DivExample(stub); diff --git a/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs b/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs index 5aa6f4162d..aadd49f795 100644 --- a/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs +++ b/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs @@ -60,7 +60,7 @@ namespace math.Tests server.AddServiceDefinition(Math.BindService(new MathServiceImpl())); int port = server.AddListeningPort(host, Server.PickUnusedPort); server.Start(); - channel = new Channel(host + ":" + port); + channel = new Channel(host, port); // TODO(jtattermusch): get rid of the custom header here once we have dedicated tests // for header support. diff --git a/src/csharp/Grpc.IntegrationTesting/InteropClient.cs b/src/csharp/Grpc.IntegrationTesting/InteropClient.cs index dfaf18cae1..f0be522bc6 100644 --- a/src/csharp/Grpc.IntegrationTesting/InteropClient.cs +++ b/src/csharp/Grpc.IntegrationTesting/InteropClient.cs @@ -104,22 +104,22 @@ namespace Grpc.IntegrationTesting { GrpcEnvironment.Initialize(); - string addr = string.Format("{0}:{1}", options.serverHost, options.serverPort); - Credentials credentials = null; if (options.useTls) { credentials = TestCredentials.CreateTestClientCredentials(options.useTestCa); } - ChannelArgs channelArgs = null; + List<ChannelOption> channelOptions = null; if (!string.IsNullOrEmpty(options.serverHostOverride)) { - channelArgs = ChannelArgs.CreateBuilder() - .AddString(ChannelArgs.SslTargetNameOverrideKey, options.serverHostOverride).Build(); + channelOptions = new List<ChannelOption> + { + new ChannelOption(ChannelOptions.SslTargetNameOverride, options.serverHostOverride) + }; } - using (Channel channel = new Channel(addr, credentials, channelArgs)) + using (Channel channel = new Channel(options.serverHost, options.serverPort.Value, credentials, channelOptions)) { var stubConfig = StubConfiguration.Default; if (options.testCase == "service_account_creds" || options.testCase == "compute_engine_creds") diff --git a/src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs b/src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs index ddbfc61a4e..1a733450c1 100644 --- a/src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs +++ b/src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs @@ -62,10 +62,11 @@ namespace Grpc.IntegrationTesting int port = server.AddListeningPort(host, Server.PickUnusedPort, TestCredentials.CreateTestServerCredentials()); server.Start(); - var channelArgs = ChannelArgs.CreateBuilder() - .AddString(ChannelArgs.SslTargetNameOverrideKey, TestCredentials.DefaultHostOverride).Build(); - - channel = new Channel(host + ":" + port, TestCredentials.CreateTestClientCredentials(true), channelArgs); + var options = new List<ChannelOption> + { + new ChannelOption(ChannelOptions.SslTargetNameOverride, TestCredentials.DefaultHostOverride) + }; + channel = new Channel(host, port, TestCredentials.CreateTestClientCredentials(true), options); client = TestService.NewStub(channel); } diff --git a/src/csharp/Grpc.Tools.nuspec b/src/csharp/Grpc.Tools.nuspec index 155c2ef8c4..913d4c8f4b 100644 --- a/src/csharp/Grpc.Tools.nuspec +++ b/src/csharp/Grpc.Tools.nuspec @@ -5,13 +5,13 @@ <title>gRPC C# Tools</title> <summary>Tools for C# implementation of gRPC - an RPC library and framework</summary> <description>Precompiled Windows binaries for generating protocol buffer messages and gRPC client/server code</description> - <version>0.5.0</version> + <version>0.5.1</version> <authors>Google Inc.</authors> <owners>grpc-packages</owners> <licenseUrl>https://github.com/grpc/grpc/blob/master/LICENSE</licenseUrl> <projectUrl>https://github.com/grpc/grpc</projectUrl> <requireLicenseAcceptance>false</requireLicenseAcceptance> - <releaseNotes>protoc.exe - protocol buffer compiler v3.0.0-alpha-3; grpc_csharp_plugin.exe - gRPC C# protoc plugin version 0.5.0</releaseNotes> + <releaseNotes>protoc.exe - protocol buffer compiler v3.0.0-alpha-3; grpc_csharp_plugin.exe - gRPC C# protoc plugin version 0.5.1</releaseNotes> <copyright>Copyright 2015, Google Inc.</copyright> <tags>gRPC RPC Protocol HTTP/2</tags> </metadata> diff --git a/src/csharp/Grpc.nuspec b/src/csharp/Grpc.nuspec index 263e016339..cf4c74fa2d 100644 --- a/src/csharp/Grpc.nuspec +++ b/src/csharp/Grpc.nuspec @@ -5,17 +5,17 @@ <title>gRPC C#</title> <summary>C# implementation of gRPC - an RPC library and framework</summary> <description>C# implementation of gRPC - an RPC library and framework. See project site for more info.</description> - <version>0.5.0.1</version> + <version>0.5.1</version> <authors>Google Inc.</authors> <owners>grpc-packages</owners> <licenseUrl>https://github.com/grpc/grpc/blob/master/LICENSE</licenseUrl> <projectUrl>https://github.com/grpc/grpc</projectUrl> <requireLicenseAcceptance>false</requireLicenseAcceptance> - <releaseNotes>Release 0.5.0 of gRPC C#</releaseNotes> + <releaseNotes>Release 0.5.1 of gRPC C#</releaseNotes> <copyright>Copyright 2015, Google Inc.</copyright> <tags>gRPC RPC Protocol HTTP/2</tags> <dependencies> - <dependency id="Grpc.Core" version="0.5.0" /> + <dependency id="Grpc.Core" version="0.5.1" /> </dependencies> </metadata> <files/> diff --git a/src/csharp/ext/grpc_csharp_ext.c b/src/csharp/ext/grpc_csharp_ext.c index 173e5c8a46..ec125db78b 100644 --- a/src/csharp/ext/grpc_csharp_ext.c +++ b/src/csharp/ext/grpc_csharp_ext.c @@ -33,10 +33,12 @@ #include "src/core/support/string.h" +#include <grpc/byte_buffer_reader.h> #include <grpc/support/port_platform.h> #include <grpc/support/alloc.h> #include <grpc/support/log.h> #include <grpc/support/slice.h> +#include <grpc/support/string_util.h> #include <grpc/support/thd.h> #include <grpc/grpc.h> #include <grpc/grpc_security.h> @@ -58,13 +60,11 @@ grpc_byte_buffer *string_to_byte_buffer(const char *buffer, size_t len) { gpr_slice slice = gpr_slice_from_copied_buffer(buffer, len); - grpc_byte_buffer *bb = grpc_byte_buffer_create(&slice, 1); + grpc_byte_buffer *bb = grpc_raw_byte_buffer_create(&slice, 1); gpr_slice_unref(slice); return bb; } -typedef void(GPR_CALLTYPE *callback_funcptr)(gpr_int32 success, void *batch_context); - /* * Helper to maintain lifetime of batch op inputs and store batch op outputs. */ @@ -89,13 +89,9 @@ typedef struct gprcsharp_batch_context { grpc_call_details call_details; grpc_metadata_array request_metadata; } server_rpc_new; - - /* callback will be called upon completion */ - callback_funcptr callback; - } grpcsharp_batch_context; -grpcsharp_batch_context *grpcsharp_batch_context_create() { +GPR_EXPORT grpcsharp_batch_context *GPR_CALLTYPE grpcsharp_batch_context_create() { grpcsharp_batch_context *ctx = gpr_malloc(sizeof(grpcsharp_batch_context)); memset(ctx, 0, sizeof(grpcsharp_batch_context)); return ctx; @@ -190,7 +186,7 @@ void grpcsharp_metadata_array_move(grpc_metadata_array *dest, src->metadata = NULL; } -void grpcsharp_batch_context_destroy(grpcsharp_batch_context *ctx) { +GPR_EXPORT void GPR_CALLTYPE grpcsharp_batch_context_destroy(grpcsharp_batch_context *ctx) { if (!ctx) { return; } @@ -236,13 +232,13 @@ GPR_EXPORT gpr_intptr GPR_CALLTYPE grpcsharp_batch_context_recv_message_length( */ GPR_EXPORT void GPR_CALLTYPE grpcsharp_batch_context_recv_message_to_buffer( const grpcsharp_batch_context *ctx, char *buffer, size_t buffer_len) { - grpc_byte_buffer_reader *reader; + grpc_byte_buffer_reader reader; gpr_slice slice; size_t offset = 0; - reader = grpc_byte_buffer_reader_create(ctx->recv_message); + grpc_byte_buffer_reader_init(&reader, ctx->recv_message); - while (grpc_byte_buffer_reader_next(reader, &slice)) { + while (grpc_byte_buffer_reader_next(&reader, &slice)) { size_t len = GPR_SLICE_LENGTH(slice); GPR_ASSERT(offset + len <= buffer_len); memcpy(buffer + offset, GPR_SLICE_START_PTR(slice), @@ -250,7 +246,6 @@ GPR_EXPORT void GPR_CALLTYPE grpcsharp_batch_context_recv_message_to_buffer( offset += len; gpr_slice_unref(slice); } - grpc_byte_buffer_reader_destroy(reader); } GPR_EXPORT grpc_status_code GPR_CALLTYPE @@ -305,25 +300,14 @@ grpcsharp_completion_queue_destroy(grpc_completion_queue *cq) { grpc_completion_queue_destroy(cq); } -GPR_EXPORT grpc_completion_type GPR_CALLTYPE -grpcsharp_completion_queue_next_with_callback(grpc_completion_queue *cq) { - grpc_event ev; - grpcsharp_batch_context *batch_context; - grpc_completion_type t; - - ev = grpc_completion_queue_next(cq, gpr_inf_future); - t = ev.type; - if (t == GRPC_OP_COMPLETE && ev.tag) { - /* NEW API handler */ - batch_context = (grpcsharp_batch_context *)ev.tag; - batch_context->callback((gpr_int32) ev.success, batch_context); - grpcsharp_batch_context_destroy(batch_context); - } +GPR_EXPORT grpc_event GPR_CALLTYPE +grpcsharp_completion_queue_next(grpc_completion_queue *cq) { + return grpc_completion_queue_next(cq, gpr_inf_future); +} - /* return completion type to allow some handling for events that have no - * tag - such as GRPC_QUEUE_SHUTDOWN - */ - return t; +GPR_EXPORT grpc_event GPR_CALLTYPE +grpcsharp_completion_queue_pluck(grpc_completion_queue *cq, void *tag) { + return grpc_completion_queue_pluck(cq, tag, gpr_inf_future); } /* Channel */ @@ -369,6 +353,16 @@ grpcsharp_channel_args_set_string(grpc_channel_args *args, size_t index, } GPR_EXPORT void GPR_CALLTYPE +grpcsharp_channel_args_set_integer(grpc_channel_args *args, size_t index, + const char *key, int value) { + GPR_ASSERT(args); + GPR_ASSERT(index < args->num_args); + args->args[index].type = GRPC_ARG_INTEGER; + args->args[index].key = gpr_strdup(key); + args->args[index].value.integer = value; +} + +GPR_EXPORT void GPR_CALLTYPE grpcsharp_channel_args_destroy(grpc_channel_args *args) { size_t i; if (args) { @@ -412,32 +406,34 @@ GPR_EXPORT void GPR_CALLTYPE grpcsharp_call_destroy(grpc_call *call) { } GPR_EXPORT grpc_call_error GPR_CALLTYPE -grpcsharp_call_start_unary(grpc_call *call, callback_funcptr callback, +grpcsharp_call_start_unary(grpc_call *call, grpcsharp_batch_context *ctx, const char *send_buffer, size_t send_buffer_len, grpc_metadata_array *initial_metadata) { /* TODO: don't use magic number */ grpc_op ops[6]; - grpcsharp_batch_context *ctx = grpcsharp_batch_context_create(); - ctx->callback = callback; - ops[0].op = GRPC_OP_SEND_INITIAL_METADATA; grpcsharp_metadata_array_move(&(ctx->send_initial_metadata), initial_metadata); ops[0].data.send_initial_metadata.count = ctx->send_initial_metadata.count; ops[0].data.send_initial_metadata.metadata = ctx->send_initial_metadata.metadata; + ops[0].flags = 0; ops[1].op = GRPC_OP_SEND_MESSAGE; ctx->send_message = string_to_byte_buffer(send_buffer, send_buffer_len); ops[1].data.send_message = ctx->send_message; + ops[1].flags = 0; ops[2].op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; + ops[2].flags = 0; ops[3].op = GRPC_OP_RECV_INITIAL_METADATA; ops[3].data.recv_initial_metadata = &(ctx->recv_initial_metadata); + ops[3].flags = 0; ops[4].op = GRPC_OP_RECV_MESSAGE; ops[4].data.recv_message = &(ctx->recv_message); + ops[4].flags = 0; ops[5].op = GRPC_OP_RECV_STATUS_ON_CLIENT; ops[5].data.recv_status_on_client.trailing_metadata = @@ -449,50 +445,32 @@ grpcsharp_call_start_unary(grpc_call *call, callback_funcptr callback, &(ctx->recv_status_on_client.status_details); ops[5].data.recv_status_on_client.status_details_capacity = &(ctx->recv_status_on_client.status_details_capacity); + ops[5].flags = 0; return grpc_call_start_batch(call, ops, sizeof(ops) / sizeof(ops[0]), ctx); } -/* Synchronous unary call */ -GPR_EXPORT void GPR_CALLTYPE -grpcsharp_call_blocking_unary(grpc_call *call, - grpc_completion_queue *dedicated_cq, - callback_funcptr callback, - const char *send_buffer, size_t send_buffer_len, - grpc_metadata_array *initial_metadata) { - GPR_ASSERT(grpcsharp_call_start_unary(call, callback, send_buffer, - send_buffer_len, - initial_metadata) == GRPC_CALL_OK); - - /* TODO: we would like to use pluck, but we don't know the tag */ - GPR_ASSERT(grpcsharp_completion_queue_next_with_callback(dedicated_cq) == - GRPC_OP_COMPLETE); - grpc_completion_queue_shutdown(dedicated_cq); - GPR_ASSERT(grpcsharp_completion_queue_next_with_callback(dedicated_cq) == - GRPC_QUEUE_SHUTDOWN); -} - GPR_EXPORT grpc_call_error GPR_CALLTYPE grpcsharp_call_start_client_streaming(grpc_call *call, - callback_funcptr callback, + grpcsharp_batch_context *ctx, grpc_metadata_array *initial_metadata) { /* TODO: don't use magic number */ grpc_op ops[4]; - grpcsharp_batch_context *ctx = grpcsharp_batch_context_create(); - ctx->callback = callback; - ops[0].op = GRPC_OP_SEND_INITIAL_METADATA; grpcsharp_metadata_array_move(&(ctx->send_initial_metadata), initial_metadata); ops[0].data.send_initial_metadata.count = ctx->send_initial_metadata.count; ops[0].data.send_initial_metadata.metadata = ctx->send_initial_metadata.metadata; + ops[0].flags = 0; ops[1].op = GRPC_OP_RECV_INITIAL_METADATA; ops[1].data.recv_initial_metadata = &(ctx->recv_initial_metadata); + ops[1].flags = 0; ops[2].op = GRPC_OP_RECV_MESSAGE; ops[2].data.recv_message = &(ctx->recv_message); + ops[2].flags = 0; ops[3].op = GRPC_OP_RECV_STATUS_ON_CLIENT; ops[3].data.recv_status_on_client.trailing_metadata = @@ -504,33 +482,35 @@ grpcsharp_call_start_client_streaming(grpc_call *call, &(ctx->recv_status_on_client.status_details); ops[3].data.recv_status_on_client.status_details_capacity = &(ctx->recv_status_on_client.status_details_capacity); + ops[3].flags = 0; return grpc_call_start_batch(call, ops, sizeof(ops) / sizeof(ops[0]), ctx); } GPR_EXPORT grpc_call_error GPR_CALLTYPE grpcsharp_call_start_server_streaming( - grpc_call *call, callback_funcptr callback, const char *send_buffer, + grpc_call *call, grpcsharp_batch_context *ctx, const char *send_buffer, size_t send_buffer_len, grpc_metadata_array *initial_metadata) { /* TODO: don't use magic number */ grpc_op ops[5]; - grpcsharp_batch_context *ctx = grpcsharp_batch_context_create(); - ctx->callback = callback; - ops[0].op = GRPC_OP_SEND_INITIAL_METADATA; grpcsharp_metadata_array_move(&(ctx->send_initial_metadata), initial_metadata); ops[0].data.send_initial_metadata.count = ctx->send_initial_metadata.count; ops[0].data.send_initial_metadata.metadata = ctx->send_initial_metadata.metadata; + ops[0].flags = 0; ops[1].op = GRPC_OP_SEND_MESSAGE; ctx->send_message = string_to_byte_buffer(send_buffer, send_buffer_len); ops[1].data.send_message = ctx->send_message; + ops[1].flags = 0; ops[2].op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; + ops[2].flags = 0; ops[3].op = GRPC_OP_RECV_INITIAL_METADATA; ops[3].data.recv_initial_metadata = &(ctx->recv_initial_metadata); + ops[3].flags = 0; ops[4].op = GRPC_OP_RECV_STATUS_ON_CLIENT; ops[4].data.recv_status_on_client.trailing_metadata = @@ -542,28 +522,28 @@ GPR_EXPORT grpc_call_error GPR_CALLTYPE grpcsharp_call_start_server_streaming( &(ctx->recv_status_on_client.status_details); ops[4].data.recv_status_on_client.status_details_capacity = &(ctx->recv_status_on_client.status_details_capacity); + ops[4].flags = 0; return grpc_call_start_batch(call, ops, sizeof(ops) / sizeof(ops[0]), ctx); } GPR_EXPORT grpc_call_error GPR_CALLTYPE grpcsharp_call_start_duplex_streaming(grpc_call *call, - callback_funcptr callback, + grpcsharp_batch_context *ctx, grpc_metadata_array *initial_metadata) { /* TODO: don't use magic number */ grpc_op ops[3]; - grpcsharp_batch_context *ctx = grpcsharp_batch_context_create(); - ctx->callback = callback; - ops[0].op = GRPC_OP_SEND_INITIAL_METADATA; grpcsharp_metadata_array_move(&(ctx->send_initial_metadata), initial_metadata); ops[0].data.send_initial_metadata.count = ctx->send_initial_metadata.count; ops[0].data.send_initial_metadata.metadata = ctx->send_initial_metadata.metadata; + ops[0].flags = 0; ops[1].op = GRPC_OP_RECV_INITIAL_METADATA; ops[1].data.recv_initial_metadata = &(ctx->recv_initial_metadata); + ops[1].flags = 0; ops[2].op = GRPC_OP_RECV_STATUS_ON_CLIENT; ops[2].data.recv_status_on_client.trailing_metadata = @@ -575,85 +555,76 @@ grpcsharp_call_start_duplex_streaming(grpc_call *call, &(ctx->recv_status_on_client.status_details); ops[2].data.recv_status_on_client.status_details_capacity = &(ctx->recv_status_on_client.status_details_capacity); + ops[2].flags = 0; return grpc_call_start_batch(call, ops, sizeof(ops) / sizeof(ops[0]), ctx); } GPR_EXPORT grpc_call_error GPR_CALLTYPE -grpcsharp_call_send_message(grpc_call *call, callback_funcptr callback, +grpcsharp_call_send_message(grpc_call *call, grpcsharp_batch_context *ctx, const char *send_buffer, size_t send_buffer_len) { /* TODO: don't use magic number */ grpc_op ops[1]; - grpcsharp_batch_context *ctx = grpcsharp_batch_context_create(); - ctx->callback = callback; - ops[0].op = GRPC_OP_SEND_MESSAGE; ctx->send_message = string_to_byte_buffer(send_buffer, send_buffer_len); ops[0].data.send_message = ctx->send_message; + ops[0].flags = 0; return grpc_call_start_batch(call, ops, sizeof(ops) / sizeof(ops[0]), ctx); } GPR_EXPORT grpc_call_error GPR_CALLTYPE grpcsharp_call_send_close_from_client(grpc_call *call, - callback_funcptr callback) { + grpcsharp_batch_context *ctx) { /* TODO: don't use magic number */ grpc_op ops[1]; - grpcsharp_batch_context *ctx = grpcsharp_batch_context_create(); - ctx->callback = callback; - ops[0].op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; + ops[0].flags = 0; return grpc_call_start_batch(call, ops, sizeof(ops) / sizeof(ops[0]), ctx); } GPR_EXPORT grpc_call_error GPR_CALLTYPE grpcsharp_call_send_status_from_server(grpc_call *call, - callback_funcptr callback, + grpcsharp_batch_context *ctx, grpc_status_code status_code, const char *status_details) { /* TODO: don't use magic number */ grpc_op ops[1]; - grpcsharp_batch_context *ctx = grpcsharp_batch_context_create(); - ctx->callback = callback; - ops[0].op = GRPC_OP_SEND_STATUS_FROM_SERVER; ops[0].data.send_status_from_server.status = status_code; ops[0].data.send_status_from_server.status_details = gpr_strdup(status_details); ops[0].data.send_status_from_server.trailing_metadata = NULL; ops[0].data.send_status_from_server.trailing_metadata_count = 0; + ops[0].flags = 0; return grpc_call_start_batch(call, ops, sizeof(ops) / sizeof(ops[0]), ctx); } GPR_EXPORT grpc_call_error GPR_CALLTYPE -grpcsharp_call_recv_message(grpc_call *call, callback_funcptr callback) { +grpcsharp_call_recv_message(grpc_call *call, grpcsharp_batch_context *ctx) { /* TODO: don't use magic number */ grpc_op ops[1]; - grpcsharp_batch_context *ctx = grpcsharp_batch_context_create(); - ctx->callback = callback; - ops[0].op = GRPC_OP_RECV_MESSAGE; ops[0].data.recv_message = &(ctx->recv_message); + ops[0].flags = 0; return grpc_call_start_batch(call, ops, sizeof(ops) / sizeof(ops[0]), ctx); } GPR_EXPORT grpc_call_error GPR_CALLTYPE -grpcsharp_call_start_serverside(grpc_call *call, callback_funcptr callback) { +grpcsharp_call_start_serverside(grpc_call *call, grpcsharp_batch_context *ctx) { /* TODO: don't use magic number */ grpc_op ops[2]; - - grpcsharp_batch_context *ctx = grpcsharp_batch_context_create(); - ctx->callback = callback; - ops[0].op = GRPC_OP_SEND_INITIAL_METADATA; ops[0].data.send_initial_metadata.count = 0; ops[0].data.send_initial_metadata.metadata = NULL; + ops[0].flags = 0; ops[1].op = GRPC_OP_RECV_CLOSE_ON_SERVER; ops[1].data.recv_close_on_server.cancelled = (&ctx->recv_close_on_server_cancelled); + ops[1].flags = 0; return grpc_call_start_batch(call, ops, sizeof(ops) / sizeof(ops[0]), ctx); } @@ -680,9 +651,7 @@ GPR_EXPORT void GPR_CALLTYPE grpcsharp_server_start(grpc_server *server) { GPR_EXPORT void GPR_CALLTYPE grpcsharp_server_shutdown_and_notify_callback(grpc_server *server, grpc_completion_queue *cq, - callback_funcptr callback) { - grpcsharp_batch_context *ctx = grpcsharp_batch_context_create(); - ctx->callback = callback; + grpcsharp_batch_context *ctx) { grpc_server_shutdown_and_notify(server, cq, ctx); } @@ -696,10 +665,7 @@ GPR_EXPORT void GPR_CALLTYPE grpcsharp_server_destroy(grpc_server *server) { GPR_EXPORT grpc_call_error GPR_CALLTYPE grpcsharp_server_request_call(grpc_server *server, grpc_completion_queue *cq, - callback_funcptr callback) { - grpcsharp_batch_context *ctx = grpcsharp_batch_context_create(); - ctx->callback = callback; - + grpcsharp_batch_context *ctx) { return grpc_server_request_call( server, &(ctx->server_rpc_new.call), &(ctx->server_rpc_new.call_details), &(ctx->server_rpc_new.request_metadata), cq, cq, ctx); @@ -788,11 +754,18 @@ GPR_EXPORT void GPR_CALLTYPE grpcsharp_redirect_log(grpcsharp_log_func func) { gpr_set_log_function(grpcsharp_log_handler); } +typedef void(GPR_CALLTYPE *test_callback_funcptr)(gpr_int32 success); + /* For testing */ GPR_EXPORT void GPR_CALLTYPE -grpcsharp_test_callback(callback_funcptr callback) { - callback(1, NULL); +grpcsharp_test_callback(test_callback_funcptr callback) { + callback(1); } /* For testing */ GPR_EXPORT void *GPR_CALLTYPE grpcsharp_test_nop(void *ptr) { return ptr; } + +/* For testing */ +GPR_EXPORT gpr_int32 GPR_CALLTYPE grpcsharp_sizeof_grpc_event(void) { + return sizeof(grpc_event); +} diff --git a/src/node/README.md b/src/node/README.md index 6e4934151e..2f4c49096d 100644 --- a/src/node/README.md +++ b/src/node/README.md @@ -1,16 +1,22 @@ # Node.js gRPC Library ## Status - Alpha : Ready for early adopters -## Prerequisites - -This requires `node` to be installed. If you instead have the `nodejs` executable on Debian, you should install the [`nodejs-legacy`](https://packages.debian.org/sid/nodejs-legacy) package. +## PREREQUISITES +- `node`: This requires `node` to be installed. If you instead have the `nodejs` executable on Debian, you should install the [`nodejs-legacy`](https://packages.debian.org/sid/nodejs-legacy) package. +- [homebrew][] on Mac OS X, [linuxbrew][] on Linux. These simplify the installation of the gRPC C core. -## Installation +## INSTALLATION +On Mac OS X, install [homebrew][]. On Linux, install [linuxbrew][]. +Run the following command to install gRPC Node.js. +```sh +$ curl -fsSL https://goo.gl/getgrpc | bash -s nodejs +``` +This will download and run the [gRPC install script][], then install the latest version of gRPC Nodejs npm package. - 1. Clone [the grpc repository](https://github.com/grpc/grpc). +## BUILD FROM SOURCE + 1. Clone [the grpc Git Repository](https://github.com/grpc/grpc). 2. Follow the instructions in the `INSTALL` file in the root of that repository to install the C core library that this package depends on. 3. Run `npm install`. @@ -20,12 +26,10 @@ If you install the gRPC C core library in a custom location, then you need to se CXXFLAGS=-I<custom location>/include LDFLAGS=-L<custom location>/lib npm install [grpc] ``` -## Tests - +## TESTING To run the test suite, simply run `npm test` in the install location. ## API - This library internally uses [ProtoBuf.js](https://github.com/dcodeIO/ProtoBuf.js), and some structures it exports match those exported by that library If you require this module, you will get an object with the following members @@ -82,3 +86,7 @@ ServerCredentials ``` An object with factory methods fro creating credential objects for servers. + +[homebrew]:http://brew.sh +[linuxbrew]:https://github.com/Homebrew/linuxbrew#installation +[gRPC install script]:https://raw.githubusercontent.com/grpc/homebrew-grpc/master/scripts/install diff --git a/src/node/cli/service_packager.js b/src/node/cli/service_packager.js index f29180b252..c92c450a01 100644 --- a/src/node/cli/service_packager.js +++ b/src/node/cli/service_packager.js @@ -36,7 +36,7 @@ var fs = require('fs'); var path = require('path'); -var _ = require('underscore'); +var _ = require('lodash'); var async = require('async'); var pbjs = require('protobufjs/cli/pbjs'); var parseArgs = require('minimist'); diff --git a/src/node/examples/perf_test.js b/src/node/examples/perf_test.js index 31083e0987..da919eced5 100644 --- a/src/node/examples/perf_test.js +++ b/src/node/examples/perf_test.js @@ -35,7 +35,7 @@ var grpc = require('..'); var testProto = grpc.load(__dirname + '/../interop/test.proto').grpc.testing; -var _ = require('underscore'); +var _ = require('lodash'); var interop_server = require('../interop/interop_server.js'); function runTest(iterations, callback) { diff --git a/src/node/examples/route_guide_client.js b/src/node/examples/route_guide_client.js index 0b3e9c5819..8cd532fef1 100644 --- a/src/node/examples/route_guide_client.js +++ b/src/node/examples/route_guide_client.js @@ -37,7 +37,7 @@ var async = require('async'); var fs = require('fs'); var parseArgs = require('minimist'); var path = require('path'); -var _ = require('underscore'); +var _ = require('lodash'); var grpc = require('..'); var examples = grpc.load(__dirname + '/route_guide.proto').examples; var client = new examples.RouteGuide('localhost:50051'); diff --git a/src/node/examples/route_guide_server.js b/src/node/examples/route_guide_server.js index 9555368422..c777eab7bc 100644 --- a/src/node/examples/route_guide_server.js +++ b/src/node/examples/route_guide_server.js @@ -36,7 +36,7 @@ var fs = require('fs'); var parseArgs = require('minimist'); var path = require('path'); -var _ = require('underscore'); +var _ = require('lodash'); var grpc = require('..'); var examples = grpc.load(__dirname + '/route_guide.proto').examples; diff --git a/src/node/examples/stock_server.js b/src/node/examples/stock_server.js index 8c22af147e..caaf9f99ba 100644 --- a/src/node/examples/stock_server.js +++ b/src/node/examples/stock_server.js @@ -33,7 +33,7 @@ 'use strict'; -var _ = require('underscore'); +var _ = require('lodash'); var grpc = require('..'); var examples = grpc.load(__dirname + '/stock.proto').examples; diff --git a/src/node/ext/byte_buffer.cc b/src/node/ext/byte_buffer.cc index 01bd92ea52..7eff11c2b3 100644 --- a/src/node/ext/byte_buffer.cc +++ b/src/node/ext/byte_buffer.cc @@ -36,6 +36,7 @@ #include <node.h> #include <nan.h> #include "grpc/grpc.h" +#include "grpc/byte_buffer_reader.h" #include "grpc/support/slice.h" #include "byte_buffer.h" @@ -56,7 +57,7 @@ grpc_byte_buffer *BufferToByteBuffer(Handle<Value> buffer) { char *data = ::node::Buffer::Data(buffer); gpr_slice slice = gpr_slice_malloc(length); memcpy(GPR_SLICE_START_PTR(slice), data, length); - grpc_byte_buffer *byte_buffer(grpc_byte_buffer_create(&slice, 1)); + grpc_byte_buffer *byte_buffer(grpc_raw_byte_buffer_create(&slice, 1)); gpr_slice_unref(slice); return byte_buffer; } @@ -69,9 +70,10 @@ Handle<Value> ByteBufferToBuffer(grpc_byte_buffer *buffer) { size_t length = grpc_byte_buffer_length(buffer); char *result = reinterpret_cast<char *>(calloc(length, sizeof(char))); size_t offset = 0; - grpc_byte_buffer_reader *reader = grpc_byte_buffer_reader_create(buffer); + grpc_byte_buffer_reader reader; + grpc_byte_buffer_reader_init(&reader, buffer); gpr_slice next; - while (grpc_byte_buffer_reader_next(reader, &next) != 0) { + while (grpc_byte_buffer_reader_next(&reader, &next) != 0) { memcpy(result + offset, GPR_SLICE_START_PTR(next), GPR_SLICE_LENGTH(next)); offset += GPR_SLICE_LENGTH(next); } diff --git a/src/node/ext/call.cc b/src/node/ext/call.cc index 8cc3e38cd9..15c9b2d97d 100644 --- a/src/node/ext/call.cc +++ b/src/node/ext/call.cc @@ -550,6 +550,7 @@ NAN_METHOD(Call::StartBatch) { } uint32_t type = keys->Get(i)->Uint32Value(); ops[i].op = static_cast<grpc_op_type>(type); + ops[i].flags = 0; switch (type) { case GRPC_OP_SEND_INITIAL_METADATA: op.reset(new SendMetadataOp()); diff --git a/src/node/index.js b/src/node/index.js index c09e416c6e..b6a4e2d0ee 100644 --- a/src/node/index.js +++ b/src/node/index.js @@ -33,7 +33,7 @@ 'use strict'; -var _ = require('underscore'); +var _ = require('lodash'); var ProtoBuf = require('protobufjs'); diff --git a/src/node/interop/interop_client.js b/src/node/interop/interop_client.js index 455055d9f3..b61b0b63c0 100644 --- a/src/node/interop/interop_client.js +++ b/src/node/interop/interop_client.js @@ -35,7 +35,7 @@ var fs = require('fs'); var path = require('path'); -var _ = require('underscore'); +var _ = require('lodash'); var grpc = require('..'); var testProto = grpc.load(__dirname + '/test.proto').grpc.testing; var GoogleAuth = require('google-auth-library'); diff --git a/src/node/interop/interop_server.js b/src/node/interop/interop_server.js index dad59c1347..0baa78a094 100644 --- a/src/node/interop/interop_server.js +++ b/src/node/interop/interop_server.js @@ -35,7 +35,7 @@ var fs = require('fs'); var path = require('path'); -var _ = require('underscore'); +var _ = require('lodash'); var grpc = require('..'); var testProto = grpc.load(__dirname + '/test.proto').grpc.testing; var Server = grpc.buildServer([testProto.TestService.service]); diff --git a/src/node/package.json b/src/node/package.json index d7e9af07bd..7d4a493af4 100644 --- a/src/node/package.json +++ b/src/node/package.json @@ -1,6 +1,6 @@ { "name": "grpc", - "version": "0.9.0", + "version": "0.9.1", "author": "Google Inc.", "description": "gRPC Library for Node", "homepage": "http://www.grpc.io/", @@ -25,10 +25,9 @@ }, "dependencies": { "bindings": "^1.2.0", + "lodash": "^3.9.3", "nan": "^1.5.0", - "protobufjs": "dcodeIO/ProtoBuf.js", - "underscore": "^1.6.0", - "underscore.string": "^3.0.0" + "protobufjs": "dcodeIO/ProtoBuf.js" }, "devDependencies": { "async": "^0.9.0", diff --git a/src/node/src/client.js b/src/node/src/client.js index 65339406b2..b7bad949d4 100644 --- a/src/node/src/client.js +++ b/src/node/src/client.js @@ -33,7 +33,7 @@ 'use strict'; -var _ = require('underscore'); +var _ = require('lodash'); var grpc = require('bindings')('grpc.node'); diff --git a/src/node/src/common.js b/src/node/src/common.js index 98917c0fdd..feaa859a4f 100644 --- a/src/node/src/common.js +++ b/src/node/src/common.js @@ -33,10 +33,7 @@ 'use strict'; -var _ = require('underscore'); - -var capitalize = require('underscore.string/capitalize'); -var decapitalize = require('underscore.string/decapitalize'); +var _ = require('lodash'); /** * Get a function that deserializes a specific type of protobuf. @@ -50,7 +47,9 @@ function deserializeCls(cls) { * @return {cls} The resulting object */ return function deserialize(arg_buf) { - return cls.decode(arg_buf).toRaw(); + // Convert to a native object with binary fields as Buffers (first argument) + // and longs as strings (second argument) + return cls.decode(arg_buf).toRaw(false, true); }; } @@ -81,7 +80,7 @@ function fullyQualifiedName(value) { } var name = value.name; if (value.className === 'Service.RPCMethod') { - name = capitalize(name); + name = _.capitalize(name); } if (value.hasOwnProperty('parent')) { var parent_name = fullyQualifiedName(value.parent); @@ -118,8 +117,8 @@ function wrapIgnoreNull(func) { function getProtobufServiceAttrs(service) { var prefix = '/' + fullyQualifiedName(service) + '/'; return _.object(_.map(service.children, function(method) { - return [decapitalize(method.name), { - path: prefix + capitalize(method.name), + return [_.camelCase(method.name), { + path: prefix + _.capitalize(method.name), requestStream: method.requestStream, responseStream: method.responseStream, requestSerialize: serializeCls(method.resolvedRequestType.build()), diff --git a/src/node/src/server.js b/src/node/src/server.js index 079495afd4..c6cf9e7eb8 100644 --- a/src/node/src/server.js +++ b/src/node/src/server.js @@ -33,7 +33,7 @@ 'use strict'; -var _ = require('underscore'); +var _ = require('lodash'); var grpc = require('bindings')('grpc.node'); @@ -48,8 +48,6 @@ var util = require('util'); var EventEmitter = require('events').EventEmitter; -var common = require('./common.js'); - /** * Handle an error on a call by sending it as a status * @param {grpc.Call} call The call to send the error on diff --git a/src/node/test/common_test.js b/src/node/test/common_test.js new file mode 100644 index 0000000000..08ba429ed7 --- /dev/null +++ b/src/node/test/common_test.js @@ -0,0 +1,90 @@ +/* + * + * 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. + * + */ + +'use strict'; + +var assert = require('assert'); + +var common = require('../src/common.js'); + +var ProtoBuf = require('protobufjs'); + +var messages_proto = ProtoBuf.loadProtoFile( + __dirname + '/test_messages.proto').build(); + +describe('Proto message serialize and deserialize', function() { + var longSerialize = common.serializeCls(messages_proto.LongValues); + var longDeserialize = common.deserializeCls(messages_proto.LongValues); + var pos_value = '314159265358979'; + var neg_value = '-27182818284590'; + it('should preserve positive int64 values', function() { + var serialized = longSerialize({int_64: pos_value}); + assert.strictEqual(longDeserialize(serialized).int_64.toString(), + pos_value); + }); + it('should preserve negative int64 values', function() { + var serialized = longSerialize({int_64: neg_value}); + assert.strictEqual(longDeserialize(serialized).int_64.toString(), + neg_value); + }); + it('should preserve uint64 values', function() { + var serialized = longSerialize({uint_64: pos_value}); + assert.strictEqual(longDeserialize(serialized).uint_64.toString(), + pos_value); + }); + it('should preserve positive sint64 values', function() { + var serialized = longSerialize({sint_64: pos_value}); + assert.strictEqual(longDeserialize(serialized).sint_64.toString(), + pos_value); + }); + it('should preserve negative sint64 values', function() { + var serialized = longSerialize({sint_64: neg_value}); + assert.strictEqual(longDeserialize(serialized).sint_64.toString(), + neg_value); + }); + it('should preserve fixed64 values', function() { + var serialized = longSerialize({fixed_64: pos_value}); + assert.strictEqual(longDeserialize(serialized).fixed_64.toString(), + pos_value); + }); + it('should preserve positive sfixed64 values', function() { + var serialized = longSerialize({sfixed_64: pos_value}); + assert.strictEqual(longDeserialize(serialized).sfixed_64.toString(), + pos_value); + }); + it('should preserve negative sfixed64 values', function() { + var serialized = longSerialize({sfixed_64: neg_value}); + assert.strictEqual(longDeserialize(serialized).sfixed_64.toString(), + neg_value); + }); +}); diff --git a/src/node/test/surface_test.js b/src/node/test/surface_test.js index b390f8b2a5..8d1f99aaee 100644 --- a/src/node/test/surface_test.js +++ b/src/node/test/surface_test.js @@ -45,9 +45,7 @@ var math_proto = ProtoBuf.loadProtoFile(__dirname + '/../examples/math.proto'); var mathService = math_proto.lookup('math.Math'); -var capitalize = require('underscore.string/capitalize'); - -var _ = require('underscore'); +var _ = require('lodash'); describe('File loader', function() { it('Should load a proto file by default', function() { @@ -159,7 +157,7 @@ describe('Generic client and server', function() { server = new Server({ string: { capitalize: function(call, callback) { - callback(null, capitalize(call.request)); + callback(null, _.capitalize(call.request)); } } }); diff --git a/src/node/test/test_messages.proto b/src/node/test/test_messages.proto new file mode 100644 index 0000000000..685e9482bd --- /dev/null +++ b/src/node/test/test_messages.proto @@ -0,0 +1,38 @@ +// 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. + +syntax = "proto3"; + +message LongValues { + int64 int_64 = 1; + uint64 uint_64 = 2; + sint64 sint_64 = 3; + fixed64 fixed_64 = 4; + sfixed64 sfixed_64 = 5; +}
\ No newline at end of file diff --git a/src/objective-c/GRPCClient/GRPCCall.h b/src/objective-c/GRPCClient/GRPCCall.h index 81b409b9ff..33aae10747 100644 --- a/src/objective-c/GRPCClient/GRPCCall.h +++ b/src/objective-c/GRPCClient/GRPCCall.h @@ -31,43 +31,55 @@ * */ +// The gRPC protocol is an RPC protocol on top of HTTP2. +// +// While the most common type of RPC receives only one request message and returns only one response +// message, the protocol also supports RPCs that return multiple individual messages in a streaming +// fashion, RPCs that accept a stream of request messages, or RPCs with both streaming requests and +// responses. +// +// Conceptually, each gRPC call consists of a bidirectional stream of binary messages, with RPCs of +// the "non-streaming type" sending only one message in the corresponding direction (the protocol +// doesn't make any distinction). +// +// Each RPC uses a different HTTP2 stream, and thus multiple simultaneous RPCs can be multiplexed +// transparently on the same TCP connection. + #import <Foundation/Foundation.h> -#import <gRPC/GRXWriter.h> +#import <RxLibrary/GRXWriter.h> @class GRPCMethodName; -@class GRPCCall; +// Key used in |NSError|'s |userInfo| dictionary to store the response metadata sent by the server. +extern id const kGRPCStatusMetadataKey; -// The gRPC protocol is an RPC protocol on top of HTTP2. -// -// While the most common type of RPC receives only one request message and -// returns only one response message, the protocol also supports RPCs that -// return multiple individual messages in a streaming fashion, RPCs that -// accept a stream of request messages, or RPCs with both streaming requests -// and responses. -// -// Conceptually, each gRPC call consists of a bidirectional stream of binary -// messages, with RPCs of the "non-streaming type" sending only one message in -// the corresponding direction (the protocol doesn't make any distinction). -// -// Each RPC uses a different HTTP2 stream, and thus multiple simultaneous RPCs -// can be multiplexed transparently on the same TCP connection. +// Represents a single gRPC remote call. @interface GRPCCall : NSObject<GRXWriter> -// These HTTP2 headers will be passed to the server as part of this call. Each -// HTTP2 header is a name-value pair with string names and either string or binary values. +// These HTTP headers will be passed to the server as part of this call. Each HTTP header is a +// name-value pair with string names and either string or binary values. +// // The passed dictionary has to use NSString keys, corresponding to the header names. The // value associated to each can be a NSString object or a NSData object. E.g.: // -// call.requestMetadata = @{ -// @"Authorization": @"Bearer ...", -// @"SomeBinaryHeader": someData -// }; +// call.requestMetadata = @{@"Authorization": @"Bearer ..."}; +// +// call.requestMetadata[@"SomeBinaryHeader"] = someData; // // After the call is started, modifying this won't have any effect. -@property(nonatomic, readwrite) NSMutableDictionary *requestMetadata; +// +// For convenience, the property is initialized to an empty NSMutableDictionary, and the setter +// accepts (and copies) both mutable and immutable dictionaries. +- (NSMutableDictionary *)requestMetadata; // nonatomic +- (void)setRequestMetadata:(NSDictionary *)requestMetadata; // nonatomic, copy -// This isn't populated until the first event is delivered to the handler. +// This dictionary is populated with the HTTP headers received from the server. When the RPC ends, +// the HTTP trailers received are added to the dictionary too. It has the same structure as the +// request metadata dictionary. +// +// The first time this object calls |writeValue| on the writeable passed to |startWithWriteable|, +// the |responseMetadata| dictionary already contains the response headers. When it calls +// |writesFinishedWithError|, the dictionary contains both the response headers and trailers. @property(atomic, readonly) NSDictionary *responseMetadata; // The request writer has to write NSData objects into the provided Writeable. The server will diff --git a/src/objective-c/GRPCClient/GRPCCall.m b/src/objective-c/GRPCClient/GRPCCall.m index a4a0ddb324..77eebeff76 100644 --- a/src/objective-c/GRPCClient/GRPCCall.m +++ b/src/objective-c/GRPCClient/GRPCCall.m @@ -34,7 +34,7 @@ #import "GRPCCall.h" #include <grpc/grpc.h> -#include <grpc/support/grpc_time.h> +#include <grpc/support/time.h> #import "GRPCMethodName.h" #import "private/GRPCChannel.h" @@ -46,9 +46,9 @@ #import "private/NSDictionary+GRPC.h" #import "private/NSError+GRPC.h" +NSString * const kGRPCStatusMetadataKey = @"io.grpc.StatusMetadataKey"; + @interface GRPCCall () <GRXWriteable> -// Makes it readwrite. -@property(atomic, strong) NSDictionary *responseMetadata; @end // The following methods of a C gRPC call object aren't reentrant, and thus @@ -82,6 +82,9 @@ // correct ordering. GRPCDelegateWrapper *_responseWriteable; id<GRXWriter> _requestWriter; + + NSMutableDictionary *_requestMetadata; + NSMutableDictionary *_responseMetadata; } @synthesize state = _state; @@ -97,7 +100,9 @@ if (!host || !method) { [NSException raise:NSInvalidArgumentException format:@"Neither host nor method can be nil."]; } - // TODO(jcanizales): Throw if the requestWriter was already started. + if (requestWriter.state != GRXWriterStateNotStarted) { + [NSException raise:NSInvalidArgumentException format:@"The requests writer can't be already started."]; + } if ((self = [super init])) { static dispatch_once_t initialization; dispatch_once(&initialization, ^{ @@ -116,10 +121,27 @@ _callQueue = dispatch_queue_create("org.grpc.call", NULL); _requestWriter = requestWriter; + + _requestMetadata = [NSMutableDictionary dictionary]; + _responseMetadata = [NSMutableDictionary dictionary]; } return self; } +#pragma mark Metadata + +- (NSMutableDictionary *)requestMetadata { + return _requestMetadata; +} + +- (void)setRequestMetadata:(NSDictionary *)requestMetadata { + _requestMetadata = [NSMutableDictionary dictionaryWithDictionary:requestMetadata]; +} + +- (NSDictionary *)responseMetadata { + return _responseMetadata; +} + #pragma mark Finish - (void)finishWithError:(NSError *)errorOrNil { @@ -277,7 +299,7 @@ // The first one (metadataHandler), when the response headers are received. // The second one (completionHandler), whenever the RPC finishes for any reason. - (void)invokeCallWithMetadataHandler:(void(^)(NSDictionary *))metadataHandler - completionHandler:(void(^)(NSError *))completionHandler { + completionHandler:(void(^)(NSError *, NSDictionary *))completionHandler { // TODO(jcanizales): Add error handlers for async failures [_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvMetadata alloc] initWithHandler:metadataHandler]]]; @@ -287,16 +309,26 @@ - (void)invokeCall { __weak GRPCCall *weakSelf = self; - [self invokeCallWithMetadataHandler:^(NSDictionary *metadata) { - // Response metadata received. + [self invokeCallWithMetadataHandler:^(NSDictionary *headers) { + // Response headers received. GRPCCall *strongSelf = weakSelf; if (strongSelf) { - strongSelf.responseMetadata = metadata; + [strongSelf->_responseMetadata addEntriesFromDictionary:headers]; [strongSelf startNextRead]; } - } completionHandler:^(NSError *error) { - // TODO(jcanizales): Merge HTTP2 trailers into response metadata. - [weakSelf finishWithError:error]; + } completionHandler:^(NSError *error, NSDictionary *trailers) { + GRPCCall *strongSelf = weakSelf; + if (strongSelf) { + [strongSelf->_responseMetadata addEntriesFromDictionary:trailers]; + + if (error) { + NSMutableDictionary *userInfo = + [NSMutableDictionary dictionaryWithDictionary:error.userInfo]; + userInfo[kGRPCStatusMetadataKey] = strongSelf->_responseMetadata; + error = [NSError errorWithDomain:error.domain code:error.code userInfo:userInfo]; + } + [strongSelf finishWithError:error]; + } }]; // Now that the RPC has been initiated, request writes can start. [_requestWriter startWithWriteable:self]; diff --git a/src/objective-c/GRPCClient/private/GRPCDelegateWrapper.m b/src/objective-c/GRPCClient/private/GRPCDelegateWrapper.m index 7d5ecb56d9..59c0565494 100644 --- a/src/objective-c/GRPCClient/private/GRPCDelegateWrapper.m +++ b/src/objective-c/GRPCClient/private/GRPCDelegateWrapper.m @@ -33,7 +33,7 @@ #import "GRPCDelegateWrapper.h" -#import <gRPC/GRXWriteable.h> +#import <RxLibrary/GRXWriteable.h> @interface GRPCDelegateWrapper () // These are atomic so that cancellation can nillify them from any thread. diff --git a/src/objective-c/GRPCClient/private/GRPCMethodName+HTTP2Encoding.h b/src/objective-c/GRPCClient/private/GRPCMethodName+HTTP2Encoding.h index 28c7374517..81c80f2a49 100644 --- a/src/objective-c/GRPCClient/private/GRPCMethodName+HTTP2Encoding.h +++ b/src/objective-c/GRPCClient/private/GRPCMethodName+HTTP2Encoding.h @@ -33,7 +33,7 @@ #import <Foundation/Foundation.h> -#import "GRPCMethodName.h" +#import "GRPCClient/GRPCMethodName.h" @interface GRPCMethodName (HTTP2Encoding) - (NSString *)HTTP2Path; diff --git a/src/objective-c/GRPCClient/private/GRPCSecureChannel.m b/src/objective-c/GRPCClient/private/GRPCSecureChannel.m index 2cbc6e0f83..43a8bd539e 100644 --- a/src/objective-c/GRPCClient/private/GRPCSecureChannel.m +++ b/src/objective-c/GRPCClient/private/GRPCSecureChannel.m @@ -38,13 +38,17 @@ @implementation GRPCSecureChannel - (instancetype)initWithHost:(NSString *)host { - static const grpc_credentials *kCredentials; + static grpc_credentials *kCredentials; static dispatch_once_t loading; dispatch_once(&loading, ^{ // Do not use NSBundle.mainBundle, as it's nil for tests of library projects. NSBundle *bundle = [NSBundle bundleForClass:self.class]; - NSString *certsPath = [bundle pathForResource:@"gRPC.bundle/roots" ofType:@"pem"]; + NSString *certsPath = [bundle pathForResource:@"gRPCCertificates.bundle/roots" ofType:@"pem"]; + NSAssert(certsPath.length, + @"gRPCCertificates.bundle/roots.pem not found under %@. This file, with the root " + "certificates, is needed to establish TLS (HTTPS) connections.", bundle.bundlePath); NSData *certsData = [NSData dataWithContentsOfFile:certsPath]; + NSAssert(certsData.length, @"No data read from %@", certsPath); NSString *certsString = [[NSString alloc] initWithData:certsData encoding:NSUTF8StringEncoding]; kCredentials = grpc_ssl_credentials_create(certsString.UTF8String, NULL); }); diff --git a/src/objective-c/GRPCClient/private/GRPCWrappedCall.h b/src/objective-c/GRPCClient/private/GRPCWrappedCall.h index 91cd703faf..c08aefc6a8 100644 --- a/src/objective-c/GRPCClient/private/GRPCWrappedCall.h +++ b/src/objective-c/GRPCClient/private/GRPCWrappedCall.h @@ -33,53 +33,51 @@ #import <Foundation/Foundation.h> #include <grpc/grpc.h> -#import "GRPCChannel.h" - -typedef void(^GRPCCompletionHandler)(NSDictionary *); - -@protocol GRPCOp <NSObject> -- (void)getOp:(grpc_op *)op; +#import "GRPCChannel.h" +@interface GRPCOperation : NSObject +@property(nonatomic, readonly) grpc_op op; +// Guaranteed to be called when the operation has finished. - (void)finish; - @end -@interface GRPCOpSendMetadata : NSObject <GRPCOp> +@interface GRPCOpSendMetadata : GRPCOperation - (instancetype)initWithMetadata:(NSDictionary *)metadata - handler:(void(^)(void))handler NS_DESIGNATED_INITIALIZER; + handler:(void(^)())handler NS_DESIGNATED_INITIALIZER; @end -@interface GRPCOpSendMessage : NSObject <GRPCOp> +@interface GRPCOpSendMessage : GRPCOperation - (instancetype)initWithMessage:(NSData *)message - handler:(void(^)(void))handler NS_DESIGNATED_INITIALIZER; + handler:(void(^)())handler NS_DESIGNATED_INITIALIZER; @end -@interface GRPCOpSendClose : NSObject <GRPCOp> +@interface GRPCOpSendClose : GRPCOperation -- (instancetype)initWithHandler:(void(^)(void))handler NS_DESIGNATED_INITIALIZER; +- (instancetype)initWithHandler:(void(^)())handler NS_DESIGNATED_INITIALIZER; @end -@interface GRPCOpRecvMetadata : NSObject <GRPCOp> +@interface GRPCOpRecvMetadata : GRPCOperation - (instancetype)initWithHandler:(void(^)(NSDictionary *))handler NS_DESIGNATED_INITIALIZER; @end -@interface GRPCOpRecvMessage : NSObject <GRPCOp> +@interface GRPCOpRecvMessage : GRPCOperation - (instancetype)initWithHandler:(void(^)(grpc_byte_buffer *))handler NS_DESIGNATED_INITIALIZER; @end -@interface GRPCOpRecvStatus : NSObject <GRPCOp> +@interface GRPCOpRecvStatus : GRPCOperation -- (instancetype)initWithHandler:(void(^)(NSError *))handler NS_DESIGNATED_INITIALIZER; +- (instancetype)initWithHandler:(void(^)(NSError *, NSDictionary *))handler + NS_DESIGNATED_INITIALIZER; @end diff --git a/src/objective-c/GRPCClient/private/GRPCWrappedCall.m b/src/objective-c/GRPCClient/private/GRPCWrappedCall.m index 9bc46930b4..d94b25091e 100644 --- a/src/objective-c/GRPCClient/private/GRPCWrappedCall.m +++ b/src/objective-c/GRPCClient/private/GRPCWrappedCall.m @@ -41,110 +41,85 @@ #import "NSData+GRPC.h" #import "NSError+GRPC.h" -@implementation GRPCOpSendMetadata{ - void(^_handler)(void); - grpc_metadata *_sendMetadata; - size_t _count; +@implementation GRPCOperation { +@protected + // Most operation subclasses don't set any flags in the grpc_op, and rely on the flag member being + // initialized to zero. + grpc_op _op; + void(^_handler)(); } +- (void)finish { + if (_handler) { + _handler(); + } +} +@end + +@implementation GRPCOpSendMetadata + - (instancetype)init { return [self initWithMetadata:nil handler:nil]; } -- (instancetype)initWithMetadata:(NSDictionary *)metadata handler:(void (^)(void))handler { +- (instancetype)initWithMetadata:(NSDictionary *)metadata handler:(void (^)())handler { if (self = [super init]) { - _sendMetadata = [metadata grpc_metadataArray]; - _count = metadata.count; + _op.op = GRPC_OP_SEND_INITIAL_METADATA; + _op.data.send_initial_metadata.count = metadata.count; + _op.data.send_initial_metadata.metadata = metadata.grpc_metadataArray; _handler = handler; } return self; } -- (void)getOp:(grpc_op *)op { - op->op = GRPC_OP_SEND_INITIAL_METADATA; - op->data.send_initial_metadata.count = _count; - op->data.send_initial_metadata.metadata = _sendMetadata; -} - -- (void)finish { - if (_handler) { - _handler(); - } -} - - (void)dealloc { - gpr_free(_sendMetadata); + gpr_free(_op.data.send_initial_metadata.metadata); } @end -@implementation GRPCOpSendMessage{ - void(^_handler)(void); - grpc_byte_buffer *_byteBuffer; -} +@implementation GRPCOpSendMessage - (instancetype)init { return [self initWithMessage:nil handler:nil]; } -- (instancetype)initWithMessage:(NSData *)message handler:(void (^)(void))handler { +- (instancetype)initWithMessage:(NSData *)message handler:(void (^)())handler { if (!message) { [NSException raise:NSInvalidArgumentException format:@"message cannot be nil"]; } if (self = [super init]) { - _byteBuffer = [message grpc_byteBuffer]; + _op.op = GRPC_OP_SEND_MESSAGE; + _op.data.send_message = message.grpc_byteBuffer; _handler = handler; } return self; } -- (void)getOp:(grpc_op *)op { - op->op = GRPC_OP_SEND_MESSAGE; - op->data.send_message = _byteBuffer; -} - -- (void)finish { - if (_handler) { - _handler(); - } -} - - (void)dealloc { - gpr_free(_byteBuffer); + gpr_free(_op.data.send_message); } @end -@implementation GRPCOpSendClose{ - void(^_handler)(void); -} +@implementation GRPCOpSendClose - (instancetype)init { return [self initWithHandler:nil]; } -- (instancetype)initWithHandler:(void (^)(void))handler { +- (instancetype)initWithHandler:(void (^)())handler { if (self = [super init]) { + _op.op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; _handler = handler; } return self; } -- (void)getOp:(grpc_op *)op { - op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; -} - -- (void)finish { - if (_handler) { - _handler(); - } -} - @end -@implementation GRPCOpRecvMetadata{ - void(^_handler)(NSDictionary *); - grpc_metadata_array _recvInitialMetadata; +@implementation GRPCOpRecvMetadata { + grpc_metadata_array _headers; } - (instancetype) init { @@ -153,35 +128,31 @@ - (instancetype) initWithHandler:(void (^)(NSDictionary *))handler { if (self = [super init]) { - _handler = handler; - grpc_metadata_array_init(&_recvInitialMetadata); + _op.op = GRPC_OP_RECV_INITIAL_METADATA; + grpc_metadata_array_init(&_headers); + _op.data.recv_initial_metadata = &_headers; + if (handler) { + // Prevent reference cycle with _handler + __weak typeof(self) weakSelf = self; + _handler = ^{ + __strong typeof(self) strongSelf = weakSelf; + NSDictionary *metadata = [NSDictionary + grpc_dictionaryFromMetadataArray:strongSelf->_headers]; + handler(metadata); + }; + } } return self; } -- (void)getOp:(grpc_op *)op { - op->op = GRPC_OP_RECV_INITIAL_METADATA; - op->data.recv_initial_metadata = &_recvInitialMetadata; -} - -- (void)finish { - NSDictionary *metadata = [NSDictionary - grpc_dictionaryFromMetadata:_recvInitialMetadata.metadata - count:_recvInitialMetadata.count]; - if (_handler) { - _handler(metadata); - } -} - - (void)dealloc { - grpc_metadata_array_destroy(&_recvInitialMetadata); + grpc_metadata_array_destroy(&_headers); } @end @implementation GRPCOpRecvMessage{ - void(^_handler)(grpc_byte_buffer *); - grpc_byte_buffer *_recvMessage; + grpc_byte_buffer *_receivedMessage; } - (instancetype)init { @@ -190,60 +161,60 @@ - (instancetype)initWithHandler:(void (^)(grpc_byte_buffer *))handler { if (self = [super init]) { - _handler = handler; + _op.op = GRPC_OP_RECV_MESSAGE; + _op.data.recv_message = &_receivedMessage; + if (handler) { + // Prevent reference cycle with _handler + __weak typeof(self) weakSelf = self; + _handler = ^{ + __strong typeof(self) strongSelf = weakSelf; + handler(strongSelf->_receivedMessage); + }; + } } return self; } -- (void)getOp:(grpc_op *)op { - op->op = GRPC_OP_RECV_MESSAGE; - op->data.recv_message = &_recvMessage; -} - -- (void)finish { - if (_handler) { - _handler(_recvMessage); - } -} - @end @implementation GRPCOpRecvStatus{ - void(^_handler)(NSError *); + grpc_status_code _statusCode; + char *_details; size_t _detailsCapacity; - grpc_status _status; + grpc_metadata_array _trailers; } - (instancetype) init { return [self initWithHandler:nil]; } -- (instancetype) initWithHandler:(void (^)(NSError *))handler { +- (instancetype) initWithHandler:(void (^)(NSError *, NSDictionary *))handler { if (self = [super init]) { - _handler = handler; - grpc_metadata_array_init(&_status.metadata); + _op.op = GRPC_OP_RECV_STATUS_ON_CLIENT; + _op.data.recv_status_on_client.status = &_statusCode; + _op.data.recv_status_on_client.status_details = &_details; + _op.data.recv_status_on_client.status_details_capacity = &_detailsCapacity; + grpc_metadata_array_init(&_trailers); + _op.data.recv_status_on_client.trailing_metadata = &_trailers; + if (handler) { + // Prevent reference cycle with _handler + __weak typeof(self) weakSelf = self; + _handler = ^{ + __strong typeof(self) strongSelf = weakSelf; + NSError *error = [NSError grpc_errorFromStatusCode:strongSelf->_statusCode + details:strongSelf->_details]; + NSDictionary *trailers = [NSDictionary + grpc_dictionaryFromMetadataArray:strongSelf->_trailers]; + handler(error, trailers); + }; + } } return self; } -- (void)getOp:(grpc_op *)op { - op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; - op->data.recv_status_on_client.status = &_status.status; - op->data.recv_status_on_client.status_details = &_status.details; - op->data.recv_status_on_client.status_details_capacity = &_detailsCapacity; - op->data.recv_status_on_client.trailing_metadata = &_status.metadata; -} - -- (void)finish { - if (_handler) { - NSError *error = [NSError grpc_errorFromStatus:&_status]; - _handler(error); - } -} - - (void)dealloc { - grpc_metadata_array_destroy(&_status.metadata); - gpr_free(_status.details); + grpc_metadata_array_destroy(&_trailers); + gpr_free(_details); } @end @@ -292,8 +263,8 @@ size_t nops = operations.count; grpc_op *ops_array = gpr_malloc(nops * sizeof(grpc_op)); size_t i = 0; - for (id op in operations) { - [op getOp:&ops_array[i++]]; + for (GRPCOperation *operation in operations) { + ops_array[i++] = operation.op; } grpc_call_error error = grpc_call_start_batch(_call, ops_array, nops, (__bridge_retained void *)(^(bool success){ @@ -304,14 +275,16 @@ return; } } - for (id<GRPCOp> operation in operations) { + for (GRPCOperation *operation in operations) { [operation finish]; } })); - + gpr_free(ops_array); + if (error != GRPC_CALL_OK) { [NSException raise:NSInternalInconsistencyException - format:@"A precondition for calling grpc_call_start_batch wasn't met"]; + format:@"A precondition for calling grpc_call_start_batch wasn't met. Error %i", + error]; } } diff --git a/src/objective-c/GRPCClient/private/NSData+GRPC.m b/src/objective-c/GRPCClient/private/NSData+GRPC.m index 6ea4ce979e..e6a6c3c605 100644 --- a/src/objective-c/GRPCClient/private/NSData+GRPC.m +++ b/src/objective-c/GRPCClient/private/NSData+GRPC.m @@ -34,25 +34,28 @@ #import "NSData+GRPC.h" #include <grpc/byte_buffer.h> +#include <grpc/byte_buffer_reader.h> #include <string.h> // TODO(jcanizales): Move these two incantations to the C library. static void CopyByteBufferToCharArray(grpc_byte_buffer *buffer, char *array) { size_t offset = 0; - grpc_byte_buffer_reader *reader = grpc_byte_buffer_reader_create(buffer); + grpc_byte_buffer_reader reader; + grpc_byte_buffer_reader_init(&reader, buffer); gpr_slice next; - while (grpc_byte_buffer_reader_next(reader, &next) != 0){ - memcpy(array + offset, GPR_SLICE_START_PTR(next), (size_t) GPR_SLICE_LENGTH(next)); + while (grpc_byte_buffer_reader_next(&reader, &next) != 0){ + memcpy(array + offset, GPR_SLICE_START_PTR(next), + (size_t)GPR_SLICE_LENGTH(next)); offset += GPR_SLICE_LENGTH(next); gpr_slice_unref(next); } - grpc_byte_buffer_reader_destroy(reader); } -static grpc_byte_buffer *CopyCharArrayToNewByteBuffer(const char *array, size_t length) { +static grpc_byte_buffer *CopyCharArrayToNewByteBuffer(const char *array, + size_t length) { gpr_slice slice = gpr_slice_from_copied_buffer(array, length); - grpc_byte_buffer *buffer = grpc_byte_buffer_create(&slice, 1); + grpc_byte_buffer *buffer = grpc_raw_byte_buffer_create(&slice, 1); gpr_slice_unref(slice); return buffer; } @@ -82,7 +85,7 @@ static grpc_byte_buffer *CopyCharArrayToNewByteBuffer(const char *array, size_t // The following implementation is thus not optimal, sometimes requiring two // copies (one by self.bytes and another by gpr_slice_from_copied_buffer). // If it turns out to be an issue, we can use enumerateByteRangesUsingblock: - // to create an array of gpr_slice objects to pass to grpc_byte_buffer_create. + // to create an array of gpr_slice objects to pass to grpc_raw_byte_buffer_create. // That would make it do exactly one copy, always. return CopyCharArrayToNewByteBuffer((const char *)self.bytes, (size_t)self.length); } diff --git a/src/objective-c/GRPCClient/private/NSDictionary+GRPC.h b/src/objective-c/GRPCClient/private/NSDictionary+GRPC.h index 622fddcf8e..7335681ac7 100644 --- a/src/objective-c/GRPCClient/private/NSDictionary+GRPC.h +++ b/src/objective-c/GRPCClient/private/NSDictionary+GRPC.h @@ -35,6 +35,7 @@ #include <grpc/grpc.h> @interface NSDictionary (GRPC) -+ (instancetype)grpc_dictionaryFromMetadata:(struct grpc_metadata *)entries count:(size_t)count; ++ (instancetype)grpc_dictionaryFromMetadataArray:(grpc_metadata_array)array; ++ (instancetype)grpc_dictionaryFromMetadata:(grpc_metadata *)entries count:(size_t)count; - (grpc_metadata *)grpc_metadataArray; @end diff --git a/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m b/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m index e14e503ae0..99c890e4ee 100644 --- a/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m +++ b/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m @@ -98,14 +98,18 @@ #pragma mark Category for metadata arrays @implementation NSDictionary (GRPC) ++ (instancetype)grpc_dictionaryFromMetadataArray:(grpc_metadata_array)array { + return [self grpc_dictionaryFromMetadata:array.metadata count:array.count]; +} + + (instancetype)grpc_dictionaryFromMetadata:(grpc_metadata *)entries count:(size_t)count { NSMutableDictionary *metadata = [NSMutableDictionary dictionaryWithCapacity:count]; for (grpc_metadata *entry = entries; entry < entries + count; entry++) { // TODO(jcanizales): Verify in a C library test that it's converting header names to lower case // automatically. NSString *name = [NSString stringWithCString:entry->key encoding:NSASCIIStringEncoding]; - if (!name) { - // log? + if (!name || metadata[name]) { + // Log if name is nil? continue; } id value; @@ -115,10 +119,7 @@ } else { value = [NSString grpc_stringFromMetadataValue:entry]; } - if (!metadata[name]) { - metadata[name] = [NSMutableArray array]; - } - [metadata[name] addObject:value]; + metadata[name] = value; } return metadata; } diff --git a/src/objective-c/GRPCClient/private/NSError+GRPC.h b/src/objective-c/GRPCClient/private/NSError+GRPC.h index 6577d34e80..e712791271 100644 --- a/src/objective-c/GRPCClient/private/NSError+GRPC.h +++ b/src/objective-c/GRPCClient/private/NSError+GRPC.h @@ -32,6 +32,7 @@ */ #import <Foundation/Foundation.h> +#include <grpc/grpc.h> // TODO(jcanizales): Make the domain string public. extern NSString *const kGRPCErrorDomain; @@ -56,17 +57,8 @@ typedef NS_ENUM(NSInteger, GRPCErrorCode) { GRPCErrorCodeDataLoss = 15 }; -// TODO(jcanizales): This is conflating trailing metadata with Status details. Fix it once there's -// a decision on how to codify Status. -#include <grpc/grpc.h> -typedef struct grpc_status { - grpc_status_code status; - char *details; - grpc_metadata_array metadata; -} grpc_status; - @interface NSError (GRPC) -// Returns nil if the status is OK. Otherwise, a NSError whose code is one of -// GRPCErrorCode and whose domain is kGRPCErrorDomain. -+ (instancetype)grpc_errorFromStatus:(struct grpc_status *)status; +// Returns nil if the status code is OK. Otherwise, a NSError whose code is one of |GRPCErrorCode| +// and whose domain is |kGRPCErrorDomain|. ++ (instancetype)grpc_errorFromStatusCode:(grpc_status_code)statusCode details:(char *)details; @end diff --git a/src/objective-c/GRPCClient/private/NSError+GRPC.m b/src/objective-c/GRPCClient/private/NSError+GRPC.m index 15c0208681..638f41cb11 100644 --- a/src/objective-c/GRPCClient/private/NSError+GRPC.m +++ b/src/objective-c/GRPCClient/private/NSError+GRPC.m @@ -33,19 +33,18 @@ #import "NSError+GRPC.h" -#include <grpc.h> +#include <grpc/grpc.h> -NSString *const kGRPCErrorDomain = @"org.grpc"; +NSString * const kGRPCErrorDomain = @"io.grpc"; @implementation NSError (GRPC) -+ (instancetype)grpc_errorFromStatus:(struct grpc_status *)status { - if (status->status == GRPC_STATUS_OK) { ++ (instancetype)grpc_errorFromStatusCode:(grpc_status_code)statusCode details:(char *)details { + if (statusCode == GRPC_STATUS_OK) { return nil; } - NSString *message = - [NSString stringWithFormat:@"Code=%i Message='%s'", status->status, status->details]; + NSString *message = [NSString stringWithCString:details encoding:NSASCIIStringEncoding]; return [NSError errorWithDomain:kGRPCErrorDomain - code:status->status + code:statusCode userInfo:@{NSLocalizedDescriptionKey: message}]; } @end diff --git a/src/objective-c/ProtoRPC/ProtoRPC.h b/src/objective-c/ProtoRPC/ProtoRPC.h index b6375f52d6..a383310619 100644 --- a/src/objective-c/ProtoRPC/ProtoRPC.h +++ b/src/objective-c/ProtoRPC/ProtoRPC.h @@ -32,7 +32,7 @@ */ #import <Foundation/Foundation.h> -#import <gRPC/GRPCCall.h> +#import <GRPCClient/GRPCCall.h> @interface ProtoRPC : GRPCCall diff --git a/src/objective-c/ProtoRPC/ProtoRPC.m b/src/objective-c/ProtoRPC/ProtoRPC.m index b8f8008422..4da646d7b4 100644 --- a/src/objective-c/ProtoRPC/ProtoRPC.m +++ b/src/objective-c/ProtoRPC/ProtoRPC.m @@ -33,10 +33,10 @@ #import "ProtoRPC.h" -#import <gRPC/GRXWriteable.h> -#import <gRPC/GRXWriter.h> -#import <gRPC/GRXWriter+Transformations.h> -#import <Protobuf/GPBProtocolBuffers.h> +#import <GPBProtocolBuffers.h> +#import <RxLibrary/GRXWriteable.h> +#import <RxLibrary/GRXWriter.h> +#import <RxLibrary/GRXWriter+Transformations.h> @implementation ProtoRPC { id<GRXWriteable> _responseWriteable; @@ -66,6 +66,8 @@ // A writer that serializes the proto messages to send. id<GRXWriter> bytesWriter = [[[GRXWriter alloc] initWithWriter:requestsWriter] map:^id(GPBMessage *proto) { + // TODO(jcanizales): Fail with an understandable error message if the requestsWriter isn't + // sending GPBMessages. return [proto data]; }]; if ((self = [super initWithHost:host method:method requestsWriter:bytesWriter])) { diff --git a/src/objective-c/ProtoRPC/ProtoService.m b/src/objective-c/ProtoRPC/ProtoService.m index 453d7b3f1a..47bdb5dc6e 100644 --- a/src/objective-c/ProtoRPC/ProtoService.m +++ b/src/objective-c/ProtoRPC/ProtoService.m @@ -33,9 +33,9 @@ #import "ProtoService.h" -#import <gRPC/GRPCMethodName.h> -#import <gRPC/GRXWriteable.h> -#import <gRPC/GRXWriter.h> +#import <GRPCClient/GRPCMethodName.h> +#import <RxLibrary/GRXWriteable.h> +#import <RxLibrary/GRXWriter.h> #import "ProtoRPC.h" diff --git a/src/objective-c/README.md b/src/objective-c/README.md index 921eb69231..e997b76d14 100644 --- a/src/objective-c/README.md +++ b/src/objective-c/README.md @@ -1,78 +1,101 @@ # gRPC for Objective-C - [Install protoc with the gRPC plugin](#install) -- [Use protoc to generate a gRPC library](#protoc) -- [Integrate the generated gRPC library in your project](#cocoapods) +- [Write your API declaration in proto format](#write-protos) +- [Integrate a proto library in your project](#cocoapods) - [Use the generated library in your code](#use) -- [Alternative methods](#alternatives) - - [Install protoc and the gRPC plugin without using Homebrew](#nohomebrew) - - [Integrate the generated gRPC library without using Cocoapods](#nococoapods) +- [Use gRPC without Protobuf](#no-proto) +- [Alternative installation methods](#alternatives) + - [Install protoc and the gRPC plugin without using Homebrew](#no-homebrew) + - [Integrate the generated gRPC library without using Cocoapods](#no-cocoapods) + +While gRPC doesn't require the use of an IDL to describe the API of services, using one simplifies +usage and adds some interoperability guarantees. Here we use [Protocol Buffers][], and provide a +plugin for the Protobuf Compiler (_protoc_) to generate client libraries to communicate with gRPC +services. <a name="install"></a> ## Install protoc with the gRPC plugin On Mac OS X, install [homebrew][]. On Linux, install [linuxbrew][]. -Run the following command to install the Protocol Buffers compiler (_protoc_) and the gRPC _protoc_ plugin: +Run the following command to install _protoc_ and the gRPC _protoc_ plugin: ```sh $ curl -fsSL https://goo.gl/getgrpc | bash - ``` -This will download and run the [gRPC install script][]. After the command completes, you're ready to proceed. - -<a name="protoc"></a> -## Use protoc to generate a gRPC library +This will download and run the [gRPC install script][]. After the command completes, you're ready to +proceed. -Run _protoc_ with the following flags to generate the client library for your `.proto` files: +<a name="write-protos"></a> +## Write your API declaration in proto format -```sh -protoc --objc_out=. --objcgrpc_out=. *.proto -``` - -This will generate a pair of `.pbobjc.h`/`.pbobjc.m` files for each `.proto` file, with the messages and enums defined in them. And a pair of `.pbrpc.h`/`.pbrpc.m` files for each `.proto` file with services defined. The latter contains the code to make remote calls to the specified API. +For this you can consult the [Protocol Buffers][]' official documentation, or learn from a quick +example [here](https://github.com/grpc/grpc-common#defining-a-service). <a name="cocoapods"></a> -## Integrate the generated gRPC library in your project +## Integrate a proto library in your project Install [Cocoapods](https://cocoapods.org/#install). -You need to create a Podspec file for the generated library. You may simply copy the following example to the directory where the source files were generated, updating the name and other metadata of the Podspec as necessary: +You need to create a Podspec file for your proto library. You may simply copy the following example +to the directory where your `.proto` files are located, updating the name, version and license as +necessary: ```ruby Pod::Spec.new do |s| s.name = '<Podspec file name>' - s.version = '...' - s.summary = 'Client library to make RPCs to <my proto API>' - s.homepage = '...' + s.version = '0.0.1' s.license = '...' - s.authors = { '<my name>' => '<my email>' } s.ios.deployment_target = '6.0' s.osx.deployment_target = '10.8' - s.subspec 'Messages' do |ms| - ms.source_files = '*.pbobjc.{h,m}', '**/*.pbobjc.{h,m}' - ms.header_mappings_dir = '.' + # Run protoc with the Objective-C and gRPC plugins to generate protocol messages and gRPC clients. + # You can run this command manually if you later change your protos and need to regenerate. + s.prepare_command = "protoc --objc_out=. --objcgrpc_out=. *.proto" + + # The --objc_out plugin generates a pair of .pbobjc.h/.pbobjc.m files for each .proto file. + s.subspec "Messages" do |ms| + ms.source_files = "*.pbobjc.{h,m}" + ms.header_mappings_dir = "." ms.requires_arc = false - ms.dependency 'Protobuf', '~> 3.0' + ms.dependency "Protobuf", "~> 3.0.0-alpha-3" end - s.subspec 'Services' do |ss| - ss.source_files = '*.pbrpc.{h,m}', '**/*.pbrpc.{h,m}' - ss.header_mappings_dir = '.' + # The --objcgrpc_out plugin generates a pair of .pbrpc.h/.pbrpc.m files for each .proto file with + # a service defined. + s.subspec "Services" do |ss| + ss.source_files = "*.pbrpc.{h,m}" + ss.header_mappings_dir = "." ss.requires_arc = true - ss.dependency 'gRPC', '~> 0.0' - ss.dependency '<Podspec file name>/Messages' + ss.dependency "gRPC", "~> 0.5" + ss.dependency "#{s.name}/Messages" end end ``` -The file should be named `<Podspec file name>.podspec`. You can refer to this [example Podspec][]. Once your library has a Podspec, Cocoapods can install it into any XCode project. For that, go into your project's directory and create a Podfile by running: +The file should be named `<Podspec file name>.podspec`. + +Note: If your proto files are in a directory hierarchy, you might want to adjust the _globs_ used in +the sample Podspec above. For example, you could use: + +```ruby + s.prepare_command = "protoc --objc_out=. --objcgrpc_out=. *.proto **/*.proto" + ... + ms.source_files = "*.pbobjc.{h,m}", "**/*.pbobjc.{h,m}" + ... + ss.source_files = "*.pbrpc.{h,m}", "**/*.pbrpc.{h,m}" +``` + +Once your library has a Podspec, Cocoapods can install it into any XCode project. For that, go into +your project's directory and create a Podfile by running: ```sh pod init ``` -Next add a line to your Podfile to refer to your library's Podspec. Use `:path` as described [here](https://guides.cocoapods.org/using/the-podfile.html#using-the-files-from-a-folder-local-to-the-machine): +Next add a line to your Podfile to refer to your library's Podspec. Use `:path` as described +[here](https://guides.cocoapods.org/using/the-podfile.html#using-the-files-from-a-folder-local-to-the-machine): ```ruby pod '<Podspec file name>', :path => 'path/to/the/directory/of/your/podspec' @@ -91,15 +114,25 @@ pod install Please check this [sample app][] for examples of how to use a generated gRPC library. +<a name="no-proto"></a> +## Use gRPC without Protobuf + +The [sample app][] has an example of how to use the generic gRPC Objective-C client without +generated files. + <a name="alternatives"></a> -## Alternative methods +## Alternative installation methods -<a name="nohomebrew"></a> +<a name="no-homebrew"></a> ### Install protoc and the gRPC plugin without using Homebrew -First install v3 of the Protocol Buffers compiler (_protoc_), by cloning [its Git repository](https://github.com/google/protobuf) and following these [installation instructions](https://github.com/google/protobuf#c-installation---unix) (the ones titled C++; don't miss the note for Mac users). +First install v3 of the Protocol Buffers compiler (_protoc_), by cloning +[its Git repository](https://github.com/google/protobuf) and following these +[installation instructions](https://github.com/google/protobuf#c-installation---unix) +(the ones titled C++; don't miss the note for Mac users). -Then clone this repository and execute the following commands from the root directory where it was cloned. +Then clone this repository and execute the following commands from the root directory where it was +cloned. Compile the gRPC plugins for _protoc_: ```sh @@ -110,23 +143,32 @@ Create a symbolic link to the compiled plugin binary somewhere in your `$PATH`: ```sh ln -s `pwd`/bins/opt/grpc_objective_c_plugin /usr/local/bin/protoc-gen-objcgrpc ``` -(Notice that the name of the created link must begin with "protoc-gen-" for _protoc_ to recognize it as a plugin). +(Notice that the name of the created link must begin with "protoc-gen-" for _protoc_ to recognize it +as a plugin). -If you don't want to create the symbolic link, you can alternatively copy the binary (with the appropriate name). Or you might prefer instead to specify the plugin's path as a flag when invoking _protoc_, in which case no system modification nor renaming is necessary. +If you don't want to create the symbolic link, you can alternatively copy the binary (with the +appropriate name). Or you might prefer instead to specify the plugin's path as a flag when invoking +_protoc_, in which case no system modification nor renaming is necessary. -<a name="nococoapods"></a> +<a name="no-cocoapods"></a> ### Integrate the generated gRPC library without using Cocoapods -You need to compile the generated `.pbpbjc.*` files (the enums and messages) without ARC support, and the generated `.pbrpc.*` files (the services) with ARC support. The generated code depends on v0.3+ of the Objective-C gRPC runtime library and v3.0+ of the Objective-C Protobuf runtime library. +You need to compile the generated `.pbpbjc.*` files (the enums and messages) without ARC support, +and the generated `.pbrpc.*` files (the services) with ARC support. The generated code depends on +v0.5+ of the Objective-C gRPC runtime library and v3.0.0-alpha-3+ of the Objective-C Protobuf +runtime library. -These libraries need to be integrated into your project as described in their respective Podspec files: +These libraries need to be integrated into your project as described in their respective Podspec +files: -* [Podspec](https://github.com/grpc/grpc/blob/master/gRPC.podspec) for the Objective-C gRPC runtime library. This can be tedious to configure manually. -* [Podspec](https://github.com/jcanizales/protobuf/blob/add-podspec/Protobuf.podspec) for the Objective-C Protobuf runtime library. +* [Podspec](https://github.com/grpc/grpc/blob/master/gRPC.podspec) for the Objective-C gRPC runtime +library. This can be tedious to configure manually. +* [Podspec](https://github.com/google/protobuf/blob/master/Protobuf.podspec) for the +Objective-C Protobuf runtime library. +[Protocol Buffers]:https://developers.google.com/protocol-buffers/ [homebrew]:http://brew.sh [linuxbrew]:https://github.com/Homebrew/linuxbrew [gRPC install script]:https://raw.githubusercontent.com/grpc/homebrew-grpc/master/scripts/install -[example Podspec]:https://github.com/grpc/grpc/blob/master/src/objective-c/examples/Sample/RemoteTestClient/RemoteTest.podspec [example Podfile]:https://github.com/grpc/grpc/blob/master/src/objective-c/examples/Sample/Podfile [sample app]: https://github.com/grpc/grpc/tree/master/src/objective-c/examples/Sample diff --git a/src/objective-c/RxLibrary/transformations/GRXMappingWriter.h b/src/objective-c/RxLibrary/transformations/GRXMappingWriter.h index dcebb6132a..55f6f82f20 100644 --- a/src/objective-c/RxLibrary/transformations/GRXMappingWriter.h +++ b/src/objective-c/RxLibrary/transformations/GRXMappingWriter.h @@ -31,7 +31,7 @@ * */ -#import "GRXWriter.h" +#import "RxLibrary/GRXWriter.h" // A "proxy" writer that transforms all the values of its input writer by using a mapping function. @interface GRXMappingWriter : GRXWriter diff --git a/src/objective-c/examples/Sample/Podfile b/src/objective-c/examples/Sample/Podfile index f3c49c12a1..72308c1619 100644 --- a/src/objective-c/examples/Sample/Podfile +++ b/src/objective-c/examples/Sample/Podfile @@ -2,7 +2,6 @@ source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' pod 'gRPC', :path => "../../../.." -pod 'Protobuf', :git => 'https://github.com/google/protobuf.git' pod 'RemoteTest', :path => "../../generated_libraries/RemoteTestClient" target 'Sample' do diff --git a/src/objective-c/examples/Sample/Sample/ViewController.m b/src/objective-c/examples/Sample/Sample/ViewController.m index 0011a4508d..05bd6fa2db 100644 --- a/src/objective-c/examples/Sample/Sample/ViewController.m +++ b/src/objective-c/examples/Sample/Sample/ViewController.m @@ -33,12 +33,12 @@ #import "ViewController.h" -#import <gRPC/GRPCCall.h> -#import <gRPC/GRPCMethodName.h> -#import <gRPC/GRXWriter+Immediate.h> -#import <gRPC/GRXWriteable.h> +#import <GRPCClient/GRPCCall.h> +#import <GRPCClient/GRPCMethodName.h> #import <RemoteTest/Messages.pbobjc.h> #import <RemoteTest/Test.pbrpc.h> +#import <RxLibrary/GRXWriter+Immediate.h> +#import <RxLibrary/GRXWriteable.h> @implementation ViewController diff --git a/src/objective-c/generated_libraries/RemoteTestClient/Empty.pbobjc.h b/src/objective-c/generated_libraries/RemoteTestClient/Empty.pbobjc.h deleted file mode 100644 index 809d0dde6e..0000000000 --- a/src/objective-c/generated_libraries/RemoteTestClient/Empty.pbobjc.h +++ /dev/null @@ -1,33 +0,0 @@ -// Generated by the protocol buffer compiler. DO NOT EDIT! -// source: empty.proto - -#import "GPBProtocolBuffers.h" - -#if GOOGLE_PROTOBUF_OBJC_GEN_VERSION != 30000 -#error This file was generated by a different version of protoc-gen-objc which is incompatible with your Protocol Buffer sources. -#endif - -CF_EXTERN_C_BEGIN - -@class RMTEmpty; - - -#pragma mark - RMTEmptyRoot - -@interface RMTEmptyRoot : GPBRootObject -@end - -#pragma mark - RMTEmpty - -// An empty message that you can re-use to avoid defining duplicated empty -// messages in your project. A typical example is to use it as argument or the -// return value of a service API. For instance: -// -// service Foo { -// rpc Bar (grpc.testing.Empty) returns (grpc.testing.Empty) { }; -// }; -@interface RMTEmpty : GPBMessage - -@end - -CF_EXTERN_C_END diff --git a/src/objective-c/generated_libraries/RemoteTestClient/Empty.pbobjc.m b/src/objective-c/generated_libraries/RemoteTestClient/Empty.pbobjc.m deleted file mode 100644 index 46d481af66..0000000000 --- a/src/objective-c/generated_libraries/RemoteTestClient/Empty.pbobjc.m +++ /dev/null @@ -1,59 +0,0 @@ -// Generated by the protocol buffer compiler. DO NOT EDIT! -// source: empty.proto - -#import "GPBProtocolBuffers_RuntimeSupport.h" - -#import "Empty.pbobjc.h" - -#pragma mark - RMTEmptyRoot - -@implementation RMTEmptyRoot - -@end - -static GPBFileDescriptor *RMTEmptyRoot_FileDescriptor(void) { - // This is called by +initialize so there is no need to worry - // about thread safety of the singleton. - static GPBFileDescriptor *descriptor = NULL; - if (!descriptor) { - descriptor = [[GPBFileDescriptor alloc] initWithPackage:@"grpc.testing" - syntax:GPBFileSyntaxProto3]; - } - return descriptor; -} - -#pragma mark - RMTEmpty - -@implementation RMTEmpty - - -typedef struct RMTEmpty_Storage { - uint32_t _has_storage_[0]; -} RMTEmpty_Storage; - -// This method is threadsafe because it is initially called -// in +initialize for each subclass. -+ (GPBDescriptor *)descriptor { - static GPBDescriptor *descriptor = NULL; - if (!descriptor) { - static GPBMessageFieldDescription fields[] = { - }; - descriptor = [GPBDescriptor allocDescriptorForClass:[RMTEmpty class] - rootClass:[RMTEmptyRoot class] - file:RMTEmptyRoot_FileDescriptor() - fields:fields - fieldCount:sizeof(fields) / sizeof(GPBMessageFieldDescription) - oneofs:NULL - oneofCount:0 - enums:NULL - enumCount:0 - ranges:NULL - rangeCount:0 - storageSize:sizeof(RMTEmpty_Storage) - wireFormat:NO]; - } - return descriptor; -} - -@end - diff --git a/src/objective-c/generated_libraries/RemoteTestClient/Messages.pbobjc.h b/src/objective-c/generated_libraries/RemoteTestClient/Messages.pbobjc.h deleted file mode 100644 index 5f32314175..0000000000 --- a/src/objective-c/generated_libraries/RemoteTestClient/Messages.pbobjc.h +++ /dev/null @@ -1,221 +0,0 @@ -// Generated by the protocol buffer compiler. DO NOT EDIT! -// source: messages.proto - -#import "GPBProtocolBuffers.h" - -#if GOOGLE_PROTOBUF_OBJC_GEN_VERSION != 30000 -#error This file was generated by a different version of protoc-gen-objc which is incompatible with your Protocol Buffer sources. -#endif - -CF_EXTERN_C_BEGIN - -@class RMTPayload; -@class RMTResponseParameters; -@class RMTSimpleRequest; -@class RMTSimpleResponse; -@class RMTStreamingInputCallRequest; -@class RMTStreamingInputCallResponse; -@class RMTStreamingOutputCallRequest; -@class RMTStreamingOutputCallResponse; - -#pragma mark - Enum RMTPayloadType - -// The type of payload that should be returned. -typedef GPB_ENUM(RMTPayloadType) { - RMTPayloadType_GPBUnrecognizedEnumeratorValue = kGPBUnrecognizedEnumeratorValue, - // Compressable text format. - RMTPayloadType_Compressable = 0, - - // Uncompressable binary format. - RMTPayloadType_Uncompressable = 1, - - // Randomly chosen from all other formats defined in this enum. - RMTPayloadType_Random = 2, -}; - -GPBEnumDescriptor *RMTPayloadType_EnumDescriptor(void); - -BOOL RMTPayloadType_IsValidValue(int32_t value); - - -#pragma mark - RMTMessagesRoot - -@interface RMTMessagesRoot : GPBRootObject -@end - -#pragma mark - RMTPayload - -typedef GPB_ENUM(RMTPayload_FieldNumber) { - RMTPayload_FieldNumber_Type = 1, - RMTPayload_FieldNumber_Body = 2, -}; - -// A block of data, to simply increase gRPC message size. -@interface RMTPayload : GPBMessage - -// The type of data in body. -@property(nonatomic, readwrite) RMTPayloadType type; - -// Primary contents of payload. -@property(nonatomic, readwrite, copy) NSData *body; - -@end - -int32_t RMTPayload_Type_RawValue(RMTPayload *message); -void SetRMTPayload_Type_RawValue(RMTPayload *message, int32_t value); - -#pragma mark - RMTSimpleRequest - -typedef GPB_ENUM(RMTSimpleRequest_FieldNumber) { - RMTSimpleRequest_FieldNumber_ResponseType = 1, - RMTSimpleRequest_FieldNumber_ResponseSize = 2, - RMTSimpleRequest_FieldNumber_Payload = 3, - RMTSimpleRequest_FieldNumber_FillUsername = 4, - RMTSimpleRequest_FieldNumber_FillOauthScope = 5, -}; - -// Unary request. -@interface RMTSimpleRequest : GPBMessage - -// Desired payload type in the response from the server. -// If response_type is RANDOM, server randomly chooses one from other formats. -@property(nonatomic, readwrite) RMTPayloadType responseType; - -// Desired payload size in the response from the server. -// If response_type is COMPRESSABLE, this denotes the size before compression. -@property(nonatomic, readwrite) int32_t responseSize; - -// Optional input payload sent along with the request. -@property(nonatomic, readwrite) BOOL hasPayload; -@property(nonatomic, readwrite, strong) RMTPayload *payload; - -// Whether SimpleResponse should include username. -@property(nonatomic, readwrite) BOOL fillUsername; - -// Whether SimpleResponse should include OAuth scope. -@property(nonatomic, readwrite) BOOL fillOauthScope; - -@end - -int32_t RMTSimpleRequest_ResponseType_RawValue(RMTSimpleRequest *message); -void SetRMTSimpleRequest_ResponseType_RawValue(RMTSimpleRequest *message, int32_t value); - -#pragma mark - RMTSimpleResponse - -typedef GPB_ENUM(RMTSimpleResponse_FieldNumber) { - RMTSimpleResponse_FieldNumber_Payload = 1, - RMTSimpleResponse_FieldNumber_Username = 2, - RMTSimpleResponse_FieldNumber_OauthScope = 3, -}; - -// Unary response, as configured by the request. -@interface RMTSimpleResponse : GPBMessage - -// Payload to increase message size. -@property(nonatomic, readwrite) BOOL hasPayload; -@property(nonatomic, readwrite, strong) RMTPayload *payload; - -// The user the request came from, for verifying authentication was -// successful when the client expected it. -@property(nonatomic, readwrite, copy) NSString *username; - -// OAuth scope. -@property(nonatomic, readwrite, copy) NSString *oauthScope; - -@end - -#pragma mark - RMTStreamingInputCallRequest - -typedef GPB_ENUM(RMTStreamingInputCallRequest_FieldNumber) { - RMTStreamingInputCallRequest_FieldNumber_Payload = 1, -}; - -// Client-streaming request. -@interface RMTStreamingInputCallRequest : GPBMessage - -// Optional input payload sent along with the request. -@property(nonatomic, readwrite) BOOL hasPayload; -@property(nonatomic, readwrite, strong) RMTPayload *payload; - -@end - -#pragma mark - RMTStreamingInputCallResponse - -typedef GPB_ENUM(RMTStreamingInputCallResponse_FieldNumber) { - RMTStreamingInputCallResponse_FieldNumber_AggregatedPayloadSize = 1, -}; - -// Client-streaming response. -@interface RMTStreamingInputCallResponse : GPBMessage - -// Aggregated size of payloads received from the client. -@property(nonatomic, readwrite) int32_t aggregatedPayloadSize; - -@end - -#pragma mark - RMTResponseParameters - -typedef GPB_ENUM(RMTResponseParameters_FieldNumber) { - RMTResponseParameters_FieldNumber_Size = 1, - RMTResponseParameters_FieldNumber_IntervalUs = 2, -}; - -// Configuration for a particular response. -@interface RMTResponseParameters : GPBMessage - -// Desired payload sizes in responses from the server. -// If response_type is COMPRESSABLE, this denotes the size before compression. -@property(nonatomic, readwrite) int32_t size; - -// Desired interval between consecutive responses in the response stream in -// microseconds. -@property(nonatomic, readwrite) int32_t intervalUs; - -@end - -#pragma mark - RMTStreamingOutputCallRequest - -typedef GPB_ENUM(RMTStreamingOutputCallRequest_FieldNumber) { - RMTStreamingOutputCallRequest_FieldNumber_ResponseType = 1, - RMTStreamingOutputCallRequest_FieldNumber_ResponseParametersArray = 2, - RMTStreamingOutputCallRequest_FieldNumber_Payload = 3, -}; - -// Server-streaming request. -@interface RMTStreamingOutputCallRequest : GPBMessage - -// Desired payload type in the response from the server. -// If response_type is RANDOM, the payload from each response in the stream -// might be of different types. This is to simulate a mixed type of payload -// stream. -@property(nonatomic, readwrite) RMTPayloadType responseType; - -// Configuration for each expected response message. -// |responseParametersArray| contains |RMTResponseParameters| -@property(nonatomic, readwrite, strong) NSMutableArray *responseParametersArray; - -// Optional input payload sent along with the request. -@property(nonatomic, readwrite) BOOL hasPayload; -@property(nonatomic, readwrite, strong) RMTPayload *payload; - -@end - -int32_t RMTStreamingOutputCallRequest_ResponseType_RawValue(RMTStreamingOutputCallRequest *message); -void SetRMTStreamingOutputCallRequest_ResponseType_RawValue(RMTStreamingOutputCallRequest *message, int32_t value); - -#pragma mark - RMTStreamingOutputCallResponse - -typedef GPB_ENUM(RMTStreamingOutputCallResponse_FieldNumber) { - RMTStreamingOutputCallResponse_FieldNumber_Payload = 1, -}; - -// Server-streaming response, as configured by the request and parameters. -@interface RMTStreamingOutputCallResponse : GPBMessage - -// Payload to increase response size. -@property(nonatomic, readwrite) BOOL hasPayload; -@property(nonatomic, readwrite, strong) RMTPayload *payload; - -@end - -CF_EXTERN_C_END diff --git a/src/objective-c/generated_libraries/RemoteTestClient/Messages.pbobjc.m b/src/objective-c/generated_libraries/RemoteTestClient/Messages.pbobjc.m deleted file mode 100644 index 98eb1dc87c..0000000000 --- a/src/objective-c/generated_libraries/RemoteTestClient/Messages.pbobjc.m +++ /dev/null @@ -1,603 +0,0 @@ -// Generated by the protocol buffer compiler. DO NOT EDIT! -// source: messages.proto - -#import "GPBProtocolBuffers_RuntimeSupport.h" - -#import "Messages.pbobjc.h" - -#pragma mark - RMTMessagesRoot - -@implementation RMTMessagesRoot - -@end - -static GPBFileDescriptor *RMTMessagesRoot_FileDescriptor(void) { - // This is called by +initialize so there is no need to worry - // about thread safety of the singleton. - static GPBFileDescriptor *descriptor = NULL; - if (!descriptor) { - descriptor = [[GPBFileDescriptor alloc] initWithPackage:@"grpc.testing" - syntax:GPBFileSyntaxProto3]; - } - return descriptor; -} - -#pragma mark - Enum RMTPayloadType - -GPBEnumDescriptor *RMTPayloadType_EnumDescriptor(void) { - static GPBEnumDescriptor *descriptor = NULL; - if (!descriptor) { - static GPBMessageEnumValueDescription values[] = { - { .name = "Compressable", .number = RMTPayloadType_Compressable }, - { .name = "Uncompressable", .number = RMTPayloadType_Uncompressable }, - { .name = "Random", .number = RMTPayloadType_Random }, - }; - descriptor = [GPBEnumDescriptor allocDescriptorForName:GPBNSStringifySymbol(RMTPayloadType) - values:values - valueCount:sizeof(values) / sizeof(GPBMessageEnumValueDescription) - enumVerifier:RMTPayloadType_IsValidValue]; - } - return descriptor; -} - -BOOL RMTPayloadType_IsValidValue(int32_t value__) { - switch (value__) { - case RMTPayloadType_Compressable: - case RMTPayloadType_Uncompressable: - case RMTPayloadType_Random: - return YES; - default: - return NO; - } -} - -#pragma mark - RMTPayload - -@implementation RMTPayload - -@dynamic type; -@dynamic body; - -typedef struct RMTPayload_Storage { - uint32_t _has_storage_[1]; - RMTPayloadType type; - NSData *body; -} RMTPayload_Storage; - -// This method is threadsafe because it is initially called -// in +initialize for each subclass. -+ (GPBDescriptor *)descriptor { - static GPBDescriptor *descriptor = NULL; - if (!descriptor) { - static GPBMessageFieldDescription fields[] = { - { - .name = "type", - .number = RMTPayload_FieldNumber_Type, - .hasIndex = 0, - .flags = GPBFieldOptional | GPBFieldHasEnumDescriptor, - .type = GPBTypeEnum, - .offset = offsetof(RMTPayload_Storage, type), - .defaultValue.valueEnum = RMTPayloadType_Compressable, - .typeSpecific.enumDescFunc = RMTPayloadType_EnumDescriptor, - .fieldOptions = NULL, - }, - { - .name = "body", - .number = RMTPayload_FieldNumber_Body, - .hasIndex = 1, - .flags = GPBFieldOptional, - .type = GPBTypeData, - .offset = offsetof(RMTPayload_Storage, body), - .defaultValue.valueData = nil, - .typeSpecific.className = NULL, - .fieldOptions = NULL, - }, - }; - descriptor = [GPBDescriptor allocDescriptorForClass:[RMTPayload class] - rootClass:[RMTMessagesRoot class] - file:RMTMessagesRoot_FileDescriptor() - fields:fields - fieldCount:sizeof(fields) / sizeof(GPBMessageFieldDescription) - oneofs:NULL - oneofCount:0 - enums:NULL - enumCount:0 - ranges:NULL - rangeCount:0 - storageSize:sizeof(RMTPayload_Storage) - wireFormat:NO]; - } - return descriptor; -} - -@end - -int32_t RMTPayload_Type_RawValue(RMTPayload *message) { - GPBDescriptor *descriptor = [RMTPayload descriptor]; - GPBFieldDescriptor *field = [descriptor fieldWithNumber:RMTPayload_FieldNumber_Type]; - return GPBGetInt32IvarWithField(message, field); -} - -void SetRMTPayload_Type_RawValue(RMTPayload *message, int32_t value) { - GPBDescriptor *descriptor = [RMTPayload descriptor]; - GPBFieldDescriptor *field = [descriptor fieldWithNumber:RMTPayload_FieldNumber_Type]; - GPBSetInt32IvarWithFieldInternal(message, field, value, descriptor.file.syntax); -} - -#pragma mark - RMTSimpleRequest - -@implementation RMTSimpleRequest - -@dynamic responseType; -@dynamic responseSize; -@dynamic hasPayload, payload; -@dynamic fillUsername; -@dynamic fillOauthScope; - -typedef struct RMTSimpleRequest_Storage { - uint32_t _has_storage_[1]; - BOOL fillUsername; - BOOL fillOauthScope; - RMTPayloadType responseType; - int32_t responseSize; - RMTPayload *payload; -} RMTSimpleRequest_Storage; - -// This method is threadsafe because it is initially called -// in +initialize for each subclass. -+ (GPBDescriptor *)descriptor { - static GPBDescriptor *descriptor = NULL; - if (!descriptor) { - static GPBMessageFieldDescription fields[] = { - { - .name = "responseType", - .number = RMTSimpleRequest_FieldNumber_ResponseType, - .hasIndex = 0, - .flags = GPBFieldOptional | GPBFieldHasEnumDescriptor, - .type = GPBTypeEnum, - .offset = offsetof(RMTSimpleRequest_Storage, responseType), - .defaultValue.valueEnum = RMTPayloadType_Compressable, - .typeSpecific.enumDescFunc = RMTPayloadType_EnumDescriptor, - .fieldOptions = NULL, - }, - { - .name = "responseSize", - .number = RMTSimpleRequest_FieldNumber_ResponseSize, - .hasIndex = 1, - .flags = GPBFieldOptional, - .type = GPBTypeInt32, - .offset = offsetof(RMTSimpleRequest_Storage, responseSize), - .defaultValue.valueInt32 = 0, - .typeSpecific.className = NULL, - .fieldOptions = NULL, - }, - { - .name = "payload", - .number = RMTSimpleRequest_FieldNumber_Payload, - .hasIndex = 2, - .flags = GPBFieldOptional, - .type = GPBTypeMessage, - .offset = offsetof(RMTSimpleRequest_Storage, payload), - .defaultValue.valueMessage = nil, - .typeSpecific.className = GPBStringifySymbol(RMTPayload), - .fieldOptions = NULL, - }, - { - .name = "fillUsername", - .number = RMTSimpleRequest_FieldNumber_FillUsername, - .hasIndex = 3, - .flags = GPBFieldOptional, - .type = GPBTypeBool, - .offset = offsetof(RMTSimpleRequest_Storage, fillUsername), - .defaultValue.valueBool = NO, - .typeSpecific.className = NULL, - .fieldOptions = NULL, - }, - { - .name = "fillOauthScope", - .number = RMTSimpleRequest_FieldNumber_FillOauthScope, - .hasIndex = 4, - .flags = GPBFieldOptional, - .type = GPBTypeBool, - .offset = offsetof(RMTSimpleRequest_Storage, fillOauthScope), - .defaultValue.valueBool = NO, - .typeSpecific.className = NULL, - .fieldOptions = NULL, - }, - }; - descriptor = [GPBDescriptor allocDescriptorForClass:[RMTSimpleRequest class] - rootClass:[RMTMessagesRoot class] - file:RMTMessagesRoot_FileDescriptor() - fields:fields - fieldCount:sizeof(fields) / sizeof(GPBMessageFieldDescription) - oneofs:NULL - oneofCount:0 - enums:NULL - enumCount:0 - ranges:NULL - rangeCount:0 - storageSize:sizeof(RMTSimpleRequest_Storage) - wireFormat:NO]; - } - return descriptor; -} - -@end - -int32_t RMTSimpleRequest_ResponseType_RawValue(RMTSimpleRequest *message) { - GPBDescriptor *descriptor = [RMTSimpleRequest descriptor]; - GPBFieldDescriptor *field = [descriptor fieldWithNumber:RMTSimpleRequest_FieldNumber_ResponseType]; - return GPBGetInt32IvarWithField(message, field); -} - -void SetRMTSimpleRequest_ResponseType_RawValue(RMTSimpleRequest *message, int32_t value) { - GPBDescriptor *descriptor = [RMTSimpleRequest descriptor]; - GPBFieldDescriptor *field = [descriptor fieldWithNumber:RMTSimpleRequest_FieldNumber_ResponseType]; - GPBSetInt32IvarWithFieldInternal(message, field, value, descriptor.file.syntax); -} - -#pragma mark - RMTSimpleResponse - -@implementation RMTSimpleResponse - -@dynamic hasPayload, payload; -@dynamic username; -@dynamic oauthScope; - -typedef struct RMTSimpleResponse_Storage { - uint32_t _has_storage_[1]; - RMTPayload *payload; - NSString *username; - NSString *oauthScope; -} RMTSimpleResponse_Storage; - -// This method is threadsafe because it is initially called -// in +initialize for each subclass. -+ (GPBDescriptor *)descriptor { - static GPBDescriptor *descriptor = NULL; - if (!descriptor) { - static GPBMessageFieldDescription fields[] = { - { - .name = "payload", - .number = RMTSimpleResponse_FieldNumber_Payload, - .hasIndex = 0, - .flags = GPBFieldOptional, - .type = GPBTypeMessage, - .offset = offsetof(RMTSimpleResponse_Storage, payload), - .defaultValue.valueMessage = nil, - .typeSpecific.className = GPBStringifySymbol(RMTPayload), - .fieldOptions = NULL, - }, - { - .name = "username", - .number = RMTSimpleResponse_FieldNumber_Username, - .hasIndex = 1, - .flags = GPBFieldOptional, - .type = GPBTypeString, - .offset = offsetof(RMTSimpleResponse_Storage, username), - .defaultValue.valueString = nil, - .typeSpecific.className = NULL, - .fieldOptions = NULL, - }, - { - .name = "oauthScope", - .number = RMTSimpleResponse_FieldNumber_OauthScope, - .hasIndex = 2, - .flags = GPBFieldOptional, - .type = GPBTypeString, - .offset = offsetof(RMTSimpleResponse_Storage, oauthScope), - .defaultValue.valueString = nil, - .typeSpecific.className = NULL, - .fieldOptions = NULL, - }, - }; - descriptor = [GPBDescriptor allocDescriptorForClass:[RMTSimpleResponse class] - rootClass:[RMTMessagesRoot class] - file:RMTMessagesRoot_FileDescriptor() - fields:fields - fieldCount:sizeof(fields) / sizeof(GPBMessageFieldDescription) - oneofs:NULL - oneofCount:0 - enums:NULL - enumCount:0 - ranges:NULL - rangeCount:0 - storageSize:sizeof(RMTSimpleResponse_Storage) - wireFormat:NO]; - } - return descriptor; -} - -@end - -#pragma mark - RMTStreamingInputCallRequest - -@implementation RMTStreamingInputCallRequest - -@dynamic hasPayload, payload; - -typedef struct RMTStreamingInputCallRequest_Storage { - uint32_t _has_storage_[1]; - RMTPayload *payload; -} RMTStreamingInputCallRequest_Storage; - -// This method is threadsafe because it is initially called -// in +initialize for each subclass. -+ (GPBDescriptor *)descriptor { - static GPBDescriptor *descriptor = NULL; - if (!descriptor) { - static GPBMessageFieldDescription fields[] = { - { - .name = "payload", - .number = RMTStreamingInputCallRequest_FieldNumber_Payload, - .hasIndex = 0, - .flags = GPBFieldOptional, - .type = GPBTypeMessage, - .offset = offsetof(RMTStreamingInputCallRequest_Storage, payload), - .defaultValue.valueMessage = nil, - .typeSpecific.className = GPBStringifySymbol(RMTPayload), - .fieldOptions = NULL, - }, - }; - descriptor = [GPBDescriptor allocDescriptorForClass:[RMTStreamingInputCallRequest class] - rootClass:[RMTMessagesRoot class] - file:RMTMessagesRoot_FileDescriptor() - fields:fields - fieldCount:sizeof(fields) / sizeof(GPBMessageFieldDescription) - oneofs:NULL - oneofCount:0 - enums:NULL - enumCount:0 - ranges:NULL - rangeCount:0 - storageSize:sizeof(RMTStreamingInputCallRequest_Storage) - wireFormat:NO]; - } - return descriptor; -} - -@end - -#pragma mark - RMTStreamingInputCallResponse - -@implementation RMTStreamingInputCallResponse - -@dynamic aggregatedPayloadSize; - -typedef struct RMTStreamingInputCallResponse_Storage { - uint32_t _has_storage_[1]; - int32_t aggregatedPayloadSize; -} RMTStreamingInputCallResponse_Storage; - -// This method is threadsafe because it is initially called -// in +initialize for each subclass. -+ (GPBDescriptor *)descriptor { - static GPBDescriptor *descriptor = NULL; - if (!descriptor) { - static GPBMessageFieldDescription fields[] = { - { - .name = "aggregatedPayloadSize", - .number = RMTStreamingInputCallResponse_FieldNumber_AggregatedPayloadSize, - .hasIndex = 0, - .flags = GPBFieldOptional, - .type = GPBTypeInt32, - .offset = offsetof(RMTStreamingInputCallResponse_Storage, aggregatedPayloadSize), - .defaultValue.valueInt32 = 0, - .typeSpecific.className = NULL, - .fieldOptions = NULL, - }, - }; - descriptor = [GPBDescriptor allocDescriptorForClass:[RMTStreamingInputCallResponse class] - rootClass:[RMTMessagesRoot class] - file:RMTMessagesRoot_FileDescriptor() - fields:fields - fieldCount:sizeof(fields) / sizeof(GPBMessageFieldDescription) - oneofs:NULL - oneofCount:0 - enums:NULL - enumCount:0 - ranges:NULL - rangeCount:0 - storageSize:sizeof(RMTStreamingInputCallResponse_Storage) - wireFormat:NO]; - } - return descriptor; -} - -@end - -#pragma mark - RMTResponseParameters - -@implementation RMTResponseParameters - -@dynamic size; -@dynamic intervalUs; - -typedef struct RMTResponseParameters_Storage { - uint32_t _has_storage_[1]; - int32_t size; - int32_t intervalUs; -} RMTResponseParameters_Storage; - -// This method is threadsafe because it is initially called -// in +initialize for each subclass. -+ (GPBDescriptor *)descriptor { - static GPBDescriptor *descriptor = NULL; - if (!descriptor) { - static GPBMessageFieldDescription fields[] = { - { - .name = "size", - .number = RMTResponseParameters_FieldNumber_Size, - .hasIndex = 0, - .flags = GPBFieldOptional, - .type = GPBTypeInt32, - .offset = offsetof(RMTResponseParameters_Storage, size), - .defaultValue.valueInt32 = 0, - .typeSpecific.className = NULL, - .fieldOptions = NULL, - }, - { - .name = "intervalUs", - .number = RMTResponseParameters_FieldNumber_IntervalUs, - .hasIndex = 1, - .flags = GPBFieldOptional, - .type = GPBTypeInt32, - .offset = offsetof(RMTResponseParameters_Storage, intervalUs), - .defaultValue.valueInt32 = 0, - .typeSpecific.className = NULL, - .fieldOptions = NULL, - }, - }; - descriptor = [GPBDescriptor allocDescriptorForClass:[RMTResponseParameters class] - rootClass:[RMTMessagesRoot class] - file:RMTMessagesRoot_FileDescriptor() - fields:fields - fieldCount:sizeof(fields) / sizeof(GPBMessageFieldDescription) - oneofs:NULL - oneofCount:0 - enums:NULL - enumCount:0 - ranges:NULL - rangeCount:0 - storageSize:sizeof(RMTResponseParameters_Storage) - wireFormat:NO]; - } - return descriptor; -} - -@end - -#pragma mark - RMTStreamingOutputCallRequest - -@implementation RMTStreamingOutputCallRequest - -@dynamic responseType; -@dynamic responseParametersArray; -@dynamic hasPayload, payload; - -typedef struct RMTStreamingOutputCallRequest_Storage { - uint32_t _has_storage_[1]; - RMTPayloadType responseType; - NSMutableArray *responseParametersArray; - RMTPayload *payload; -} RMTStreamingOutputCallRequest_Storage; - -// This method is threadsafe because it is initially called -// in +initialize for each subclass. -+ (GPBDescriptor *)descriptor { - static GPBDescriptor *descriptor = NULL; - if (!descriptor) { - static GPBMessageFieldDescription fields[] = { - { - .name = "responseType", - .number = RMTStreamingOutputCallRequest_FieldNumber_ResponseType, - .hasIndex = 0, - .flags = GPBFieldOptional | GPBFieldHasEnumDescriptor, - .type = GPBTypeEnum, - .offset = offsetof(RMTStreamingOutputCallRequest_Storage, responseType), - .defaultValue.valueEnum = RMTPayloadType_Compressable, - .typeSpecific.enumDescFunc = RMTPayloadType_EnumDescriptor, - .fieldOptions = NULL, - }, - { - .name = "responseParametersArray", - .number = RMTStreamingOutputCallRequest_FieldNumber_ResponseParametersArray, - .hasIndex = GPBNoHasBit, - .flags = GPBFieldRepeated, - .type = GPBTypeMessage, - .offset = offsetof(RMTStreamingOutputCallRequest_Storage, responseParametersArray), - .defaultValue.valueMessage = nil, - .typeSpecific.className = GPBStringifySymbol(RMTResponseParameters), - .fieldOptions = NULL, - }, - { - .name = "payload", - .number = RMTStreamingOutputCallRequest_FieldNumber_Payload, - .hasIndex = 2, - .flags = GPBFieldOptional, - .type = GPBTypeMessage, - .offset = offsetof(RMTStreamingOutputCallRequest_Storage, payload), - .defaultValue.valueMessage = nil, - .typeSpecific.className = GPBStringifySymbol(RMTPayload), - .fieldOptions = NULL, - }, - }; - descriptor = [GPBDescriptor allocDescriptorForClass:[RMTStreamingOutputCallRequest class] - rootClass:[RMTMessagesRoot class] - file:RMTMessagesRoot_FileDescriptor() - fields:fields - fieldCount:sizeof(fields) / sizeof(GPBMessageFieldDescription) - oneofs:NULL - oneofCount:0 - enums:NULL - enumCount:0 - ranges:NULL - rangeCount:0 - storageSize:sizeof(RMTStreamingOutputCallRequest_Storage) - wireFormat:NO]; - } - return descriptor; -} - -@end - -int32_t RMTStreamingOutputCallRequest_ResponseType_RawValue(RMTStreamingOutputCallRequest *message) { - GPBDescriptor *descriptor = [RMTStreamingOutputCallRequest descriptor]; - GPBFieldDescriptor *field = [descriptor fieldWithNumber:RMTStreamingOutputCallRequest_FieldNumber_ResponseType]; - return GPBGetInt32IvarWithField(message, field); -} - -void SetRMTStreamingOutputCallRequest_ResponseType_RawValue(RMTStreamingOutputCallRequest *message, int32_t value) { - GPBDescriptor *descriptor = [RMTStreamingOutputCallRequest descriptor]; - GPBFieldDescriptor *field = [descriptor fieldWithNumber:RMTStreamingOutputCallRequest_FieldNumber_ResponseType]; - GPBSetInt32IvarWithFieldInternal(message, field, value, descriptor.file.syntax); -} - -#pragma mark - RMTStreamingOutputCallResponse - -@implementation RMTStreamingOutputCallResponse - -@dynamic hasPayload, payload; - -typedef struct RMTStreamingOutputCallResponse_Storage { - uint32_t _has_storage_[1]; - RMTPayload *payload; -} RMTStreamingOutputCallResponse_Storage; - -// This method is threadsafe because it is initially called -// in +initialize for each subclass. -+ (GPBDescriptor *)descriptor { - static GPBDescriptor *descriptor = NULL; - if (!descriptor) { - static GPBMessageFieldDescription fields[] = { - { - .name = "payload", - .number = RMTStreamingOutputCallResponse_FieldNumber_Payload, - .hasIndex = 0, - .flags = GPBFieldOptional, - .type = GPBTypeMessage, - .offset = offsetof(RMTStreamingOutputCallResponse_Storage, payload), - .defaultValue.valueMessage = nil, - .typeSpecific.className = GPBStringifySymbol(RMTPayload), - .fieldOptions = NULL, - }, - }; - descriptor = [GPBDescriptor allocDescriptorForClass:[RMTStreamingOutputCallResponse class] - rootClass:[RMTMessagesRoot class] - file:RMTMessagesRoot_FileDescriptor() - fields:fields - fieldCount:sizeof(fields) / sizeof(GPBMessageFieldDescription) - oneofs:NULL - oneofCount:0 - enums:NULL - enumCount:0 - ranges:NULL - rangeCount:0 - storageSize:sizeof(RMTStreamingOutputCallResponse_Storage) - wireFormat:NO]; - } - return descriptor; -} - -@end - diff --git a/src/objective-c/generated_libraries/RemoteTestClient/RemoteTest.podspec b/src/objective-c/generated_libraries/RemoteTestClient/RemoteTest.podspec index 36c2b509e8..dd0dab352d 100644 --- a/src/objective-c/generated_libraries/RemoteTestClient/RemoteTest.podspec +++ b/src/objective-c/generated_libraries/RemoteTestClient/RemoteTest.podspec @@ -1,24 +1,26 @@ Pod::Spec.new do |s| - s.name = 'RemoteTest' - s.version = '0.0.1' - s.summary = 'Protobuf library generated from test.proto, messages.proto, and empty.proto' - s.homepage = 'https://github.com/grpc/grpc/tree/master/src/objective-c/examples/Sample/RemoteTestClient' - s.license = 'New BSD' - s.authors = { 'Jorge Canizales' => 'jcanizales@google.com' } + s.name = "RemoteTest" + s.version = "0.0.1" + s.license = "New BSD" - s.ios.deployment_target = '6.0' - s.osx.deployment_target = '10.8' + s.ios.deployment_target = "6.0" + s.osx.deployment_target = "10.8" - s.subspec 'Messages' do |ms| - ms.source_files = '*.pbobjc.{h,m}' + # Run protoc with the Objective-C and gRPC plugins to generate protocol messages and gRPC clients. + s.prepare_command = "protoc --objc_out=. --objcgrpc_out=. *.proto" + + s.subspec "Messages" do |ms| + ms.source_files = "*.pbobjc.{h,m}" + ms.header_mappings_dir = "." ms.requires_arc = false - ms.dependency 'Protobuf', '~> 3.0' + ms.dependency "Protobuf", "~> 3.0.0-alpha-3" end - s.subspec 'Services' do |ss| - ss.source_files = '*.pbrpc.{h,m}' + s.subspec "Services" do |ss| + ss.source_files = "*.pbrpc.{h,m}" + ss.header_mappings_dir = "." ss.requires_arc = true - ss.dependency 'gRPC', '~> 0.0' - ss.dependency 'RemoteTest/Messages' + ss.dependency "gRPC", "~> 0.5" + ss.dependency "#{s.name}/Messages" end end diff --git a/src/objective-c/generated_libraries/RemoteTestClient/Test.pbobjc.h b/src/objective-c/generated_libraries/RemoteTestClient/Test.pbobjc.h deleted file mode 100644 index aaa0b6ce2d..0000000000 --- a/src/objective-c/generated_libraries/RemoteTestClient/Test.pbobjc.h +++ /dev/null @@ -1,12 +0,0 @@ -// Generated by the protocol buffer compiler. DO NOT EDIT! -// source: test.proto - -#import "GPBProtocolBuffers.h" - -#if GOOGLE_PROTOBUF_OBJC_GEN_VERSION != 30000 -#error This file was generated by a different version of protoc-gen-objc which is incompatible with your Protocol Buffer sources. -#endif - -CF_EXTERN_C_BEGIN - -CF_EXTERN_C_END diff --git a/src/objective-c/generated_libraries/RemoteTestClient/Test.pbobjc.m b/src/objective-c/generated_libraries/RemoteTestClient/Test.pbobjc.m deleted file mode 100644 index 08562b828a..0000000000 --- a/src/objective-c/generated_libraries/RemoteTestClient/Test.pbobjc.m +++ /dev/null @@ -1,9 +0,0 @@ -// Generated by the protocol buffer compiler. DO NOT EDIT! -// source: test.proto - -// File empty because all messages, extensions and enum have been filtered. - - -// Dummy symbol that will be stripped but will avoid linker warnings about -// no symbols in the .o form compiling this file. -static int RMTTestRoot_dummy __attribute__((unused,used)) = 0; diff --git a/src/objective-c/generated_libraries/RemoteTestClient/Test.pbrpc.h b/src/objective-c/generated_libraries/RemoteTestClient/Test.pbrpc.h deleted file mode 100644 index f799c4fc92..0000000000 --- a/src/objective-c/generated_libraries/RemoteTestClient/Test.pbrpc.h +++ /dev/null @@ -1,59 +0,0 @@ -#import "Test.pbobjc.h" -#import <gRPC/ProtoService.h> - -#import "Empty.pbobjc.h" -#import "Messages.pbobjc.h" - -@protocol GRXWriteable; -@protocol GRXWriter; - -@protocol RMTTestService <NSObject> - -#pragma mark EmptyCall(Empty) returns (Empty) - -- (void)emptyCallWithRequest:(RMTEmpty *)request handler:(void(^)(RMTEmpty *response, NSError *error))handler; - -- (ProtoRPC *)RPCToEmptyCallWithRequest:(RMTEmpty *)request handler:(void(^)(RMTEmpty *response, NSError *error))handler; - - -#pragma mark UnaryCall(SimpleRequest) returns (SimpleResponse) - -- (void)unaryCallWithRequest:(RMTSimpleRequest *)request handler:(void(^)(RMTSimpleResponse *response, NSError *error))handler; - -- (ProtoRPC *)RPCToUnaryCallWithRequest:(RMTSimpleRequest *)request handler:(void(^)(RMTSimpleResponse *response, NSError *error))handler; - - -#pragma mark StreamingOutputCall(StreamingOutputCallRequest) returns (stream StreamingOutputCallResponse) - -- (void)streamingOutputCallWithRequest:(RMTStreamingOutputCallRequest *)request handler:(void(^)(BOOL done, RMTStreamingOutputCallResponse *response, NSError *error))handler; - -- (ProtoRPC *)RPCToStreamingOutputCallWithRequest:(RMTStreamingOutputCallRequest *)request handler:(void(^)(BOOL done, RMTStreamingOutputCallResponse *response, NSError *error))handler; - - -#pragma mark StreamingInputCall(stream StreamingInputCallRequest) returns (StreamingInputCallResponse) - -- (void)streamingInputCallWithRequestsWriter:(id<GRXWriter>)request handler:(void(^)(RMTStreamingInputCallResponse *response, NSError *error))handler; - -- (ProtoRPC *)RPCToStreamingInputCallWithRequestsWriter:(id<GRXWriter>)request handler:(void(^)(RMTStreamingInputCallResponse *response, NSError *error))handler; - - -#pragma mark FullDuplexCall(stream StreamingOutputCallRequest) returns (stream StreamingOutputCallResponse) - -- (void)fullDuplexCallWithRequestsWriter:(id<GRXWriter>)request handler:(void(^)(BOOL done, RMTStreamingOutputCallResponse *response, NSError *error))handler; - -- (ProtoRPC *)RPCToFullDuplexCallWithRequestsWriter:(id<GRXWriter>)request handler:(void(^)(BOOL done, RMTStreamingOutputCallResponse *response, NSError *error))handler; - - -#pragma mark HalfDuplexCall(stream StreamingOutputCallRequest) returns (stream StreamingOutputCallResponse) - -- (void)halfDuplexCallWithRequestsWriter:(id<GRXWriter>)request handler:(void(^)(BOOL done, RMTStreamingOutputCallResponse *response, NSError *error))handler; - -- (ProtoRPC *)RPCToHalfDuplexCallWithRequestsWriter:(id<GRXWriter>)request handler:(void(^)(BOOL done, RMTStreamingOutputCallResponse *response, NSError *error))handler; - - -@end - -// Basic service implementation, over gRPC, that only does marshalling and parsing. -@interface RMTTestService : ProtoService<RMTTestService> -- (instancetype)initWithHost:(NSString *)host NS_DESIGNATED_INITIALIZER; -@end diff --git a/src/objective-c/generated_libraries/RemoteTestClient/Test.pbrpc.m b/src/objective-c/generated_libraries/RemoteTestClient/Test.pbrpc.m deleted file mode 100644 index faba07769e..0000000000 --- a/src/objective-c/generated_libraries/RemoteTestClient/Test.pbrpc.m +++ /dev/null @@ -1,96 +0,0 @@ -#import "Test.pbrpc.h" -#import <gRPC/GRXWriteable.h> -#import <gRPC/GRXWriter+Immediate.h> -#import <gRPC/ProtoRPC.h> - -static NSString *const kPackageName = @"grpc.testing"; -static NSString *const kServiceName = @"TestService"; - -@implementation RMTTestService - -// Designated initializer -- (instancetype)initWithHost:(NSString *)host { - return (self = [super initWithHost:host packageName:kPackageName serviceName:kServiceName]); -} - -// Override superclass initializer to disallow different package and service names. -- (instancetype)initWithHost:(NSString *)host - packageName:(NSString *)packageName - serviceName:(NSString *)serviceName { - return [self initWithHost:host]; -} - - -#pragma mark EmptyCall(Empty) returns (Empty) - -- (void)emptyCallWithRequest:(RMTEmpty *)request handler:(void(^)(RMTEmpty *response, NSError *error))handler{ - [[self RPCToEmptyCallWithRequest:request handler:handler] start]; -} -// Returns a not-yet-started RPC object. -- (ProtoRPC *)RPCToEmptyCallWithRequest:(RMTEmpty *)request handler:(void(^)(RMTEmpty *response, NSError *error))handler{ - return [self RPCToMethod:@"EmptyCall" - requestsWriter:[GRXWriter writerWithValue:request] - responseClass:[RMTEmpty class] - responsesWriteable:[GRXWriteable writeableWithSingleValueHandler:handler]]; -} -#pragma mark UnaryCall(SimpleRequest) returns (SimpleResponse) - -- (void)unaryCallWithRequest:(RMTSimpleRequest *)request handler:(void(^)(RMTSimpleResponse *response, NSError *error))handler{ - [[self RPCToUnaryCallWithRequest:request handler:handler] start]; -} -// Returns a not-yet-started RPC object. -- (ProtoRPC *)RPCToUnaryCallWithRequest:(RMTSimpleRequest *)request handler:(void(^)(RMTSimpleResponse *response, NSError *error))handler{ - return [self RPCToMethod:@"UnaryCall" - requestsWriter:[GRXWriter writerWithValue:request] - responseClass:[RMTSimpleResponse class] - responsesWriteable:[GRXWriteable writeableWithSingleValueHandler:handler]]; -} -#pragma mark StreamingOutputCall(StreamingOutputCallRequest) returns (stream StreamingOutputCallResponse) - -- (void)streamingOutputCallWithRequest:(RMTStreamingOutputCallRequest *)request handler:(void(^)(BOOL done, RMTStreamingOutputCallResponse *response, NSError *error))handler{ - [[self RPCToStreamingOutputCallWithRequest:request handler:handler] start]; -} -// Returns a not-yet-started RPC object. -- (ProtoRPC *)RPCToStreamingOutputCallWithRequest:(RMTStreamingOutputCallRequest *)request handler:(void(^)(BOOL done, RMTStreamingOutputCallResponse *response, NSError *error))handler{ - return [self RPCToMethod:@"StreamingOutputCall" - requestsWriter:[GRXWriter writerWithValue:request] - responseClass:[RMTStreamingOutputCallResponse class] - responsesWriteable:[GRXWriteable writeableWithStreamHandler:handler]]; -} -#pragma mark StreamingInputCall(stream StreamingInputCallRequest) returns (StreamingInputCallResponse) - -- (void)streamingInputCallWithRequestsWriter:(id<GRXWriter>)request handler:(void(^)(RMTStreamingInputCallResponse *response, NSError *error))handler{ - [[self RPCToStreamingInputCallWithRequestsWriter:request handler:handler] start]; -} -// Returns a not-yet-started RPC object. -- (ProtoRPC *)RPCToStreamingInputCallWithRequestsWriter:(id<GRXWriter>)request handler:(void(^)(RMTStreamingInputCallResponse *response, NSError *error))handler{ - return [self RPCToMethod:@"StreamingInputCall" - requestsWriter:request - responseClass:[RMTStreamingInputCallResponse class] - responsesWriteable:[GRXWriteable writeableWithSingleValueHandler:handler]]; -} -#pragma mark FullDuplexCall(stream StreamingOutputCallRequest) returns (stream StreamingOutputCallResponse) - -- (void)fullDuplexCallWithRequestsWriter:(id<GRXWriter>)request handler:(void(^)(BOOL done, RMTStreamingOutputCallResponse *response, NSError *error))handler{ - [[self RPCToFullDuplexCallWithRequestsWriter:request handler:handler] start]; -} -// Returns a not-yet-started RPC object. -- (ProtoRPC *)RPCToFullDuplexCallWithRequestsWriter:(id<GRXWriter>)request handler:(void(^)(BOOL done, RMTStreamingOutputCallResponse *response, NSError *error))handler{ - return [self RPCToMethod:@"FullDuplexCall" - requestsWriter:request - responseClass:[RMTStreamingOutputCallResponse class] - responsesWriteable:[GRXWriteable writeableWithStreamHandler:handler]]; -} -#pragma mark HalfDuplexCall(stream StreamingOutputCallRequest) returns (stream StreamingOutputCallResponse) - -- (void)halfDuplexCallWithRequestsWriter:(id<GRXWriter>)request handler:(void(^)(BOOL done, RMTStreamingOutputCallResponse *response, NSError *error))handler{ - [[self RPCToHalfDuplexCallWithRequestsWriter:request handler:handler] start]; -} -// Returns a not-yet-started RPC object. -- (ProtoRPC *)RPCToHalfDuplexCallWithRequestsWriter:(id<GRXWriter>)request handler:(void(^)(BOOL done, RMTStreamingOutputCallResponse *response, NSError *error))handler{ - return [self RPCToMethod:@"HalfDuplexCall" - requestsWriter:request - responseClass:[RMTStreamingOutputCallResponse class] - responsesWriteable:[GRXWriteable writeableWithStreamHandler:handler]]; -} -@end diff --git a/src/objective-c/generated_libraries/RouteGuideClient/RouteGuide.pbobjc.h b/src/objective-c/generated_libraries/RouteGuideClient/RouteGuide.pbobjc.h deleted file mode 100644 index 6efaec7f02..0000000000 --- a/src/objective-c/generated_libraries/RouteGuideClient/RouteGuide.pbobjc.h +++ /dev/null @@ -1,134 +0,0 @@ -// Generated by the protocol buffer compiler. DO NOT EDIT! -// source: route_guide.proto - -#import "GPBProtocolBuffers.h" - -#if GOOGLE_PROTOBUF_OBJC_GEN_VERSION != 30000 -#error This file was generated by a different version of protoc-gen-objc which is incompatible with your Protocol Buffer sources. -#endif - -CF_EXTERN_C_BEGIN - -@class RGDFeature; -@class RGDPoint; -@class RGDRectangle; -@class RGDRouteNote; -@class RGDRouteSummary; - - -#pragma mark - RGDRouteGuideRoot - -@interface RGDRouteGuideRoot : GPBRootObject -@end - -#pragma mark - RGDPoint - -typedef GPB_ENUM(RGDPoint_FieldNumber) { - RGDPoint_FieldNumber_Latitude = 1, - RGDPoint_FieldNumber_Longitude = 2, -}; - -// Points are represented as latitude-longitude pairs in the E7 representation -// (degrees multiplied by 10**7 and rounded to the nearest integer). -// Latitudes should be in the range +/- 90 degrees and longitude should be in -// the range +/- 180 degrees (inclusive). -@interface RGDPoint : GPBMessage - -@property(nonatomic, readwrite) int32_t latitude; - -@property(nonatomic, readwrite) int32_t longitude; - -@end - -#pragma mark - RGDRectangle - -typedef GPB_ENUM(RGDRectangle_FieldNumber) { - RGDRectangle_FieldNumber_Lo = 1, - RGDRectangle_FieldNumber_Hi = 2, -}; - -// A latitude-longitude rectangle, represented as two diagonally opposite -// points "lo" and "hi". -@interface RGDRectangle : GPBMessage - -// One corner of the rectangle. -@property(nonatomic, readwrite) BOOL hasLo; -@property(nonatomic, readwrite, strong) RGDPoint *lo; - -// The other corner of the rectangle. -@property(nonatomic, readwrite) BOOL hasHi; -@property(nonatomic, readwrite, strong) RGDPoint *hi; - -@end - -#pragma mark - RGDFeature - -typedef GPB_ENUM(RGDFeature_FieldNumber) { - RGDFeature_FieldNumber_Name = 1, - RGDFeature_FieldNumber_Location = 2, -}; - -// A feature names something at a given point. -// -// If a feature could not be named, the name is empty. -@interface RGDFeature : GPBMessage - -// The name of the feature. -@property(nonatomic, readwrite, copy) NSString *name; - -// The point where the feature is detected. -@property(nonatomic, readwrite) BOOL hasLocation; -@property(nonatomic, readwrite, strong) RGDPoint *location; - -@end - -#pragma mark - RGDRouteNote - -typedef GPB_ENUM(RGDRouteNote_FieldNumber) { - RGDRouteNote_FieldNumber_Location = 1, - RGDRouteNote_FieldNumber_Message = 2, -}; - -// A RouteNote is a message sent while at a given point. -@interface RGDRouteNote : GPBMessage - -// The location from which the message is sent. -@property(nonatomic, readwrite) BOOL hasLocation; -@property(nonatomic, readwrite, strong) RGDPoint *location; - -// The message to be sent. -@property(nonatomic, readwrite, copy) NSString *message; - -@end - -#pragma mark - RGDRouteSummary - -typedef GPB_ENUM(RGDRouteSummary_FieldNumber) { - RGDRouteSummary_FieldNumber_PointCount = 1, - RGDRouteSummary_FieldNumber_FeatureCount = 2, - RGDRouteSummary_FieldNumber_Distance = 3, - RGDRouteSummary_FieldNumber_ElapsedTime = 4, -}; - -// A RouteSummary is received in response to a RecordRoute rpc. -// -// It contains the number of individual points received, the number of -// detected features, and the total distance covered as the cumulative sum of -// the distance between each point. -@interface RGDRouteSummary : GPBMessage - -// The number of points received. -@property(nonatomic, readwrite) int32_t pointCount; - -// The number of known features passed while traversing the route. -@property(nonatomic, readwrite) int32_t featureCount; - -// The distance covered in metres. -@property(nonatomic, readwrite) int32_t distance; - -// The duration of the traversal in seconds. -@property(nonatomic, readwrite) int32_t elapsedTime; - -@end - -CF_EXTERN_C_END diff --git a/src/objective-c/generated_libraries/RouteGuideClient/RouteGuide.pbobjc.m b/src/objective-c/generated_libraries/RouteGuideClient/RouteGuide.pbobjc.m deleted file mode 100644 index 16f291a61c..0000000000 --- a/src/objective-c/generated_libraries/RouteGuideClient/RouteGuide.pbobjc.m +++ /dev/null @@ -1,355 +0,0 @@ -// Generated by the protocol buffer compiler. DO NOT EDIT! -// source: route_guide.proto - -#import "GPBProtocolBuffers_RuntimeSupport.h" - -#import "RouteGuide.pbobjc.h" - -#pragma mark - RGDRouteGuideRoot - -@implementation RGDRouteGuideRoot - -@end - -static GPBFileDescriptor *RGDRouteGuideRoot_FileDescriptor(void) { - // This is called by +initialize so there is no need to worry - // about thread safety of the singleton. - static GPBFileDescriptor *descriptor = NULL; - if (!descriptor) { - descriptor = [[GPBFileDescriptor alloc] initWithPackage:@"grpc.example.routeguide" - syntax:GPBFileSyntaxProto3]; - } - return descriptor; -} - -#pragma mark - RGDPoint - -@implementation RGDPoint - -@dynamic latitude; -@dynamic longitude; - -typedef struct RGDPoint_Storage { - uint32_t _has_storage_[1]; - int32_t latitude; - int32_t longitude; -} RGDPoint_Storage; - -// This method is threadsafe because it is initially called -// in +initialize for each subclass. -+ (GPBDescriptor *)descriptor { - static GPBDescriptor *descriptor = NULL; - if (!descriptor) { - static GPBMessageFieldDescription fields[] = { - { - .name = "latitude", - .number = RGDPoint_FieldNumber_Latitude, - .hasIndex = 0, - .flags = GPBFieldOptional, - .type = GPBTypeInt32, - .offset = offsetof(RGDPoint_Storage, latitude), - .defaultValue.valueInt32 = 0, - .typeSpecific.className = NULL, - .fieldOptions = NULL, - }, - { - .name = "longitude", - .number = RGDPoint_FieldNumber_Longitude, - .hasIndex = 1, - .flags = GPBFieldOptional, - .type = GPBTypeInt32, - .offset = offsetof(RGDPoint_Storage, longitude), - .defaultValue.valueInt32 = 0, - .typeSpecific.className = NULL, - .fieldOptions = NULL, - }, - }; - descriptor = [GPBDescriptor allocDescriptorForClass:[RGDPoint class] - rootClass:[RGDRouteGuideRoot class] - file:RGDRouteGuideRoot_FileDescriptor() - fields:fields - fieldCount:sizeof(fields) / sizeof(GPBMessageFieldDescription) - oneofs:NULL - oneofCount:0 - enums:NULL - enumCount:0 - ranges:NULL - rangeCount:0 - storageSize:sizeof(RGDPoint_Storage) - wireFormat:NO]; - } - return descriptor; -} - -@end - -#pragma mark - RGDRectangle - -@implementation RGDRectangle - -@dynamic hasLo, lo; -@dynamic hasHi, hi; - -typedef struct RGDRectangle_Storage { - uint32_t _has_storage_[1]; - RGDPoint *lo; - RGDPoint *hi; -} RGDRectangle_Storage; - -// This method is threadsafe because it is initially called -// in +initialize for each subclass. -+ (GPBDescriptor *)descriptor { - static GPBDescriptor *descriptor = NULL; - if (!descriptor) { - static GPBMessageFieldDescription fields[] = { - { - .name = "lo", - .number = RGDRectangle_FieldNumber_Lo, - .hasIndex = 0, - .flags = GPBFieldOptional, - .type = GPBTypeMessage, - .offset = offsetof(RGDRectangle_Storage, lo), - .defaultValue.valueMessage = nil, - .typeSpecific.className = GPBStringifySymbol(RGDPoint), - .fieldOptions = NULL, - }, - { - .name = "hi", - .number = RGDRectangle_FieldNumber_Hi, - .hasIndex = 1, - .flags = GPBFieldOptional, - .type = GPBTypeMessage, - .offset = offsetof(RGDRectangle_Storage, hi), - .defaultValue.valueMessage = nil, - .typeSpecific.className = GPBStringifySymbol(RGDPoint), - .fieldOptions = NULL, - }, - }; - descriptor = [GPBDescriptor allocDescriptorForClass:[RGDRectangle class] - rootClass:[RGDRouteGuideRoot class] - file:RGDRouteGuideRoot_FileDescriptor() - fields:fields - fieldCount:sizeof(fields) / sizeof(GPBMessageFieldDescription) - oneofs:NULL - oneofCount:0 - enums:NULL - enumCount:0 - ranges:NULL - rangeCount:0 - storageSize:sizeof(RGDRectangle_Storage) - wireFormat:NO]; - } - return descriptor; -} - -@end - -#pragma mark - RGDFeature - -@implementation RGDFeature - -@dynamic name; -@dynamic hasLocation, location; - -typedef struct RGDFeature_Storage { - uint32_t _has_storage_[1]; - NSString *name; - RGDPoint *location; -} RGDFeature_Storage; - -// This method is threadsafe because it is initially called -// in +initialize for each subclass. -+ (GPBDescriptor *)descriptor { - static GPBDescriptor *descriptor = NULL; - if (!descriptor) { - static GPBMessageFieldDescription fields[] = { - { - .name = "name", - .number = RGDFeature_FieldNumber_Name, - .hasIndex = 0, - .flags = GPBFieldOptional, - .type = GPBTypeString, - .offset = offsetof(RGDFeature_Storage, name), - .defaultValue.valueString = nil, - .typeSpecific.className = NULL, - .fieldOptions = NULL, - }, - { - .name = "location", - .number = RGDFeature_FieldNumber_Location, - .hasIndex = 1, - .flags = GPBFieldOptional, - .type = GPBTypeMessage, - .offset = offsetof(RGDFeature_Storage, location), - .defaultValue.valueMessage = nil, - .typeSpecific.className = GPBStringifySymbol(RGDPoint), - .fieldOptions = NULL, - }, - }; - descriptor = [GPBDescriptor allocDescriptorForClass:[RGDFeature class] - rootClass:[RGDRouteGuideRoot class] - file:RGDRouteGuideRoot_FileDescriptor() - fields:fields - fieldCount:sizeof(fields) / sizeof(GPBMessageFieldDescription) - oneofs:NULL - oneofCount:0 - enums:NULL - enumCount:0 - ranges:NULL - rangeCount:0 - storageSize:sizeof(RGDFeature_Storage) - wireFormat:NO]; - } - return descriptor; -} - -@end - -#pragma mark - RGDRouteNote - -@implementation RGDRouteNote - -@dynamic hasLocation, location; -@dynamic message; - -typedef struct RGDRouteNote_Storage { - uint32_t _has_storage_[1]; - RGDPoint *location; - NSString *message; -} RGDRouteNote_Storage; - -// This method is threadsafe because it is initially called -// in +initialize for each subclass. -+ (GPBDescriptor *)descriptor { - static GPBDescriptor *descriptor = NULL; - if (!descriptor) { - static GPBMessageFieldDescription fields[] = { - { - .name = "location", - .number = RGDRouteNote_FieldNumber_Location, - .hasIndex = 0, - .flags = GPBFieldOptional, - .type = GPBTypeMessage, - .offset = offsetof(RGDRouteNote_Storage, location), - .defaultValue.valueMessage = nil, - .typeSpecific.className = GPBStringifySymbol(RGDPoint), - .fieldOptions = NULL, - }, - { - .name = "message", - .number = RGDRouteNote_FieldNumber_Message, - .hasIndex = 1, - .flags = GPBFieldOptional, - .type = GPBTypeString, - .offset = offsetof(RGDRouteNote_Storage, message), - .defaultValue.valueString = nil, - .typeSpecific.className = NULL, - .fieldOptions = NULL, - }, - }; - descriptor = [GPBDescriptor allocDescriptorForClass:[RGDRouteNote class] - rootClass:[RGDRouteGuideRoot class] - file:RGDRouteGuideRoot_FileDescriptor() - fields:fields - fieldCount:sizeof(fields) / sizeof(GPBMessageFieldDescription) - oneofs:NULL - oneofCount:0 - enums:NULL - enumCount:0 - ranges:NULL - rangeCount:0 - storageSize:sizeof(RGDRouteNote_Storage) - wireFormat:NO]; - } - return descriptor; -} - -@end - -#pragma mark - RGDRouteSummary - -@implementation RGDRouteSummary - -@dynamic pointCount; -@dynamic featureCount; -@dynamic distance; -@dynamic elapsedTime; - -typedef struct RGDRouteSummary_Storage { - uint32_t _has_storage_[1]; - int32_t pointCount; - int32_t featureCount; - int32_t distance; - int32_t elapsedTime; -} RGDRouteSummary_Storage; - -// This method is threadsafe because it is initially called -// in +initialize for each subclass. -+ (GPBDescriptor *)descriptor { - static GPBDescriptor *descriptor = NULL; - if (!descriptor) { - static GPBMessageFieldDescription fields[] = { - { - .name = "pointCount", - .number = RGDRouteSummary_FieldNumber_PointCount, - .hasIndex = 0, - .flags = GPBFieldOptional, - .type = GPBTypeInt32, - .offset = offsetof(RGDRouteSummary_Storage, pointCount), - .defaultValue.valueInt32 = 0, - .typeSpecific.className = NULL, - .fieldOptions = NULL, - }, - { - .name = "featureCount", - .number = RGDRouteSummary_FieldNumber_FeatureCount, - .hasIndex = 1, - .flags = GPBFieldOptional, - .type = GPBTypeInt32, - .offset = offsetof(RGDRouteSummary_Storage, featureCount), - .defaultValue.valueInt32 = 0, - .typeSpecific.className = NULL, - .fieldOptions = NULL, - }, - { - .name = "distance", - .number = RGDRouteSummary_FieldNumber_Distance, - .hasIndex = 2, - .flags = GPBFieldOptional, - .type = GPBTypeInt32, - .offset = offsetof(RGDRouteSummary_Storage, distance), - .defaultValue.valueInt32 = 0, - .typeSpecific.className = NULL, - .fieldOptions = NULL, - }, - { - .name = "elapsedTime", - .number = RGDRouteSummary_FieldNumber_ElapsedTime, - .hasIndex = 3, - .flags = GPBFieldOptional, - .type = GPBTypeInt32, - .offset = offsetof(RGDRouteSummary_Storage, elapsedTime), - .defaultValue.valueInt32 = 0, - .typeSpecific.className = NULL, - .fieldOptions = NULL, - }, - }; - descriptor = [GPBDescriptor allocDescriptorForClass:[RGDRouteSummary class] - rootClass:[RGDRouteGuideRoot class] - file:RGDRouteGuideRoot_FileDescriptor() - fields:fields - fieldCount:sizeof(fields) / sizeof(GPBMessageFieldDescription) - oneofs:NULL - oneofCount:0 - enums:NULL - enumCount:0 - ranges:NULL - rangeCount:0 - storageSize:sizeof(RGDRouteSummary_Storage) - wireFormat:NO]; - } - return descriptor; -} - -@end - diff --git a/src/objective-c/generated_libraries/RouteGuideClient/RouteGuide.pbrpc.h b/src/objective-c/generated_libraries/RouteGuideClient/RouteGuide.pbrpc.h deleted file mode 100644 index 9e60865d98..0000000000 --- a/src/objective-c/generated_libraries/RouteGuideClient/RouteGuide.pbrpc.h +++ /dev/null @@ -1,43 +0,0 @@ -#import "RouteGuide.pbobjc.h" -#import <gRPC/ProtoService.h> - - -@protocol GRXWriteable; -@protocol GRXWriter; - -@protocol RGDRouteGuide <NSObject> - -#pragma mark GetFeature(Point) returns (Feature) - -- (void)getFeatureWithRequest:(RGDPoint *)request handler:(void(^)(RGDFeature *response, NSError *error))handler; - -- (ProtoRPC *)RPCToGetFeatureWithRequest:(RGDPoint *)request handler:(void(^)(RGDFeature *response, NSError *error))handler; - - -#pragma mark ListFeatures(Rectangle) returns (stream Feature) - -- (void)listFeaturesWithRequest:(RGDRectangle *)request handler:(void(^)(BOOL done, RGDFeature *response, NSError *error))handler; - -- (ProtoRPC *)RPCToListFeaturesWithRequest:(RGDRectangle *)request handler:(void(^)(BOOL done, RGDFeature *response, NSError *error))handler; - - -#pragma mark RecordRoute(stream Point) returns (RouteSummary) - -- (void)recordRouteWithRequestsWriter:(id<GRXWriter>)request handler:(void(^)(RGDRouteSummary *response, NSError *error))handler; - -- (ProtoRPC *)RPCToRecordRouteWithRequestsWriter:(id<GRXWriter>)request handler:(void(^)(RGDRouteSummary *response, NSError *error))handler; - - -#pragma mark RouteChat(stream RouteNote) returns (stream RouteNote) - -- (void)routeChatWithRequestsWriter:(id<GRXWriter>)request handler:(void(^)(BOOL done, RGDRouteNote *response, NSError *error))handler; - -- (ProtoRPC *)RPCToRouteChatWithRequestsWriter:(id<GRXWriter>)request handler:(void(^)(BOOL done, RGDRouteNote *response, NSError *error))handler; - - -@end - -// Basic service implementation, over gRPC, that only does marshalling and parsing. -@interface RGDRouteGuide : ProtoService<RGDRouteGuide> -- (instancetype)initWithHost:(NSString *)host NS_DESIGNATED_INITIALIZER; -@end diff --git a/src/objective-c/generated_libraries/RouteGuideClient/RouteGuide.pbrpc.m b/src/objective-c/generated_libraries/RouteGuideClient/RouteGuide.pbrpc.m deleted file mode 100644 index 2ca3dc6768..0000000000 --- a/src/objective-c/generated_libraries/RouteGuideClient/RouteGuide.pbrpc.m +++ /dev/null @@ -1,72 +0,0 @@ -#import "RouteGuide.pbrpc.h" -#import <gRPC/GRXWriteable.h> -#import <gRPC/GRXWriter+Immediate.h> -#import <gRPC/ProtoRPC.h> - -static NSString *const kPackageName = @"grpc.example.routeguide"; -static NSString *const kServiceName = @"RouteGuide"; - -@implementation RGDRouteGuide - -// Designated initializer -- (instancetype)initWithHost:(NSString *)host { - return (self = [super initWithHost:host packageName:kPackageName serviceName:kServiceName]); -} - -// Override superclass initializer to disallow different package and service names. -- (instancetype)initWithHost:(NSString *)host - packageName:(NSString *)packageName - serviceName:(NSString *)serviceName { - return [self initWithHost:host]; -} - - -#pragma mark GetFeature(Point) returns (Feature) - -- (void)getFeatureWithRequest:(RGDPoint *)request handler:(void(^)(RGDFeature *response, NSError *error))handler{ - [[self RPCToGetFeatureWithRequest:request handler:handler] start]; -} -// Returns a not-yet-started RPC object. -- (ProtoRPC *)RPCToGetFeatureWithRequest:(RGDPoint *)request handler:(void(^)(RGDFeature *response, NSError *error))handler{ - return [self RPCToMethod:@"GetFeature" - requestsWriter:[GRXWriter writerWithValue:request] - responseClass:[RGDFeature class] - responsesWriteable:[GRXWriteable writeableWithSingleValueHandler:handler]]; -} -#pragma mark ListFeatures(Rectangle) returns (stream Feature) - -- (void)listFeaturesWithRequest:(RGDRectangle *)request handler:(void(^)(BOOL done, RGDFeature *response, NSError *error))handler{ - [[self RPCToListFeaturesWithRequest:request handler:handler] start]; -} -// Returns a not-yet-started RPC object. -- (ProtoRPC *)RPCToListFeaturesWithRequest:(RGDRectangle *)request handler:(void(^)(BOOL done, RGDFeature *response, NSError *error))handler{ - return [self RPCToMethod:@"ListFeatures" - requestsWriter:[GRXWriter writerWithValue:request] - responseClass:[RGDFeature class] - responsesWriteable:[GRXWriteable writeableWithStreamHandler:handler]]; -} -#pragma mark RecordRoute(stream Point) returns (RouteSummary) - -- (void)recordRouteWithRequestsWriter:(id<GRXWriter>)request handler:(void(^)(RGDRouteSummary *response, NSError *error))handler{ - [[self RPCToRecordRouteWithRequestsWriter:request handler:handler] start]; -} -// Returns a not-yet-started RPC object. -- (ProtoRPC *)RPCToRecordRouteWithRequestsWriter:(id<GRXWriter>)request handler:(void(^)(RGDRouteSummary *response, NSError *error))handler{ - return [self RPCToMethod:@"RecordRoute" - requestsWriter:request - responseClass:[RGDRouteSummary class] - responsesWriteable:[GRXWriteable writeableWithSingleValueHandler:handler]]; -} -#pragma mark RouteChat(stream RouteNote) returns (stream RouteNote) - -- (void)routeChatWithRequestsWriter:(id<GRXWriter>)request handler:(void(^)(BOOL done, RGDRouteNote *response, NSError *error))handler{ - [[self RPCToRouteChatWithRequestsWriter:request handler:handler] start]; -} -// Returns a not-yet-started RPC object. -- (ProtoRPC *)RPCToRouteChatWithRequestsWriter:(id<GRXWriter>)request handler:(void(^)(BOOL done, RGDRouteNote *response, NSError *error))handler{ - return [self RPCToMethod:@"RouteChat" - requestsWriter:request - responseClass:[RGDRouteNote class] - responsesWriteable:[GRXWriteable writeableWithStreamHandler:handler]]; -} -@end diff --git a/src/objective-c/generated_libraries/RouteGuideClient/RouteGuide.podspec b/src/objective-c/generated_libraries/RouteGuideClient/RouteGuide.podspec new file mode 100644 index 0000000000..e26e62f5bb --- /dev/null +++ b/src/objective-c/generated_libraries/RouteGuideClient/RouteGuide.podspec @@ -0,0 +1,26 @@ +Pod::Spec.new do |s| + s.name = "RouteGuide" + s.version = "0.0.1" + s.license = "New BSD" + + s.ios.deployment_target = "6.0" + s.osx.deployment_target = "10.8" + + # Run protoc with the Objective-C and gRPC plugins to generate protocol messages and gRPC clients. + s.prepare_command = "protoc --objc_out=. --objcgrpc_out=. *.proto" + + s.subspec "Messages" do |ms| + ms.source_files = "*.pbobjc.{h,m}" + ms.header_mappings_dir = "." + ms.requires_arc = false + ms.dependency "Protobuf", "~> 3.0.0-alpha-3" + end + + s.subspec "Services" do |ss| + ss.source_files = "*.pbrpc.{h,m}" + ss.header_mappings_dir = "." + ss.requires_arc = true + ss.dependency "gRPC", "~> 0.5" + ss.dependency "#{s.name}/Messages" + end +end diff --git a/src/objective-c/generated_libraries/RouteGuideClient/Route_guide.podspec b/src/objective-c/generated_libraries/RouteGuideClient/Route_guide.podspec deleted file mode 100644 index 5b1a68aad5..0000000000 --- a/src/objective-c/generated_libraries/RouteGuideClient/Route_guide.podspec +++ /dev/null @@ -1,24 +0,0 @@ -Pod::Spec.new do |s| - s.name = 'Route_guide' - s.version = '0.0.1' - s.summary = 'Protobuf library generated from route_guide.proto' - s.homepage = 'https://github.com/grpc/grpc/tree/master/src/objective-c/examples/Sample/RouteGuideClient' - s.license = 'New BSD' - s.authors = { 'Jorge Canizales' => 'jcanizales@google.com' } - - s.ios.deployment_target = '6.0' - s.osx.deployment_target = '10.8' - - s.subspec 'Messages' do |ms| - ms.source_files = '*.pbobjc.{h,m}' - ms.requires_arc = false - ms.dependency 'Protobuf', '~> 3.0' - end - - s.subspec 'Services' do |ss| - ss.source_files = '*.pbrpc.{h,m}' - ss.requires_arc = true - ss.dependency 'gRPC', '~> 0.0' - ss.dependency 'Route_guide/Messages' - end -end diff --git a/src/objective-c/generated_libraries/RouteGuideClient/route_guide.proto b/src/objective-c/generated_libraries/RouteGuideClient/route_guide.proto index 16dce26a2b..dace1a5d26 100644 --- a/src/objective-c/generated_libraries/RouteGuideClient/route_guide.proto +++ b/src/objective-c/generated_libraries/RouteGuideClient/route_guide.proto @@ -29,7 +29,7 @@ syntax = "proto3"; -package grpc.example.routeguide; +package examples; option objc_class_prefix = "RGD"; diff --git a/src/objective-c/tests/GRPCClientTests.m b/src/objective-c/tests/GRPCClientTests.m index 713ea2848a..e421127ea1 100644 --- a/src/objective-c/tests/GRPCClientTests.m +++ b/src/objective-c/tests/GRPCClientTests.m @@ -34,33 +34,47 @@ #import <UIKit/UIKit.h> #import <XCTest/XCTest.h> -#import <gRPC/GRPCCall.h> -#import <gRPC/GRPCMethodName.h> -#import <gRPC/GRXWriter+Immediate.h> -#import <gRPC/GRXWriteable.h> +#import <GRPCClient/GRPCCall.h> +#import <GRPCClient/GRPCMethodName.h> #import <RemoteTest/Messages.pbobjc.h> +#import <RxLibrary/GRXWriteable.h> +#import <RxLibrary/GRXWriter+Immediate.h> // These are a few tests similar to InteropTests, but which use the generic gRPC client (GRPCCall) // rather than a generated proto library on top of it. +static NSString * const kHostAddress = @"grpc-test.sandbox.google.com"; +static NSString * const kPackage = @"grpc.testing"; +static NSString * const kService = @"TestService"; + +static GRPCMethodName *kInexistentMethod; +static GRPCMethodName *kEmptyCallMethod; +static GRPCMethodName *kUnaryCallMethod; + @interface GRPCClientTests : XCTestCase @end @implementation GRPCClientTests -- (void)testConnectionToRemoteServer { - __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Server reachable."]; - +- (void)setUp { // This method isn't implemented by the remote server. - GRPCMethodName *method = [[GRPCMethodName alloc] initWithPackage:@"grpc.testing" - interface:@"TestService" - method:@"Nonexistent"]; + kInexistentMethod = [[GRPCMethodName alloc] initWithPackage:kPackage + interface:kService + method:@"Inexistent"]; + kEmptyCallMethod = [[GRPCMethodName alloc] initWithPackage:kPackage + interface:kService + method:@"EmptyCall"]; + kUnaryCallMethod = [[GRPCMethodName alloc] initWithPackage:kPackage + interface:kService + method:@"UnaryCall"]; +} - id<GRXWriter> requestsWriter = [GRXWriter writerWithValue:[NSData data]]; +- (void)testConnectionToRemoteServer { + __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Server reachable."]; - GRPCCall *call = [[GRPCCall alloc] initWithHost:@"grpc-test.sandbox.google.com" - method:method - requestsWriter:requestsWriter]; + GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress + method:kInexistentMethod + requestsWriter:[GRXWriter writerWithValue:[NSData data]]]; id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { XCTFail(@"Received unexpected response: %@", value); @@ -80,15 +94,9 @@ __weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."]; __weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."]; - GRPCMethodName *method = [[GRPCMethodName alloc] initWithPackage:@"grpc.testing" - interface:@"TestService" - method:@"EmptyCall"]; - - id<GRXWriter> requestsWriter = [GRXWriter writerWithValue:[NSData data]]; - - GRPCCall *call = [[GRPCCall alloc] initWithHost:@"grpc-test.sandbox.google.com" - method:method - requestsWriter:requestsWriter]; + GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress + method:kEmptyCallMethod + requestsWriter:[GRXWriter writerWithValue:[NSData data]]]; id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { XCTAssertNotNil(value, @"nil value received as response."); @@ -105,34 +113,27 @@ } - (void)testSimpleProtoRPC { - __weak XCTestExpectation *response = [self expectationWithDescription:@"Response received."]; - __weak XCTestExpectation *expectedResponse = - [self expectationWithDescription:@"Expected response."]; + __weak XCTestExpectation *response = [self expectationWithDescription:@"Expected response."]; __weak XCTestExpectation *completion = [self expectationWithDescription:@"RPC completed."]; - GRPCMethodName *method = [[GRPCMethodName alloc] initWithPackage:@"grpc.testing" - interface:@"TestService" - method:@"UnaryCall"]; - - RMTSimpleRequest *request = [[RMTSimpleRequest alloc] init]; + RMTSimpleRequest *request = [RMTSimpleRequest message]; request.responseSize = 100; request.fillUsername = YES; request.fillOauthScope = YES; id<GRXWriter> requestsWriter = [GRXWriter writerWithValue:[request data]]; - GRPCCall *call = [[GRPCCall alloc] initWithHost:@"grpc-test.sandbox.google.com" - method:method + GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress + method:kUnaryCallMethod requestsWriter:requestsWriter]; id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { XCTAssertNotNil(value, @"nil value received as response."); - [response fulfill]; XCTAssertGreaterThan(value.length, 0, @"Empty response received."); - RMTSimpleResponse *response = [RMTSimpleResponse parseFromData:value error:NULL]; + RMTSimpleResponse *responseProto = [RMTSimpleResponse parseFromData:value error:NULL]; // We expect empty strings, not nil: - XCTAssertNotNil(response.username, @"Response's username is nil."); - XCTAssertNotNil(response.oauthScope, @"Response's OAuth scope is nil."); - [expectedResponse fulfill]; + XCTAssertNotNil(responseProto.username, @"Response's username is nil."); + XCTAssertNotNil(responseProto.oauthScope, @"Response's OAuth scope is nil."); + [response fulfill]; } completionHandler:^(NSError *errorOrNil) { XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil); [completion fulfill]; @@ -143,4 +144,36 @@ [self waitForExpectationsWithTimeout:2. handler:nil]; } +- (void)testMetadata { + __weak XCTestExpectation *expectation = [self expectationWithDescription:@"RPC unauthorized."]; + + RMTSimpleRequest *request = [RMTSimpleRequest message]; + request.fillUsername = YES; + request.fillOauthScope = YES; + id<GRXWriter> requestsWriter = [GRXWriter writerWithValue:[request data]]; + + GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress + method:kUnaryCallMethod + requestsWriter:requestsWriter]; + + call.requestMetadata[@"Authorization"] = @"Bearer bogusToken"; + + id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { + XCTFail(@"Received unexpected response: %@", value); + } completionHandler:^(NSError *errorOrNil) { + XCTAssertNotNil(errorOrNil, @"Finished without error!"); + XCTAssertEqual(errorOrNil.code, 16, @"Finished with unexpected error: %@", errorOrNil); + XCTAssertEqualObjects(call.responseMetadata, errorOrNil.userInfo[kGRPCStatusMetadataKey], + @"Metadata in the NSError object and call object differ."); + NSString *challengeHeader = call.responseMetadata[@"www-authenticate"]; + XCTAssertGreaterThan(challengeHeader.length, 0, + @"No challenge in response headers %@", call.responseMetadata); + [expectation fulfill]; + }]; + + [call startWithWriteable:responsesWriteable]; + + [self waitForExpectationsWithTimeout:2. handler:nil]; +} + @end diff --git a/src/objective-c/tests/InteropTests.m b/src/objective-c/tests/InteropTests.m index 0a512c17dc..74f6b231cf 100644 --- a/src/objective-c/tests/InteropTests.m +++ b/src/objective-c/tests/InteropTests.m @@ -36,13 +36,13 @@ #import <UIKit/UIKit.h> #import <XCTest/XCTest.h> -#import <gRPC/GRXWriter+Immediate.h> -#import <gRPC/GRXBufferedPipe.h> -#import <gRPC/ProtoRPC.h> +#import <ProtoRPC/ProtoRPC.h> #import <RemoteTest/Empty.pbobjc.h> #import <RemoteTest/Messages.pbobjc.h> #import <RemoteTest/Test.pbobjc.h> #import <RemoteTest/Test.pbrpc.h> +#import <RxLibrary/GRXBufferedPipe.h> +#import <RxLibrary/GRXWriter+Immediate.h> // Convenience constructors for the generated proto messages: @@ -174,7 +174,7 @@ __block int index = 0; [_service streamingOutputCallWithRequest:request - handler:^(BOOL done, + eventHandler:^(BOOL done, RMTStreamingOutputCallResponse *response, NSError *error){ XCTAssertNil(error, @"Finished with unexpected error: %@", error); @@ -211,7 +211,7 @@ [requestsBuffer writeValue:request]; [_service fullDuplexCallWithRequestsWriter:requestsBuffer - handler:^(BOOL done, + eventHandler:^(BOOL done, RMTStreamingOutputCallResponse *response, NSError *error) { XCTAssertNil(error, @"Finished with unexpected error: %@", error); @@ -242,7 +242,7 @@ - (void)testEmptyStreamRPC { __weak XCTestExpectation *expectation = [self expectationWithDescription:@"EmptyStream"]; [_service fullDuplexCallWithRequestsWriter:[GRXWriter emptyWriter] - handler:^(BOOL done, + eventHandler:^(BOOL done, RMTStreamingOutputCallResponse *response, NSError *error) { XCTAssertNil(error, @"Finished with unexpected error: %@", error); @@ -283,7 +283,7 @@ [requestsBuffer writeValue:request]; __block ProtoRPC *call = [_service RPCToFullDuplexCallWithRequestsWriter:requestsBuffer - handler:^(BOOL done, + eventHandler:^(BOOL done, RMTStreamingOutputCallResponse *response, NSError *error) { if (receivedResponse) { diff --git a/src/objective-c/tests/LocalClearTextTests.m b/src/objective-c/tests/LocalClearTextTests.m index 6a9496b623..05cc10410a 100644 --- a/src/objective-c/tests/LocalClearTextTests.m +++ b/src/objective-c/tests/LocalClearTextTests.m @@ -34,60 +34,66 @@ #import <UIKit/UIKit.h> #import <XCTest/XCTest.h> -#import <gRPC/GRPCCall.h> -#import <gRPC/GRPCMethodName.h> -#import <gRPC/GRXWriter+Immediate.h> -#import <gRPC/GRXWriteable.h> -#import <Route_guide/RouteGuide.pbobjc.h> -#import <Route_guide/RouteGuide.pbrpc.h> - -// These tests require the gRPC-Java "RouteGuide" sample server to be running locally. To do so, -// install Gradle by following the instructions here: https://docs.gradle.org/current/userguide/installation.html -// And use it to run the server by following the instructions here: https://github.com/grpc/grpc-java/tree/master/examples +#import <GRPCClient/GRPCCall.h> +#import <GRPCClient/GRPCMethodName.h> +#import <RouteGuide/RouteGuide.pbobjc.h> +#import <RouteGuide/RouteGuide.pbrpc.h> +#import <RxLibrary/GRXWriteable.h> +#import <RxLibrary/GRXWriter+Immediate.h> + +// These tests require a gRPC "RouteGuide" sample server to be running locally. You can compile and +// run one by following the instructions here: https://github.com/grpc/grpc-common/blob/master/cpp/cpptutorial.md#try-it-out +// Be sure to have the C gRPC library installed in your system (for example, by having followed the +// instructions at https://github.com/grpc/homebrew-grpc + +static NSString * const kRouteGuideHost = @"http://localhost:50051"; +static NSString * const kPackage = @"examples"; +static NSString * const kService = @"RouteGuide"; @interface LocalClearTextTests : XCTestCase @end @implementation LocalClearTextTests -- (void)testConnectionToLocalServer { - __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Server reachable."]; - - // This method isn't implemented by the local server. - GRPCMethodName *method = [[GRPCMethodName alloc] initWithPackage:@"grpc.testing" - interface:@"TestService" - method:@"EmptyCall"]; - - id<GRXWriter> requestsWriter = [GRXWriter writerWithValue:[NSData data]]; - - GRPCCall *call = [[GRPCCall alloc] initWithHost:@"http://127.0.0.1:8980" - method:method - requestsWriter:requestsWriter]; - - id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { - XCTFail(@"Received unexpected response: %@", value); - } completionHandler:^(NSError *errorOrNil) { - XCTAssertNotNil(errorOrNil, @"Finished without error!"); - XCTAssertEqual(errorOrNil.code, 12, @"Finished with unexpected error: %@", errorOrNil); - [expectation fulfill]; - }]; - - [call startWithWriteable:responsesWriteable]; - - [self waitForExpectationsWithTimeout:2.0 handler:nil]; -} +// This test currently fails: see Issue #1907. +//- (void)testConnectionToLocalServer { +// __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Server reachable."]; +// +// // This method isn't implemented by the local server. +// GRPCMethodName *method = [[GRPCMethodName alloc] initWithPackage:kPackage +// interface:kService +// method:@"EmptyCall"]; +// +// id<GRXWriter> requestsWriter = [GRXWriter writerWithValue:[NSData data]]; +// +// GRPCCall *call = [[GRPCCall alloc] initWithHost:kRouteGuideHost +// method:method +// requestsWriter:requestsWriter]; +// +// id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { +// XCTFail(@"Received unexpected response: %@", value); +// } completionHandler:^(NSError *errorOrNil) { +// XCTAssertNotNil(errorOrNil, @"Finished without error!"); +// XCTAssertEqual(errorOrNil.code, 12, @"Finished with unexpected error: %@", errorOrNil); +// [expectation fulfill]; +// }]; +// +// [call startWithWriteable:responsesWriteable]; +// +// [self waitForExpectationsWithTimeout:8.0 handler:nil]; +//} - (void)testEmptyRPC { __weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."]; __weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."]; - GRPCMethodName *method = [[GRPCMethodName alloc] initWithPackage:@"grpc.example.routeguide" - interface:@"RouteGuide" + GRPCMethodName *method = [[GRPCMethodName alloc] initWithPackage:kPackage + interface:kService method:@"RecordRoute"]; id<GRXWriter> requestsWriter = [GRXWriter emptyWriter]; - GRPCCall *call = [[GRPCCall alloc] initWithHost:@"http://127.0.0.1:8980" + GRPCCall *call = [[GRPCCall alloc] initWithHost:kRouteGuideHost method:method requestsWriter:requestsWriter]; @@ -109,8 +115,8 @@ __weak XCTestExpectation *response = [self expectationWithDescription:@"Response received."]; __weak XCTestExpectation *completion = [self expectationWithDescription:@"RPC completed."]; - GRPCMethodName *method = [[GRPCMethodName alloc] initWithPackage:@"grpc.example.routeguide" - interface:@"RouteGuide" + GRPCMethodName *method = [[GRPCMethodName alloc] initWithPackage:kPackage + interface:kService method:@"GetFeature"]; RGDPoint *point = [RGDPoint message]; @@ -118,7 +124,7 @@ point.longitude = -15E7; id<GRXWriter> requestsWriter = [GRXWriter writerWithValue:[point data]]; - GRPCCall *call = [[GRPCCall alloc] initWithHost:@"http://127.0.0.1:8980" + GRPCCall *call = [[GRPCCall alloc] initWithHost:kRouteGuideHost method:method requestsWriter:requestsWriter]; @@ -145,7 +151,7 @@ point.latitude = 28E7; point.longitude = -15E7; - RGDRouteGuide *service = [[RGDRouteGuide alloc] initWithHost:@"http://127.0.0.1:8980"]; + RGDRouteGuide *service = [[RGDRouteGuide alloc] initWithHost:kRouteGuideHost]; [service getFeatureWithRequest:point handler:^(RGDFeature *response, NSError *error) { XCTAssertNil(error, @"Finished with unexpected error: %@", error); XCTAssertEqualObjects(point, response.location); diff --git a/src/objective-c/tests/Podfile b/src/objective-c/tests/Podfile index c099fb5182..026868db12 100644 --- a/src/objective-c/tests/Podfile +++ b/src/objective-c/tests/Podfile @@ -2,9 +2,8 @@ source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' pod 'gRPC', :path => "../../.." -pod 'Protobuf', :git => 'https://github.com/google/protobuf.git' pod 'RemoteTest', :path => "../generated_libraries/RemoteTestClient" -pod 'Route_guide', :path => "../generated_libraries/RouteGuideClient" +pod 'RouteGuide', :path => "../generated_libraries/RouteGuideClient" link_with 'AllTests' diff --git a/src/objective-c/tests/RxLibraryUnitTests.m b/src/objective-c/tests/RxLibraryUnitTests.m index 89984d9481..5e3162875a 100644 --- a/src/objective-c/tests/RxLibraryUnitTests.m +++ b/src/objective-c/tests/RxLibraryUnitTests.m @@ -34,9 +34,9 @@ #import <UIKit/UIKit.h> #import <XCTest/XCTest.h> -#import <gRPC/GRXBufferedPipe.h> -#import <gRPC/GRXWriter.h> -#import <gRPC/GRXWriteable.h> +#import <RxLibrary/GRXBufferedPipe.h> +#import <RxLibrary/GRXWriteable.h> +#import <RxLibrary/GRXWriter.h> // A mock of a GRXSingleValueHandler block that can be queried for how many times it was called and // what were the last values passed to it. diff --git a/src/php/README.md b/src/php/README.md index 40c79e0dd4..1804606e09 100644 --- a/src/php/README.md +++ b/src/php/README.md @@ -5,53 +5,141 @@ This directory contains source code for PHP implementation of gRPC layered on sh #Status -Pre-Alpha : This gRPC PHP implementation is work-in-progress and is not expected to work yet. +Alpha : Ready for early adopters +## ENVIRONMENT -## LAYOUT +Prerequisite: PHP 5.5 or later, `phpunit`, `pecl` -Directory structure is as generated by the PHP utility -[ext_skel](http://php.net/manual/en/internals2.buildsys.skeleton.php) +Linux: -## ENVIRONMENT +```sh +$ sudo apt-get install php5 php5-dev phpunit php-pear +``` + +OS X: + +```sh +$ curl https://phar.phpunit.de/phpunit.phar -o phpunit.phar +$ chmod +x phpunit.phar +$ sudo mv phpunit.phar /usr/local/bin/phpunit + +$ curl -O http://pear.php.net/go-pear.phar +$ sudo php -d detect_unicode=0 go-pear.phar +``` + +## Build from Homebrew + +On Mac OS X, install [homebrew][]. On Linux, install [linuxbrew][]. Run the following command to +install gRPC. + +```sh +$ curl -fsSL https://goo.gl/getgrpc | bash -s php +``` + +This will download and run the [gRPC install script][] and compile the gRPC PHP extension. + +## Build from Source + +Clone this repository + +``` +$ git clone https://github.com/grpc/grpc.git +``` + +Build and install the Protocol Buffers compiler (protoc) + +``` +$ cd grpc +$ git pull --recurse-submodules && git submodule update --init --recursive +$ cd third_party/protobuf +$ ./autogen.sh +$ ./configure +$ make +$ make check +$ sudo make install +``` -Install `php5` and `php5-dev`. +Build and install the gRPC C core libraries -To run the tests, additionally install `php5-readline` and `phpunit`. +```sh +$ cd grpc +$ make +$ sudo make install +``` + +Install the gRPC PHP extension -Alternatively, build and install PHP 5.5 or later from source with standard -configuration options. +```sh +$ sudo pecl install grpc +``` -To also download and install protoc and the PHP code generator. +OR -```bash -apt-get install -y procps -curl -sSL https://get.rvm.io | sudo bash -s stable --ruby -git clone git@github.com:google/protobuf.git -cd protobuf -./configure -make -make install -git clone git@github.com:murgatroid99/Protobuf-PHP.git -cd Protobuf-PHP -rake pear:package version=1.0 -pear install Protobuf-1.0.tgz +```sh +$ cd grpc/src/php/ext/grpc +$ phpize +$ ./configure +$ make +$ sudo make install ``` -## BUILDING +In your php.ini file, add the line `extension=grpc.so` to load the extension +at PHP startup. + +Install Composer + +```sh +$ cd grpc/src/php +$ curl -sS https://getcomposer.org/installer | php +$ php composer.phar install +``` - 1. In ./ext/grpc, run the command `phpize` (distributed with PHP) - 2. Run `./ext/grpc/configure` - 3. In ./ext/grpc, run `make` and `sudo make install` - 4. In your php.ini file, add the line `extension=grpc.so` to load the - extension at PHP startup. +## Unit Tests -## PHPUnit +Run unit tests -This repo now has PHPUnit tests, which can by run by executing -`./bin/run_tests.sh` after building. +```sh +$ cd grpc/src/php +$ ./bin/run_tests.sh +``` + +## Generated Code Tests + +Install `protoc-gen-php` + +```sh +$ cd grpc/src/php/vendor/datto/protobuf-php +$ gem install rake ronn +$ rake pear:package version=1.0 +$ sudo pear install Protobuf-1.0.tgz +``` + +Generate client stub code + +```sh +$ cd grpc/src/php +$ ./bin/generate_proto_php.sh +``` + +Run a local server serving the math services + + - Please see [Node][] on how to run an example server + +```sh +$ cd grpc/src/node +$ npm install +$ nodejs examples/math_server.js +``` + +Run the generated code tests + +```sh +$ cd grpc/src/php +$ ./bin/run_gen_code_test.sh +``` -There is also a generated code test (`./bin/run_gen_code_test.sh`), which tests -the stub `./tests/generated_code/math.php` against a running localhost server -serving the math service. That stub is generated from -`./tests/generated_code/math.proto`. +[homebrew]:http://brew.sh +[linuxbrew]:https://github.com/Homebrew/linuxbrew#installation +[gRPC install script]:https://raw.githubusercontent.com/grpc/homebrew-grpc/master/scripts/install +[Node]:https://github.com/grpc/grpc/tree/master/src/node/examples diff --git a/src/php/bin/determine_extension_dir.sh b/src/php/bin/determine_extension_dir.sh new file mode 100755 index 0000000000..6bbd934bf1 --- /dev/null +++ b/src/php/bin/determine_extension_dir.sh @@ -0,0 +1,49 @@ +#!/bin/bash +# 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. + +set -e +default_extension_dir=$(php-config --extension-dir) +if command -v brew >/dev/null && [ -d $(brew --prefix)/opt/grpc-php ]; then + # homebrew and the grpc-php formula are installed + extension_dir="-d extension_dir="$(brew --prefix)/opt/grpc-php +elif [ ! -e $default_extension_dir/grpc.so ]; then + # the grpc extension is not found in the default PHP extension dir + # try the source modules directory + module_dir=../ext/grpc/modules + if [ ! -e $module_dir/grpc.so ]; then + echo "Please run 'phpize && ./configure && make' from ext/grpc first" + exit 1 + fi + # sym-link in system supplied extensions + for f in $default_extension_dir/*.so; do + ln -s $f $module_dir/$(basename $f) &> /dev/null || true + done + extension_dir="-d extension_dir="$module_dir +fi diff --git a/src/php/bin/interop_client.sh b/src/php/bin/interop_client.sh index 4fe63788c1..42e075cbe8 100755 --- a/src/php/bin/interop_client.sh +++ b/src/php/bin/interop_client.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # Copyright 2015, Google Inc. # All rights reserved. # @@ -28,11 +28,8 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -set +e +set -e cd $(dirname $0) - -module_dir=../ext/grpc/modules - -php -d extension_dir=$module_dir -d extension=grpc.so \ +source ./determine_extension_dir.sh +php $extension_dir -d extension=grpc.so \ ../tests/interop/interop_client.php $@ 1>&2 diff --git a/src/php/bin/run_gen_code_test.sh b/src/php/bin/run_gen_code_test.sh index 4882a2b846..03a9101a45 100755 --- a/src/php/bin/run_gen_code_test.sh +++ b/src/php/bin/run_gen_code_test.sh @@ -1,4 +1,4 @@ -# Runs the generated code test against the ruby server +#!/bin/bash # Copyright 2015, Google Inc. # All rights reserved. # @@ -28,10 +28,11 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +set -e cd $(dirname $0) -GRPC_TEST_HOST=localhost:50051 php -d extension_dir=../ext/grpc/modules/ \ - -d extension=grpc.so /usr/local/bin/phpunit -v --debug --strict \ +source ./determine_extension_dir.sh +export GRPC_TEST_HOST=localhost:7071 +php $extension_dir -d extension=grpc.so $(which phpunit) -v --debug --strict \ ../tests/generated_code/GeneratedCodeTest.php -GRPC_TEST_HOST=localhost:50051 php -d extension_dir=../ext/grpc/modules/ \ - -d extension=grpc.so /usr/local/bin/phpunit -v --debug --strict \ +php $extension_dir -d extension=grpc.so $(which phpunit) -v --debug --strict \ ../tests/generated_code/GeneratedCodeWithCallbackTest.php diff --git a/src/php/bin/run_tests.sh b/src/php/bin/run_tests.sh index 551be12514..4c37285455 100755 --- a/src/php/bin/run_tests.sh +++ b/src/php/bin/run_tests.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # Copyright 2015, Google Inc. # All rights reserved. # @@ -32,17 +32,6 @@ # against it set -e cd $(dirname $0) -default_extension_dir=`php -i | grep extension_dir | sed 's/.*=> //g'` - -module_dir=../ext/grpc/modules - -# sym-link in system supplied extensions -for f in $default_extension_dir/*.so -do - ln -s $f $module_dir/$(basename $f) &> /dev/null || true -done - -php \ - -d extension_dir=$module_dir \ - -d extension=grpc.so \ - `which phpunit` -v --debug --strict ../tests/unit_tests +source ./determine_extension_dir.sh +php $extension_dir -d extension=grpc.so $(which phpunit) -v --debug --strict \ + ../tests/unit_tests diff --git a/src/php/composer.json b/src/php/composer.json index ba7a1302f2..b0115bdadd 100644 --- a/src/php/composer.json +++ b/src/php/composer.json @@ -4,8 +4,15 @@ "version": "0.5.0", "homepage": "http://grpc.io", "license": "BSD-3-Clause", + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/stanley-cheung/Protobuf-PHP" + } + ], "require": { "php": ">=5.5.0", + "datto/protobuf-php": "dev-master", "google/auth": "dev-master" }, "autoload": { diff --git a/src/php/ext/grpc/CREDITS b/src/php/ext/grpc/CREDITS new file mode 100644 index 0000000000..17b94fedc0 --- /dev/null +++ b/src/php/ext/grpc/CREDITS @@ -0,0 +1,3 @@ +Michael Lumish (mlumish@google.com) +Tim Emiola (temiola@google.com) +Stanley Cheung (stanleycheung@google.com) diff --git a/src/cpp/client/client_unary_call.cc b/src/php/ext/grpc/LICENSE index 7e7ea78bcd..704b523970 100644 --- a/src/cpp/client/client_unary_call.cc +++ b/src/php/ext/grpc/LICENSE @@ -30,35 +30,3 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ - -#include <grpc++/impl/client_unary_call.h> -#include <grpc++/impl/call.h> -#include <grpc++/channel_interface.h> -#include <grpc++/client_context.h> -#include <grpc++/completion_queue.h> -#include <grpc++/status.h> -#include <grpc/support/log.h> - -namespace grpc { - -// Wrapper that performs a blocking unary call -Status BlockingUnaryCall(ChannelInterface* channel, const RpcMethod& method, - ClientContext* context, - const grpc::protobuf::Message& request, - grpc::protobuf::Message* result) { - CompletionQueue cq; - Call call(channel->CreateCall(method, context, &cq)); - CallOpBuffer buf; - Status status; - buf.AddSendInitialMetadata(context); - buf.AddSendMessage(request); - buf.AddRecvInitialMetadata(context); - buf.AddRecvMessage(result); - buf.AddClientSendClose(); - buf.AddClientRecvStatus(context, &status); - call.PerformOps(&buf); - GPR_ASSERT((cq.Pluck(&buf) && buf.got_message) || !status.IsOk()); - return status; -} - -} // namespace grpc diff --git a/src/php/ext/grpc/README.md b/src/php/ext/grpc/README.md new file mode 100644 index 0000000000..0ac09e1835 --- /dev/null +++ b/src/php/ext/grpc/README.md @@ -0,0 +1,72 @@ +gRPC PHP Extension +================== + +# Requirements + + * PHP 5.5+ + * [gRPC core library](https://github.com/grpc/grpc) 0.9.1 + +# Installation + +## Install PHP 5 + +``` +$ sudo apt-get install git php5 php5-dev php-pear unzip +``` + +## Compile gRPC Core Library + +Clone the gRPC source code repository + +``` +$ git clone https://github.com/grpc/grpc.git +``` + +Build and install the Protocol Buffers compiler (protoc) + +``` +$ # from grpc +$ git checkout --track origin/release-0_9 +$ git pull --recurse-submodules && git submodule update --init --recursive +$ cd third_party/protobuf +$ ./autogen.sh +$ ./configure +$ make +$ make check +$ sudo make install +``` + +Build and install the gRPC C core library + +```sh +$ # from grpc +$ make +$ sudo make install +``` + +## Install the gRPC PHP extension + +Quick install + +```sh +$ sudo pecl install grpc +``` + +Note: before a stable release, you may need to do + +```sh +$ sudo pecl install grpc-0.5.0 +``` + +OR + +Compile from source + +```sh +$ # from grpc +$ cd src/php/ext/grpc +$ phpize +$ ./configure +$ make +$ sudo make install +``` diff --git a/src/php/ext/grpc/byte_buffer.c b/src/php/ext/grpc/byte_buffer.c index 4f3e6b67af..8be0a20607 100644 --- a/src/php/ext/grpc/byte_buffer.c +++ b/src/php/ext/grpc/byte_buffer.c @@ -46,11 +46,12 @@ #include "byte_buffer.h" #include <grpc/grpc.h> +#include <grpc/byte_buffer_reader.h> #include <grpc/support/slice.h> grpc_byte_buffer *string_to_byte_buffer(char *string, size_t length) { gpr_slice slice = gpr_slice_from_copied_buffer(string, length); - grpc_byte_buffer *buffer = grpc_byte_buffer_create(&slice, 1); + grpc_byte_buffer *buffer = grpc_raw_byte_buffer_create(&slice, 1); gpr_slice_unref(slice); return buffer; } @@ -65,9 +66,10 @@ void byte_buffer_to_string(grpc_byte_buffer *buffer, char **out_string, size_t length = grpc_byte_buffer_length(buffer); char *string = ecalloc(length + 1, sizeof(char)); size_t offset = 0; - grpc_byte_buffer_reader *reader = grpc_byte_buffer_reader_create(buffer); + grpc_byte_buffer_reader reader; + grpc_byte_buffer_reader_init(&reader, buffer); gpr_slice next; - while (grpc_byte_buffer_reader_next(reader, &next) != 0) { + while (grpc_byte_buffer_reader_next(&reader, &next) != 0) { memcpy(string + offset, GPR_SLICE_START_PTR(next), GPR_SLICE_LENGTH(next)); offset += GPR_SLICE_LENGTH(next); } diff --git a/src/php/ext/grpc/call.c b/src/php/ext/grpc/call.c index 9f651ff56f..10a4946ea6 100644 --- a/src/php/ext/grpc/call.c +++ b/src/php/ext/grpc/call.c @@ -397,6 +397,7 @@ PHP_METHOD(Call, startBatch) { goto cleanup; } ops[op_num].op = (grpc_op_type)index; + ops[op_num].flags = 0; op_num++; } error = grpc_call_start_batch(call->wrapped, ops, op_num, call->wrapped); diff --git a/src/php/ext/grpc/config.m4 b/src/php/ext/grpc/config.m4 index b485aabf40..8bacdfbfec 100755 --- a/src/php/ext/grpc/config.m4 +++ b/src/php/ext/grpc/config.m4 @@ -35,8 +35,13 @@ if test "$PHP_GRPC" != "no"; then PHP_ADD_LIBRARY(dl,,GRPC_SHARED_LIBADD) PHP_ADD_LIBRARY(dl) - PHP_ADD_LIBRARY(rt,,GRPC_SHARED_LIBADD) - PHP_ADD_LIBRARY(rt) + case $host in + *darwin*) ;; + *) + PHP_ADD_LIBRARY(rt,,GRPC_SHARED_LIBADD) + PHP_ADD_LIBRARY(rt) + ;; + esac GRPC_LIBDIR=$GRPC_DIR/${GRPC_LIB_SUBDIR-lib} diff --git a/src/php/ext/grpc/package.xml b/src/php/ext/grpc/package.xml new file mode 100644 index 0000000000..2c89829d51 --- /dev/null +++ b/src/php/ext/grpc/package.xml @@ -0,0 +1,82 @@ +<?xml version="1.0" encoding="UTF-8"?> +<package packagerversion="1.9.5" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd"> + <name>grpc</name> + <channel>pecl.php.net</channel> + <summary>A high performance, open source, general RPC framework that puts mobile and HTTP/2 first.</summary> + <description>Remote Procedure Calls (RPCs) provide a useful abstraction for building distributed applications and services. The libraries in this repository provide a concrete implementation of the gRPC protocol, layered over HTTP/2. These libraries enable communication between clients and servers using any combination of the supported languages.</description> + <lead> + <name>Stanley Cheung</name> + <user>stanleycheung</user> + <email>grpc-packages@google.com</email> + <active>yes</active> + </lead> + <date>2015-06-16</date> + <time>20:12:55</time> + <version> + <release>0.5.0</release> + <api>0.5.0</api> + </version> + <stability> + <release>alpha</release> + <api>alpha</api> + </stability> + <license>BSD</license> + <notes> +First alpha release + </notes> + <contents> + <dir baseinstalldir="/" name="/"> + <file baseinstalldir="/" md5sum="6f19828fb869b7b8a590cbb76b4f996d" name="byte_buffer.c" role="src" /> + <file baseinstalldir="/" md5sum="c8de0f819499c48adfc8d7f472c0196b" name="byte_buffer.h" role="src" /> + <file baseinstalldir="/" md5sum="cb45b62f767ae7b4377761df696649fc" name="call.c" role="src" /> + <file baseinstalldir="/" md5sum="26acbf04c30162c2d2aca4688bb2aec8" name="call.h" role="src" /> + <file baseinstalldir="/" md5sum="50837fbdb2892795f1871b22e5979762" name="channel.c" role="src" /> + <file baseinstalldir="/" md5sum="f1b66029daeced20b47cf00cc6523fc8" name="channel.h" role="src" /> + <file baseinstalldir="/" md5sum="81a1193e93d8b6602add8ac360de565b" name="completion_queue.c" role="src" /> + <file baseinstalldir="/" md5sum="f10b5bb232d74a6878e829e2e76cdaa2" name="completion_queue.h" role="src" /> + <file baseinstalldir="/" md5sum="a9181ed994a072ac5f41e7c9705c170f" name="config.m4" role="src" /> + <file baseinstalldir="/" md5sum="8c3f1e11dac623001378bfd53b554f08" name="credentials.c" role="src" /> + <file baseinstalldir="/" md5sum="6988d6e97c19c8f8e3eb92371cf8246b" name="credentials.h" role="src" /> + <file baseinstalldir="/" md5sum="38a1bc979d810c36ebc2a52d4b7b5319" name="CREDITS" role="doc" /> + <file baseinstalldir="/" md5sum="3f35b472bbdef5a788cd90617d7d0847" name="LICENSE" role="doc" /> + <file baseinstalldir="/" md5sum="6aaa7a290122d230f2d8c4e4e05da4a9" name="php_grpc.c" role="src" /> + <file baseinstalldir="/" md5sum="673b07859d9f69232f8a754c56780686" name="php_grpc.h" role="src" /> + <file baseinstalldir="/" md5sum="4d4d3382f8d10cae2e4378468e5516b9" name="README.md" role="doc" /> + <file baseinstalldir="/" md5sum="53fda0ee6937f6ddc8e271886018d441" name="server.c" role="src" /> + <file baseinstalldir="/" md5sum="4b730f06d14cbbb0642bdbd194749595" name="server.h" role="src" /> + <file baseinstalldir="/" md5sum="f6930beafb6c0e061899262f2f077e98" name="server_credentials.c" role="src" /> + <file baseinstalldir="/" md5sum="9c4b4cc06356a8a39a16a085a9b85996" name="server_credentials.h" role="src" /> + <file baseinstalldir="/" md5sum="c89c623cd17177ebde18313fc5c17122" name="timeval.c" role="src" /> + <file baseinstalldir="/" md5sum="496e27a100b4d93ca3fb35c924c5e163" name="timeval.h" role="src" /> + </dir> + </contents> + <dependencies> + <required> + <php> + <min>5.5.0</min> + </php> + <pearinstaller> + <min>1.4.0</min> + </pearinstaller> + </required> + </dependencies> + <providesextension>grpc</providesextension> + <extsrcrelease /> + <changelog> + <release> + <version> + <release>0.5.0</release> + <api>0.5.0</api> + </version> + <stability> + <release>alpha</release> + <api>alpha</api> + </stability> + <date>2015-06-16</date> + <license>BSD</license> + <notes> +First alpha release + </notes> + </release> + </changelog> +</package> diff --git a/src/php/lib/Grpc/AbstractCall.php b/src/php/lib/Grpc/AbstractCall.php index 1add972589..5b28417a0d 100644 --- a/src/php/lib/Grpc/AbstractCall.php +++ b/src/php/lib/Grpc/AbstractCall.php @@ -43,9 +43,19 @@ abstract class AbstractCall { * Create a new Call wrapper object. * @param Channel $channel The channel to communicate on * @param string $method The method to call on the remote server + * @param callback $deserialize A callback function to deserialize + * the response + * @param (optional) long $timeout Timeout in microseconds */ - public function __construct(Channel $channel, $method, $deserialize) { - $this->call = new Call($channel, $method, Timeval::infFuture()); + public function __construct(Channel $channel, $method, $deserialize, $timeout = false) { + if ($timeout) { + $now = Timeval::now(); + $delta = new Timeval($timeout); + $deadline = $now->add($delta); + } else { + $deadline = Timeval::infFuture(); + } + $this->call = new Call($channel, $method, $deadline); $this->deserialize = $deserialize; $this->metadata = null; } diff --git a/src/php/lib/Grpc/BaseStub.php b/src/php/lib/Grpc/BaseStub.php index b84b6b86f8..48c00977eb 100755 --- a/src/php/lib/Grpc/BaseStub.php +++ b/src/php/lib/Grpc/BaseStub.php @@ -83,6 +83,21 @@ class BaseStub { return "https://" . $this->hostname . $service_name; } + /** + * extract $timeout from $metadata + * @param $metadata The metadata map + * @return list($metadata_copy, $timeout) + */ + private function _extract_timeout_from_metadata($metadata) { + $timeout = false; + $metadata_copy = $metadata; + if (isset($metadata['timeout'])) { + $timeout = $metadata['timeout']; + unset($metadata_copy['timeout']); + } + return array($metadata_copy, $timeout); + } + /* This class is intended to be subclassed by generated code, so all functions begin with "_" to avoid name collisions. */ @@ -99,8 +114,8 @@ class BaseStub { $argument, callable $deserialize, $metadata = array()) { - $call = new UnaryCall($this->channel, $method, $deserialize); - $actual_metadata = $metadata; + list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata); + $call = new UnaryCall($this->channel, $method, $deserialize, $timeout); $jwt_aud_uri = $this->_get_jwt_aud_uri($method); if (is_callable($this->update_metadata)) { $actual_metadata = call_user_func($this->update_metadata, @@ -126,8 +141,8 @@ class BaseStub { $arguments, callable $deserialize, $metadata = array()) { - $call = new ClientStreamingCall($this->channel, $method, $deserialize); - $actual_metadata = $metadata; + list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata); + $call = new ClientStreamingCall($this->channel, $method, $deserialize, $timeout); $jwt_aud_uri = $this->_get_jwt_aud_uri($method); if (is_callable($this->update_metadata)) { $actual_metadata = call_user_func($this->update_metadata, @@ -152,8 +167,8 @@ class BaseStub { $argument, callable $deserialize, $metadata = array()) { - $call = new ServerStreamingCall($this->channel, $method, $deserialize); - $actual_metadata = $metadata; + list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata); + $call = new ServerStreamingCall($this->channel, $method, $deserialize, $timeout); $jwt_aud_uri = $this->_get_jwt_aud_uri($method); if (is_callable($this->update_metadata)) { $actual_metadata = call_user_func($this->update_metadata, @@ -175,8 +190,8 @@ class BaseStub { public function _bidiRequest($method, callable $deserialize, $metadata = array()) { - $call = new BidiStreamingCall($this->channel, $method, $deserialize); - $actual_metadata = $metadata; + list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata); + $call = new BidiStreamingCall($this->channel, $method, $deserialize, $timeout); $jwt_aud_uri = $this->_get_jwt_aud_uri($method); if (is_callable($this->update_metadata)) { $actual_metadata = call_user_func($this->update_metadata, diff --git a/src/php/tests/generated_code/AbstractGeneratedCodeTest.php b/src/php/tests/generated_code/AbstractGeneratedCodeTest.php index 2d2352b199..6102aaf0a8 100644 --- a/src/php/tests/generated_code/AbstractGeneratedCodeTest.php +++ b/src/php/tests/generated_code/AbstractGeneratedCodeTest.php @@ -32,8 +32,6 @@ * */ require_once realpath(dirname(__FILE__) . '/../../vendor/autoload.php'); -require 'DrSlump/Protobuf.php'; -\DrSlump\Protobuf::autoload(); require 'math.php'; abstract class AbstractGeneratedCodeTest extends PHPUnit_Framework_TestCase { /* These tests require that a server exporting the math service must be diff --git a/src/php/tests/interop/interop_client.php b/src/php/tests/interop/interop_client.php index bf8d0cd93c..2041577557 100755 --- a/src/php/tests/interop/interop_client.php +++ b/src/php/tests/interop/interop_client.php @@ -32,8 +32,6 @@ * */ require_once realpath(dirname(__FILE__) . '/../../vendor/autoload.php'); -require 'DrSlump/Protobuf.php'; -\DrSlump\Protobuf::autoload(); require 'empty.php'; require 'message_set.php'; require 'messages.php'; @@ -272,6 +270,24 @@ function cancelAfterFirstResponse($stub) { 'Call status was not CANCELLED'); } +function timeoutOnSleepingServer($stub) { + $call = $stub->FullDuplexCall(array('timeout' => 500000)); + $request = new grpc\testing\StreamingOutputCallRequest(); + $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); + $response_parameters = new grpc\testing\ResponseParameters(); + $response_parameters->setSize(8); + $request->addResponseParameters($response_parameters); + $payload = new grpc\testing\Payload(); + $payload->setBody(str_repeat("\0", 9)); + $request->setPayload($payload); + + $call->write($request); + $response = $call->read(); + + hardAssert($call->getStatus()->code === Grpc\STATUS_DEADLINE_EXCEEDED, + 'Call status was not DEADLINE_EXCEEDED'); +} + $args = getopt('', array('server_host:', 'server_port:', 'test_case:', 'server_host_override:', 'oauth_scope:', 'default_service_account:')); @@ -343,6 +359,9 @@ switch ($args['test_case']) { case 'cancel_after_first_response': cancelAfterFirstResponse($stub); break; + case 'timeout_on_sleeping_server': + timeoutOnSleepingServer($stub); + break; case 'service_account_creds': serviceAccountCreds($stub, $args); break; diff --git a/src/php/tests/unit_tests/TimevalTest.php b/src/php/tests/unit_tests/TimevalTest.php index a8bfcf0ac4..7b4925cad6 100755 --- a/src/php/tests/unit_tests/TimevalTest.php +++ b/src/php/tests/unit_tests/TimevalTest.php @@ -61,4 +61,26 @@ class TimevalTest extends PHPUnit_Framework_TestCase{ $this->assertLessThan(0, Grpc\Timeval::compare($zero, $now)); $this->assertLessThan(0, Grpc\Timeval::compare($now, $future)); } + + public function testNowAndAdd() { + $now = Grpc\Timeval::now(); + $delta = new Grpc\Timeval(1000); + $deadline = $now->add($delta); + $this->assertGreaterThan(0, Grpc\Timeval::compare($deadline, $now)); + } + + public function testNowAndSubtract() { + $now = Grpc\Timeval::now(); + $delta = new Grpc\Timeval(1000); + $deadline = $now->subtract($delta); + $this->assertLessThan(0, Grpc\Timeval::compare($deadline, $now)); + } + + public function testAddAndSubtract() { + $now = Grpc\Timeval::now(); + $delta = new Grpc\Timeval(1000); + $deadline = $now->add($delta); + $back_to_now = $deadline->subtract($delta); + $this->assertSame(0, Grpc\Timeval::compare($back_to_now, $now)); + } } diff --git a/src/python/README.md b/src/python/README.md index c67201b670..2beb3a913a 100644 --- a/src/python/README.md +++ b/src/python/README.md @@ -1,25 +1,34 @@ gRPC Python ========= - The Python facility of gRPC. - Status ------- - Alpha : Ready for early adopters -Prerequisites ------------------------ - -Python 2.7, virtualenv, pip, libprotobuf-dev, and libprotoc-dev. - +PREREQUISITES +------------- +- Python 2.7, virtualenv, pip +- [homebrew][] on Mac OS X, [linuxbrew][] on Linux. These simplify the installation of the gRPC C core. + +INSTALLATION +------------- +On Mac OS X, install [homebrew][]. On Linux, install [linuxbrew][]. +Run the following command to install gRPC Python. +```sh +$ curl -fsSL https://goo.gl/getgrpc | bash -s python +``` +This will download and run the [gRPC install script][], then install the latest version of the gRPC Python package. It also installs the Protocol Buffers compiler (_protoc_) and the gRPC _protoc_ plugin for python. -Building from source ----------------------- +EXAMPLES +-------- +Please read our online documentation for a [Quick Start][] and a [detailed example][] +BUILDING FROM SOURCE +--------------------- +- Clone this repository - Build the gRPC core from the root of the - [gRPC git repo](https://github.com/grpc/grpc) + [gRPC Git repository](https://github.com/grpc/grpc) ``` $ make shared_c static_c ``` @@ -29,40 +38,16 @@ $ make shared_c static_c $ tools/run_tests/build_python.sh ``` - -Testing ------------------------ +TESTING +------- - Use run_python.sh to run gRPC as it was installed into the virtual environment ``` $ tools/run_tests/run_python.sh ``` - -Installing ------------------------ - -- Install the gRPC core - - [Debian package](https://github.com/grpc/grpc/releases) - ``` - $ wget https://github.com/grpc/grpc/releases/download/release-0_5_0/libgrpc_0.5.0_amd64.deb - $ wget https://github.com/grpc/grpc/releases/download/release-0_5_0/libgrpc-dev_0.5.0_amd64.deb - $ sudo dpkg -i libgrpc_0.5.0_amd64.deb libgrpc-dev_0.5.0_amd64.deb - ``` - - [From source](https://github.com/grpc/grpc/blob/master/INSTALL) - -- Install gRPC Python's dependencies -``` -$ pip install -r src/python/requirements.txt -``` - -- Install gRPC Python -``` -$ pip install src/python/src -``` - -Packaging to PyPI ------------------------ +PACKAGING +--------- - Install packaging dependencies ``` @@ -73,3 +58,9 @@ $ pip install setuptools twine ``` $ ../../tools/distrib/python/submit.py ``` + +[homebrew]:http://brew.sh +[linuxbrew]:https://github.com/Homebrew/linuxbrew#installation +[gRPC install script]:https://raw.githubusercontent.com/grpc/homebrew-grpc/master/scripts/install +[Quick Start]:http://www.grpc.io/docs/tutorials/basic/python.html +[detailed example]:http://www.grpc.io/docs/installation/python.html diff --git a/src/python/requirements.txt b/src/python/requirements.txt index d32d436d3c..43395df03b 100644 --- a/src/python/requirements.txt +++ b/src/python/requirements.txt @@ -1,3 +1,3 @@ enum34==1.0.4 futures==2.2.0 -protobuf==3.0.0a2 +protobuf==3.0.0a3 diff --git a/src/python/src/README.rst b/src/python/src/README.rst index bc1815febc..00bdecf56f 100644 --- a/src/python/src/README.rst +++ b/src/python/src/README.rst @@ -6,22 +6,18 @@ Package for GRPC Python. Dependencies ------------ -Ensure that you have installed GRPC core. - -On debian linux systems, install from our released deb package: +Ensure you have installed the gRPC core. On Mac OS X, install homebrew_. On Linux, install linuxbrew_. +Run the following command to install gRPC Python. :: - $ wget https://github.com/grpc/grpc/releases/download/release-0_5_0/libgrpc_0.5.0_amd64.deb - $ wget https://github.com/grpc/grpc/releases/download/release-0_5_0/libgrpc-dev_0.5.0_amd64.deb - $ sudo dpkg -i libgrpc_0.5.0_amd64.deb libgrpc-dev_0.5.0_amd64.deb - -Otherwise, install from source: + $ curl -fsSL https://goo.gl/getgrpc | bash -s python -:: +This will download and run the [gRPC install script][] to install grpc core. The script then uses pip to install this package. It also installs the Protocol Buffers compiler (_protoc_) and the gRPC _protoc_ plugin for python. - git clone https://github.com/grpc/grpc.git - cd grpc - ./configure - make && make install +Otherwise, `install from source`_ +.. _`install from source`: https://github.com/grpc/grpc/blob/master/src/python/README.md#building-from-source +.. _homebrew: http://brew.sh +.. _linuxbrew: https://github.com/Homebrew/linuxbrew#installation +.. _`gRPC install script`: https://raw.githubusercontent.com/grpc/homebrew-grpc/master/scripts/install diff --git a/src/python/src/grpc/_adapter/_c/types.h b/src/python/src/grpc/_adapter/_c/types.h index e189ae2566..3449f0643f 100644 --- a/src/python/src/grpc/_adapter/_c/types.h +++ b/src/python/src/grpc/_adapter/_c/types.h @@ -241,10 +241,10 @@ double pygrpc_cast_gpr_timespec_to_double(gpr_timespec timespec); gpr_timespec pygrpc_cast_double_to_gpr_timespec(double seconds); /* Returns true on success, false on failure. */ -int pygrpc_cast_pylist_to_send_metadata( - PyObject *pylist, grpc_metadata **metadata, size_t *count); +int pygrpc_cast_pyseq_to_send_metadata( + PyObject *pyseq, grpc_metadata **metadata, size_t *count); /* Returns a metadata array as a Python object on success, else NULL. */ -PyObject *pygrpc_cast_metadata_array_to_pylist(grpc_metadata_array metadata); +PyObject *pygrpc_cast_metadata_array_to_pyseq(grpc_metadata_array metadata); /* Transliterate from a list of python channel arguments (2-tuples of string and string|integer|None) to a grpc_channel_args object. The strings placed diff --git a/src/python/src/grpc/_adapter/_c/types/server.c b/src/python/src/grpc/_adapter/_c/types/server.c index 65d84b58fe..2a00f34039 100644 --- a/src/python/src/grpc/_adapter/_c/types/server.c +++ b/src/python/src/grpc/_adapter/_c/types/server.c @@ -105,6 +105,7 @@ Server *pygrpc_Server_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) } self = (Server *)type->tp_alloc(type, 0); self->c_serv = grpc_server_create(&c_args); + grpc_server_register_completion_queue(self->c_serv, cq->c_cq); pygrpc_discard_channel_args(c_args); self->cq = cq; Py_INCREF(self->cq); @@ -167,17 +168,13 @@ PyObject *pygrpc_Server_start(Server *self, PyObject *ignored) { PyObject *pygrpc_Server_shutdown( Server *self, PyObject *args, PyObject *kwargs) { - PyObject *user_tag = NULL; + PyObject *user_tag; pygrpc_tag *tag; static char *keywords[] = {"tag", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", keywords, &user_tag)) { + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", keywords, &user_tag)) { return NULL; } - if (user_tag) { - tag = pygrpc_produce_server_shutdown_tag(user_tag); - grpc_server_shutdown_and_notify(self->c_serv, tag); - } else { - grpc_server_shutdown(self->c_serv); - } + tag = pygrpc_produce_server_shutdown_tag(user_tag); + grpc_server_shutdown_and_notify(self->c_serv, self->cq->c_cq, tag); Py_RETURN_NONE; } diff --git a/src/python/src/grpc/_adapter/_c/utility.c b/src/python/src/grpc/_adapter/_c/utility.c index 6d228c73fe..480a720c21 100644 --- a/src/python/src/grpc/_adapter/_c/utility.c +++ b/src/python/src/grpc/_adapter/_c/utility.c @@ -32,13 +32,16 @@ */ #include <math.h> +#include <string.h> #define PY_SSIZE_T_CLEAN #include <Python.h> #include <grpc/grpc.h> +#include <grpc/byte_buffer_reader.h> #include <grpc/support/alloc.h> #include <grpc/support/slice.h> #include <grpc/support/time.h> +#include <grpc/support/string_util.h> #include "grpc/_adapter/_c/types.h" @@ -116,12 +119,13 @@ PyObject *pygrpc_consume_event(grpc_event event) { tag->request_call_details.method, tag->request_call_details.host, pygrpc_cast_gpr_timespec_to_double(tag->request_call_details.deadline), GRPC_OP_RECV_INITIAL_METADATA, - pygrpc_cast_metadata_array_to_pylist(tag->request_metadata), Py_None, + pygrpc_cast_metadata_array_to_pyseq(tag->request_metadata), Py_None, Py_None, Py_None, Py_None, event.success ? Py_True : Py_False); } else { result = Py_BuildValue("iOOONO", GRPC_OP_COMPLETE, tag->user_tag, - tag->call, Py_None, pygrpc_consume_ops(tag->ops, tag->nops), + tag->call ? (PyObject*)tag->call : Py_None, Py_None, + pygrpc_consume_ops(tag->ops, tag->nops), event.success ? Py_True : Py_False); } break; @@ -144,29 +148,32 @@ int pygrpc_produce_op(PyObject *op, grpc_op *result) { static const int STATUS_INDEX = 4; static const int STATUS_CODE_INDEX = 0; static const int STATUS_DETAILS_INDEX = 1; + int type; + Py_ssize_t message_size; + char *message; + char *status_details; + gpr_slice message_slice; grpc_op c_op; if (!PyTuple_Check(op)) { PyErr_SetString(PyExc_TypeError, "expected tuple op"); return 0; } if (PyTuple_Size(op) != OP_TUPLE_SIZE) { - char buf[64]; - snprintf(buf, sizeof(buf), "expected tuple op of length %d", OP_TUPLE_SIZE); + char *buf; + gpr_asprintf(&buf, "expected tuple op of length %d", OP_TUPLE_SIZE); PyErr_SetString(PyExc_ValueError, buf); + gpr_free(buf); return 0; } - int type = PyInt_AsLong(PyTuple_GET_ITEM(op, TYPE_INDEX)); + type = PyInt_AsLong(PyTuple_GET_ITEM(op, TYPE_INDEX)); if (PyErr_Occurred()) { return 0; } - Py_ssize_t message_size; - char *message; - char *status_details; - gpr_slice message_slice; c_op.op = type; + c_op.flags = 0; switch (type) { case GRPC_OP_SEND_INITIAL_METADATA: - if (!pygrpc_cast_pylist_to_send_metadata( + if (!pygrpc_cast_pyseq_to_send_metadata( PyTuple_GetItem(op, INITIAL_METADATA_INDEX), &c_op.data.send_initial_metadata.metadata, &c_op.data.send_initial_metadata.count)) { @@ -177,24 +184,25 @@ int pygrpc_produce_op(PyObject *op, grpc_op *result) { PyString_AsStringAndSize( PyTuple_GET_ITEM(op, MESSAGE_INDEX), &message, &message_size); message_slice = gpr_slice_from_copied_buffer(message, message_size); - c_op.data.send_message = grpc_byte_buffer_create(&message_slice, 1); + c_op.data.send_message = grpc_raw_byte_buffer_create(&message_slice, 1); gpr_slice_unref(message_slice); break; case GRPC_OP_SEND_CLOSE_FROM_CLIENT: /* Don't need to fill in any other fields. */ break; case GRPC_OP_SEND_STATUS_FROM_SERVER: - if (!pygrpc_cast_pylist_to_send_metadata( + if (!pygrpc_cast_pyseq_to_send_metadata( PyTuple_GetItem(op, TRAILING_METADATA_INDEX), &c_op.data.send_status_from_server.trailing_metadata, &c_op.data.send_status_from_server.trailing_metadata_count)) { return 0; } if (!PyTuple_Check(PyTuple_GET_ITEM(op, STATUS_INDEX))) { - char buf[64]; - snprintf(buf, sizeof(buf), "expected tuple status in op of length %d", - STATUS_TUPLE_SIZE); - PyErr_SetString(PyExc_TypeError, buf); + char *buf; + gpr_asprintf(&buf, "expected tuple status in op of length %d", + STATUS_TUPLE_SIZE); + PyErr_SetString(PyExc_ValueError, buf); + gpr_free(buf); return 0; } c_op.data.send_status_from_server.status = PyInt_AsLong( @@ -240,8 +248,16 @@ int pygrpc_produce_op(PyObject *op, grpc_op *result) { } void pygrpc_discard_op(grpc_op op) { + size_t i; switch(op.op) { case GRPC_OP_SEND_INITIAL_METADATA: + /* Whenever we produce send-metadata, we allocate new strings (to handle + arbitrary sequence input as opposed to just lists or just tuples). We + thus must free those elements. */ + for (i = 0; i < op.data.send_initial_metadata.count; ++i) { + gpr_free((void *)op.data.send_initial_metadata.metadata[i].key); + gpr_free((void *)op.data.send_initial_metadata.metadata[i].value); + } gpr_free(op.data.send_initial_metadata.metadata); break; case GRPC_OP_SEND_MESSAGE: @@ -251,6 +267,16 @@ void pygrpc_discard_op(grpc_op op) { /* Don't need to free any fields. */ break; case GRPC_OP_SEND_STATUS_FROM_SERVER: + /* Whenever we produce send-metadata, we allocate new strings (to handle + arbitrary sequence input as opposed to just lists or just tuples). We + thus must free those elements. */ + for (i = 0; i < op.data.send_status_from_server.trailing_metadata_count; + ++i) { + gpr_free( + (void *)op.data.send_status_from_server.trailing_metadata[i].key); + gpr_free( + (void *)op.data.send_status_from_server.trailing_metadata[i].value); + } gpr_free(op.data.send_status_from_server.trailing_metadata); gpr_free((char *)op.data.send_status_from_server.status_details); break; @@ -351,9 +377,14 @@ double pygrpc_cast_gpr_timespec_to_double(gpr_timespec timespec) { return timespec.tv_sec + 1e-9*timespec.tv_nsec; } +/* Because C89 doesn't have a way to check for infinity... */ +static int pygrpc_isinf(double x) { + return x * 0 != 0; +} + gpr_timespec pygrpc_cast_double_to_gpr_timespec(double seconds) { gpr_timespec result; - if isinf(seconds) { + if (pygrpc_isinf(seconds)) { result = seconds > 0.0 ? gpr_inf_future : gpr_inf_past; } else { result.tv_sec = (time_t)seconds; @@ -365,7 +396,9 @@ gpr_timespec pygrpc_cast_double_to_gpr_timespec(double seconds) { int pygrpc_produce_channel_args(PyObject *py_args, grpc_channel_args *c_args) { size_t num_args = PyList_Size(py_args); size_t i; - grpc_channel_args args = {num_args, gpr_malloc(sizeof(grpc_arg) * num_args)}; + grpc_channel_args args; + args.num_args = num_args; + args.args = gpr_malloc(sizeof(grpc_arg) * num_args); for (i = 0; i < args.num_args; ++i) { char *key; PyObject *value; @@ -405,31 +438,41 @@ void pygrpc_discard_channel_args(grpc_channel_args args) { gpr_free(args.args); } -int pygrpc_cast_pylist_to_send_metadata( - PyObject *pylist, grpc_metadata **metadata, size_t *count) { +int pygrpc_cast_pyseq_to_send_metadata( + PyObject *pyseq, grpc_metadata **metadata, size_t *count) { size_t i; Py_ssize_t value_length; - *count = PyList_Size(pylist); + char *key; + char *value; + if (!PySequence_Check(pyseq)) { + return 0; + } + *count = PySequence_Size(pyseq); *metadata = gpr_malloc(sizeof(grpc_metadata) * *count); for (i = 0; i < *count; ++i) { - if (!PyArg_ParseTuple( - PyList_GetItem(pylist, i), "ss#", - &(*metadata)[i].key, &(*metadata)[i].value, &value_length)) { + PyObject *item = PySequence_GetItem(pyseq, i); + if (!PyArg_ParseTuple(item, "ss#", &key, &value, &value_length)) { + Py_DECREF(item); gpr_free(*metadata); *count = 0; *metadata = NULL; return 0; + } else { + (*metadata)[i].key = gpr_strdup(key); + (*metadata)[i].value = gpr_malloc(value_length); + memcpy((void *)(*metadata)[i].value, value, value_length); + Py_DECREF(item); } (*metadata)[i].value_length = value_length; } return 1; } -PyObject *pygrpc_cast_metadata_array_to_pylist(grpc_metadata_array metadata) { - PyObject *result = PyList_New(metadata.count); +PyObject *pygrpc_cast_metadata_array_to_pyseq(grpc_metadata_array metadata) { + PyObject *result = PyTuple_New(metadata.count); size_t i; for (i = 0; i < metadata.count; ++i) { - PyList_SetItem( + PyTuple_SetItem( result, i, Py_BuildValue( "ss#", metadata.metadata[i].key, metadata.metadata[i].value, (Py_ssize_t)metadata.metadata[i].value_length)); @@ -443,18 +486,18 @@ PyObject *pygrpc_cast_metadata_array_to_pylist(grpc_metadata_array metadata) { void pygrpc_byte_buffer_to_bytes( grpc_byte_buffer *buffer, char **result, size_t *result_size) { - grpc_byte_buffer_reader *reader = grpc_byte_buffer_reader_create(buffer); + grpc_byte_buffer_reader reader; + grpc_byte_buffer_reader_init(&reader, buffer); gpr_slice slice; char *read_result = NULL; size_t size = 0; - while (grpc_byte_buffer_reader_next(reader, &slice)) { + while (grpc_byte_buffer_reader_next(&reader, &slice)) { read_result = gpr_realloc(read_result, size + GPR_SLICE_LENGTH(slice)); memcpy(read_result + size, GPR_SLICE_START_PTR(slice), GPR_SLICE_LENGTH(slice)); size = size + GPR_SLICE_LENGTH(slice); gpr_slice_unref(slice); } - grpc_byte_buffer_reader_destroy(reader); *result_size = size; *result = read_result; } diff --git a/src/python/src/grpc/_adapter/_intermediary_low.py b/src/python/src/grpc/_adapter/_intermediary_low.py index a6e325c4e5..3c7f0a2619 100644 --- a/src/python/src/grpc/_adapter/_intermediary_low.py +++ b/src/python/src/grpc/_adapter/_intermediary_low.py @@ -100,7 +100,7 @@ class _TagAdapter(collections.namedtuple('_TagAdapter', [ class Call(object): """Adapter from old _low.Call interface to new _low.Call.""" - + def __init__(self, channel, completion_queue, method, host, deadline): self._internal = channel._internal.create_call( completion_queue._internal, method, host, deadline) @@ -144,10 +144,11 @@ class Call(object): self._metadata.append((key, value)) def premetadata(self): - return self._internal.start_batch([ + result = self._internal.start_batch([ _types.OpArgs.send_initial_metadata(self._metadata) ], _IGNORE_ME_TAG) self._metadata = [] + return result def read(self, tag): return self._internal.start_batch([ @@ -207,7 +208,7 @@ class CompletionQueue(object): complete_accepted = ev.success if kind == Event.Kind.COMPLETE_ACCEPTED else None service_acceptance = ServiceAcceptance(Call._from_internal(ev.call), ev.call_details.method, ev.call_details.host, ev.call_details.deadline) if kind == Event.Kind.SERVICE_ACCEPTED else None message_bytes = ev.results[0].message if kind == Event.Kind.READ_ACCEPTED else None - status = Status(ev.results[0].status.code, ev.results[0].status.details) if (kind == Event.Kind.FINISH and ev.results[0].status) else Status(_types.StatusCode.CANCELLED if ev.results[0].cancelled else _types.StatusCode.OK, '') if ev.results[0].cancelled is not None else None + status = Status(ev.results[0].status.code, ev.results[0].status.details) if (kind == Event.Kind.FINISH and ev.results[0].status) else Status(_types.StatusCode.CANCELLED if ev.results[0].cancelled else _types.StatusCode.OK, '') if len(ev.results) > 0 and ev.results[0].cancelled is not None else None metadata = ev.results[0].initial_metadata if (kind in [Event.Kind.SERVICE_ACCEPTED, Event.Kind.METADATA_ACCEPTED]) else (ev.results[0].trailing_metadata if kind == Event.Kind.FINISH else None) else: raise RuntimeError('unknown event') @@ -241,7 +242,7 @@ class Server(object): return self._internal.request_call(self._internal_cq, _TagAdapter(tag, Event.Kind.SERVICE_ACCEPTED)) def stop(self): - return self._internal.shutdown() + return self._internal.shutdown(_TagAdapter(None, Event.Kind.STOP)) class ClientCredentials(object): @@ -253,6 +254,6 @@ class ClientCredentials(object): class ServerCredentials(object): """Adapter from old _low.ServerCredentials interface to new _low.ServerCredentials.""" - + def __init__(self, root_credentials, pair_sequence): self._internal = _low.ServerCredentials.ssl(root_credentials, list(pair_sequence)) diff --git a/src/python/src/grpc/_adapter/_intermediary_low_test.py b/src/python/src/grpc/_adapter/_intermediary_low_test.py index 6ff51c43a6..27a5b82e9c 100644 --- a/src/python/src/grpc/_adapter/_intermediary_low_test.py +++ b/src/python/src/grpc/_adapter/_intermediary_low_test.py @@ -29,6 +29,8 @@ """Tests for the old '_low'.""" +import Queue +import threading import time import unittest @@ -43,6 +45,7 @@ _BYTE_SEQUENCE_SEQUENCE = tuple( bytes(bytearray((row + column) % 256 for column in range(row))) for row in range(_STREAM_LENGTH)) + class LonelyClientTest(unittest.TestCase): def testLonelyClient(self): @@ -79,6 +82,14 @@ class LonelyClientTest(unittest.TestCase): del completion_queue +def _drive_completion_queue(completion_queue, event_queue): + while True: + event = completion_queue.get(_FUTURE) + if event.kind is _low.Event.Kind.STOP: + break + event_queue.put(event) + + class EchoTest(unittest.TestCase): def setUp(self): @@ -88,32 +99,27 @@ class EchoTest(unittest.TestCase): self.server = _low.Server(self.server_completion_queue) port = self.server.add_http2_addr('[::]:0') self.server.start() + self.server_events = Queue.Queue() + self.server_completion_queue_thread = threading.Thread( + target=_drive_completion_queue, + args=(self.server_completion_queue, self.server_events)) + self.server_completion_queue_thread.start() self.client_completion_queue = _low.CompletionQueue() self.channel = _low.Channel('%s:%d' % (self.host, port), None) + self.client_events = Queue.Queue() + self.client_completion_queue_thread = threading.Thread( + target=_drive_completion_queue, + args=(self.client_completion_queue, self.client_events)) + self.client_completion_queue_thread.start() def tearDown(self): self.server.stop() - # NOTE(nathaniel): Yep, this is weird; it's a consequence of - # grpc_server_destroy's being what has the effect of telling the server's - # completion queue to pump out all pending events/tags immediately rather - # than gracefully completing all outstanding RPCs while accepting no new - # ones. - # TODO(nathaniel): Deallocation of a Python object shouldn't have this kind - # of observable side effect let alone such an important one. - del self.server self.server_completion_queue.stop() self.client_completion_queue.stop() - while True: - event = self.server_completion_queue.get(_FUTURE) - if event is not None and event.kind is _low.Event.Kind.STOP: - break - while True: - event = self.client_completion_queue.get(_FUTURE) - if event is not None and event.kind is _low.Event.Kind.STOP: - break - self.server_completion_queue = None - self.client_completion_queue = None + self.server_completion_queue_thread.join() + self.client_completion_queue_thread.join() + del self.server def _perform_echo_test(self, test_data): method = 'test method' @@ -151,7 +157,7 @@ class EchoTest(unittest.TestCase): client_call.invoke(self.client_completion_queue, metadata_tag, finish_tag) self.server.service(service_tag) - service_accepted = self.server_completion_queue.get(_FUTURE) + service_accepted = self.server_events.get() self.assertIsNotNone(service_accepted) self.assertIs(service_accepted.kind, _low.Event.Kind.SERVICE_ACCEPTED) self.assertIs(service_accepted.tag, service_tag) @@ -172,7 +178,7 @@ class EchoTest(unittest.TestCase): server_leading_binary_metadata_value) server_call.premetadata() - metadata_accepted = self.client_completion_queue.get(_FUTURE) + metadata_accepted = self.client_events.get() self.assertIsNotNone(metadata_accepted) self.assertEqual(_low.Event.Kind.METADATA_ACCEPTED, metadata_accepted.kind) self.assertEqual(metadata_tag, metadata_accepted.tag) @@ -186,14 +192,14 @@ class EchoTest(unittest.TestCase): for datum in test_data: client_call.write(datum, write_tag) - write_accepted = self.client_completion_queue.get(_FUTURE) + write_accepted = self.client_events.get() self.assertIsNotNone(write_accepted) self.assertIs(write_accepted.kind, _low.Event.Kind.WRITE_ACCEPTED) self.assertIs(write_accepted.tag, write_tag) self.assertIs(write_accepted.write_accepted, True) server_call.read(read_tag) - read_accepted = self.server_completion_queue.get(_FUTURE) + read_accepted = self.server_events.get() self.assertIsNotNone(read_accepted) self.assertEqual(_low.Event.Kind.READ_ACCEPTED, read_accepted.kind) self.assertEqual(read_tag, read_accepted.tag) @@ -201,14 +207,14 @@ class EchoTest(unittest.TestCase): server_data.append(read_accepted.bytes) server_call.write(read_accepted.bytes, write_tag) - write_accepted = self.server_completion_queue.get(_FUTURE) + write_accepted = self.server_events.get() self.assertIsNotNone(write_accepted) self.assertEqual(_low.Event.Kind.WRITE_ACCEPTED, write_accepted.kind) self.assertEqual(write_tag, write_accepted.tag) self.assertTrue(write_accepted.write_accepted) client_call.read(read_tag) - read_accepted = self.client_completion_queue.get(_FUTURE) + read_accepted = self.client_events.get() self.assertIsNotNone(read_accepted) self.assertEqual(_low.Event.Kind.READ_ACCEPTED, read_accepted.kind) self.assertEqual(read_tag, read_accepted.tag) @@ -216,14 +222,14 @@ class EchoTest(unittest.TestCase): client_data.append(read_accepted.bytes) client_call.complete(complete_tag) - complete_accepted = self.client_completion_queue.get(_FUTURE) + complete_accepted = self.client_events.get() self.assertIsNotNone(complete_accepted) self.assertIs(complete_accepted.kind, _low.Event.Kind.COMPLETE_ACCEPTED) self.assertIs(complete_accepted.tag, complete_tag) self.assertIs(complete_accepted.complete_accepted, True) server_call.read(read_tag) - read_accepted = self.server_completion_queue.get(_FUTURE) + read_accepted = self.server_events.get() self.assertIsNotNone(read_accepted) self.assertEqual(_low.Event.Kind.READ_ACCEPTED, read_accepted.kind) self.assertEqual(read_tag, read_accepted.tag) @@ -235,8 +241,8 @@ class EchoTest(unittest.TestCase): server_trailing_binary_metadata_value) server_call.status(_low.Status(_low.Code.OK, details), status_tag) - server_terminal_event_one = self.server_completion_queue.get(_FUTURE) - server_terminal_event_two = self.server_completion_queue.get(_FUTURE) + server_terminal_event_one = self.server_events.get() + server_terminal_event_two = self.server_events.get() if server_terminal_event_one.kind == _low.Event.Kind.COMPLETE_ACCEPTED: status_accepted = server_terminal_event_one rpc_accepted = server_terminal_event_two @@ -253,8 +259,8 @@ class EchoTest(unittest.TestCase): self.assertEqual(_low.Status(_low.Code.OK, ''), rpc_accepted.status) client_call.read(read_tag) - client_terminal_event_one = self.client_completion_queue.get(_FUTURE) - client_terminal_event_two = self.client_completion_queue.get(_FUTURE) + client_terminal_event_one = self.client_events.get() + client_terminal_event_two = self.client_events.get() if client_terminal_event_one.kind == _low.Event.Kind.READ_ACCEPTED: read_accepted = client_terminal_event_one finish_accepted = client_terminal_event_two @@ -276,6 +282,9 @@ class EchoTest(unittest.TestCase): self.assertIn(server_trailing_binary_metadata_key, metadata) self.assertEqual(server_trailing_binary_metadata_value, metadata[server_trailing_binary_metadata_key]) + self.assertSetEqual(set(key for key, _ in finish_accepted.metadata), + set((server_trailing_metadata_key, + server_trailing_binary_metadata_key,))) server_timeout_none_event = self.server_completion_queue.get(0) self.assertIsNone(server_timeout_none_event) @@ -310,23 +319,27 @@ class CancellationTest(unittest.TestCase): self.server = _low.Server(self.server_completion_queue) port = self.server.add_http2_addr('[::]:0') self.server.start() + self.server_events = Queue.Queue() + self.server_completion_queue_thread = threading.Thread( + target=_drive_completion_queue, + args=(self.server_completion_queue, self.server_events)) + self.server_completion_queue_thread.start() self.client_completion_queue = _low.CompletionQueue() self.channel = _low.Channel('%s:%d' % (self.host, port), None) + self.client_events = Queue.Queue() + self.client_completion_queue_thread = threading.Thread( + target=_drive_completion_queue, + args=(self.client_completion_queue, self.client_events)) + self.client_completion_queue_thread.start() def tearDown(self): self.server.stop() - del self.server self.server_completion_queue.stop() self.client_completion_queue.stop() - while True: - event = self.server_completion_queue.get(0) - if event is not None and event.kind is _low.Event.Kind.STOP: - break - while True: - event = self.client_completion_queue.get(0) - if event is not None and event.kind is _low.Event.Kind.STOP: - break + self.server_completion_queue_thread.join() + self.client_completion_queue_thread.join() + del self.server def testCancellation(self): method = 'test method' @@ -347,29 +360,29 @@ class CancellationTest(unittest.TestCase): client_call.invoke(self.client_completion_queue, metadata_tag, finish_tag) self.server.service(service_tag) - service_accepted = self.server_completion_queue.get(_FUTURE) + service_accepted = self.server_events.get() server_call = service_accepted.service_acceptance.call server_call.accept(self.server_completion_queue, finish_tag) server_call.premetadata() - metadata_accepted = self.client_completion_queue.get(_FUTURE) + metadata_accepted = self.client_events.get() self.assertIsNotNone(metadata_accepted) for datum in test_data: client_call.write(datum, write_tag) - write_accepted = self.client_completion_queue.get(_FUTURE) + write_accepted = self.client_events.get() server_call.read(read_tag) - read_accepted = self.server_completion_queue.get(_FUTURE) + read_accepted = self.server_events.get() server_data.append(read_accepted.bytes) server_call.write(read_accepted.bytes, write_tag) - write_accepted = self.server_completion_queue.get(_FUTURE) + write_accepted = self.server_events.get() self.assertIsNotNone(write_accepted) client_call.read(read_tag) - read_accepted = self.client_completion_queue.get(_FUTURE) + read_accepted = self.client_events.get() client_data.append(read_accepted.bytes) client_call.cancel() @@ -380,8 +393,8 @@ class CancellationTest(unittest.TestCase): server_call.read(read_tag) - server_terminal_event_one = self.server_completion_queue.get(_FUTURE) - server_terminal_event_two = self.server_completion_queue.get(_FUTURE) + server_terminal_event_one = self.server_events.get() + server_terminal_event_two = self.server_events.get() if server_terminal_event_one.kind == _low.Event.Kind.READ_ACCEPTED: read_accepted = server_terminal_event_one rpc_accepted = server_terminal_event_two @@ -395,7 +408,7 @@ class CancellationTest(unittest.TestCase): self.assertEqual(_low.Event.Kind.FINISH, rpc_accepted.kind) self.assertEqual(_low.Status(_low.Code.CANCELLED, ''), rpc_accepted.status) - finish_event = self.client_completion_queue.get(_FUTURE) + finish_event = self.client_events.get() self.assertEqual(_low.Event.Kind.FINISH, finish_event.kind) self.assertEqual(_low.Status(_low.Code.CANCELLED, 'Cancelled'), finish_event.status) diff --git a/src/python/src/grpc/_adapter/_links_test.py b/src/python/src/grpc/_adapter/_links_test.py index 50257d8691..4729b84f84 100644 --- a/src/python/src/grpc/_adapter/_links_test.py +++ b/src/python/src/grpc/_adapter/_links_test.py @@ -40,7 +40,7 @@ from grpc.framework.base import interfaces from grpc.framework.foundation import logging_pool _IDENTITY = lambda x: x -_TIMEOUT = 2 +_TIMEOUT = 32 # TODO(nathaniel): End-to-end metadata testing. diff --git a/src/python/src/grpc/_adapter/_low.py b/src/python/src/grpc/_adapter/_low.py index 0c1d3b40a5..dcf67dbc11 100644 --- a/src/python/src/grpc/_adapter/_low.py +++ b/src/python/src/grpc/_adapter/_low.py @@ -101,11 +101,8 @@ class Server(_types.Server): def start(self): return self.server.start() - def shutdown(self, tag=_NO_TAG): - if tag is _NO_TAG: - return self.server.shutdown() - else: - return self.server.shutdown(tag) + def shutdown(self, tag=None): + return self.server.shutdown(tag) def request_call(self, completion_queue, tag): return self.server.request_call(completion_queue.completion_queue, tag) diff --git a/src/python/src/grpc/_adapter/_low_test.py b/src/python/src/grpc/_adapter/_low_test.py index e53b176caf..268e5fe765 100644 --- a/src/python/src/grpc/_adapter/_low_test.py +++ b/src/python/src/grpc/_adapter/_low_test.py @@ -27,6 +27,7 @@ # (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 threading import time import unittest @@ -34,6 +35,33 @@ from grpc._adapter import _types from grpc._adapter import _low +def WaitForEvents(completion_queues, deadline): + """ + Args: + completion_queues: list of completion queues to wait for events on + deadline: absolute deadline to wait until + + Returns: + a sequence of events of length len(completion_queues). + """ + + results = [None] * len(completion_queues) + lock = threading.Lock() + threads = [] + def set_ith_result(i, completion_queue): + result = completion_queue.next(deadline) + with lock: + print i, completion_queue, result, time.time() - deadline + results[i] = result + for i, completion_queue in enumerate(completion_queues): + thread = threading.Thread(target=set_ith_result, + args=[i, completion_queue]) + thread.start() + threads.append(thread) + for thread in threads: + thread.join() + return results + class InsecureServerInsecureClient(unittest.TestCase): def setUp(self): @@ -48,7 +76,6 @@ class InsecureServerInsecureClient(unittest.TestCase): def tearDown(self): self.server.shutdown() del self.client_channel - del self.server self.client_completion_queue.shutdown() while self.client_completion_queue.next().type != _types.EventType.QUEUE_SHUTDOWN: @@ -59,6 +86,7 @@ class InsecureServerInsecureClient(unittest.TestCase): del self.client_completion_queue del self.server_completion_queue + del self.server def testEcho(self): DEADLINE = time.time()+5 @@ -95,7 +123,8 @@ class InsecureServerInsecureClient(unittest.TestCase): ], client_call_tag) self.assertEquals(_types.CallError.OK, client_start_batch_result) - request_event = self.server_completion_queue.next(DEADLINE) + client_no_event, request_event, = WaitForEvents([self.client_completion_queue, self.server_completion_queue], time.time() + 2) + self.assertEquals(client_no_event, None) self.assertEquals(_types.EventType.OP_COMPLETE, request_event.type) self.assertIsInstance(request_event.call, _low.Call) self.assertIs(server_request_tag, request_event.tag) @@ -118,8 +147,7 @@ class InsecureServerInsecureClient(unittest.TestCase): ], server_call_tag) self.assertEquals(_types.CallError.OK, server_start_batch_result) - client_event = self.client_completion_queue.next(DEADLINE) - server_event = self.server_completion_queue.next(DEADLINE) + client_event, server_event, = WaitForEvents([self.client_completion_queue, self.server_completion_queue], time.time() + 1) self.assertEquals(6, len(client_event.results)) found_client_op_types = set() diff --git a/src/python/src/setup.py b/src/python/src/setup.py index dc655a70f9..5398b09936 100644 --- a/src/python/src/setup.py +++ b/src/python/src/setup.py @@ -86,13 +86,13 @@ _PACKAGE_DIRECTORIES = { setuptools.setup( name='grpcio', - version='0.9.0a0', + version='0.9.0a1', ext_modules=[_EXTENSION_MODULE], packages=list(_PACKAGES), package_dir=_PACKAGE_DIRECTORIES, install_requires=[ 'enum34==1.0.4', 'futures==2.2.0', - 'protobuf==3.0.0a2' + 'protobuf==3.0.0a3' ] ) diff --git a/src/ruby/.rspec b/src/ruby/.rspec index dd579f7a13..cd7c5fb5b2 100755 --- a/src/ruby/.rspec +++ b/src/ruby/.rspec @@ -1,2 +1,4 @@ -I. --require spec_helper +--format documentation +--color diff --git a/src/ruby/.rubocop_todo.yml b/src/ruby/.rubocop_todo.yml index c35e970df6..05db404582 100644 --- a/src/ruby/.rubocop_todo.yml +++ b/src/ruby/.rubocop_todo.yml @@ -12,7 +12,7 @@ Metrics/AbcSize: # Offense count: 3 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 192 + Max: 200 # Offense count: 35 # Configuration parameters: CountComments. diff --git a/src/ruby/README.md b/src/ruby/README.md index 9c0eef49b0..4b657c0bd4 100644 --- a/src/ruby/README.md +++ b/src/ruby/README.md @@ -4,48 +4,44 @@ gRPC Ruby A Ruby implementation of gRPC. Status -------- +------ Alpha : Ready for early adopters -INSTALLATION PREREQUISITES --------------------------- +PREREQUISITES +------------- -This requires Ruby 2.1, as the RPC API surface uses keyword args. +- Ruby 2.x. The gRPC API uses keyword args. +- [homebrew][] on Mac OS X, [linuxbrew][] on Linux. These simplify the installation of the gRPC C core. - -QUICK - INSTALL +INSTALLATION --------------- - -- Clone this repository. -- Follow the instructions in [INSTALL](../../INSTALL) to install the gRPC C core. -- If you don't have Ruby 2.1 installed, switch to the more detailed instructions below -- Use bundler to install +On Mac OS X, install [homebrew][]. On Linux, install [linuxbrew][]. +Run the following command to install gRPC Ruby. ```sh -$ # from this directory -$ gem install bundler && bundle install +$ curl -fsSL https://goo.gl/getgrpc | bash -s ruby ``` +This will download and run the [gRPC install script][], then install the latest version of gRPC Ruby gem. It also installs Protocol Buffers compiler (_protoc_) and the gRPC _protoc_ plugin for ruby. -Installing from source ----------------------- - +BUILD FROM SOURCE +--------------------- +- Clone this repository - Build the gRPC C core -E.g, from the root of the gRPC [git repo](https://github.com/google/grpc) +E.g, from the root of the gRPC [Git repository](https://github.com/google/grpc) ```sh $ cd ../.. $ make && sudo make install ``` -- Install Ruby 2.1. Consider doing this with [RVM](http://rvm.io), it's a nice way of controlling +- Install Ruby 2.x. Consider doing this with [RVM](http://rvm.io), it's a nice way of controlling the exact ruby version that's used. ```sh $ command curl -sSL https://rvm.io/mpapis.asc | gpg --import - -$ \curl -sSL https://get.rvm.io | bash -s stable --ruby=ruby-2.1 +$ \curl -sSL https://get.rvm.io | bash -s stable --ruby=ruby-2 $ $ # follow the instructions to ensure that your're using the latest stable version of Ruby $ # and that the rvm command is installed ``` - - Make sure your run `source $HOME/.rvm/scripts/rvm` as instructed to complete the set up of RVM - Install [bundler](http://bundler.io/) @@ -53,30 +49,36 @@ $ # and that the rvm command is installed $ gem install bundler ``` -- Finally, install the gRPC gem locally. +- Finally, build and install the gRPC gem locally. ```sh $ # from this directory $ bundle install # creates the ruby bundle, including building the grpc extension $ rake # runs the unit tests, see rake -T for other options ``` +DOCUMENTATION +------------- +- rubydoc for the gRPC gem is available online at [rubydoc][]. +- the gRPC Ruby reference documentation is available online at [grpc.io][] + CONTENTS -------- - -Directory structure is the layout for [ruby extensions](http://guides.rubygems.org/gems-with-extensions/) - -- ext: - the gRPC ruby extension -- lib: - the entrypoint gRPC ruby library to be used in a 'require' statement -- spec: - Rspec unittest -- bin: - example gRPC clients and servers, e.g, -```ruby -stub = Math::Math::Stub.new('my.test.math.server.com:8080') -req = Math::DivArgs.new(dividend: 7, divisor: 3) -logger.info("div(7/3): req=#{req.inspect}") -resp = stub.div(req) -logger.info("Answer: #{resp.inspect}") -``` +Directory structure is the layout for [ruby extensions][] +- ext: the gRPC ruby extension +- lib: the entrypoint gRPC ruby library to be used in a 'require' statement +- spec: Rspec unittests +- bin: example gRPC clients and servers, e.g, + + ```ruby + stub = Math::Math::Stub.new('my.test.math.server.com:8080') + req = Math::DivArgs.new(dividend: 7, divisor: 3) + GRPC.logger.info("div(7/3): req=#{req.inspect}") + resp = stub.div(req) + GRPC.logger.info("Answer: #{resp.inspect}") + ``` +[homebrew]:http://brew.sh +[linuxbrew]:https://github.com/Homebrew/linuxbrew#installation +[gRPC install script]:https://raw.githubusercontent.com/grpc/homebrew-grpc/master/scripts/install +[ruby extensions]:http://guides.rubygems.org/gems-with-extensions/ +[rubydoc]: http://www.rubydoc.info/gems/grpc +[grpc.io]: http://www.grpc.io/docs/installation/ruby.html diff --git a/src/ruby/bin/interop/interop_client.rb b/src/ruby/bin/interop/interop_client.rb index 16fb1b199d..da4caa842b 100755 --- a/src/ruby/bin/interop/interop_client.rb +++ b/src/ruby/bin/interop/interop_client.rb @@ -284,7 +284,8 @@ class NamedTests op = @stub.full_duplex_call(ppp.each_item, return_op: true) ppp.canceller_op = op # causes ppp to cancel after the 1st message op.execute.each { |r| ppp.queue.push(r) } - assert(op.cancelled, 'call operation should be CANCELLED') + op.wait + assert(op.cancelled, 'call operation was not CANCELLED') p 'OK: cancel_after_first_response' end diff --git a/src/ruby/ext/grpc/extconf.rb b/src/ruby/ext/grpc/extconf.rb index 1b801edc8e..6dd0234489 100644 --- a/src/ruby/ext/grpc/extconf.rb +++ b/src/ruby/ext/grpc/extconf.rb @@ -32,29 +32,6 @@ require 'mkmf' LIBDIR = RbConfig::CONFIG['libdir'] INCLUDEDIR = RbConfig::CONFIG['includedir'] -if ENV.key? 'GRPC_ROOT' - GRPC_ROOT = ENV['GRPC_ROOT'] -else - grpc_root = File.expand_path(File.join(File.dirname(__FILE__), '../../../..')) - if File.exist?(File.join(grpc_root, 'include/grpc/grpc.h')) - GRPC_ROOT = grpc_root - else - GRPC_ROOT = nil - end -end - -if ENV.key? 'CONFIG' - GRPC_CONFIG = ENV['CONFIG'] -else - GRPC_CONFIG = 'opt' -end - -if (ENV.key? 'GRPC_LIB_DIR') && (!GRPC_ROOT.nil?) - GRPC_LIB_DIR = File.join(GRPC_ROOT, ENV['GRPC_LIB_DIR']) -else - GRPC_LIB_DIR = File.join(File.join(GRPC_ROOT, 'libs'), GRPC_CONFIG) -end - HEADER_DIRS = [ # Search /opt/local (Mac source install) '/opt/local/include', @@ -77,12 +54,26 @@ LIB_DIRS = [ LIBDIR ] -unless GRPC_ROOT.nil? - HEADER_DIRS.unshift File.join(GRPC_ROOT, 'include') - LIB_DIRS.unshift GRPC_LIB_DIR - unless File.exist?(File.join(GRPC_LIB_DIR, 'libgrpc.a')) - system("make -C #{GRPC_ROOT} static_c CONFIG=#{GRPC_CONFIG}") +# Check to see if GRPC_ROOT is defined or available +grpc_root = ENV['GRPC_ROOT'] +if grpc_root.nil? + r = File.expand_path(File.join(File.dirname(__FILE__), '../../../..')) + grpc_root = r if File.exist?(File.join(r, 'include/grpc/grpc.h')) +end + +# When grpc_root is available attempt to build the grpc core. +unless grpc_root.nil? + grpc_config = ENV['GRPC_CONFIG'] || 'opt' + if ENV.key?('GRPC_LIB_DIR') + grpc_lib_dir = File.join(grpc_root, ENV['GRPC_LIB_DIR']) + else + grpc_lib_dir = File.join(File.join(grpc_root, 'libs'), grpc_config) + end + unless File.exist?(File.join(grpc_lib_dir, 'libgrpc.a')) + system("make -C #{grpc_root} static_c CONFIG=#{grpc_config}") end + HEADER_DIRS.unshift File.join(grpc_root, 'include') + LIB_DIRS.unshift grpc_lib_dir end def crash(msg) @@ -98,7 +89,7 @@ $CFLAGS << ' -Wno-return-type ' $CFLAGS << ' -Wall ' $CFLAGS << ' -pedantic ' -$LDFLAGS << ' -lgrpc -lgpr -ldl' +$LDFLAGS << ' -lgrpc -lgpr -lz -ldl' crash('need grpc lib') unless have_library('grpc', 'grpc_channel_destroy') have_library('grpc', 'grpc_channel_destroy') diff --git a/src/ruby/ext/grpc/rb_byte_buffer.c b/src/ruby/ext/grpc/rb_byte_buffer.c index edf0d3b115..0aa34c844e 100644 --- a/src/ruby/ext/grpc/rb_byte_buffer.c +++ b/src/ruby/ext/grpc/rb_byte_buffer.c @@ -36,12 +36,13 @@ #include <ruby/ruby.h> #include <grpc/grpc.h> +#include <grpc/byte_buffer_reader.h> #include <grpc/support/slice.h> #include "rb_grpc.h" grpc_byte_buffer* grpc_rb_s_to_byte_buffer(char *string, size_t length) { gpr_slice slice = gpr_slice_from_copied_buffer(string, length); - grpc_byte_buffer *buffer = grpc_byte_buffer_create(&slice, 1); + grpc_byte_buffer *buffer = grpc_raw_byte_buffer_create(&slice, 1); gpr_slice_unref(slice); return buffer; } @@ -50,7 +51,7 @@ VALUE grpc_rb_byte_buffer_to_s(grpc_byte_buffer *buffer) { size_t length = 0; char *string = NULL; size_t offset = 0; - grpc_byte_buffer_reader *reader = NULL; + grpc_byte_buffer_reader reader; gpr_slice next; if (buffer == NULL) { return Qnil; @@ -58,8 +59,8 @@ VALUE grpc_rb_byte_buffer_to_s(grpc_byte_buffer *buffer) { } length = grpc_byte_buffer_length(buffer); string = xmalloc(length + 1); - reader = grpc_byte_buffer_reader_create(buffer); - while (grpc_byte_buffer_reader_next(reader, &next) != 0) { + grpc_byte_buffer_reader_init(&reader, buffer); + while (grpc_byte_buffer_reader_next(&reader, &next) != 0) { memcpy(string + offset, GPR_SLICE_START_PTR(next), GPR_SLICE_LENGTH(next)); offset += GPR_SLICE_LENGTH(next); } diff --git a/src/ruby/ext/grpc/rb_call.c b/src/ruby/ext/grpc/rb_call.c index 29f870f929..33bfd006da 100644 --- a/src/ruby/ext/grpc/rb_call.c +++ b/src/ruby/ext/grpc/rb_call.c @@ -507,6 +507,7 @@ static void grpc_run_batch_stack_fill_ops(run_batch_stack *st, VALUE ops_hash) { NUM2INT(this_op)); }; st->ops[st->op_num].op = (grpc_op_type)NUM2INT(this_op); + st->ops[st->op_num].flags = 0; st->op_num++; } } diff --git a/src/ruby/ext/grpc/rb_completion_queue.c b/src/ruby/ext/grpc/rb_completion_queue.c index fa4c566004..8fb3949b3d 100644 --- a/src/ruby/ext/grpc/rb_completion_queue.c +++ b/src/ruby/ext/grpc/rb_completion_queue.c @@ -142,8 +142,16 @@ grpc_event grpc_rb_completion_queue_pluck_event(VALUE self, VALUE tag, MEMZERO(&next_call, next_call_stack, 1); TypedData_Get_Struct(self, grpc_completion_queue, &grpc_rb_completion_queue_data_type, next_call.cq); - next_call.timeout = grpc_rb_time_timeval(timeout, /* absolute time*/ 0); - next_call.tag = ROBJECT(tag); + if (TYPE(timeout) == T_NIL) { + next_call.timeout = gpr_inf_future; + } else { + next_call.timeout = grpc_rb_time_timeval(timeout, /* absolute time*/ 0); + } + if (TYPE(tag) == T_NIL) { + next_call.tag = NULL; + } else { + next_call.tag = ROBJECT(tag); + } next_call.event.type = GRPC_QUEUE_TIMEOUT; rb_thread_call_without_gvl(grpc_rb_completion_queue_pluck_no_gil, (void *)&next_call, NULL, NULL); diff --git a/src/ruby/ext/grpc/rb_server.c b/src/ruby/ext/grpc/rb_server.c index 837ca3b5e8..9c0d24bf8f 100644 --- a/src/ruby/ext/grpc/rb_server.c +++ b/src/ruby/ext/grpc/rb_server.c @@ -210,7 +210,7 @@ static VALUE grpc_rb_server_request_call(VALUE self, VALUE cqueue, VALUE result; TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s); if (s->wrapped == NULL) { - rb_raise(rb_eRuntimeError, "closed!"); + rb_raise(rb_eRuntimeError, "destroyed!"); return Qnil; } else { grpc_request_call_stack_init(&st); @@ -259,21 +259,69 @@ static VALUE grpc_rb_server_start(VALUE self) { grpc_rb_server *s = NULL; TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s); if (s->wrapped == NULL) { - rb_raise(rb_eRuntimeError, "closed!"); + rb_raise(rb_eRuntimeError, "destroyed!"); } else { grpc_server_start(s->wrapped); } return Qnil; } -static VALUE grpc_rb_server_destroy(VALUE self) { +/* + call-seq: + cq = CompletionQueue.new + server = Server.new(cq, {'arg1': 'value1'}) + ... // do stuff with server + ... + ... // to shutdown the server + server.destroy(cq) + + ... // to shutdown the server with a timeout + server.destroy(cq, timeout) + + Destroys server instances. */ +static VALUE grpc_rb_server_destroy(int argc, VALUE *argv, VALUE self) { + VALUE cqueue = Qnil; + VALUE timeout = Qnil; + grpc_completion_queue *cq = NULL; + grpc_event ev; grpc_rb_server *s = NULL; + + /* "11" == 1 mandatory args, 1 (timeout) is optional */ + rb_scan_args(argc, argv, "11", &cqueue, &timeout); + cq = grpc_rb_get_wrapped_completion_queue(cqueue); TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s); + if (s->wrapped != NULL) { - grpc_server_shutdown(s->wrapped); + grpc_server_shutdown_and_notify(s->wrapped, cq, NULL); + ev = grpc_rb_completion_queue_pluck_event(cqueue, Qnil, timeout); + + if (!ev.success) { + rb_warn("server shutdown failed, there will be a LEAKED object warning"); + return Qnil; + /* + TODO: renable the rb_raise below. + + At the moment if the timeout is INFINITE_FUTURE as recommended, the + pluck blocks forever, even though + + the outstanding server_request_calls correctly fail on the other + thread that they are running on. + + it's almost as if calls that fail on the other thread do not get + cleaned up by shutdown request, even though it caused htem to + terminate. + + rb_raise(rb_eRuntimeError, "grpc server shutdown did not succeed"); + return Qnil; + + The workaround is just to use a timeout and return without really + shutting down the server, and rely on the grpc core garbage collection + it down as a 'LEAKED OBJECT'. + + */ + } grpc_server_destroy(s->wrapped); s->wrapped = NULL; - s->mark = Qnil; } return Qnil; } @@ -302,7 +350,7 @@ static VALUE grpc_rb_server_add_http2_port(int argc, VALUE *argv, VALUE self) { TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s); if (s->wrapped == NULL) { - rb_raise(rb_eRuntimeError, "closed!"); + rb_raise(rb_eRuntimeError, "destroyed!"); return Qnil; } else if (rb_creds == Qnil) { recvd_port = grpc_server_add_http2_port(s->wrapped, StringValueCStr(port)); @@ -315,7 +363,7 @@ static VALUE grpc_rb_server_add_http2_port(int argc, VALUE *argv, VALUE self) { creds = grpc_rb_get_wrapped_server_credentials(rb_creds); recvd_port = grpc_server_add_secure_http2_port(s->wrapped, StringValueCStr(port), - creds); + creds); if (recvd_port == 0) { rb_raise(rb_eRuntimeError, "could not add secure port %s to server, not sure why", @@ -341,7 +389,7 @@ void Init_grpc_server() { rb_define_method(grpc_rb_cServer, "request_call", grpc_rb_server_request_call, 3); rb_define_method(grpc_rb_cServer, "start", grpc_rb_server_start, 0); - rb_define_method(grpc_rb_cServer, "destroy", grpc_rb_server_destroy, 0); + rb_define_method(grpc_rb_cServer, "destroy", grpc_rb_server_destroy, -1); rb_define_alias(grpc_rb_cServer, "close", "destroy"); rb_define_method(grpc_rb_cServer, "add_http2_port", grpc_rb_server_add_http2_port, diff --git a/src/ruby/grpc.gemspec b/src/ruby/grpc.gemspec index 3ddb7fac18..319da9470a 100755 --- a/src/ruby/grpc.gemspec +++ b/src/ruby/grpc.gemspec @@ -14,7 +14,7 @@ Gem::Specification.new do |s| s.license = 'BSD-3-Clause' s.required_ruby_version = '>= 2.0.0' - s.requirements << 'libgrpc ~> 0.6.0 needs to be installed' + s.requirements << 'libgrpc ~> 0.9.1 needs to be installed' s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- spec/*`.split("\n") diff --git a/src/ruby/lib/grpc/generic/active_call.rb b/src/ruby/lib/grpc/generic/active_call.rb index 3814ef34b4..215c0069a3 100644 --- a/src/ruby/lib/grpc/generic/active_call.rb +++ b/src/ruby/lib/grpc/generic/active_call.rb @@ -120,6 +120,7 @@ module GRPC @started = started @unmarshal = unmarshal @metadata_tag = metadata_tag + @op_notifier = nil end # output_metadata are provides access to hash that can be used to @@ -148,6 +149,7 @@ module GRPC # operation provides a restricted view of this ActiveCall for use as # a Operation. def operation + @op_notifier = Notifier.new Operation.new(self) end @@ -167,6 +169,7 @@ module GRPC batch_result = @call.run_batch(@cq, self, INFINITE_FUTURE, ops) return unless assert_finished @call.status = batch_result.status + op_is_done batch_result.check_status end @@ -184,6 +187,7 @@ module GRPC end end @call.status = batch_result.status + op_is_done batch_result.check_status end @@ -415,7 +419,7 @@ module GRPC def bidi_streamer(requests, **kw, &blk) start_call(**kw) unless @started bd = BidiCall.new(@call, @cq, @marshal, @unmarshal, @deadline) - bd.run_on_client(requests, &blk) + bd.run_on_client(requests, @op_notifier, &blk) end # run_server_bidi orchestrates a BiDi stream processing on a server. @@ -434,6 +438,19 @@ module GRPC bd.run_on_server(gen_each_reply) end + # Waits till an operation completes + def wait + return if @op_notifier.nil? + GRPC.logger.debug("active_call.wait: on #{@op_notifier}") + @op_notifier.wait + end + + # Signals that an operation is done + def op_is_done + return if @op_notifier.nil? + @op_notifier.notify(self) + end + private # Starts the call if not already started @@ -468,6 +485,6 @@ module GRPC # Operation limits access to an ActiveCall's methods for use as # a Operation on the client. Operation = view_class(:cancel, :cancelled, :deadline, :execute, - :metadata, :status, :start_call) + :metadata, :status, :start_call, :wait) end end diff --git a/src/ruby/lib/grpc/generic/bidi_call.rb b/src/ruby/lib/grpc/generic/bidi_call.rb index 489dd5162a..3b0c71395c 100644 --- a/src/ruby/lib/grpc/generic/bidi_call.rb +++ b/src/ruby/lib/grpc/generic/bidi_call.rb @@ -66,6 +66,7 @@ module GRPC @cq = q @deadline = deadline @marshal = marshal + @op_notifier = nil # signals completion on clients @readq = Queue.new @unmarshal = unmarshal end @@ -76,8 +77,10 @@ module GRPC # block that can be invoked with each response. # # @param requests the Enumerable of requests to send + # @op_notifier a Notifier used to signal completion # @return an Enumerator of requests to yield - def run_on_client(requests, &blk) + def run_on_client(requests, op_notifier, &blk) + @op_notifier = op_notifier @enq_th = Thread.new { write_loop(requests) } @loop_th = start_read_loop each_queued_msg(&blk) @@ -105,6 +108,13 @@ module GRPC END_OF_READS = :end_of_reads END_OF_WRITES = :end_of_writes + # signals that bidi operation is complete + def notify_done + return unless @op_notifier + GRPC.logger.debug("bidi-notify-done: notifying #{@op_notifier}") + @op_notifier.notify(self) + end + # each_queued_msg yields each message on this instances readq # # - messages are added to the readq by #read_loop @@ -143,11 +153,13 @@ module GRPC @call.status = batch_result.status batch_result.check_status GRPC.logger.debug("bidi-write-loop: done status #{@call.status}") + notify_done end GRPC.logger.debug('bidi-write-loop: finished') rescue StandardError => e GRPC.logger.warn('bidi-write-loop: failed') GRPC.logger.warn(e) + notify_done raise e end diff --git a/src/ruby/lib/grpc/generic/rpc_server.rb b/src/ruby/lib/grpc/generic/rpc_server.rb index dcb11bfbef..a7e20d6b82 100644 --- a/src/ruby/lib/grpc/generic/rpc_server.rb +++ b/src/ruby/lib/grpc/generic/rpc_server.rb @@ -278,7 +278,9 @@ module GRPC @stopped = true end @pool.stop - @server.close + deadline = from_relative_time(@poll_period) + + @server.close(@cq, deadline) end # determines if the server has been stopped @@ -410,17 +412,18 @@ module GRPC # handles calls to the server def loop_handle_server_calls fail 'not running' unless @running - request_call_tag = Object.new + loop_tag = Object.new until stopped? deadline = from_relative_time(@poll_period) begin - an_rpc = @server.request_call(@cq, request_call_tag, deadline) + an_rpc = @server.request_call(@cq, loop_tag, deadline) + c = new_active_server_call(an_rpc) rescue Core::CallError, RuntimeError => e - # can happen during server shutdown + # these might happen for various reasonse. The correct behaviour of + # the server is to log them and continue. GRPC.logger.warn("server call failed: #{e}") next end - c = new_active_server_call(an_rpc) unless c.nil? mth = an_rpc.method.to_sym @pool.schedule(c) do |call| diff --git a/src/ruby/lib/grpc/logconfig.rb b/src/ruby/lib/grpc/logconfig.rb index 96812170ba..e9b4aa3c95 100644 --- a/src/ruby/lib/grpc/logconfig.rb +++ b/src/ruby/lib/grpc/logconfig.rb @@ -38,6 +38,6 @@ Logging.logger.root.appenders = Logging.appenders.stdout Logging.logger.root.level = :info # TODO: provide command-line configuration for logging -Logging.logger['GRPC'].level = :debug +Logging.logger['GRPC'].level = :info Logging.logger['GRPC::ActiveCall'].level = :info Logging.logger['GRPC::BidiCall'].level = :info diff --git a/src/ruby/lib/grpc/version.rb b/src/ruby/lib/grpc/version.rb index 40b6df9a2b..028fea5a4c 100644 --- a/src/ruby/lib/grpc/version.rb +++ b/src/ruby/lib/grpc/version.rb @@ -29,5 +29,5 @@ # GRPC contains the General RPC module. module GRPC - VERSION = '0.9.0' + VERSION = '0.9.4' end diff --git a/src/ruby/spec/client_server_spec.rb b/src/ruby/spec/client_server_spec.rb index b247882241..0e85441209 100644 --- a/src/ruby/spec/client_server_spec.rb +++ b/src/ruby/spec/client_server_spec.rb @@ -42,11 +42,8 @@ shared_context 'setup: tags' do let(:sent_message) { 'sent message' } let(:reply_text) { 'the reply' } before(:example) do - @server_finished_tag = Object.new - @client_finished_tag = Object.new - @client_metadata_tag = Object.new + @client_tag = Object.new @server_tag = Object.new - @tag = Object.new end def deadline @@ -395,7 +392,7 @@ describe 'the http client/server' do after(:example) do @ch.close - @server.close + @server.close(@server_queue, deadline) end it_behaves_like 'basic GRPC message delivery is OK' do @@ -421,7 +418,7 @@ describe 'the secure http client/server' do end after(:example) do - @server.close + @server.close(@server_queue, deadline) end it_behaves_like 'basic GRPC message delivery is OK' do diff --git a/src/ruby/spec/generic/active_call_spec.rb b/src/ruby/spec/generic/active_call_spec.rb index 575871afb1..bc3bee3d44 100644 --- a/src/ruby/spec/generic/active_call_spec.rb +++ b/src/ruby/spec/generic/active_call_spec.rb @@ -51,7 +51,7 @@ describe GRPC::ActiveCall do end after(:each) do - @server.close + @server.close(@server_queue, deadline) end describe 'restricted view methods' do diff --git a/src/ruby/spec/generic/client_stub_spec.rb b/src/ruby/spec/generic/client_stub_spec.rb index 98d68ccfbb..68d4b11790 100644 --- a/src/ruby/spec/generic/client_stub_spec.rb +++ b/src/ruby/spec/generic/client_stub_spec.rb @@ -54,6 +54,7 @@ describe 'ClientStub' do before(:each) do Thread.abort_on_exception = true @server = nil + @server_queue = nil @method = 'an_rpc_method' @pass = OK @fail = INTERNAL @@ -61,7 +62,7 @@ describe 'ClientStub' do end after(:each) do - @server.close unless @server.nil? + @server.close(@server_queue) unless @server_queue.nil? end describe '#new' do diff --git a/src/ruby/spec/generic/rpc_server_spec.rb b/src/ruby/spec/generic/rpc_server_spec.rb index e60a8b27c3..f2403de77c 100644 --- a/src/ruby/spec/generic/rpc_server_spec.rb +++ b/src/ruby/spec/generic/rpc_server_spec.rb @@ -136,10 +136,6 @@ describe GRPC::RpcServer do @ch = GRPC::Core::Channel.new(@host, nil) end - after(:each) do - @server.close - end - describe '#new' do it 'can be created with just some args' do opts = { a_channel_arg: 'an_arg' } @@ -344,10 +340,6 @@ describe GRPC::RpcServer do @srv = RpcServer.new(**server_opts) end - after(:each) do - @srv.stop - end - it 'should return NOT_FOUND status on unknown methods', server: true do @srv.handle(EchoService) t = Thread.new { @srv.run } @@ -527,10 +519,6 @@ describe GRPC::RpcServer do @srv = RpcServer.new(**server_opts) end - after(:each) do - @srv.stop - end - it 'should send connect metadata to the client', server: true do service = EchoService.new @srv.handle(service) diff --git a/src/ruby/spec/server_spec.rb b/src/ruby/spec/server_spec.rb index bb566d1b1f..47fe575343 100644 --- a/src/ruby/spec/server_spec.rb +++ b/src/ruby/spec/server_spec.rb @@ -54,7 +54,7 @@ describe Server do it 'fails if the server is closed' do s = Server.new(@cq, nil) - s.close + s.close(@cq) expect { s.start }.to raise_error(RuntimeError) end end @@ -62,19 +62,19 @@ describe Server do describe '#destroy' do it 'destroys a server ok' do s = start_a_server - blk = proc { s.destroy } + blk = proc { s.destroy(@cq) } expect(&blk).to_not raise_error end it 'can be called more than once without error' do s = start_a_server begin - blk = proc { s.destroy } + blk = proc { s.destroy(@cq) } expect(&blk).to_not raise_error blk.call expect(&blk).to_not raise_error ensure - s.close + s.close(@cq) end end end @@ -83,16 +83,16 @@ describe Server do it 'closes a server ok' do s = start_a_server begin - blk = proc { s.close } + blk = proc { s.close(@cq) } expect(&blk).to_not raise_error ensure - s.close + s.close(@cq) end end it 'can be called more than once without error' do s = start_a_server - blk = proc { s.close } + blk = proc { s.close(@cq) } expect(&blk).to_not raise_error blk.call expect(&blk).to_not raise_error @@ -105,14 +105,14 @@ describe Server do blk = proc do s = Server.new(@cq, nil) s.add_http2_port('localhost:0') - s.close + s.close(@cq) end expect(&blk).to_not raise_error end it 'fails if the server is closed' do s = Server.new(@cq, nil) - s.close + s.close(@cq) expect { s.add_http2_port('localhost:0') }.to raise_error(RuntimeError) end end @@ -123,14 +123,14 @@ describe Server do blk = proc do s = Server.new(@cq, nil) s.add_http2_port('localhost:0', cert) - s.close + s.close(@cq) end expect(&blk).to_not raise_error end it 'fails if the server is closed' do s = Server.new(@cq, nil) - s.close + s.close(@cq) blk = proc { s.add_http2_port('localhost:0', cert) } expect(&blk).to raise_error(RuntimeError) end diff --git a/src/ruby/spec/spec_helper.rb b/src/ruby/spec/spec_helper.rb index 101165c146..270d2e97d3 100644 --- a/src/ruby/spec/spec_helper.rb +++ b/src/ruby/spec/spec_helper.rb @@ -53,3 +53,5 @@ RSpec.configure do |config| include RSpec::LoggingHelper config.capture_log_messages end + +RSpec::Expectations.configuration.warn_about_potential_false_positives = false |